Mark some paint classes STACK_ALLOCATED()

Also remove unnecessary raw_ptr/raw_ref from these classes.

Temporarily add RAW_PTR_EXCLUSION in some stack scoped classes. They
can't use STACK_ALLOCATED() because they are allocated in Vector
though the vector itself is stack allocated.

Bug: 330759291
Change-Id: Id1afc1becbeec3b6d358522122af7c70dbc7981e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5370643
Reviewed-by: Bartek Nowierski <bartekn@chromium.org>
Reviewed-by: Stefan Zager <szager@chromium.org>
Commit-Queue: Xianzhu Wang <wangxianzhu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1277847}
diff --git a/third_party/blink/renderer/core/paint/rounded_inner_rect_clipper.h b/third_party/blink/renderer/core/paint/rounded_inner_rect_clipper.h
index 16911ee..47e5bee 100644
--- a/third_party/blink/renderer/core/paint/rounded_inner_rect_clipper.h
+++ b/third_party/blink/renderer/core/paint/rounded_inner_rect_clipper.h
@@ -14,7 +14,7 @@
 struct PhysicalRect;
 
 class RoundedInnerRectClipper {
-  DISALLOW_NEW();
+  STACK_ALLOCATED();
 
  public:
   RoundedInnerRectClipper(GraphicsContext&,
diff --git a/third_party/blink/renderer/core/paint/text_painter_base_test.cc b/third_party/blink/renderer/core/paint/text_painter_base_test.cc
index b3e5762..3ca9404d 100644
--- a/third_party/blink/renderer/core/paint/text_painter_base_test.cc
+++ b/third_party/blink/renderer/core/paint/text_painter_base_test.cc
@@ -27,19 +27,17 @@
  public:
   TextPainterBaseTest()
       : layout_text_(nullptr),
-        paint_controller_(MakeGarbageCollected<PaintController>()),
-        context_(*paint_controller_) {}
+        paint_controller_(MakeGarbageCollected<PaintController>()) {}
 
  protected:
   const LayoutText& GetLayoutText() { return *layout_text_; }
 
-  PaintInfo CreatePaintInfoForBackground() {
-    return PaintInfo(context_, CullRect(),
-                     PaintPhase::kSelfBlockBackgroundOnly);
+  PaintInfo CreatePaintInfoForBackground(GraphicsContext& context) {
+    return PaintInfo(context, CullRect(), PaintPhase::kSelfBlockBackgroundOnly);
   }
 
-  PaintInfo CreatePaintInfoForTextClip() {
-    return PaintInfo(context_, CullRect(), PaintPhase::kTextClip);
+  PaintInfo CreatePaintInfoForTextClip(GraphicsContext& context) {
+    return PaintInfo(context, CullRect(), PaintPhase::kTextClip);
   }
 
  protected:
@@ -57,7 +55,6 @@
 
   Persistent<LayoutText> layout_text_;
   Persistent<PaintController> paint_controller_;
-  GraphicsContext context_;
 };
 
 TEST_F(TextPainterBaseTest, TextPaintingStyle_Simple) {
@@ -65,9 +62,10 @@
                                                CSSValueID::kBlue);
   UpdateAllLifecyclePhasesForTest();
 
+  GraphicsContext context(*paint_controller_);
   TextPaintStyle text_style = TextPainterBase::TextPaintingStyle(
       GetLayoutText().GetDocument(), GetLayoutText().StyleRef(),
-      CreatePaintInfoForBackground());
+      CreatePaintInfoForBackground(context));
   EXPECT_EQ(Color(0, 0, 255), text_style.fill_color);
   EXPECT_EQ(Color(0, 0, 255), text_style.stroke_color);
   EXPECT_EQ(Color(0, 0, 255), text_style.emphasis_mark_color);
@@ -89,9 +87,10 @@
                                                "1px 2px 3px yellow");
   UpdateAllLifecyclePhasesForTest();
 
+  GraphicsContext context(*paint_controller_);
   TextPaintStyle text_style = TextPainterBase::TextPaintingStyle(
       GetLayoutText().GetDocument(), GetLayoutText().StyleRef(),
-      CreatePaintInfoForBackground());
+      CreatePaintInfoForBackground(context));
   EXPECT_EQ(Color(255, 0, 0), text_style.fill_color);
   EXPECT_EQ(Color(0, 255, 0), text_style.stroke_color);
   EXPECT_EQ(Color(0, 0, 255), text_style.emphasis_mark_color);
@@ -119,9 +118,10 @@
                                                "1px 2px 3px yellow");
   UpdateAllLifecyclePhasesForTest();
 
+  GraphicsContext context(*paint_controller_);
   TextPaintStyle text_style = TextPainterBase::TextPaintingStyle(
       GetLayoutText().GetDocument(), GetLayoutText().StyleRef(),
-      CreatePaintInfoForTextClip());
+      CreatePaintInfoForTextClip(context));
   EXPECT_EQ(Color::kBlack, text_style.fill_color);
   EXPECT_EQ(Color::kBlack, text_style.stroke_color);
   EXPECT_EQ(Color::kBlack, text_style.emphasis_mark_color);
@@ -147,9 +147,10 @@
   // so we need to re-get layout_text_.
   UpdateLayoutText();
 
+  GraphicsContext context(*paint_controller_);
   TextPaintStyle text_style = TextPainterBase::TextPaintingStyle(
       GetLayoutText().GetDocument(), GetLayoutText().StyleRef(),
-      CreatePaintInfoForBackground());
+      CreatePaintInfoForBackground(context));
   EXPECT_EQ(Color(255, 0, 0), text_style.fill_color);
   EXPECT_EQ(Color(0, 255, 0), text_style.stroke_color);
   EXPECT_EQ(Color(0, 0, 255), text_style.emphasis_mark_color);
@@ -172,9 +173,10 @@
   // so we need to re-get layout_text_.
   UpdateLayoutText();
 
+  GraphicsContext context(*paint_controller_);
   TextPaintStyle text_style = TextPainterBase::TextPaintingStyle(
       GetLayoutText().GetDocument(), GetLayoutText().StyleRef(),
-      CreatePaintInfoForBackground());
+      CreatePaintInfoForBackground(context));
   EXPECT_EQ(Color(255, 220, 220).Dark(), text_style.fill_color);
   EXPECT_EQ(Color(220, 255, 220).Dark(), text_style.stroke_color);
   EXPECT_EQ(Color(220, 220, 255).Dark(), text_style.emphasis_mark_color);
diff --git a/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.cc b/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.cc
index fca64fc83..ff93ff5 100644
--- a/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.cc
+++ b/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.cc
@@ -5,7 +5,6 @@
 #include "third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.h"
 
 #include "base/logging.h"
-#include "base/memory/raw_ptr.h"
 #include "base/memory/raw_ptr_exclusion.h"
 #include "base/numerics/safe_conversions.h"
 #include "cc/input/layer_selection_bound.h"
@@ -238,11 +237,15 @@
     // These fields are never nullptr.
     //
     // RAW_PTR_EXCLUSION: Performance reasons: regressions in MotionMark
-    // (crbug.com/1495275#c116).
+    // (crbug.com/1495275#c116). The struct is performance critical and stack
+    // scoped.
     RAW_PTR_EXCLUSION const TransformPaintPropertyNode* transform;
+    // RAW_PTR_EXCLUSION: The struct is performance critical and stack scoped.
     RAW_PTR_EXCLUSION const ClipPaintPropertyNode* clip;
+    // RAW_PTR_EXCLUSION: The struct is performance critical and stack scoped.
     RAW_PTR_EXCLUSION const EffectPaintPropertyNode* effect;
     // See ConversionContext<Result>::previous_transform_.
+    // RAW_PTR_EXCLUSION: The struct is performance critical and stack scoped.
     RAW_PTR_EXCLUSION const TransformPaintPropertyNode* previous_transform;
 #if DCHECK_IS_ON()
     bool has_pre_cap_effect_hierarchy_issue = false;
@@ -283,7 +286,8 @@
     // UpdateSaveLayerBounds().
     size_t save_layer_id;
     // The transform space when the SaveLayer[Alpha]Op was emitted.
-    raw_ptr<const TransformPaintPropertyNode> transform;
+    // RAW_PTR_EXCLUSION: The struct is performance critical and stack scoped.
+    RAW_PTR_EXCLUSION const TransformPaintPropertyNode* transform;
     // Records the bounds of the effect which initiated the entry. Note that
     // the effect is not |effect| (which is the previous effect), but the
     // |current_effect_| when this entry is the top of the stack.
@@ -861,10 +865,10 @@
     PaintOpBufferExt buffer;
     ConversionContext(layer_state, layer_offset, buffer).Convert(chunks);
     recorder.getRecordingCanvas()->drawPicture(buffer.ReleaseAsRecord());
-    params.tracking->CheckUnderInvalidations(
-        params.debug_name, recorder.finishRecordingAsPicture(),
-        params.interest_rect);
-    auto under_invalidation_record = params.tracking->UnderInvalidationRecord();
+    params.tracking.CheckUnderInvalidations(params.debug_name,
+                                            recorder.finishRecordingAsPicture(),
+                                            params.interest_rect);
+    auto under_invalidation_record = params.tracking.UnderInvalidationRecord();
     if (!under_invalidation_record.empty()) {
       cc_list.StartPaint();
       cc_list.push<cc::DrawRecordOp>(std::move(under_invalidation_record));
diff --git a/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.h b/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.h
index d7e9e76c..e132069e 100644
--- a/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.h
+++ b/third_party/blink/renderer/platform/graphics/compositing/paint_chunks_to_cc_layer.h
@@ -5,8 +5,6 @@
 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_COMPOSITING_PAINT_CHUNKS_TO_CC_LAYER_H_
 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_COMPOSITING_PAINT_CHUNKS_TO_CC_LAYER_H_
 
-#include "base/memory/raw_ref.h"
-#include "base/memory/scoped_refptr.h"
 #include "cc/input/layer_selection_bound.h"
 #include "cc/paint/display_item_list.h"
 #include "third_party/blink/renderer/platform/graphics/paint/paint_record.h"
@@ -28,6 +26,9 @@
 class RasterInvalidationTracking;
 
 struct RasterUnderInvalidationCheckingParams {
+  STACK_ALLOCATED();
+
+ public:
   RasterUnderInvalidationCheckingParams(RasterInvalidationTracking& tracking,
                                         const gfx::Rect& interest_rect,
                                         const String& debug_name)
@@ -35,7 +36,7 @@
         interest_rect(interest_rect),
         debug_name(debug_name) {}
 
-  const raw_ref<RasterInvalidationTracking> tracking;
+  RasterInvalidationTracking& tracking;
   gfx::Rect interest_rect;
   String debug_name;
 };
diff --git a/third_party/blink/renderer/platform/graphics/compositing/property_tree_manager.h b/third_party/blink/renderer/platform/graphics/compositing/property_tree_manager.h
index 9be171d..7e7c130 100644
--- a/third_party/blink/renderer/platform/graphics/compositing/property_tree_manager.h
+++ b/third_party/blink/renderer/platform/graphics/compositing/property_tree_manager.h
@@ -5,6 +5,7 @@
 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_COMPOSITING_PROPERTY_TREE_MANAGER_H_
 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_COMPOSITING_PROPERTY_TREE_MANAGER_H_
 
+#include "base/memory/raw_ptr_exclusion.h"
 #include "cc/layers/layer_collections.h"
 #include "third_party/blink/renderer/platform/graphics/compositor_element_id.h"
 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
@@ -223,20 +224,23 @@
     CcEffectType effect_type;
 
     // The effect state of the cc effect node. It's never nullptr.
-    const EffectPaintPropertyNode* effect;
+    // RAW_PTR_EXCLUSION: The struct is performance critical and stack scoped.
+    RAW_PTR_EXCLUSION const EffectPaintPropertyNode* effect;
 
     // The clip state of the cc effect node. This value may be shallower than
     // the one passed into SwitchToEffectNodeWithSynthesizedClip because not
     // every clip needs to be synthesized as cc effect. Is set to output clip of
     // the effect if the type is kEffect, or set to the synthesized clip node.
     // It's never nullptr.
-    const ClipPaintPropertyNode* clip;
+    // RAW_PTR_EXCLUSION: The struct is performance critical and stack scoped.
+    RAW_PTR_EXCLUSION const ClipPaintPropertyNode* clip;
 
     // The transform space of this state. It's |&effect->LocalTransformSpace()|
     // if this state is of kEffect type or synthetic with backdrop filters
     // moved up from the original effect.
     // Otherwise it's |&clip->LocalTransformSpace()|.
-    const TransformPaintPropertyNode* transform;
+    // RAW_PTR_EXCLUSION: The struct is performance critical and stack scoped.
+    RAW_PTR_EXCLUSION const TransformPaintPropertyNode* transform;
 
     // Whether the transform space of this state may be 2d axis misaligned to
     // the containing render surface. As there may be new render surfaces
diff --git a/third_party/blink/renderer/platform/graphics/graphics_context.cc b/third_party/blink/renderer/platform/graphics/graphics_context.cc
index 4322f03b..997a31a 100644
--- a/third_party/blink/renderer/platform/graphics/graphics_context.cc
+++ b/third_party/blink/renderer/platform/graphics/graphics_context.cc
@@ -507,7 +507,7 @@
                                DOMNodeId node_id,
                                const AutoDarkMode& auto_dark_mode) {
   DarkModeFlags dark_mode_flags(this, auto_dark_mode, flags);
-  if (sk_sp<SkTextBlob> text_blob = paint_controller_->CachedTextBlob()) {
+  if (sk_sp<SkTextBlob> text_blob = paint_controller_.CachedTextBlob()) {
     canvas_->drawTextBlob(text_blob, point.x(), point.y(), node_id,
                           dark_mode_flags);
     return;
@@ -594,7 +594,7 @@
                           DarkModeFlags(this, auto_dark_mode, flags),
                           printing_ ? Font::DrawType::kGlyphsAndClusters
                                     : Font::DrawType::kGlyphsOnly)) {
-      paint_controller_->SetTextPainted();
+      paint_controller_.SetTextPainted();
     }
   });
 }
@@ -691,7 +691,7 @@
     return;
   }
 
-  paint_controller_->SetImagePainted();
+  paint_controller_.SetImagePainted();
 }
 
 cc::PaintFlags::FilterQuality GraphicsContext::ComputeFilterQuality(
diff --git a/third_party/blink/renderer/platform/graphics/graphics_context.h b/third_party/blink/renderer/platform/graphics/graphics_context.h
index 1e88530..fb13144 100644
--- a/third_party/blink/renderer/platform/graphics/graphics_context.h
+++ b/third_party/blink/renderer/platform/graphics/graphics_context.h
@@ -31,8 +31,6 @@
 #include <memory>
 
 #include "base/dcheck_is_on.h"
-#include "base/memory/raw_ptr.h"
-#include "base/memory/raw_ref.h"
 #include "cc/paint/paint_flags.h"
 #include "third_party/blink/renderer/platform/fonts/font.h"
 #include "third_party/blink/renderer/platform/graphics/dark_mode_filter.h"
@@ -93,6 +91,9 @@
 };
 
 struct ImageDrawOptions {
+  STACK_ALLOCATED();
+
+ public:
   ImageDrawOptions() = default;
   explicit ImageDrawOptions(DarkModeFilter* dark_mode_filter,
                             SkSamplingOptions& sampling_options,
@@ -108,7 +109,7 @@
         decode_mode(decode_mode),
         apply_dark_mode(apply_dark_mode),
         may_be_lcp_candidate(may_be_lcp_candidate) {}
-  raw_ptr<DarkModeFilter> dark_mode_filter = nullptr;
+  DarkModeFilter* dark_mode_filter = nullptr;
   SkSamplingOptions sampling_options;
   RespectImageOrientationEnum respect_orientation = kRespectImageOrientation;
   Image::ImageClampingMode clamping_mode = Image::kClampImageToSourceRect;
@@ -118,6 +119,9 @@
 };
 
 struct AutoDarkMode {
+  STACK_ALLOCATED();
+
+ public:
   AutoDarkMode(DarkModeFilter::ElementRole role, bool enabled)
       : role(role), enabled(enabled) {}
 
@@ -156,6 +160,9 @@
 };
 
 struct ImagePaintTimingInfo {
+  STACK_ALLOCATED();
+
+ public:
   explicit ImagePaintTimingInfo(bool image_may_be_lcp_candidate)
       : image_may_be_lcp_candidate(image_may_be_lcp_candidate) {}
   ImagePaintTimingInfo(bool image_may_be_lcp_candidate,
@@ -170,7 +177,7 @@
 };
 
 class PLATFORM_EXPORT GraphicsContext {
-  USING_FAST_MALLOC(GraphicsContext);
+  STACK_ALLOCATED();
 
  public:
   explicit GraphicsContext(PaintController&);
@@ -193,9 +200,9 @@
   cc::PaintCanvas* Canvas() { return canvas_; }
   const cc::PaintCanvas* Canvas() const { return canvas_; }
 
-  PaintController& GetPaintController() { return *paint_controller_; }
+  PaintController& GetPaintController() { return paint_controller_; }
   const PaintController& GetPaintController() const {
-    return *paint_controller_;
+    return paint_controller_;
   }
 
   DarkModeFilter* GetDarkModeFilter();
@@ -553,9 +560,9 @@
   // This is owned by paint_recorder_. Never delete this object.
   // Drawing operations are allowed only after the first BeginRecording() which
   // initializes this to not null.
-  raw_ptr<cc::PaintCanvas> canvas_ = nullptr;
+  cc::PaintCanvas* canvas_ = nullptr;
 
-  const raw_ref<PaintController, DanglingUntriaged> paint_controller_;
+  PaintController& paint_controller_;
 
   // Paint states stack. The state controls the appearance of drawn content, so
   // this stack enables local drawing state changes with Save()/Restore() calls.
@@ -566,14 +573,12 @@
   wtf_size_t paint_state_index_ = 0;
 
   // Raw pointer to the current state.
-  raw_ptr<GraphicsContextState> paint_state_ = nullptr;
+  GraphicsContextState* paint_state_ = nullptr;
 
   PaintRecorder paint_recorder_;
 
-  raw_ptr<printing::MetafileSkia, DanglingUntriaged> printing_metafile_ =
-      nullptr;
-  raw_ptr<paint_preview::PaintPreviewTracker, DanglingUntriaged>
-      paint_preview_tracker_ = nullptr;
+  printing::MetafileSkia* printing_metafile_ = nullptr;
+  paint_preview::PaintPreviewTracker* paint_preview_tracker_ = nullptr;
 
 #if DCHECK_IS_ON()
   int layer_count_ = 0;
diff --git a/third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h b/third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h
index c5d32589..60aaf01e 100644
--- a/third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h
+++ b/third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h
@@ -29,7 +29,6 @@
 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_GRAPHICS_CONTEXT_STATE_SAVER_H_
 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_GRAPHICS_CONTEXT_STATE_SAVER_H_
 
-#include "base/memory/raw_ref.h"
 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
 #include "third_party/blink/renderer/platform/platform_export.h"
 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
@@ -37,14 +36,15 @@
 namespace blink {
 
 class PLATFORM_EXPORT GraphicsContextStateSaver final {
-  USING_FAST_MALLOC(GraphicsContextStateSaver);
+  STACK_ALLOCATED();
 
  public:
   GraphicsContextStateSaver(GraphicsContext& context,
                             bool save_and_restore = true)
       : context_(context), save_and_restore_(save_and_restore) {
-    if (save_and_restore_)
-      context_->Save();
+    if (save_and_restore_) {
+      context_.Save();
+    }
   }
 
   GraphicsContextStateSaver(const GraphicsContextStateSaver&) = delete;
@@ -52,13 +52,14 @@
       delete;
 
   ~GraphicsContextStateSaver() {
-    if (save_and_restore_)
-      context_->Restore();
+    if (save_and_restore_) {
+      context_.Restore();
+    }
   }
 
   void Save() {
     DCHECK(!save_and_restore_);
-    context_->Save();
+    context_.Save();
     save_and_restore_ = true;
   }
 
@@ -70,15 +71,15 @@
 
   void Restore() {
     DCHECK(save_and_restore_);
-    context_->Restore();
+    context_.Restore();
     save_and_restore_ = false;
   }
 
-  GraphicsContext& Context() const { return *context_; }
+  GraphicsContext& Context() const { return context_; }
   bool Saved() const { return save_and_restore_; }
 
  private:
-  const raw_ref<GraphicsContext> context_;
+  GraphicsContext& context_;
   bool save_and_restore_;
 };