Return retained object from CPDF_Form and CPDF_GeneralState methods.

-- Introduce locals where appropriate to de-duplicate calls.

Change-Id: Iefae24eaba96e93a1d62b895af8ef73e60795657
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98616
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 03e16cb..80f9142 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -340,14 +340,14 @@
     return;
   }
 
-  const CPDF_Stream* pStream = pFormObj->form()->GetStream();
+  RetainPtr<const CPDF_Stream> pStream = pFormObj->form()->GetStream();
   if (!pStream)
     return;
 
   *buf << "q\n";
   WriteMatrix(*buf, pFormObj->form_matrix()) << " cm ";
 
-  ByteString name = RealizeResource(pStream, "XObject");
+  ByteString name = RealizeResource(pStream.Get(), "XObject");
   *buf << "/" << PDF_NameEncode(name) << " Do Q\n";
 }
 
diff --git a/core/fpdfapi/page/cpdf_allstates.cpp b/core/fpdfapi/page/cpdf_allstates.cpp
index bba6333..a9d8820 100644
--- a/core/fpdfapi/page/cpdf_allstates.cpp
+++ b/core/fpdfapi/page/cpdf_allstates.cpp
@@ -97,7 +97,7 @@
         }
         [[fallthrough]];
       case FXBSTR_ID('T', 'R', '2', 0):
-        m_GeneralState.SetTR(!pObject->IsName() ? pObject.Get() : nullptr);
+        m_GeneralState.SetTR(!pObject->IsName() ? std::move(pObject) : nullptr);
         break;
       case FXBSTR_ID('B', 'M', 0, 0): {
         const CPDF_Array* pArray = pObject->AsArray();
diff --git a/core/fpdfapi/page/cpdf_contentparser.cpp b/core/fpdfapi/page/cpdf_contentparser.cpp
index 282c1e3..33bf057 100644
--- a/core/fpdfapi/page/cpdf_contentparser.cpp
+++ b/core/fpdfapi/page/cpdf_contentparser.cpp
@@ -111,8 +111,7 @@
     pState->SetFillAlpha(1.0f);
     pState->SetSoftMask(nullptr);
   }
-  m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(
-      pdfium::WrapRetain(pForm->GetStream()));
+  m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(pForm->GetStream());
   m_pSingleStream->LoadAllDataFiltered();
   m_Data = m_pSingleStream->GetSpan();
 }
diff --git a/core/fpdfapi/page/cpdf_form.cpp b/core/fpdfapi/page/cpdf_form.cpp
index daa810a..8b5d20b 100644
--- a/core/fpdfapi/page/cpdf_form.cpp
+++ b/core/fpdfapi/page/cpdf_form.cpp
@@ -107,8 +107,8 @@
   return CFX_FloatRect(left, bottom, right, top);
 }
 
-const CPDF_Stream* CPDF_Form::GetStream() const {
-  return m_pFormStream.Get();
+RetainPtr<const CPDF_Stream> CPDF_Form::GetStream() const {
+  return m_pFormStream;
 }
 
 absl::optional<std::pair<RetainPtr<CFX_DIBitmap>, CFX_Matrix>>
diff --git a/core/fpdfapi/page/cpdf_form.h b/core/fpdfapi/page/cpdf_form.h
index 2586407..f5c0a28 100644
--- a/core/fpdfapi/page/cpdf_form.h
+++ b/core/fpdfapi/page/cpdf_form.h
@@ -50,7 +50,7 @@
                     const CFX_Matrix* pParentMatrix,
                     std::set<const uint8_t*>* pParsedSet);
 
-  const CPDF_Stream* GetStream() const;
+  RetainPtr<const CPDF_Stream> GetStream() const;
 
  private:
   void ParseContentInternal(const CPDF_AllStates* pGraphicStates,
diff --git a/core/fpdfapi/page/cpdf_generalstate.cpp b/core/fpdfapi/page/cpdf_generalstate.cpp
index 75cf5a4..c418462 100644
--- a/core/fpdfapi/page/cpdf_generalstate.cpp
+++ b/core/fpdfapi/page/cpdf_generalstate.cpp
@@ -145,9 +145,9 @@
   m_Ref.GetPrivateCopy()->m_StrokeAlpha = alpha;
 }
 
-const CPDF_Dictionary* CPDF_GeneralState::GetSoftMask() const {
+RetainPtr<const CPDF_Dictionary> CPDF_GeneralState::GetSoftMask() const {
   const StateData* pData = m_Ref.GetObject();
-  return pData ? pData->m_pSoftMask.Get() : nullptr;
+  return pData ? pData->m_pSoftMask : nullptr;
 }
 
 RetainPtr<CPDF_Dictionary> CPDF_GeneralState::GetMutableSoftMask() {
@@ -159,13 +159,13 @@
   m_Ref.GetPrivateCopy()->m_pSoftMask = std::move(pDict);
 }
 
-const CPDF_Object* CPDF_GeneralState::GetTR() const {
+RetainPtr<const CPDF_Object> CPDF_GeneralState::GetTR() const {
   const StateData* pData = m_Ref.GetObject();
-  return pData ? pData->m_pTR.Get() : nullptr;
+  return pData ? pData->m_pTR : nullptr;
 }
 
-void CPDF_GeneralState::SetTR(const CPDF_Object* pObject) {
-  m_Ref.GetPrivateCopy()->m_pTR.Reset(pObject);
+void CPDF_GeneralState::SetTR(RetainPtr<const CPDF_Object> pObject) {
+  m_Ref.GetPrivateCopy()->m_pTR = std::move(pObject);
 }
 
 RetainPtr<CPDF_TransferFunc> CPDF_GeneralState::GetTransferFunc() const {
diff --git a/core/fpdfapi/page/cpdf_generalstate.h b/core/fpdfapi/page/cpdf_generalstate.h
index 140b6bd..e91c4af 100644
--- a/core/fpdfapi/page/cpdf_generalstate.h
+++ b/core/fpdfapi/page/cpdf_generalstate.h
@@ -39,12 +39,12 @@
   float GetStrokeAlpha() const;
   void SetStrokeAlpha(float alpha);
 
-  const CPDF_Dictionary* GetSoftMask() const;
+  RetainPtr<const CPDF_Dictionary> GetSoftMask() const;
   RetainPtr<CPDF_Dictionary> GetMutableSoftMask();
   void SetSoftMask(RetainPtr<CPDF_Dictionary> pDict);
 
-  const CPDF_Object* GetTR() const;
-  void SetTR(const CPDF_Object* pObject);
+  RetainPtr<const CPDF_Object> GetTR() const;
+  void SetTR(RetainPtr<const CPDF_Object> pObject);
 
   RetainPtr<CPDF_TransferFunc> GetTransferFunc() const;
   void SetTransferFunc(RetainPtr<CPDF_TransferFunc> pFunc);
diff --git a/core/fpdfapi/render/cpdf_imagerenderer.cpp b/core/fpdfapi/render/cpdf_imagerenderer.cpp
index 8b4f5ca..26c2d6a 100644
--- a/core/fpdfapi/render/cpdf_imagerenderer.cpp
+++ b/core/fpdfapi/render/cpdf_imagerenderer.cpp
@@ -108,9 +108,10 @@
       !m_Loader.GetMask()) {
     return StartBitmapAlpha();
   }
-  if (state.GetTR()) {
+  RetainPtr<const CPDF_Object> pTR = state.GetTR();
+  if (pTR) {
     if (!state.GetTransferFunc())
-      state.SetTransferFunc(m_pRenderStatus->GetTransferFunc(state.GetTR()));
+      state.SetTransferFunc(m_pRenderStatus->GetTransferFunc(std::move(pTR)));
 
     if (state.GetTransferFunc() && !state.GetTransferFunc()->GetIdentity())
       m_pDIBBase = m_Loader.TranslateImage(state.GetTransferFunc());
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index 205b8aa..affd4eb 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -480,10 +480,10 @@
 
   int32_t alpha =
       static_cast<int32_t>((pObj->m_GeneralState.GetFillAlpha() * 255));
-  if (pObj->m_GeneralState.GetTR()) {
+  RetainPtr<const CPDF_Object> pTR = pObj->m_GeneralState.GetTR();
+  if (pTR) {
     if (!pObj->m_GeneralState.GetTransferFunc()) {
-      pObj->m_GeneralState.SetTransferFunc(
-          GetTransferFunc(pObj->m_GeneralState.GetTR()));
+      pObj->m_GeneralState.SetTransferFunc(GetTransferFunc(std::move(pTR)));
     }
     if (pObj->m_GeneralState.GetTransferFunc()) {
       colorref =
@@ -509,10 +509,10 @@
 
   int32_t alpha = static_cast<int32_t>(pObj->m_GeneralState.GetStrokeAlpha() *
                                        255);  // not rounded.
-  if (pObj->m_GeneralState.GetTR()) {
+  RetainPtr<const CPDF_Object> pTR = pObj->m_GeneralState.GetTR();
+  if (pTR) {
     if (!pObj->m_GeneralState.GetTransferFunc()) {
-      pObj->m_GeneralState.SetTransferFunc(
-          GetTransferFunc(pObj->m_GeneralState.GetTR()));
+      pObj->m_GeneralState.SetTransferFunc(GetTransferFunc(std::move(pTR)));
     }
     if (pObj->m_GeneralState.GetTransferFunc()) {
       colorref =
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 7b034f4..2d6ff76 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -481,8 +481,7 @@
   if (pPageObj->m_GeneralState.GetBlendType() != BlendMode::kNormal)
     return true;
 
-  const CPDF_Dictionary* pSMaskDict = pPageObj->m_GeneralState.GetSoftMask();
-  if (pSMaskDict)
+  if (pPageObj->m_GeneralState.GetSoftMask())
     return true;
 
   if (pPageObj->m_GeneralState.GetFillAlpha() != 1.0f)
diff --git a/fpdfsdk/fpdf_ppo_embeddertest.cpp b/fpdfsdk/fpdf_ppo_embeddertest.cpp
index 4e93f9b..54fb00a 100644
--- a/fpdfsdk/fpdf_ppo_embeddertest.cpp
+++ b/fpdfsdk/fpdf_ppo_embeddertest.cpp
@@ -6,6 +6,7 @@
 
 #include "core/fpdfapi/page/cpdf_form.h"
 #include "core/fpdfapi/page/cpdf_formobject.h"
+#include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fxge/cfx_defaultrenderdevice.h"
 #include "fpdfsdk/cpdfsdk_helpers.h"
 #include "public/cpp/fpdf_scopers.h"