File: test_paint_artifact.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (240 lines) | stat: -rw-r--r-- 8,984 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/testing/test_paint_artifact.h"

#include <memory>

#include "cc/layers/layer.h"
#include "cc/paint/paint_flags.h"
#include "third_party/blink/renderer/platform/graphics/paint/display_item_client.h"
#include "third_party/blink/renderer/platform/graphics/paint/drawing_display_item.h"
#include "third_party/blink/renderer/platform/graphics/paint/foreign_layer_display_item.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_artifact.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_record.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_recorder.h"
#include "third_party/blink/renderer/platform/graphics/paint/scroll_paint_property_node.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "ui/gfx/geometry/skia_conversions.h"

namespace blink {

static DisplayItemClient& StaticDummyClient() {
  DEFINE_STATIC_LOCAL(Persistent<FakeDisplayItemClient>, client,
                      (MakeGarbageCollected<FakeDisplayItemClient>()));
  client->Validate();
  return *client;
}

TestPaintArtifact& TestPaintArtifact::Chunk(int id) {
  Chunk(StaticDummyClient(),
        static_cast<DisplayItem::Type>(DisplayItem::kDrawingFirst + id));
  // The default bounds with magic numbers make the chunks have different bounds
  // from each other, for e.g. RasterInvalidatorTest to check the tracked raster
  // invalidation rects of chunks. The actual values don't matter. If the chunk
  // has display items, we will recalculate the bounds from the display items
  // when constructing the PaintArtifact.
  gfx::Rect bounds(id * 110, id * 220, id * 220 + 200, id * 110 + 200);
  Bounds(bounds);
  DrawableBounds(bounds);
  return *this;
}

TestPaintArtifact& TestPaintArtifact::Chunk(const DisplayItemClient& client,
                                            DisplayItem::Type type) {
  auto& display_item_list = paint_artifact_->GetDisplayItemList();
  paint_artifact_->GetPaintChunks().emplace_back(
      display_item_list.size(), display_item_list.size(), client,
      PaintChunk::Id(client.Id(), type), PropertyTreeState::Root());
  paint_artifact_->RecordDebugInfo(client.Id(), client.DebugName(),
                                   client.OwnerNodeId());
  // Assume PaintController has processed this chunk.
  paint_artifact_->GetPaintChunks().back().client_is_just_created = false;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::Properties(
    const PropertyTreeStateOrAlias& properties) {
  paint_artifact_->GetPaintChunks().back().properties = properties;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::RectDrawing(const gfx::Rect& bounds,
                                                  Color color) {
  return RectDrawing(NewClient(), bounds, color);
}

TestPaintArtifact& TestPaintArtifact::ForeignLayerChunk(
    scoped_refptr<cc::Layer> layer,
    const gfx::Point& origin) {
  DEFINE_STATIC_DISPLAY_ITEM_CLIENT(client, "ForeignLayer");
  Chunk().Bounds(gfx::Rect(origin, layer->bounds()));
  paint_artifact_->GetDisplayItemList()
      .AllocateAndConstruct<ForeignLayerDisplayItem>(
          client->Id(), DisplayItem::kForeignLayerFirst, std::move(layer),
          origin, RasterEffectOutset::kNone,
          client->GetPaintInvalidationReason());
  paint_artifact_->RecordDebugInfo(client->Id(), client->DebugName(),
                                   client->OwnerNodeId());
  DidAddDisplayItem();
  return *this;
}

TestPaintArtifact& TestPaintArtifact::RectDrawing(
    const DisplayItemClient& client,
    const gfx::Rect& bounds,
    Color color) {
  PaintRecorder recorder;
  cc::PaintCanvas* canvas = recorder.beginRecording();
  if (!bounds.IsEmpty()) {
    cc::PaintFlags flags;
    flags.setColor(color.toSkColor4f());
    canvas->drawRect(gfx::RectToSkRect(bounds), flags);
  }
  paint_artifact_->GetDisplayItemList()
      .AllocateAndConstruct<DrawingDisplayItem>(
          client.Id(), DisplayItem::kDrawingFirst, bounds,
          recorder.finishRecordingAsPicture(),
          client.VisualRectOutsetForRasterEffects(),
          client.GetPaintInvalidationReason());
  paint_artifact_->RecordDebugInfo(client.Id(), client.DebugName(),
                                   client.OwnerNodeId());
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  chunk.background_color.color = color.toSkColor4f();
  chunk.background_color.area = bounds.size().GetArea();
  // is_solid_color should be set explicitly with IsSolidColor().
  chunk.background_color.is_solid_color = false;
  DidAddDisplayItem();
  return *this;
}

TestPaintArtifact& TestPaintArtifact::ScrollHitTestChunk(
    const DisplayItemClient& client,
    const PropertyTreeState& contents_state) {
  const auto& scroll_translation = contents_state.Transform();
  DCHECK(scroll_translation.ScrollNode());
  Chunk(client, DisplayItem::kScrollHitTest)
      .Properties(*scroll_translation.Parent(), *contents_state.Clip().Parent(),
                  contents_state.Effect());
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  chunk.hit_test_opaqueness = cc::HitTestOpaqueness::kOpaque;
  auto& hit_test_data = chunk.EnsureHitTestData();
  hit_test_data.scroll_hit_test_rect =
      scroll_translation.ScrollNode()->ContainerRect();
  hit_test_data.scroll_translation = &scroll_translation;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::ScrollingContentsChunk(
    const DisplayItemClient& client,
    const PropertyTreeState& state,
    bool opaque) {
  gfx::Rect contents_rect = state.Transform().ScrollNode()->ContentsRect();
  Chunk(client).Properties(state).Bounds(contents_rect);
  if (opaque) {
    RectKnownToBeOpaque(contents_rect);
  }
  return *this;
}

TestPaintArtifact& TestPaintArtifact::ScrollChunks(
    const PropertyTreeState& contents_state,
    bool contents_opaque) {
  return ScrollHitTestChunk(contents_state)
      .ScrollingContentsChunk(contents_state, contents_opaque);
}

TestPaintArtifact& TestPaintArtifact::SetRasterEffectOutset(
    RasterEffectOutset outset) {
  paint_artifact_->GetPaintChunks().back().raster_effect_outset = outset;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::RectKnownToBeOpaque(const gfx::Rect& r) {
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  chunk.rect_known_to_be_opaque = r;
  DCHECK(chunk.bounds.Contains(r));
  return *this;
}

TestPaintArtifact& TestPaintArtifact::TextKnownToBeOnOpaqueBackground() {
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  DCHECK(chunk.has_text);
  paint_artifact_->GetPaintChunks()
      .back()
      .text_known_to_be_on_opaque_background = true;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::HasText() {
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  chunk.has_text = true;
  chunk.text_known_to_be_on_opaque_background = false;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::IsSolidColor() {
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  DCHECK_EQ(chunk.size(), 1u);
  chunk.background_color.is_solid_color = true;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::EffectivelyInvisible() {
  paint_artifact_->GetPaintChunks().back().effectively_invisible = true;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::Bounds(const gfx::Rect& bounds) {
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  chunk.bounds = bounds;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::DrawableBounds(
    const gfx::Rect& drawable_bounds) {
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  chunk.drawable_bounds = drawable_bounds;
  DCHECK(chunk.bounds.Contains(drawable_bounds));
  return *this;
}

TestPaintArtifact& TestPaintArtifact::Uncacheable() {
  paint_artifact_->GetPaintChunks().back().is_cacheable = false;
  return *this;
}

TestPaintArtifact& TestPaintArtifact::IsMovedFromCachedSubsequence() {
  paint_artifact_->GetPaintChunks().back().is_moved_from_cached_subsequence =
      true;
  return *this;
}

const PaintArtifact& TestPaintArtifact::Build() {
  const PaintArtifact& result = *paint_artifact_;
  paint_artifact_ = nullptr;
  return result;
}

FakeDisplayItemClient& TestPaintArtifact::NewClient() {
  clients_.push_back(MakeGarbageCollected<FakeDisplayItemClient>());
  return *clients_.back();
}

FakeDisplayItemClient& TestPaintArtifact::Client(wtf_size_t i) const {
  return *clients_[i];
}

void TestPaintArtifact::DidAddDisplayItem() {
  auto& chunk = paint_artifact_->GetPaintChunks().back();
  DCHECK_EQ(chunk.end_index, paint_artifact_->GetDisplayItemList().size() - 1);
  const auto& item = UNSAFE_TODO(paint_artifact_->GetDisplayItemList().back());
  chunk.bounds.Union(item.VisualRect());
  if (item.DrawsContent()) {
    chunk.drawable_bounds.Union(item.VisualRect());
  }
  chunk.end_index++;
}

}  // namespace blink