Add RetainPtr<>::AsRaw()

Avoid some ref-churn in creating temporaries when all that is needed
is an unowned reference.

Change-Id: I7d3661562d70835fcaccf888f54973794b1bf8a3
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/101910
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_pageimagecache.cpp b/core/fpdfapi/page/cpdf_pageimagecache.cpp
index a9d2a0b..63a1ad6 100644
--- a/core/fpdfapi/page/cpdf_pageimagecache.cpp
+++ b/core/fpdfapi/page/cpdf_pageimagecache.cpp
@@ -183,7 +183,7 @@
   }
 
   m_pCurBitmap = m_pImage->CreateNewDIB();
-  CPDF_DIB::LoadState ret = m_pCurBitmap.As<CPDF_DIB>()->StartLoadDIBBase(
+  CPDF_DIB::LoadState ret = m_pCurBitmap.AsRaw<CPDF_DIB>()->StartLoadDIBBase(
       true, pFormResources, pPageResources, bStdCS, eFamily, bLoadMask,
       max_size_required);
   if (ret == CPDF_DIB::LoadState::kContinue)
@@ -200,7 +200,7 @@
     PauseIndicatorIface* pPause,
     CPDF_PageImageCache* pPageImageCache) {
   CPDF_DIB::LoadState ret =
-      m_pCurBitmap.As<CPDF_DIB>()->ContinueLoadDIBBase(pPause);
+      m_pCurBitmap.AsRaw<CPDF_DIB>()->ContinueLoadDIBBase(pPause);
   if (ret == CPDF_DIB::LoadState::kContinue)
     return true;
 
@@ -213,8 +213,8 @@
 
 void CPDF_PageImageCache::Entry::ContinueGetCachedBitmap(
     CPDF_PageImageCache* pPageImageCache) {
-  m_MatteColor = m_pCurBitmap.As<CPDF_DIB>()->GetMatteColor();
-  m_pCurMask = m_pCurBitmap.As<CPDF_DIB>()->DetachMask();
+  m_MatteColor = m_pCurBitmap.AsRaw<CPDF_DIB>()->GetMatteColor();
+  m_pCurMask = m_pCurBitmap.AsRaw<CPDF_DIB>()->DetachMask();
   m_dwTimeCount = pPageImageCache->GetTimeCount();
   if (m_pCurBitmap->GetPitch() * m_pCurBitmap->GetHeight() < kHugeImageSize) {
     m_pCachedBitmap = m_pCurBitmap->Realize();
diff --git a/core/fxcrt/css/cfx_csscomputedstyle.cpp b/core/fxcrt/css/cfx_csscomputedstyle.cpp
index 5f2a405..489eed1 100644
--- a/core/fxcrt/css/cfx_csscomputedstyle.cpp
+++ b/core/fxcrt/css/cfx_csscomputedstyle.cpp
@@ -33,7 +33,7 @@
 
   return m_InheritedData.m_pFontFamily->values()
       .back()
-      .As<CFX_CSSStringValue>()
+      .AsRaw<CFX_CSSStringValue>()
       ->Value();
 }
 
diff --git a/core/fxcrt/css/cfx_cssstyleselector.cpp b/core/fxcrt/css/cfx_cssstyleselector.cpp
index 380033f..864dae8 100644
--- a/core/fxcrt/css/cfx_cssstyleselector.cpp
+++ b/core/fxcrt/css/cfx_cssstyleselector.cpp
@@ -178,16 +178,16 @@
       case CFX_CSSProperty::Display:
         if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
           pComputedStyle->m_NonInheritedData.m_eDisplay =
-              ToDisplay(pValue.As<CFX_CSSEnumValue>()->Value());
+              ToDisplay(pValue.AsRaw<CFX_CSSEnumValue>()->Value());
         }
         break;
       case CFX_CSSProperty::FontSize: {
         float& fFontSize = pComputedStyle->m_InheritedData.m_fFontSize;
         if (eType == CFX_CSSValue::PrimitiveType::kNumber) {
-          fFontSize = pValue.As<CFX_CSSNumberValue>()->Apply(fFontSize);
+          fFontSize = pValue.AsRaw<CFX_CSSNumberValue>()->Apply(fFontSize);
         } else if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
           fFontSize =
-              ToFontSize(pValue.As<CFX_CSSEnumValue>()->Value(), fFontSize);
+              ToFontSize(pValue.AsRaw<CFX_CSSEnumValue>()->Value(), fFontSize);
         }
       } break;
       case CFX_CSSProperty::LineHeight:
@@ -205,7 +205,7 @@
       case CFX_CSSProperty::TextAlign:
         if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
           pComputedStyle->m_InheritedData.m_eTextAlign =
-              ToTextAlign(pValue.As<CFX_CSSEnumValue>()->Value());
+              ToTextAlign(pValue.AsRaw<CFX_CSSEnumValue>()->Value());
         }
         break;
       case CFX_CSSProperty::TextIndent:
@@ -216,11 +216,11 @@
       case CFX_CSSProperty::FontWeight:
         if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
           pComputedStyle->m_InheritedData.m_wFontWeight =
-              ToFontWeight(pValue.As<CFX_CSSEnumValue>()->Value());
+              ToFontWeight(pValue.AsRaw<CFX_CSSEnumValue>()->Value());
         } else if (eType == CFX_CSSValue::PrimitiveType::kNumber) {
-          int32_t iValue =
-              static_cast<int32_t>(pValue.As<CFX_CSSNumberValue>()->value()) /
-              100;
+          int32_t iValue = static_cast<int32_t>(
+                               pValue.AsRaw<CFX_CSSNumberValue>()->value()) /
+                           100;
           if (iValue >= 1 && iValue <= 9) {
             pComputedStyle->m_InheritedData.m_wFontWeight = iValue * 100;
           }
@@ -229,13 +229,13 @@
       case CFX_CSSProperty::FontStyle:
         if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
           pComputedStyle->m_InheritedData.m_eFontStyle =
-              ToFontStyle(pValue.As<CFX_CSSEnumValue>()->Value());
+              ToFontStyle(pValue.AsRaw<CFX_CSSEnumValue>()->Value());
         }
         break;
       case CFX_CSSProperty::Color:
         if (eType == CFX_CSSValue::PrimitiveType::kRGB) {
           pComputedStyle->m_InheritedData.m_dwFontColor =
-              pValue.As<CFX_CSSColorValue>()->Value();
+              pValue.AsRaw<CFX_CSSColorValue>()->Value();
         }
         break;
       case CFX_CSSProperty::MarginLeft:
@@ -325,19 +325,19 @@
       case CFX_CSSProperty::VerticalAlign:
         if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
           pComputedStyle->m_NonInheritedData.m_eVerticalAlignType =
-              ToVerticalAlign(pValue.As<CFX_CSSEnumValue>()->Value());
+              ToVerticalAlign(pValue.AsRaw<CFX_CSSEnumValue>()->Value());
         } else if (eType == CFX_CSSValue::PrimitiveType::kNumber) {
           pComputedStyle->m_NonInheritedData.m_eVerticalAlignType =
               CFX_CSSVerticalAlign::Number;
           pComputedStyle->m_NonInheritedData.m_fVerticalAlign =
-              pValue.As<CFX_CSSNumberValue>()->Apply(
+              pValue.AsRaw<CFX_CSSNumberValue>()->Apply(
                   pComputedStyle->m_InheritedData.m_fFontSize);
         }
         break;
       case CFX_CSSProperty::FontVariant:
         if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
           pComputedStyle->m_InheritedData.m_eFontVariant =
-              ToFontVariant(pValue.As<CFX_CSSEnumValue>()->Value());
+              ToFontVariant(pValue.AsRaw<CFX_CSSEnumValue>()->Value());
         }
         break;
       case CFX_CSSProperty::LetterSpacing:
@@ -345,7 +345,7 @@
           pComputedStyle->m_InheritedData.m_LetterSpacing.Set(
               CFX_CSSLengthUnit::Normal);
         } else if (eType == CFX_CSSValue::PrimitiveType::kNumber) {
-          if (pValue.As<CFX_CSSNumberValue>()->unit() ==
+          if (pValue.AsRaw<CFX_CSSNumberValue>()->unit() ==
               CFX_CSSNumberValue::Unit::kPercent) {
             break;
           }
@@ -360,7 +360,7 @@
           pComputedStyle->m_InheritedData.m_WordSpacing.Set(
               CFX_CSSLengthUnit::Normal);
         } else if (eType == CFX_CSSValue::PrimitiveType::kNumber) {
-          if (pValue.As<CFX_CSSNumberValue>()->unit() ==
+          if (pValue.AsRaw<CFX_CSSNumberValue>()->unit() ==
               CFX_CSSNumberValue::Unit::kPercent) {
             break;
           }
@@ -479,7 +479,7 @@
     RetainPtr<CFX_CSSNumberValue> v = pValue.As<CFX_CSSNumberValue>();
     if (v->unit() == CFX_CSSNumberValue::Unit::kPercent) {
       width.Set(CFX_CSSLengthUnit::Percent,
-                pValue.As<CFX_CSSNumberValue>()->value() / 100.0f);
+                pValue.AsRaw<CFX_CSSNumberValue>()->value() / 100.0f);
       return width.NonZero();
     }
 
@@ -487,7 +487,7 @@
     width.Set(CFX_CSSLengthUnit::Point, fValue);
     return width.NonZero();
   } else if (eType == CFX_CSSValue::PrimitiveType::kEnum) {
-    switch (pValue.As<CFX_CSSEnumValue>()->Value()) {
+    switch (pValue.AsRaw<CFX_CSSEnumValue>()->Value()) {
       case CFX_CSSPropertyValue::Auto:
         width.Set(CFX_CSSLengthUnit::Auto);
         return true;
@@ -567,7 +567,7 @@
     if (val->GetType() != CFX_CSSValue::PrimitiveType::kEnum)
       continue;
 
-    switch (val.As<CFX_CSSEnumValue>()->Value()) {
+    switch (val.AsRaw<CFX_CSSEnumValue>()->Value()) {
       case CFX_CSSPropertyValue::Underline:
         dwDecoration |= CFX_CSSTEXTDECORATION::kUnderline;
         break;
diff --git a/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp b/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp
index 41c193c..627c8e9 100644
--- a/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp
+++ b/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp
@@ -63,8 +63,8 @@
     bool important;
     RetainPtr<CFX_CSSValue> v = decl_->GetProperty(prop, &important);
     EXPECT_EQ(v->GetType(), CFX_CSSValue::PrimitiveType::kNumber);
-    EXPECT_EQ(v.As<CFX_CSSNumberValue>()->unit(), unit);
-    EXPECT_EQ(v.As<CFX_CSSNumberValue>()->value(), val);
+    EXPECT_EQ(v.AsRaw<CFX_CSSNumberValue>()->unit(), unit);
+    EXPECT_EQ(v.AsRaw<CFX_CSSNumberValue>()->value(), val);
   }
 
   void VerifyEnum(CFX_CSSProperty prop, CFX_CSSPropertyValue val) {
@@ -73,7 +73,7 @@
     bool important;
     RetainPtr<CFX_CSSValue> v = decl_->GetProperty(prop, &important);
     EXPECT_EQ(v->GetType(), CFX_CSSValue::PrimitiveType::kEnum);
-    EXPECT_EQ(v.As<CFX_CSSEnumValue>()->Value(), val);
+    EXPECT_EQ(v.AsRaw<CFX_CSSEnumValue>()->Value(), val);
   }
 
   void VerifyList(CFX_CSSProperty prop,
@@ -90,7 +90,7 @@
     for (size_t i = 0; i < expected_values.size(); ++i) {
       const auto& val = values[i];
       EXPECT_EQ(val->GetType(), CFX_CSSValue::PrimitiveType::kEnum);
-      EXPECT_EQ(val.As<CFX_CSSEnumValue>()->Value(), expected_values[i]);
+      EXPECT_EQ(val.AsRaw<CFX_CSSEnumValue>()->Value(), expected_values[i]);
     }
   }
 
diff --git a/core/fxcrt/retain_ptr.h b/core/fxcrt/retain_ptr.h
index 329567e..5b88a66 100644
--- a/core/fxcrt/retain_ptr.h
+++ b/core/fxcrt/retain_ptr.h
@@ -104,8 +104,13 @@
   ~RetainPtr() = default;
 
   template <class U>
+  U* AsRaw() const {
+    return static_cast<U*>(Get());
+  }
+
+  template <class U>
   RetainPtr<U> As() const {
-    return RetainPtr<U>(static_cast<U*>(Get()));
+    return RetainPtr<U>(AsRaw<U>());
   }
 
   void Reset(T* obj = nullptr) {