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
|
// Copyright 2016 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 <algorithm>
#include <vector>
#include "core/fpdfapi/page/cpdf_image.h"
#include "core/fpdfapi/page/cpdf_page.h"
#include "core/fpdfapi/render/cpdf_imagecacheentry.h"
#include "core/fpdfapi/render/cpdf_renderstatus.h"
namespace {
struct CacheInfo {
CacheInfo(uint32_t t, CPDF_Stream* stream) : time(t), pStream(stream) {}
uint32_t time;
CPDF_Stream* pStream;
bool operator<(const CacheInfo& other) const { return time < other.time; }
};
} // namespace
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();
std::vector<CacheInfo> cache_info;
cache_info.reserve(nCount);
for (const auto& it : m_ImageCache) {
cache_info.emplace_back(it.second->GetTimeCount(),
it.second->GetImage()->GetStream());
}
std::sort(cache_info.begin(), cache_info.end());
// Check if time value is about to roll over and reset all entries.
// The comparision is legal because uint32_t is an unsigned type.
uint32_t nTimeCount = m_nTimeCount;
if (nTimeCount + 1 < nTimeCount) {
for (size_t i = 0; i < nCount; i++)
m_ImageCache[cache_info[i].pStream]->m_dwTimeCount = i;
m_nTimeCount = nCount;
}
size_t i = 0;
while (i + 15 < nCount)
ClearImageCacheEntry(cache_info[i++].pStream);
while (i < nCount && m_nCacheSize > (uint32_t)dwLimitCacheSize)
ClearImageCacheEntry(cache_info[i++].pStream);
}
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);
}
bool CPDF_PageRenderCache::StartGetCachedBitmap(
const RetainPtr<CPDF_Image>& pImage,
bool bStdCS,
uint32_t GroupFamily,
bool bLoadMask,
CPDF_RenderStatus* pRenderStatus) {
CPDF_Stream* pStream = pImage->GetStream();
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->GetDocument(), pImage);
}
CPDF_DIBBase::LoadState ret = m_pCurImageCacheEntry->StartGetCachedBitmap(
pRenderStatus->GetFormResource(), m_pPage->m_pPageResources.Get(), bStdCS,
GroupFamily, bLoadMask, pRenderStatus);
if (ret == CPDF_DIBBase::LoadState::kContinue)
return true;
m_nTimeCount++;
if (!m_bCurFindCache)
m_ImageCache[pStream] = m_pCurImageCacheEntry;
if (ret == CPDF_DIBBase::LoadState::kFail)
m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
return false;
}
bool CPDF_PageRenderCache::Continue(PauseIndicatorIface* pPause,
CPDF_RenderStatus* pRenderStatus) {
bool ret = m_pCurImageCacheEntry->Continue(pPause, pRenderStatus);
if (ret)
return true;
m_nTimeCount++;
if (!m_bCurFindCache) {
m_ImageCache[m_pCurImageCacheEntry->GetImage()->GetStream()] =
m_pCurImageCacheEntry;
}
m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
return false;
}
void CPDF_PageRenderCache::ResetBitmap(const RetainPtr<CPDF_Image>& pImage,
const RetainPtr<CFX_DIBitmap>& pBitmap) {
CPDF_ImageCacheEntry* pEntry;
CPDF_Stream* pStream = pImage->GetStream();
const auto it = m_ImageCache.find(pStream);
if (it == m_ImageCache.end()) {
if (!pBitmap)
return;
pEntry = new CPDF_ImageCacheEntry(m_pPage->GetDocument(), pImage);
m_ImageCache[pStream] = pEntry;
} else {
pEntry = it->second;
}
m_nCacheSize -= pEntry->EstimateSize();
pEntry->Reset(pBitmap);
m_nCacheSize += pEntry->EstimateSize();
}
|