OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfapi/render/cpdf_pagerendercache.h" | 7 #include "core/fpdfapi/render/cpdf_pagerendercache.h" |
8 | 8 |
9 #include "core/fpdfapi/page/cpdf_page.h" | 9 #include "core/fpdfapi/page/cpdf_page.h" |
10 #include "core/fpdfapi/page/pageint.h" | 10 #include "core/fpdfapi/page/pageint.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 static int compare(const void* data1, const void* data2) { | 21 static int compare(const void* data1, const void* data2) { |
22 return ((CACHEINFO*)data1)->time - ((CACHEINFO*)data2)->time; | 22 return ((CACHEINFO*)data1)->time - ((CACHEINFO*)data2)->time; |
23 } | 23 } |
24 } // extern "C" | 24 } // extern "C" |
25 | 25 |
26 CPDF_PageRenderCache::CPDF_PageRenderCache(CPDF_Page* pPage) | 26 CPDF_PageRenderCache::CPDF_PageRenderCache(CPDF_Page* pPage) |
27 : m_pPage(pPage), | 27 : m_pPage(pPage), |
28 m_pCurImageCacheEntry(nullptr), | 28 m_pCurImageCacheEntry(nullptr), |
29 m_nTimeCount(0), | 29 m_nTimeCount(0), |
30 m_nCacheSize(0), | 30 m_nCacheSize(0), |
31 m_bCurFindCache(FALSE) {} | 31 m_bCurFindCache(false) {} |
32 | 32 |
33 CPDF_PageRenderCache::~CPDF_PageRenderCache() { | 33 CPDF_PageRenderCache::~CPDF_PageRenderCache() { |
34 for (const auto& it : m_ImageCache) | 34 for (const auto& it : m_ImageCache) |
35 delete it.second; | 35 delete it.second; |
36 } | 36 } |
37 void CPDF_PageRenderCache::CacheOptimization(int32_t dwLimitCacheSize) { | 37 void CPDF_PageRenderCache::CacheOptimization(int32_t dwLimitCacheSize) { |
38 if (m_nCacheSize <= (uint32_t)dwLimitCacheSize) | 38 if (m_nCacheSize <= (uint32_t)dwLimitCacheSize) |
39 return; | 39 return; |
40 | 40 |
41 size_t nCount = m_ImageCache.size(); | 41 size_t nCount = m_ImageCache.size(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 for (const auto& it : m_ImageCache) | 79 for (const auto& it : m_ImageCache) |
80 dwSize += it.second->EstimateSize(); | 80 dwSize += it.second->EstimateSize(); |
81 | 81 |
82 m_nCacheSize = dwSize; | 82 m_nCacheSize = dwSize; |
83 return dwSize; | 83 return dwSize; |
84 } | 84 } |
85 void CPDF_PageRenderCache::GetCachedBitmap(CPDF_Stream* pStream, | 85 void CPDF_PageRenderCache::GetCachedBitmap(CPDF_Stream* pStream, |
86 CFX_DIBSource*& pBitmap, | 86 CFX_DIBSource*& pBitmap, |
87 CFX_DIBSource*& pMask, | 87 CFX_DIBSource*& pMask, |
88 uint32_t& MatteColor, | 88 uint32_t& MatteColor, |
89 FX_BOOL bStdCS, | 89 bool bStdCS, |
90 uint32_t GroupFamily, | 90 uint32_t GroupFamily, |
91 FX_BOOL bLoadMask, | 91 bool bLoadMask, |
92 CPDF_RenderStatus* pRenderStatus, | 92 CPDF_RenderStatus* pRenderStatus, |
93 int32_t downsampleWidth, | 93 int32_t downsampleWidth, |
94 int32_t downsampleHeight) { | 94 int32_t downsampleHeight) { |
95 CPDF_ImageCacheEntry* pEntry; | 95 CPDF_ImageCacheEntry* pEntry; |
96 const auto it = m_ImageCache.find(pStream); | 96 const auto it = m_ImageCache.find(pStream); |
97 FX_BOOL bFound = it != m_ImageCache.end(); | 97 bool bFound = it != m_ImageCache.end(); |
98 if (bFound) | 98 if (bFound) |
99 pEntry = it->second; | 99 pEntry = it->second; |
100 else | 100 else |
101 pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream); | 101 pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream); |
102 | 102 |
103 m_nTimeCount++; | 103 m_nTimeCount++; |
104 FX_BOOL bAlreadyCached = pEntry->GetCachedBitmap( | 104 bool bAlreadyCached = pEntry->GetCachedBitmap( |
105 pBitmap, pMask, MatteColor, m_pPage->m_pPageResources, bStdCS, | 105 pBitmap, pMask, MatteColor, m_pPage->m_pPageResources, bStdCS, |
106 GroupFamily, bLoadMask, pRenderStatus, downsampleWidth, downsampleHeight); | 106 GroupFamily, bLoadMask, pRenderStatus, downsampleWidth, downsampleHeight); |
107 | 107 |
108 if (!bFound) | 108 if (!bFound) |
109 m_ImageCache[pStream] = pEntry; | 109 m_ImageCache[pStream] = pEntry; |
110 | 110 |
111 if (!bAlreadyCached) | 111 if (!bAlreadyCached) |
112 m_nCacheSize += pEntry->EstimateSize(); | 112 m_nCacheSize += pEntry->EstimateSize(); |
113 } | 113 } |
114 FX_BOOL CPDF_PageRenderCache::StartGetCachedBitmap( | 114 bool CPDF_PageRenderCache::StartGetCachedBitmap( |
115 CPDF_Stream* pStream, | 115 CPDF_Stream* pStream, |
116 FX_BOOL bStdCS, | 116 bool bStdCS, |
117 uint32_t GroupFamily, | 117 uint32_t GroupFamily, |
118 FX_BOOL bLoadMask, | 118 bool bLoadMask, |
119 CPDF_RenderStatus* pRenderStatus, | 119 CPDF_RenderStatus* pRenderStatus, |
120 int32_t downsampleWidth, | 120 int32_t downsampleWidth, |
121 int32_t downsampleHeight) { | 121 int32_t downsampleHeight) { |
122 const auto it = m_ImageCache.find(pStream); | 122 const auto it = m_ImageCache.find(pStream); |
123 m_bCurFindCache = it != m_ImageCache.end(); | 123 m_bCurFindCache = it != m_ImageCache.end(); |
124 if (m_bCurFindCache) { | 124 if (m_bCurFindCache) { |
125 m_pCurImageCacheEntry = it->second; | 125 m_pCurImageCacheEntry = it->second; |
126 } else { | 126 } else { |
127 m_pCurImageCacheEntry = | 127 m_pCurImageCacheEntry = |
128 new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream); | 128 new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream); |
129 } | 129 } |
130 int ret = m_pCurImageCacheEntry->StartGetCachedBitmap( | 130 int ret = m_pCurImageCacheEntry->StartGetCachedBitmap( |
131 pRenderStatus->m_pFormResource, m_pPage->m_pPageResources, bStdCS, | 131 pRenderStatus->m_pFormResource, m_pPage->m_pPageResources, bStdCS, |
132 GroupFamily, bLoadMask, pRenderStatus, downsampleWidth, downsampleHeight); | 132 GroupFamily, bLoadMask, pRenderStatus, downsampleWidth, downsampleHeight); |
133 if (ret == 2) | 133 if (ret == 2) |
134 return TRUE; | 134 return true; |
135 | 135 |
136 m_nTimeCount++; | 136 m_nTimeCount++; |
137 if (!m_bCurFindCache) | 137 if (!m_bCurFindCache) |
138 m_ImageCache[pStream] = m_pCurImageCacheEntry; | 138 m_ImageCache[pStream] = m_pCurImageCacheEntry; |
139 | 139 |
140 if (!ret) | 140 if (!ret) |
141 m_nCacheSize += m_pCurImageCacheEntry->EstimateSize(); | 141 m_nCacheSize += m_pCurImageCacheEntry->EstimateSize(); |
142 | 142 |
143 return FALSE; | 143 return false; |
144 } | 144 } |
145 FX_BOOL CPDF_PageRenderCache::Continue(IFX_Pause* pPause) { | 145 bool CPDF_PageRenderCache::Continue(IFX_Pause* pPause) { |
146 int ret = m_pCurImageCacheEntry->Continue(pPause); | 146 int ret = m_pCurImageCacheEntry->Continue(pPause); |
147 if (ret == 2) | 147 if (ret == 2) |
148 return TRUE; | 148 return true; |
149 m_nTimeCount++; | 149 m_nTimeCount++; |
150 if (!m_bCurFindCache) | 150 if (!m_bCurFindCache) |
151 m_ImageCache[m_pCurImageCacheEntry->GetStream()] = m_pCurImageCacheEntry; | 151 m_ImageCache[m_pCurImageCacheEntry->GetStream()] = m_pCurImageCacheEntry; |
152 if (!ret) | 152 if (!ret) |
153 m_nCacheSize += m_pCurImageCacheEntry->EstimateSize(); | 153 m_nCacheSize += m_pCurImageCacheEntry->EstimateSize(); |
154 return FALSE; | 154 return false; |
155 } | 155 } |
156 void CPDF_PageRenderCache::ResetBitmap(CPDF_Stream* pStream, | 156 void CPDF_PageRenderCache::ResetBitmap(CPDF_Stream* pStream, |
157 const CFX_DIBitmap* pBitmap) { | 157 const CFX_DIBitmap* pBitmap) { |
158 CPDF_ImageCacheEntry* pEntry; | 158 CPDF_ImageCacheEntry* pEntry; |
159 const auto it = m_ImageCache.find(pStream); | 159 const auto it = m_ImageCache.find(pStream); |
160 if (it == m_ImageCache.end()) { | 160 if (it == m_ImageCache.end()) { |
161 if (!pBitmap) | 161 if (!pBitmap) |
162 return; | 162 return; |
163 pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream); | 163 pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream); |
164 m_ImageCache[pStream] = pEntry; | 164 m_ImageCache[pStream] = pEntry; |
(...skipping 28 matching lines...) Expand all Loading... |
193 } | 193 } |
194 CalcSize(); | 194 CalcSize(); |
195 } | 195 } |
196 | 196 |
197 static uint32_t FPDF_ImageCache_EstimateImageSize(const CFX_DIBSource* pDIB) { | 197 static uint32_t FPDF_ImageCache_EstimateImageSize(const CFX_DIBSource* pDIB) { |
198 return pDIB && pDIB->GetBuffer() | 198 return pDIB && pDIB->GetBuffer() |
199 ? (uint32_t)pDIB->GetHeight() * pDIB->GetPitch() + | 199 ? (uint32_t)pDIB->GetHeight() * pDIB->GetPitch() + |
200 (uint32_t)pDIB->GetPaletteSize() * 4 | 200 (uint32_t)pDIB->GetPaletteSize() * 4 |
201 : 0; | 201 : 0; |
202 } | 202 } |
203 FX_BOOL CPDF_ImageCacheEntry::GetCachedBitmap(CFX_DIBSource*& pBitmap, | 203 bool CPDF_ImageCacheEntry::GetCachedBitmap(CFX_DIBSource*& pBitmap, |
204 CFX_DIBSource*& pMask, | 204 CFX_DIBSource*& pMask, |
205 uint32_t& MatteColor, | 205 uint32_t& MatteColor, |
206 CPDF_Dictionary* pPageResources, | 206 CPDF_Dictionary* pPageResources, |
207 FX_BOOL bStdCS, | 207 bool bStdCS, |
208 uint32_t GroupFamily, | 208 uint32_t GroupFamily, |
209 FX_BOOL bLoadMask, | 209 bool bLoadMask, |
210 CPDF_RenderStatus* pRenderStatus, | 210 CPDF_RenderStatus* pRenderStatus, |
211 int32_t downsampleWidth, | 211 int32_t downsampleWidth, |
212 int32_t downsampleHeight) { | 212 int32_t downsampleHeight) { |
213 if (m_pCachedBitmap) { | 213 if (m_pCachedBitmap) { |
214 pBitmap = m_pCachedBitmap; | 214 pBitmap = m_pCachedBitmap; |
215 pMask = m_pCachedMask; | 215 pMask = m_pCachedMask; |
216 MatteColor = m_MatteColor; | 216 MatteColor = m_MatteColor; |
217 return TRUE; | 217 return true; |
218 } | 218 } |
219 if (!pRenderStatus) { | 219 if (!pRenderStatus) { |
220 return FALSE; | 220 return false; |
221 } | 221 } |
222 CPDF_RenderContext* pContext = pRenderStatus->GetContext(); | 222 CPDF_RenderContext* pContext = pRenderStatus->GetContext(); |
223 CPDF_PageRenderCache* pPageRenderCache = pContext->GetPageCache(); | 223 CPDF_PageRenderCache* pPageRenderCache = pContext->GetPageCache(); |
224 m_dwTimeCount = pPageRenderCache->GetTimeCount(); | 224 m_dwTimeCount = pPageRenderCache->GetTimeCount(); |
225 CPDF_DIBSource* pSrc = new CPDF_DIBSource; | 225 CPDF_DIBSource* pSrc = new CPDF_DIBSource; |
226 CPDF_DIBSource* pMaskSrc = nullptr; | 226 CPDF_DIBSource* pMaskSrc = nullptr; |
227 if (!pSrc->Load(m_pDocument, m_pStream, &pMaskSrc, &MatteColor, | 227 if (!pSrc->Load(m_pDocument, m_pStream, &pMaskSrc, &MatteColor, |
228 pRenderStatus->m_pFormResource, pPageResources, bStdCS, | 228 pRenderStatus->m_pFormResource, pPageResources, bStdCS, |
229 GroupFamily, bLoadMask)) { | 229 GroupFamily, bLoadMask)) { |
230 delete pSrc; | 230 delete pSrc; |
231 pBitmap = nullptr; | 231 pBitmap = nullptr; |
232 return FALSE; | 232 return false; |
233 } | 233 } |
234 m_MatteColor = MatteColor; | 234 m_MatteColor = MatteColor; |
235 if (pSrc->GetPitch() * pSrc->GetHeight() < FPDF_HUGE_IMAGE_SIZE) { | 235 if (pSrc->GetPitch() * pSrc->GetHeight() < FPDF_HUGE_IMAGE_SIZE) { |
236 m_pCachedBitmap = pSrc->Clone(); | 236 m_pCachedBitmap = pSrc->Clone(); |
237 delete pSrc; | 237 delete pSrc; |
238 } else { | 238 } else { |
239 m_pCachedBitmap = pSrc; | 239 m_pCachedBitmap = pSrc; |
240 } | 240 } |
241 if (pMaskSrc) { | 241 if (pMaskSrc) { |
242 m_pCachedMask = pMaskSrc->Clone(); | 242 m_pCachedMask = pMaskSrc->Clone(); |
243 delete pMaskSrc; | 243 delete pMaskSrc; |
244 } | 244 } |
245 | 245 |
246 pBitmap = m_pCachedBitmap; | 246 pBitmap = m_pCachedBitmap; |
247 pMask = m_pCachedMask; | 247 pMask = m_pCachedMask; |
248 CalcSize(); | 248 CalcSize(); |
249 return FALSE; | 249 return false; |
250 } | 250 } |
251 CFX_DIBSource* CPDF_ImageCacheEntry::DetachBitmap() { | 251 CFX_DIBSource* CPDF_ImageCacheEntry::DetachBitmap() { |
252 CFX_DIBSource* pDIBSource = m_pCurBitmap; | 252 CFX_DIBSource* pDIBSource = m_pCurBitmap; |
253 m_pCurBitmap = nullptr; | 253 m_pCurBitmap = nullptr; |
254 return pDIBSource; | 254 return pDIBSource; |
255 } | 255 } |
256 CFX_DIBSource* CPDF_ImageCacheEntry::DetachMask() { | 256 CFX_DIBSource* CPDF_ImageCacheEntry::DetachMask() { |
257 CFX_DIBSource* pDIBSource = m_pCurMask; | 257 CFX_DIBSource* pDIBSource = m_pCurMask; |
258 m_pCurMask = nullptr; | 258 m_pCurMask = nullptr; |
259 return pDIBSource; | 259 return pDIBSource; |
260 } | 260 } |
261 int CPDF_ImageCacheEntry::StartGetCachedBitmap(CPDF_Dictionary* pFormResources, | 261 int CPDF_ImageCacheEntry::StartGetCachedBitmap(CPDF_Dictionary* pFormResources, |
262 CPDF_Dictionary* pPageResources, | 262 CPDF_Dictionary* pPageResources, |
263 FX_BOOL bStdCS, | 263 bool bStdCS, |
264 uint32_t GroupFamily, | 264 uint32_t GroupFamily, |
265 FX_BOOL bLoadMask, | 265 bool bLoadMask, |
266 CPDF_RenderStatus* pRenderStatus, | 266 CPDF_RenderStatus* pRenderStatus, |
267 int32_t downsampleWidth, | 267 int32_t downsampleWidth, |
268 int32_t downsampleHeight) { | 268 int32_t downsampleHeight) { |
269 if (m_pCachedBitmap) { | 269 if (m_pCachedBitmap) { |
270 m_pCurBitmap = m_pCachedBitmap; | 270 m_pCurBitmap = m_pCachedBitmap; |
271 m_pCurMask = m_pCachedMask; | 271 m_pCurMask = m_pCachedMask; |
272 return 1; | 272 return 1; |
273 } | 273 } |
274 if (!pRenderStatus) { | 274 if (!pRenderStatus) { |
275 return 0; | 275 return 0; |
276 } | 276 } |
277 m_pRenderStatus = pRenderStatus; | 277 m_pRenderStatus = pRenderStatus; |
278 m_pCurBitmap = new CPDF_DIBSource; | 278 m_pCurBitmap = new CPDF_DIBSource; |
279 int ret = | 279 int ret = |
280 ((CPDF_DIBSource*)m_pCurBitmap) | 280 ((CPDF_DIBSource*)m_pCurBitmap) |
281 ->StartLoadDIBSource(m_pDocument, m_pStream, TRUE, pFormResources, | 281 ->StartLoadDIBSource(m_pDocument, m_pStream, true, pFormResources, |
282 pPageResources, bStdCS, GroupFamily, bLoadMask); | 282 pPageResources, bStdCS, GroupFamily, bLoadMask); |
283 if (ret == 2) { | 283 if (ret == 2) { |
284 return ret; | 284 return ret; |
285 } | 285 } |
286 if (!ret) { | 286 if (!ret) { |
287 delete m_pCurBitmap; | 287 delete m_pCurBitmap; |
288 m_pCurBitmap = nullptr; | 288 m_pCurBitmap = nullptr; |
289 return 0; | 289 return 0; |
290 } | 290 } |
291 ContinueGetCachedBitmap(); | 291 ContinueGetCachedBitmap(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 m_pCurBitmap = nullptr; | 324 m_pCurBitmap = nullptr; |
325 return 0; | 325 return 0; |
326 } | 326 } |
327 ContinueGetCachedBitmap(); | 327 ContinueGetCachedBitmap(); |
328 return 0; | 328 return 0; |
329 } | 329 } |
330 void CPDF_ImageCacheEntry::CalcSize() { | 330 void CPDF_ImageCacheEntry::CalcSize() { |
331 m_dwCacheSize = FPDF_ImageCache_EstimateImageSize(m_pCachedBitmap) + | 331 m_dwCacheSize = FPDF_ImageCache_EstimateImageSize(m_pCachedBitmap) + |
332 FPDF_ImageCache_EstimateImageSize(m_pCachedMask); | 332 FPDF_ImageCache_EstimateImageSize(m_pCachedMask); |
333 } | 333 } |
OLD | NEW |