1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fpdfapi/render/cpdf_pagerendercache.h"
#include "core/fpdfapi/page/cpdf_page.h"
#include "core/fpdfapi/page/pageint.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/render/cpdf_rendercontext.h"
#include "core/fpdfapi/render/cpdf_renderstatus.h"
#include "core/fpdfapi/render/render_int.h"
struct CACHEINFO {
uint32_t time;
CPDF_Stream* pStream;
};
extern "C" {
static int compare(const void* data1, const void* data2) {
return ((CACHEINFO*)data1)->time - ((CACHEINFO*)data2)->time;
}
} // extern "C"
CPDF_PageRenderCache::CPDF_PageRenderCache(CPDF_Page* pPage)
: m_pPage(pPage),
m_pCurImageCacheEntry(nullptr),
m_nTimeCount(0),
m_nCacheSize(0),
m_bCurFindCache(false) {}
CPDF_PageRenderCache::~CPDF_PageRenderCache() {
for (const auto& it : m_ImageCache)
delete it.second;
}
void CPDF_PageRenderCache::CacheOptimization(int32_t dwLimitCacheSize) {
if (m_nCacheSize <= (uint32_t)dwLimitCacheSize)
return;
size_t nCount = m_ImageCache.size();
CACHEINFO* pCACHEINFO = FX_Alloc(CACHEINFO, nCount);
size_t i = 0;
for (const auto& it : m_ImageCache) {
pCACHEINFO[i].time = it.second->GetTimeCount();
pCACHEINFO[i++].pStream = it.second->GetStream();
}
FXSYS_qsort(pCACHEINFO, nCount, sizeof(CACHEINFO), compare);
uint32_t nTimeCount = m_nTimeCount;
// Check if time value is about to roll over and reset all entries.
// The comparision is legal because uint32_t is an unsigned type.
if (nTimeCount + 1 < nTimeCount) {
for (i = 0; i < nCount; i++)
m_ImageCache[pCACHEINFO[i].pStream]->m_dwTimeCount = i;
m_nTimeCount = nCount;
}
i = 0;
while (i + 15 < nCount)
ClearImageCacheEntry(pCACHEINFO[i++].pStream);
while (i < nCount && m_nCacheSize > (uint32_t)dwLimitCacheSize)
ClearImageCacheEntry(pCACHEINFO[i++].pStream);
FX_Free(pCACHEINFO);
}
void CPDF_PageRenderCache::ClearImageCacheEntry(CPDF_Stream* pStream) {
auto it = m_ImageCache.find(pStream);
if (it == m_ImageCache.end())
return;
m_nCacheSize -= it->second->EstimateSize();
delete it->second;
m_ImageCache.erase(it);
}
uint32_t CPDF_PageRenderCache::EstimateSize() {
uint32_t dwSize = 0;
for (const auto& it : m_ImageCache)
dwSize += it.second->EstimateSize();
m_nCacheSize = dwSize;
return dwSize;
}
void CPDF_PageRenderCache::GetCachedBitmap(CPDF_Stream* pStream,
CFX_DIBSource*& pBitmap,
CFX_DIBSource*& pMask,
uint32_t& MatteColor,
bool bStdCS,
uint32_t GroupFamily,
bool bLoadMask,
CPDF_RenderStatus* pRenderStatus,
int32_t downsampleWidth,
int32_t downsampleHeight) {
CPDF_ImageCacheEntry* pEntry;
const auto it = m_ImageCache.find(pStream);
bool bFound = it != m_ImageCache.end();
if (bFound)
pEntry = it->second;
else
pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream);
m_nTimeCount++;
bool bAlreadyCached = pEntry->GetCachedBitmap(
pBitmap, pMask, MatteColor, m_pPage->m_pPageResources, bStdCS,
GroupFamily, bLoadMask, pRenderStatus, downsampleWidth, downsampleHeight);
if (!bFound)
m_ImageCache[pStream] = pEntry;
if (!bAlreadyCached)
m_nCacheSize += pEntry->EstimateSize();
}
bool CPDF_PageRenderCache::StartGetCachedBitmap(
CPDF_Stream* pStream,
bool bStdCS,
uint32_t GroupFamily,
bool bLoadMask,
CPDF_RenderStatus* pRenderStatus,
int32_t downsampleWidth,
int32_t downsampleHeight) {
const auto it = m_ImageCache.find(pStream);
m_bCurFindCache = it != m_ImageCache.end();
if (m_bCurFindCache) {
m_pCurImageCacheEntry = it->second;
} else {
m_pCurImageCacheEntry =
new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream);
}
int ret = m_pCurImageCacheEntry->StartGetCachedBitmap(
pRenderStatus->m_pFormResource, m_pPage->m_pPageResources, bStdCS,
GroupFamily, bLoadMask, pRenderStatus, downsampleWidth, downsampleHeight);
if (ret == 2)
return true;
m_nTimeCount++;
if (!m_bCurFindCache)
m_ImageCache[pStream] = m_pCurImageCacheEntry;
if (!ret)
m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
return false;
}
bool CPDF_PageRenderCache::Continue(IFX_Pause* pPause) {
int ret = m_pCurImageCacheEntry->Continue(pPause);
if (ret == 2)
return true;
m_nTimeCount++;
if (!m_bCurFindCache)
m_ImageCache[m_pCurImageCacheEntry->GetStream()] = m_pCurImageCacheEntry;
if (!ret)
m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
return false;
}
void CPDF_PageRenderCache::ResetBitmap(CPDF_Stream* pStream,
const CFX_DIBitmap* pBitmap) {
CPDF_ImageCacheEntry* pEntry;
const auto it = m_ImageCache.find(pStream);
if (it == m_ImageCache.end()) {
if (!pBitmap)
return;
pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream);
m_ImageCache[pStream] = pEntry;
} else {
pEntry = it->second;
}
m_nCacheSize -= pEntry->EstimateSize();
pEntry->Reset(pBitmap);
m_nCacheSize += pEntry->EstimateSize();
}
CPDF_ImageCacheEntry::CPDF_ImageCacheEntry(CPDF_Document* pDoc,
CPDF_Stream* pStream)
: m_dwTimeCount(0),
m_pCurBitmap(nullptr),
m_pCurMask(nullptr),
m_MatteColor(0),
m_pRenderStatus(nullptr),
m_pDocument(pDoc),
m_pStream(pStream),
m_pCachedBitmap(nullptr),
m_pCachedMask(nullptr),
m_dwCacheSize(0) {}
CPDF_ImageCacheEntry::~CPDF_ImageCacheEntry() {
delete m_pCachedBitmap;
delete m_pCachedMask;
}
void CPDF_ImageCacheEntry::Reset(const CFX_DIBitmap* pBitmap) {
delete m_pCachedBitmap;
m_pCachedBitmap = nullptr;
if (pBitmap) {
m_pCachedBitmap = pBitmap->Clone();
}
CalcSize();
}
static uint32_t FPDF_ImageCache_EstimateImageSize(const CFX_DIBSource* pDIB) {
return pDIB && pDIB->GetBuffer()
? (uint32_t)pDIB->GetHeight() * pDIB->GetPitch() +
(uint32_t)pDIB->GetPaletteSize() * 4
: 0;
}
bool CPDF_ImageCacheEntry::GetCachedBitmap(CFX_DIBSource*& pBitmap,
CFX_DIBSource*& pMask,
uint32_t& MatteColor,
CPDF_Dictionary* pPageResources,
bool bStdCS,
uint32_t GroupFamily,
bool bLoadMask,
CPDF_RenderStatus* pRenderStatus,
int32_t downsampleWidth,
int32_t downsampleHeight) {
if (m_pCachedBitmap) {
pBitmap = m_pCachedBitmap;
pMask = m_pCachedMask;
MatteColor = m_MatteColor;
return true;
}
if (!pRenderStatus) {
return false;
}
CPDF_RenderContext* pContext = pRenderStatus->GetContext();
CPDF_PageRenderCache* pPageRenderCache = pContext->GetPageCache();
m_dwTimeCount = pPageRenderCache->GetTimeCount();
CPDF_DIBSource* pSrc = new CPDF_DIBSource;
CPDF_DIBSource* pMaskSrc = nullptr;
if (!pSrc->Load(m_pDocument, m_pStream, &pMaskSrc, &MatteColor,
pRenderStatus->m_pFormResource, pPageResources, bStdCS,
GroupFamily, bLoadMask)) {
delete pSrc;
pBitmap = nullptr;
return false;
}
m_MatteColor = MatteColor;
if (pSrc->GetPitch() * pSrc->GetHeight() < FPDF_HUGE_IMAGE_SIZE) {
m_pCachedBitmap = pSrc->Clone();
delete pSrc;
} else {
m_pCachedBitmap = pSrc;
}
if (pMaskSrc) {
m_pCachedMask = pMaskSrc->Clone();
delete pMaskSrc;
}
pBitmap = m_pCachedBitmap;
pMask = m_pCachedMask;
CalcSize();
return false;
}
CFX_DIBSource* CPDF_ImageCacheEntry::DetachBitmap() {
CFX_DIBSource* pDIBSource = m_pCurBitmap;
m_pCurBitmap = nullptr;
return pDIBSource;
}
CFX_DIBSource* CPDF_ImageCacheEntry::DetachMask() {
CFX_DIBSource* pDIBSource = m_pCurMask;
m_pCurMask = nullptr;
return pDIBSource;
}
int CPDF_ImageCacheEntry::StartGetCachedBitmap(CPDF_Dictionary* pFormResources,
CPDF_Dictionary* pPageResources,
bool bStdCS,
uint32_t GroupFamily,
bool bLoadMask,
CPDF_RenderStatus* pRenderStatus,
int32_t downsampleWidth,
int32_t downsampleHeight) {
if (m_pCachedBitmap) {
m_pCurBitmap = m_pCachedBitmap;
m_pCurMask = m_pCachedMask;
return 1;
}
if (!pRenderStatus) {
return 0;
}
m_pRenderStatus = pRenderStatus;
m_pCurBitmap = new CPDF_DIBSource;
int ret =
((CPDF_DIBSource*)m_pCurBitmap)
->StartLoadDIBSource(m_pDocument, m_pStream, true, pFormResources,
pPageResources, bStdCS, GroupFamily, bLoadMask);
if (ret == 2) {
return ret;
}
if (!ret) {
delete m_pCurBitmap;
m_pCurBitmap = nullptr;
return 0;
}
ContinueGetCachedBitmap();
return 0;
}
void CPDF_ImageCacheEntry::ContinueGetCachedBitmap() {
m_MatteColor = ((CPDF_DIBSource*)m_pCurBitmap)->GetMatteColor();
m_pCurMask = ((CPDF_DIBSource*)m_pCurBitmap)->DetachMask();
CPDF_RenderContext* pContext = m_pRenderStatus->GetContext();
CPDF_PageRenderCache* pPageRenderCache = pContext->GetPageCache();
m_dwTimeCount = pPageRenderCache->GetTimeCount();
if (m_pCurBitmap->GetPitch() * m_pCurBitmap->GetHeight() <
FPDF_HUGE_IMAGE_SIZE) {
m_pCachedBitmap = m_pCurBitmap->Clone();
delete m_pCurBitmap;
m_pCurBitmap = nullptr;
} else {
m_pCachedBitmap = m_pCurBitmap;
}
if (m_pCurMask) {
m_pCachedMask = m_pCurMask->Clone();
delete m_pCurMask;
m_pCurMask = nullptr;
}
m_pCurBitmap = m_pCachedBitmap;
m_pCurMask = m_pCachedMask;
CalcSize();
}
int CPDF_ImageCacheEntry::Continue(IFX_Pause* pPause) {
int ret = ((CPDF_DIBSource*)m_pCurBitmap)->ContinueLoadDIBSource(pPause);
if (ret == 2) {
return ret;
}
if (!ret) {
delete m_pCurBitmap;
m_pCurBitmap = nullptr;
return 0;
}
ContinueGetCachedBitmap();
return 0;
}
void CPDF_ImageCacheEntry::CalcSize() {
m_dwCacheSize = FPDF_ImageCache_EstimateImageSize(m_pCachedBitmap) +
FPDF_ImageCache_EstimateImageSize(m_pCachedMask);
}
|