• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_pagerendercache.h"
8 
9 #include <algorithm>
10 #include <vector>
11 
12 #include "core/fpdfapi/page/cpdf_image.h"
13 #include "core/fpdfapi/page/cpdf_page.h"
14 #include "core/fpdfapi/render/cpdf_imagecacheentry.h"
15 #include "core/fpdfapi/render/cpdf_renderstatus.h"
16 #include "core/fxge/dib/cfx_dibitmap.h"
17 #include "third_party/base/ptr_util.h"
18 
19 namespace {
20 
21 struct CacheInfo {
CacheInfo__anon21e0f99a0111::CacheInfo22   CacheInfo(uint32_t t, CPDF_Stream* stream) : time(t), pStream(stream) {}
23 
24   uint32_t time;
25   CPDF_Stream* pStream;
26 
operator <__anon21e0f99a0111::CacheInfo27   bool operator<(const CacheInfo& other) const { return time < other.time; }
28 };
29 
30 }  // namespace
31 
CPDF_PageRenderCache(CPDF_Page * pPage)32 CPDF_PageRenderCache::CPDF_PageRenderCache(CPDF_Page* pPage) : m_pPage(pPage) {}
33 
34 CPDF_PageRenderCache::~CPDF_PageRenderCache() = default;
35 
CacheOptimization(int32_t dwLimitCacheSize)36 void CPDF_PageRenderCache::CacheOptimization(int32_t dwLimitCacheSize) {
37   if (m_nCacheSize <= (uint32_t)dwLimitCacheSize)
38     return;
39 
40   size_t nCount = m_ImageCache.size();
41   std::vector<CacheInfo> cache_info;
42   cache_info.reserve(nCount);
43   for (const auto& it : m_ImageCache) {
44     cache_info.emplace_back(it.second->GetTimeCount(),
45                             it.second->GetImage()->GetStream());
46   }
47   std::sort(cache_info.begin(), cache_info.end());
48 
49   // Check if time value is about to roll over and reset all entries.
50   // The comparision is legal because uint32_t is an unsigned type.
51   uint32_t nTimeCount = m_nTimeCount;
52   if (nTimeCount + 1 < nTimeCount) {
53     for (size_t i = 0; i < nCount; i++)
54       m_ImageCache[cache_info[i].pStream]->m_dwTimeCount = i;
55     m_nTimeCount = nCount;
56   }
57 
58   size_t i = 0;
59   while (i + 15 < nCount)
60     ClearImageCacheEntry(cache_info[i++].pStream);
61 
62   while (i < nCount && m_nCacheSize > (uint32_t)dwLimitCacheSize)
63     ClearImageCacheEntry(cache_info[i++].pStream);
64 }
65 
ClearImageCacheEntry(CPDF_Stream * pStream)66 void CPDF_PageRenderCache::ClearImageCacheEntry(CPDF_Stream* pStream) {
67   auto it = m_ImageCache.find(pStream);
68   if (it == m_ImageCache.end())
69     return;
70 
71   m_nCacheSize -= it->second->EstimateSize();
72   m_ImageCache.erase(it);
73 }
74 
StartGetCachedBitmap(const RetainPtr<CPDF_Image> & pImage,bool bStdCS,uint32_t GroupFamily,bool bLoadMask,CPDF_RenderStatus * pRenderStatus)75 bool CPDF_PageRenderCache::StartGetCachedBitmap(
76     const RetainPtr<CPDF_Image>& pImage,
77     bool bStdCS,
78     uint32_t GroupFamily,
79     bool bLoadMask,
80     CPDF_RenderStatus* pRenderStatus) {
81   CPDF_Stream* pStream = pImage->GetStream();
82   const auto it = m_ImageCache.find(pStream);
83   m_bCurFindCache = it != m_ImageCache.end();
84   if (m_bCurFindCache) {
85     m_pCurImageCacheEntry = it->second.get();
86   } else {
87     m_pCurImageCacheEntry = pdfium::MakeUnique<CPDF_ImageCacheEntry>(
88         m_pPage->GetDocument(), pImage);
89   }
90   CPDF_DIB::LoadState ret = m_pCurImageCacheEntry->StartGetCachedBitmap(
91       pRenderStatus->GetFormResource(), m_pPage->m_pPageResources.Get(), bStdCS,
92       GroupFamily, bLoadMask, pRenderStatus);
93   if (ret == CPDF_DIB::LoadState::kContinue)
94     return true;
95 
96   m_nTimeCount++;
97   if (!m_bCurFindCache)
98     m_ImageCache[pStream] = m_pCurImageCacheEntry.Release();
99 
100   if (ret == CPDF_DIB::LoadState::kFail)
101     m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
102 
103   return false;
104 }
105 
Continue(PauseIndicatorIface * pPause,CPDF_RenderStatus * pRenderStatus)106 bool CPDF_PageRenderCache::Continue(PauseIndicatorIface* pPause,
107                                     CPDF_RenderStatus* pRenderStatus) {
108   bool ret = m_pCurImageCacheEntry->Continue(pPause, pRenderStatus);
109   if (ret)
110     return true;
111 
112   m_nTimeCount++;
113   if (!m_bCurFindCache) {
114     m_ImageCache[m_pCurImageCacheEntry->GetImage()->GetStream()] =
115         m_pCurImageCacheEntry.Release();
116   }
117   m_nCacheSize += m_pCurImageCacheEntry->EstimateSize();
118   return false;
119 }
120 
ResetBitmapForImage(const RetainPtr<CPDF_Image> & pImage)121 void CPDF_PageRenderCache::ResetBitmapForImage(
122     const RetainPtr<CPDF_Image>& pImage) {
123   CPDF_ImageCacheEntry* pEntry;
124   CPDF_Stream* pStream = pImage->GetStream();
125   const auto it = m_ImageCache.find(pStream);
126   if (it == m_ImageCache.end())
127     return;
128 
129   pEntry = it->second.get();
130   m_nCacheSize -= pEntry->EstimateSize();
131   pEntry->Reset();
132   m_nCacheSize += pEntry->EstimateSize();
133 }
134