1 // Copyright 2016 The PDFium Authors 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 #ifndef CORE_FPDFAPI_PAGE_CPDF_PAGEIMAGECACHE_H_ 8 #define CORE_FPDFAPI_PAGE_CPDF_PAGEIMAGECACHE_H_ 9 10 #include <stdint.h> 11 12 #include <functional> 13 #include <map> 14 #include <memory> 15 16 #include "core/fpdfapi/page/cpdf_dib.h" 17 #include "core/fxcrt/maybe_owned.h" 18 #include "core/fxcrt/retain_ptr.h" 19 #include "core/fxcrt/unowned_ptr.h" 20 21 class CPDF_Dictionary; 22 class CPDF_Image; 23 class CPDF_Page; 24 class CPDF_Stream; 25 class PauseIndicatorIface; 26 27 class CPDF_PageImageCache { 28 public: 29 explicit CPDF_PageImageCache(CPDF_Page* pPage); 30 ~CPDF_PageImageCache(); 31 32 void ResetBitmapForImage(RetainPtr<CPDF_Image> pImage); 33 void CacheOptimization(int32_t dwLimitCacheSize); GetTimeCount()34 uint32_t GetTimeCount() const { return m_nTimeCount; } GetPage()35 CPDF_Page* GetPage() const { return m_pPage; } 36 37 bool StartGetCachedBitmap(RetainPtr<CPDF_Image> pImage, 38 const CPDF_Dictionary* pFormResources, 39 const CPDF_Dictionary* pPageResources, 40 bool bStdCS, 41 CPDF_ColorSpace::Family eFamily, 42 bool bLoadMask, 43 const CFX_Size& max_size_required); 44 45 bool Continue(PauseIndicatorIface* pPause); 46 47 uint32_t GetCurMatteColor() const; 48 RetainPtr<CFX_DIBBase> DetachCurBitmap(); 49 RetainPtr<CFX_DIBBase> DetachCurMask(); 50 51 private: 52 class Entry { 53 public: 54 explicit Entry(RetainPtr<CPDF_Image> pImage); 55 ~Entry(); 56 57 void Reset(); EstimateSize()58 uint32_t EstimateSize() const { return m_dwCacheSize; } GetMatteColor()59 uint32_t GetMatteColor() const { return m_MatteColor; } GetTimeCount()60 uint32_t GetTimeCount() const { return m_dwTimeCount; } SetTimeCount(uint32_t count)61 void SetTimeCount(uint32_t count) { m_dwTimeCount = count; } GetImage()62 CPDF_Image* GetImage() const { return m_pImage.Get(); } 63 64 CPDF_DIB::LoadState StartGetCachedBitmap( 65 CPDF_PageImageCache* pPageImageCache, 66 const CPDF_Dictionary* pFormResources, 67 const CPDF_Dictionary* pPageResources, 68 bool bStdCS, 69 CPDF_ColorSpace::Family eFamily, 70 bool bLoadMask, 71 const CFX_Size& max_size_required); 72 73 // Returns whether to Continue() or not. 74 bool Continue(PauseIndicatorIface* pPause, 75 CPDF_PageImageCache* pPageImageCache); 76 77 RetainPtr<CFX_DIBBase> DetachBitmap(); 78 RetainPtr<CFX_DIBBase> DetachMask(); 79 80 private: 81 void ContinueGetCachedBitmap(CPDF_PageImageCache* pPageImageCache); 82 void CalcSize(); 83 bool IsCacheValid(const CFX_Size& max_size_required) const; 84 85 uint32_t m_dwTimeCount = 0; 86 uint32_t m_MatteColor = 0; 87 uint32_t m_dwCacheSize = 0; 88 RetainPtr<CPDF_Image> const m_pImage; 89 RetainPtr<CFX_DIBBase> m_pCurBitmap; 90 RetainPtr<CFX_DIBBase> m_pCurMask; 91 RetainPtr<CFX_DIBBase> m_pCachedBitmap; 92 RetainPtr<CFX_DIBBase> m_pCachedMask; 93 bool m_bCachedSetMaxSizeRequired = false; 94 }; 95 96 void ClearImageCacheEntry(const CPDF_Stream* pStream); 97 98 UnownedPtr<CPDF_Page> const m_pPage; 99 std::map<RetainPtr<const CPDF_Stream>, std::unique_ptr<Entry>, std::less<>> 100 m_ImageCache; 101 MaybeOwned<Entry> m_pCurImageCacheEntry; 102 uint32_t m_nTimeCount = 0; 103 uint32_t m_nCacheSize = 0; 104 bool m_bCurFindCache = false; 105 }; 106 107 #endif // CORE_FPDFAPI_PAGE_CPDF_PAGEIMAGECACHE_H_ 108