7#include "core/fpdfapi/page/cpdf_pageimagecache.h"
16#include "core/fpdfapi/page/cpdf_dib.h"
17#include "core/fpdfapi/page/cpdf_image.h"
18#include "core/fpdfapi/page/cpdf_page.h"
19#include "core/fpdfapi/parser/cpdf_dictionary.h"
20#include "core/fpdfapi/parser/cpdf_document.h"
21#include "core/fpdfapi/parser/cpdf_stream.h"
22#include "core/fxcrt/retain_ptr.h"
23#include "core/fxcrt/stl_util.h"
24#include "core/fxge/dib/cfx_dibbase.h"
25#include "core/fxge/dib/cfx_dibitmap.h"
26#include "third_party/base/check.h"
28#if defined(PDF_USE_SKIA)
29#include "core/fxcrt/data_vector.h"
30#include "core/fxge/cfx_defaultrenderdevice.h"
31#include "third_party/skia/include/core/SkImage.h"
32#include "third_party/skia/include/core/SkRefCnt.h"
38 CacheInfo(uint32_t t,
RetainPtr<
const CPDF_Stream> stream)
39 : time(t), pStream(std::move(stream)) {}
44 bool operator<(
const CacheInfo& other)
const {
return time < other.time; }
47#if defined(PDF_USE_SKIA)
50class CachedImage final :
public CFX_DIBBase {
52 explicit CachedImage(RetainPtr<CFX_DIBBase> image)
53 : image_(std::move(image)) {
54 m_Format = image_->GetFormat();
55 m_Width = image_->GetWidth();
56 m_Height = image_->GetHeight();
57 m_Pitch = image_->GetPitch();
59 if (image_->HasPalette()) {
60 pdfium::span<
const uint32_t> palette = image_->GetPaletteSpan();
61 m_palette = DataVector<uint32_t>(palette.begin(), palette.end());
65 pdfium::span<
const uint8_t> GetScanline(
int line)
const override {
68 return image_->GetScanline(line);
71 bool SkipToScanline(
int line, PauseIndicatorIface* pause)
const override {
72 return image_->SkipToScanline(line, pause);
75 size_t GetEstimatedImageMemoryBurden()
const override {
77 return image_->GetEstimatedImageMemoryBurden();
80#if BUILDFLAG(IS_WIN) || defined(PDF_USE_SKIA)
81 RetainPtr<
const CFX_DIBitmap> RealizeIfNeeded()
const override {
82 return image_->RealizeIfNeeded();
86 sk_sp<SkImage> RealizeSkImage()
const override {
87 if (!cached_skia_image_) {
88 cached_skia_image_ = image_->RealizeSkImage();
90 return cached_skia_image_;
94 RetainPtr<CFX_DIBBase> image_;
95 mutable sk_sp<SkImage> cached_skia_image_;
104#if defined(PDF_USE_SKIA)
105 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
107 return pdfium::MakeRetain<CachedImage>(std::move(image));
110 return realize_hint ? image->Realize() : image;
120 if (m_nCacheSize <= (uint32_t)dwLimitCacheSize)
123 uint32_t nCount = fxcrt::CollectionSize<uint32_t>(m_ImageCache);
124 std::vector<CacheInfo> cache_info;
125 cache_info.reserve(nCount);
126 for (
const auto& it : m_ImageCache) {
127 cache_info.emplace_back(it.second->GetTimeCount(),
128 it.second->GetImage()->GetStream());
130 std::sort(cache_info.begin(), cache_info.end());
134 uint32_t nTimeCount = m_nTimeCount;
135 if (nTimeCount + 1 < nTimeCount) {
136 for (uint32_t i = 0; i < nCount; i++)
137 m_ImageCache[cache_info[i].pStream]->SetTimeCount(i);
138 m_nTimeCount = nCount;
142 while (i + 15 < nCount)
143 ClearImageCacheEntry(cache_info[i++].pStream);
145 while (i < nCount && m_nCacheSize > (uint32_t)dwLimitCacheSize)
146 ClearImageCacheEntry(cache_info[i++].pStream);
150 auto it = m_ImageCache.find(pStream);
151 if (it == m_ImageCache.end())
154 m_nCacheSize -= it->second->EstimateSize();
158 if (m_pCurImageCacheEntry.Get() == it->second.get()) {
159 DCHECK(!m_pCurImageCacheEntry.IsOwned());
160 m_pCurImageCacheEntry.Reset();
162 m_ImageCache.erase(it);
167 const CPDF_Dictionary* pFormResources,
168 const CPDF_Dictionary* pPageResources,
172 const CFX_Size& max_size_required) {
174 if (m_pPage->GetDocument() != pImage->GetDocument())
177 RetainPtr<
const CPDF_Stream> pStream = pImage->GetStream();
178 const auto it = m_ImageCache.find(pStream);
179 m_bCurFindCache = it != m_ImageCache.end();
180 if (m_bCurFindCache) {
181 m_pCurImageCacheEntry = it->second.get();
183 m_pCurImageCacheEntry = std::make_unique<Entry>(std::move(pImage));
185 CPDF_DIB::
LoadState ret = m_pCurImageCacheEntry->StartGetCachedBitmap(
186 this, pFormResources, pPageResources, bStdCS, eFamily, bLoadMask,
192 if (!m_bCurFindCache)
193 m_ImageCache[pStream] = m_pCurImageCacheEntry.Release();
195 if (ret == CPDF_DIB::LoadState::kFail)
196 m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
202 bool ret = m_pCurImageCacheEntry->Continue(pPause,
this);
207 if (!m_bCurFindCache) {
208 m_ImageCache[m_pCurImageCacheEntry->GetImage()->GetStream()] =
209 m_pCurImageCacheEntry.Release();
211 m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
216 RetainPtr<
const CPDF_Stream> pStream = pImage->GetStream();
217 const auto it = m_ImageCache.find(pStream);
218 if (it == m_ImageCache.end())
221 Entry* pEntry = it->second.get();
222 m_nCacheSize -= pEntry->EstimateSize();
224 m_nCacheSize += pEntry->EstimateSize();
228 return m_pCurImageCacheEntry->GetMatteColor();
232 return m_pCurImageCacheEntry->DetachBitmap();
236 return m_pCurImageCacheEntry->DetachMask();
240 : m_pImage(std::move(pImage)) {}
245 m_pCachedBitmap.Reset();
250 return std::move(m_pCurBitmap);
254 return std::move(m_pCurMask);
259 const CPDF_Dictionary* pFormResources,
260 const CPDF_Dictionary* pPageResources,
264 const CFX_Size& max_size_required) {
265 if (m_pCachedBitmap && IsCacheValid(max_size_required)) {
266 m_pCurBitmap = m_pCachedBitmap;
267 m_pCurMask = m_pCachedMask;
271 m_pCurBitmap = m_pImage->CreateNewDIB();
272 CPDF_DIB::
LoadState ret = m_pCurBitmap.AsRaw<CPDF_DIB>()->StartLoadDIBBase(
273 true, pFormResources, pPageResources, bStdCS, eFamily, bLoadMask,
275 m_bCachedSetMaxSizeRequired =
276 (max_size_required.width != 0 && max_size_required.height != 0);
281 ContinueGetCachedBitmap(pPageImageCache);
283 m_pCurBitmap.Reset();
291 m_pCurBitmap.AsRaw<CPDF_DIB>()->ContinueLoadDIBBase(pPause);
296 ContinueGetCachedBitmap(pPageImageCache);
298 m_pCurBitmap.Reset();
304 m_MatteColor = m_pCurBitmap.AsRaw<CPDF_DIB>()->GetMatteColor();
305 m_pCurMask = m_pCurBitmap.AsRaw<CPDF_DIB>()->DetachMask();
307 if (m_pCurBitmap->GetPitch() * m_pCurBitmap->GetHeight() < kHugeImageSize) {
308 m_pCachedBitmap = MakeCachedImage(m_pCurBitmap,
true);
309 m_pCurBitmap.Reset();
311 m_pCachedBitmap = MakeCachedImage(m_pCurBitmap,
false);
314 m_pCachedMask = MakeCachedImage(m_pCurMask,
true);
317 m_pCurBitmap = m_pCachedBitmap;
318 m_pCurMask = m_pCachedMask;
325 m_dwCacheSize += m_pCachedBitmap->GetEstimatedImageMemoryBurden();
327 m_dwCacheSize += m_pCachedMask->GetEstimatedImageMemoryBurden();
331 const CFX_Size& max_size_required)
const {
332 if (!m_bCachedSetMaxSizeRequired) {
335 if (max_size_required.width == 0 && max_size_required.height == 0) {
339 return (m_pCachedBitmap->GetWidth() >= max_size_required.width) &&
340 (m_pCachedBitmap->GetHeight() >= max_size_required.height);
void ResetBitmapForImage(RetainPtr< CPDF_Image > pImage)
RetainPtr< CFX_DIBBase > DetachCurBitmap()
bool StartGetCachedBitmap(RetainPtr< CPDF_Image > pImage, const CPDF_Dictionary *pFormResources, const CPDF_Dictionary *pPageResources, bool bStdCS, CPDF_ColorSpace::Family eFamily, bool bLoadMask, const CFX_Size &max_size_required)
bool Continue(PauseIndicatorIface *pPause)
uint32_t GetCurMatteColor() const
void CacheOptimization(int32_t dwLimitCacheSize)
CPDF_PageImageCache(CPDF_Page *pPage)
RetainPtr< CFX_DIBBase > DetachCurMask()
uint32_t GetTimeCount() const