From 05e6a807311df5c3dfc4cafa56b85614f9bae6d3 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 3 Apr 2017 14:51:26 -0700 Subject: RefCount CPDF_Type3Cache all the time. Prefer internal refcounts to external scheme. Change-Id: Ia99d5fc635a51a38f41b1d64ea44d8202685ddc7 Reviewed-on: https://pdfium-review.googlesource.com/3617 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- core/fpdfapi/render/cpdf_docrenderdata.cpp | 35 ++++++++++-------------------- core/fpdfapi/render/cpdf_docrenderdata.h | 9 +++----- core/fpdfapi/render/cpdf_renderstatus.cpp | 21 ++++++++++-------- core/fpdfapi/render/cpdf_renderstatus.h | 2 +- core/fpdfapi/render/cpdf_type3cache.h | 10 ++++++--- 5 files changed, 35 insertions(+), 42 deletions(-) (limited to 'core/fpdfapi') diff --git a/core/fpdfapi/render/cpdf_docrenderdata.cpp b/core/fpdfapi/render/cpdf_docrenderdata.cpp index 39d1fcd0b4..2d0bca4497 100644 --- a/core/fpdfapi/render/cpdf_docrenderdata.cpp +++ b/core/fpdfapi/render/cpdf_docrenderdata.cpp @@ -32,10 +32,7 @@ CPDF_DocRenderData::~CPDF_DocRenderData() { void CPDF_DocRenderData::Clear(bool bRelease) { for (auto it = m_Type3FaceMap.begin(); it != m_Type3FaceMap.end();) { auto curr_it = it++; - CPDF_CountedObject* cache = curr_it->second; - if (bRelease || cache->use_count() < 2) { - delete cache->get(); - delete cache; + if (bRelease || curr_it->second->HasOneRef()) { m_Type3FaceMap.erase(curr_it); } } @@ -47,29 +44,21 @@ void CPDF_DocRenderData::Clear(bool bRelease) { } } -CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont) { - CPDF_CountedObject* pCache; +CFX_RetainPtr CPDF_DocRenderData::GetCachedType3( + CPDF_Type3Font* pFont) { auto it = m_Type3FaceMap.find(pFont); - if (it == m_Type3FaceMap.end()) { - pCache = new CPDF_CountedObject( - pdfium::MakeUnique(pFont)); - m_Type3FaceMap[pFont] = pCache; - } else { - pCache = it->second; - } - return pCache->AddRef(); + if (it != m_Type3FaceMap.end()) + return it->second; + + auto pCache = pdfium::MakeRetain(pFont); + m_Type3FaceMap[pFont] = pCache; + return pCache; } -void CPDF_DocRenderData::ReleaseCachedType3(CPDF_Type3Font* pFont) { +void CPDF_DocRenderData::MaybePurgeCachedType3(CPDF_Type3Font* pFont) { auto it = m_Type3FaceMap.find(pFont); - if (it != m_Type3FaceMap.end()) { - it->second->RemoveRef(); - if (it->second->use_count() < 2) { - delete it->second->get(); - delete it->second; - m_Type3FaceMap.erase(it); - } - } + if (it != m_Type3FaceMap.end() && it->second->HasOneRef()) + m_Type3FaceMap.erase(it); } CFX_RetainPtr CPDF_DocRenderData::GetTransferFunc( diff --git a/core/fpdfapi/render/cpdf_docrenderdata.h b/core/fpdfapi/render/cpdf_docrenderdata.h index 5daee34176..7952f95991 100644 --- a/core/fpdfapi/render/cpdf_docrenderdata.h +++ b/core/fpdfapi/render/cpdf_docrenderdata.h @@ -23,8 +23,8 @@ class CPDF_DocRenderData { explicit CPDF_DocRenderData(CPDF_Document* pPDFDoc); ~CPDF_DocRenderData(); - CPDF_Type3Cache* GetCachedType3(CPDF_Type3Font* pFont); - void ReleaseCachedType3(CPDF_Type3Font* pFont); + CFX_RetainPtr GetCachedType3(CPDF_Type3Font* pFont); + void MaybePurgeCachedType3(CPDF_Type3Font* pFont); CFX_RetainPtr GetTransferFunc(CPDF_Object* pObj); void MaybePurgeTransferFunc(CPDF_Object* pOb); @@ -32,11 +32,8 @@ class CPDF_DocRenderData { void Clear(bool bRelease); private: - using CPDF_Type3CacheMap = - std::map*>; - CPDF_Document* m_pPDFDoc; // Not Owned - CPDF_Type3CacheMap m_Type3FaceMap; + std::map> m_Type3FaceMap; std::map> m_TransferFuncMap; }; diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp index 69293e6ed3..09e5bc1433 100644 --- a/core/fpdfapi/render/cpdf_renderstatus.cpp +++ b/core/fpdfapi/render/cpdf_renderstatus.cpp @@ -66,11 +66,12 @@ namespace { void ReleaseCachedType3(CPDF_Type3Font* pFont) { - if (!pFont->m_pDocument) + CPDF_Document* pDoc = pFont->m_pDocument; + if (!pDoc) return; - pFont->m_pDocument->GetRenderData()->ReleaseCachedType3(pFont); - pFont->m_pDocument->GetPageData()->ReleaseFont(pFont->GetFontDict()); + pDoc->GetRenderData()->MaybePurgeCachedType3(pFont); + pDoc->GetPageData()->ReleaseFont(pFont->GetFontDict()); } class CPDF_RefType3Cache { @@ -1795,12 +1796,14 @@ bool CPDF_RenderStatus::ProcessText(CPDF_TextObject* textobj, &text_matrix, fill_argb, &m_Options); } -CPDF_Type3Cache* CPDF_RenderStatus::GetCachedType3(CPDF_Type3Font* pFont) { - if (!pFont->m_pDocument) { +CFX_RetainPtr CPDF_RenderStatus::GetCachedType3( + CPDF_Type3Font* pFont) { + CPDF_Document* pDoc = pFont->m_pDocument; + if (!pDoc) return nullptr; - } - pFont->m_pDocument->GetPageData()->GetFont(pFont->GetFontDict()); - return pFont->m_pDocument->GetRenderData()->GetCachedType3(pFont); + + pDoc->GetPageData()->GetFont(pFont->GetFontDict()); + return pDoc->GetRenderData()->GetCachedType3(pFont); } // TODO(npm): Font fallback for type 3 fonts? (Completely separate code!!) @@ -1902,7 +1905,7 @@ bool CPDF_RenderStatus::ProcessType3Text(CPDF_TextObject* textobj, delete pStates; } else if (pType3Char->m_pBitmap) { if (device_class == FXDC_DISPLAY) { - CPDF_Type3Cache* pCache = GetCachedType3(pType3Font); + CFX_RetainPtr pCache = GetCachedType3(pType3Font); refTypeCache.m_dwCount++; CFX_GlyphBitmap* pBitmap = pCache->LoadGlyph(charcode, &matrix, sa, sd); if (!pBitmap) diff --git a/core/fpdfapi/render/cpdf_renderstatus.h b/core/fpdfapi/render/cpdf_renderstatus.h index 354c7b163a..e9d1f87387 100644 --- a/core/fpdfapi/render/cpdf_renderstatus.h +++ b/core/fpdfapi/render/cpdf_renderstatus.h @@ -141,7 +141,7 @@ class CPDF_RenderStatus { CFX_RetainPtr LoadSMask(CPDF_Dictionary* pSMaskDict, FX_RECT* pClipRect, const CFX_Matrix* pMatrix); - static CPDF_Type3Cache* GetCachedType3(CPDF_Type3Font* pFont); + static CFX_RetainPtr GetCachedType3(CPDF_Type3Font* pFont); static CPDF_GraphicStates* CloneObjStates(const CPDF_GraphicStates* pPathObj, bool bStroke); CFX_RetainPtr GetTransferFunc(CPDF_Object* pObject) const; diff --git a/core/fpdfapi/render/cpdf_type3cache.h b/core/fpdfapi/render/cpdf_type3cache.h index f03578650b..0b3cdcbfa4 100644 --- a/core/fpdfapi/render/cpdf_type3cache.h +++ b/core/fpdfapi/render/cpdf_type3cache.h @@ -10,16 +10,17 @@ #include #include "core/fpdfapi/font/cpdf_type3font.h" +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" class CPDF_Type3Glyphs; -class CPDF_Type3Cache { +class CPDF_Type3Cache : public CFX_Retainable { public: - explicit CPDF_Type3Cache(CPDF_Type3Font* pFont); - ~CPDF_Type3Cache(); + template + friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); CFX_GlyphBitmap* LoadGlyph(uint32_t charcode, const CFX_Matrix* pMatrix, @@ -27,6 +28,9 @@ class CPDF_Type3Cache { float retinaScaleY); private: + explicit CPDF_Type3Cache(CPDF_Type3Font* pFont); + ~CPDF_Type3Cache() override; + CFX_GlyphBitmap* RenderGlyph(CPDF_Type3Glyphs* pSize, uint32_t charcode, const CFX_Matrix* pMatrix, -- cgit v1.2.3