1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/render/cpdf_imagecacheentry.h"
8
9 #include <memory>
10 #include <utility>
11
12 #include "core/fpdfapi/page/cpdf_dib.h"
13 #include "core/fpdfapi/page/cpdf_image.h"
14 #include "core/fpdfapi/page/cpdf_page.h"
15 #include "core/fpdfapi/parser/cpdf_dictionary.h"
16 #include "core/fpdfapi/parser/cpdf_document.h"
17 #include "core/fpdfapi/parser/cpdf_stream.h"
18 #include "core/fpdfapi/render/cpdf_pagerendercache.h"
19 #include "core/fpdfapi/render/cpdf_rendercontext.h"
20 #include "core/fpdfapi/render/cpdf_renderstatus.h"
21 #include "core/fxge/dib/cfx_dibitmap.h"
22
23 namespace {
24
GetEstimatedImageSize(const RetainPtr<CFX_DIBBase> & pDIB)25 uint32_t GetEstimatedImageSize(const RetainPtr<CFX_DIBBase>& pDIB) {
26 if (!pDIB || !pDIB->GetBuffer())
27 return 0;
28
29 int height = pDIB->GetHeight();
30 ASSERT(pdfium::base::IsValueInRangeForNumericType<uint32_t>(height));
31 return static_cast<uint32_t>(height) * pDIB->GetPitch() +
32 pDIB->GetPaletteSize() * 4;
33 }
34
35 } // namespace
36
CPDF_ImageCacheEntry(CPDF_Document * pDoc,const RetainPtr<CPDF_Image> & pImage)37 CPDF_ImageCacheEntry::CPDF_ImageCacheEntry(CPDF_Document* pDoc,
38 const RetainPtr<CPDF_Image>& pImage)
39 : m_pDocument(pDoc), m_pImage(pImage) {}
40
41 CPDF_ImageCacheEntry::~CPDF_ImageCacheEntry() = default;
42
Reset()43 void CPDF_ImageCacheEntry::Reset() {
44 m_pCachedBitmap.Reset();
45 CalcSize();
46 }
47
DetachBitmap()48 RetainPtr<CFX_DIBBase> CPDF_ImageCacheEntry::DetachBitmap() {
49 return std::move(m_pCurBitmap);
50 }
51
DetachMask()52 RetainPtr<CFX_DIBBase> CPDF_ImageCacheEntry::DetachMask() {
53 return std::move(m_pCurMask);
54 }
55
StartGetCachedBitmap(const CPDF_Dictionary * pFormResources,CPDF_Dictionary * pPageResources,bool bStdCS,uint32_t GroupFamily,bool bLoadMask,CPDF_RenderStatus * pRenderStatus)56 CPDF_DIB::LoadState CPDF_ImageCacheEntry::StartGetCachedBitmap(
57 const CPDF_Dictionary* pFormResources,
58 CPDF_Dictionary* pPageResources,
59 bool bStdCS,
60 uint32_t GroupFamily,
61 bool bLoadMask,
62 CPDF_RenderStatus* pRenderStatus) {
63 ASSERT(pRenderStatus);
64
65 if (m_pCachedBitmap) {
66 m_pCurBitmap = m_pCachedBitmap;
67 m_pCurMask = m_pCachedMask;
68 return CPDF_DIB::LoadState::kSuccess;
69 }
70
71 m_pCurBitmap = pdfium::MakeRetain<CPDF_DIB>();
72 CPDF_DIB::LoadState ret = m_pCurBitmap.As<CPDF_DIB>()->StartLoadDIBBase(
73 m_pDocument.Get(), m_pImage->GetStream(), true, pFormResources,
74 pPageResources, bStdCS, GroupFamily, bLoadMask);
75 if (ret == CPDF_DIB::LoadState::kContinue)
76 return CPDF_DIB::LoadState::kContinue;
77
78 if (ret == CPDF_DIB::LoadState::kSuccess)
79 ContinueGetCachedBitmap(pRenderStatus);
80 else
81 m_pCurBitmap.Reset();
82 return CPDF_DIB::LoadState::kFail;
83 }
84
Continue(PauseIndicatorIface * pPause,CPDF_RenderStatus * pRenderStatus)85 bool CPDF_ImageCacheEntry::Continue(PauseIndicatorIface* pPause,
86 CPDF_RenderStatus* pRenderStatus) {
87 CPDF_DIB::LoadState ret =
88 m_pCurBitmap.As<CPDF_DIB>()->ContinueLoadDIBBase(pPause);
89 if (ret == CPDF_DIB::LoadState::kContinue)
90 return true;
91
92 if (ret == CPDF_DIB::LoadState::kSuccess)
93 ContinueGetCachedBitmap(pRenderStatus);
94 else
95 m_pCurBitmap.Reset();
96 return false;
97 }
98
ContinueGetCachedBitmap(CPDF_RenderStatus * pRenderStatus)99 void CPDF_ImageCacheEntry::ContinueGetCachedBitmap(
100 CPDF_RenderStatus* pRenderStatus) {
101 m_MatteColor = m_pCurBitmap.As<CPDF_DIB>()->GetMatteColor();
102 m_pCurMask = m_pCurBitmap.As<CPDF_DIB>()->DetachMask();
103 CPDF_RenderContext* pContext = pRenderStatus->GetContext();
104 CPDF_PageRenderCache* pPageRenderCache = pContext->GetPageCache();
105 m_dwTimeCount = pPageRenderCache->GetTimeCount();
106 if (m_pCurBitmap->GetPitch() * m_pCurBitmap->GetHeight() < kHugeImageSize) {
107 m_pCachedBitmap = m_pCurBitmap->Clone(nullptr);
108 m_pCurBitmap.Reset();
109 } else {
110 m_pCachedBitmap = m_pCurBitmap;
111 }
112 if (m_pCurMask) {
113 m_pCachedMask = m_pCurMask->Clone(nullptr);
114 m_pCurMask.Reset();
115 }
116 m_pCurBitmap = m_pCachedBitmap;
117 m_pCurMask = m_pCachedMask;
118 CalcSize();
119 }
120
CalcSize()121 void CPDF_ImageCacheEntry::CalcSize() {
122 m_dwCacheSize = GetEstimatedImageSize(m_pCachedBitmap) +
123 GetEstimatedImageSize(m_pCachedMask);
124 }
125