From 1629f609d3722f25491bbbb53b2cce97a03a5867 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 21 Apr 2017 14:11:26 -0700 Subject: Add CFX_RenderDevice::AutoRestorer() Avoid cleanup on every return path. Change-Id: I6978adb6f31020d812ac88c5d46c703d1461d373 Reviewed-on: https://pdfium-review.googlesource.com/4435 Reviewed-by: Lei Zhang Reviewed-by: Tom Sepez Commit-Queue: Tom Sepez --- core/fpdfapi/render/cpdf_rendercontext.cpp | 22 ++++---------- core/fpdfapi/render/cpdf_renderstatus.cpp | 47 ++++++++++++------------------ 2 files changed, 25 insertions(+), 44 deletions(-) (limited to 'core/fpdfapi/render') diff --git a/core/fpdfapi/render/cpdf_rendercontext.cpp b/core/fpdfapi/render/cpdf_rendercontext.cpp index 07af2cc172..e4ef9af94d 100644 --- a/core/fpdfapi/render/cpdf_rendercontext.cpp +++ b/core/fpdfapi/render/cpdf_rendercontext.cpp @@ -64,34 +64,24 @@ void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice, const CPDF_RenderOptions* pOptions, const CFX_Matrix* pLastMatrix) { for (auto& layer : m_Layers) { - pDevice->SaveState(); + CFX_RenderDevice::StateRestorer restorer(pDevice); + CPDF_RenderStatus status; if (pLastMatrix) { CFX_Matrix FinalMatrix = layer.m_Matrix; FinalMatrix.Concat(*pLastMatrix); - CPDF_RenderStatus status; status.Initialize(this, pDevice, pLastMatrix, pStopObj, nullptr, nullptr, pOptions, layer.m_pObjectHolder->m_Transparency, false, nullptr); status.RenderObjectList(layer.m_pObjectHolder, &FinalMatrix); - if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) - m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize); - if (status.m_bStopped) { - pDevice->RestoreState(false); - break; - } } else { - CPDF_RenderStatus status; status.Initialize(this, pDevice, nullptr, pStopObj, nullptr, nullptr, pOptions, layer.m_pObjectHolder->m_Transparency, false, nullptr); status.RenderObjectList(layer.m_pObjectHolder, &layer.m_Matrix); - if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) - m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize); - if (status.m_bStopped) { - pDevice->RestoreState(false); - break; - } } - pDevice->RestoreState(false); + if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) + m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize); + if (status.m_bStopped) + break; } } diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp index 465105a64d..d41561c057 100644 --- a/core/fpdfapi/render/cpdf_renderstatus.cpp +++ b/core/fpdfapi/render/cpdf_renderstatus.cpp @@ -1252,10 +1252,11 @@ bool CPDF_RenderStatus::ProcessForm(const CPDF_FormObject* pFormObj, &m_Options, m_Transparency, m_bDropObjects, pResources, false); status.m_curBlend = m_curBlend; - m_pDevice->SaveState(); - status.RenderObjectList(pFormObj->m_pForm.get(), &matrix); - m_bStopped = status.m_bStopped; - m_pDevice->RestoreState(false); + { + CFX_RenderDevice::StateRestorer restorer(m_pDevice); + status.RenderObjectList(pFormObj->m_pForm.get(), &matrix); + m_bStopped = status.m_bStopped; + } #if defined _SKIA_SUPPORT_ DebugVerifyDeviceIsPreMultiplied(); #endif @@ -1876,9 +1877,9 @@ bool CPDF_RenderStatus::ProcessType3Text(CPDF_TextObject* textobj, pFormResource, false, pType3Char, fill_argb); status.m_Type3FontCache = m_Type3FontCache; status.m_Type3FontCache.push_back(pType3Font); - m_pDevice->SaveState(); + + CFX_RenderDevice::StateRestorer restorer(m_pDevice); status.RenderObjectList(pType3Char->m_pForm.get(), &matrix); - m_pDevice->RestoreState(false); } else { CFX_FloatRect rect_f = pType3Char->m_pForm->CalcBoundingBox(); matrix.TransformRect(rect_f); @@ -2131,22 +2132,19 @@ void CPDF_RenderStatus::DrawShadingPattern(CPDF_ShadingPattern* pattern, if (!pattern->Load()) return; - m_pDevice->SaveState(); + CFX_RenderDevice::StateRestorer restorer(m_pDevice); if (pPageObj->IsPath()) { - if (!SelectClipPath(pPageObj->AsPath(), pObj2Device, bStroke)) { - m_pDevice->RestoreState(false); + if (!SelectClipPath(pPageObj->AsPath(), pObj2Device, bStroke)) return; - } } else if (pPageObj->IsImage()) { m_pDevice->SetClip_Rect(pPageObj->GetBBox(pObj2Device)); } else { return; } FX_RECT rect; - if (GetObjectClippedRect(pPageObj, pObj2Device, false, rect)) { - m_pDevice->RestoreState(false); + if (GetObjectClippedRect(pPageObj, pObj2Device, false, rect)) return; - } + CFX_Matrix matrix = *pattern->pattern_to_form(); matrix.Concat(*pObj2Device); GetScaledMatrix(matrix); @@ -2155,7 +2153,6 @@ void CPDF_RenderStatus::DrawShadingPattern(CPDF_ShadingPattern* pattern, : pPageObj->m_GeneralState.GetFillAlpha())); DrawShading(pattern, &matrix, rect, alpha, m_Options.m_ColorMode == RENDER_COLOR_ALPHA); - m_pDevice->RestoreState(false); } void CPDF_RenderStatus::ProcessShading(const CPDF_ShadingObject* pShadingObj, @@ -2180,22 +2177,20 @@ void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern, if (!pPattern->Load()) return; - m_pDevice->SaveState(); + CFX_RenderDevice::StateRestorer restorer(m_pDevice); if (pPageObj->IsPath()) { - if (!SelectClipPath(pPageObj->AsPath(), pObj2Device, bStroke)) { - m_pDevice->RestoreState(false); + if (!SelectClipPath(pPageObj->AsPath(), pObj2Device, bStroke)) return; - } } else if (pPageObj->IsImage()) { m_pDevice->SetClip_Rect(pPageObj->GetBBox(pObj2Device)); } else { return; } + FX_RECT clip_box = m_pDevice->GetClipBox(); - if (clip_box.IsEmpty()) { - m_pDevice->RestoreState(false); + if (clip_box.IsEmpty()) return; - } + CFX_Matrix dCTM = m_pDevice->GetCTM(); float sa = fabs(dCTM.a); float sd = fabs(dCTM.d); @@ -2254,17 +2249,15 @@ void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern, CFX_Matrix matrix = *pObj2Device; matrix.Translate(original.x - mtPattern2Device.e, original.y - mtPattern2Device.f); - m_pDevice->SaveState(); + CFX_RenderDevice::StateRestorer restorer2(m_pDevice); CPDF_RenderStatus status; status.Initialize(m_pContext, m_pDevice, nullptr, nullptr, this, pStates.get(), &m_Options, pPattern->form()->m_Transparency, m_bDropObjects, pFormResource); status.RenderObjectList(pPattern->form(), &matrix); - m_pDevice->RestoreState(false); } } - m_pDevice->RestoreState(false); return; } if (bAligned) { @@ -2299,10 +2292,9 @@ void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern, m_pContext->GetDocument(), m_pContext->GetPageCache(), pPattern, pObj2Device, width, height, m_Options.m_Flags); } - if (!pPatternBitmap) { - m_pDevice->RestoreState(false); + if (!pPatternBitmap) return; - } + if (m_Options.m_ColorMode == RENDER_COLOR_GRAY) { pPatternBitmap->ConvertColorScale(m_Options.m_ForeColor, m_Options.m_BackColor); @@ -2364,7 +2356,6 @@ void CPDF_RenderStatus::DrawTilingPattern(CPDF_TilingPattern* pPattern, } CompositeDIBitmap(pScreen, clip_box.left, clip_box.top, 0, 255, FXDIB_BLEND_NORMAL, false); - m_pDevice->RestoreState(false); } void CPDF_RenderStatus::DrawPathWithPattern(CPDF_PathObject* pPathObj, -- cgit v1.2.3