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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
cc / layers / tile_display_layer_impl.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/layers/tile_display_layer_impl.h"
#include <algorithm>
#include <limits>
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/functional/overloaded.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "cc/layers/append_quads_data.h"
#include "cc/tiles/tiling_set_coverage_iterator.h"
#include "cc/trees/layer_tree_impl.h"
#include "components/viz/common/quads/solid_color_draw_quad.h"
#include "components/viz/common/quads/tile_draw_quad.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
namespace cc {
namespace {
class TilingOrder {
public:
bool operator()(const std::unique_ptr<TileDisplayLayerImpl::Tiling>& left,
const std::unique_ptr<TileDisplayLayerImpl::Tiling>& right) {
return left->contents_scale_key() > right->contents_scale_key();
}
};
} // namespace
TileDisplayLayerImpl::TileResource::TileResource(
const viz::TransferableResource& resource,
bool is_premultiplied,
bool is_checkered)
: resource(resource),
is_premultiplied(is_premultiplied),
is_checkered(is_checkered) {}
TileDisplayLayerImpl::TileResource::TileResource(const TileResource&) = default;
TileDisplayLayerImpl::TileResource&
TileDisplayLayerImpl::TileResource::operator=(const TileResource&) = default;
TileDisplayLayerImpl::TileResource::~TileResource() = default;
TileDisplayLayerImpl::Tile::Tile() = default;
TileDisplayLayerImpl::Tile::Tile(const TileContents& contents)
: contents_(contents) {
DCHECK(!absl::holds_alternative<NoContents>(contents_));
}
TileDisplayLayerImpl::Tile::Tile(Tile&&) = default;
TileDisplayLayerImpl::Tile& TileDisplayLayerImpl::Tile::operator=(Tile&&) =
default;
TileDisplayLayerImpl::Tile::~Tile() = default;
TileDisplayLayerImpl::Tiling::Tiling(TileDisplayLayerImpl& layer,
float scale_key)
: layer_(layer), scale_key_(scale_key) {}
TileDisplayLayerImpl::Tiling::~Tiling() = default;
TileDisplayLayerImpl::Tile* TileDisplayLayerImpl::Tiling::TileAt(
const TileIndex& index) const {
auto it = tiles_.find(index);
if (it == tiles_.end()) {
return nullptr;
}
return it->second.get();
}
void TileDisplayLayerImpl::Tiling::SetRasterTransform(
const gfx::AxisTransform2d& transform) {
DCHECK_EQ(std::max(transform.scale().x(), transform.scale().y()), scale_key_);
raster_transform_ = transform;
}
void TileDisplayLayerImpl::Tiling::SetTileSize(const gfx::Size& size) {
if (size == tiling_data_.max_texture_size()) {
return;
}
tiling_data_.SetMaxTextureSize(size);
tiles_.clear();
}
void TileDisplayLayerImpl::Tiling::SetTilingRect(const gfx::Rect& rect) {
if (rect == tiling_data_.tiling_rect()) {
return;
}
tiling_data_.SetTilingRect(rect);
tiles_.clear();
}
void TileDisplayLayerImpl::Tiling::SetTileContents(
const TileIndex& key,
const TileContents& contents) {
std::unique_ptr<Tile> old_tile;
if (absl::holds_alternative<NoContents>(contents)) {
auto it = tiles_.find(key);
if (it != tiles_.end()) {
old_tile = std::move(it->second);
tiles_.erase(it);
}
} else {
old_tile = std::exchange(tiles_[key], std::make_unique<Tile>(contents));
}
if (old_tile) {
if (auto* resource = absl::get_if<TileResource>(&old_tile->contents())) {
layer_->discarded_resources_.push_back(resource->resource);
}
}
}
TileDisplayLayerImpl::DisplayTilingCoverageIterator
TileDisplayLayerImpl::Tiling::Cover(const gfx::Rect& coverage_rect,
float coverage_scale) const {
return DisplayTilingCoverageIterator(this, coverage_scale, coverage_rect);
}
TileDisplayLayerImpl::TileDisplayLayerImpl(Client& client,
LayerTreeImpl& tree,
int id)
: LayerImpl(&tree, id), client_(client) {}
TileDisplayLayerImpl::~TileDisplayLayerImpl() = default;
TileDisplayLayerImpl::Tiling&
TileDisplayLayerImpl::GetOrCreateTilingFromScaleKey(float scale_key) {
auto it = std::find_if(tilings_.begin(), tilings_.end(),
[scale_key](const auto& tiling) {
return tiling->contents_scale_key() == scale_key;
});
if (it != tilings_.end()) {
return **it;
}
tilings_.push_back(std::make_unique<Tiling>(*this, scale_key));
Tiling& tiling = *tilings_.back();
std::sort(tilings_.begin(), tilings_.end(), TilingOrder());
return tiling;
}
mojom::LayerType TileDisplayLayerImpl::GetLayerType() const {
return mojom::LayerType::kTileDisplay;
}
std::unique_ptr<LayerImpl> TileDisplayLayerImpl::CreateLayerImpl(
LayerTreeImpl* tree_impl) const {
NOTREACHED();
}
void TileDisplayLayerImpl::PushPropertiesTo(LayerImpl* layer) {
NOTREACHED();
}
void TileDisplayLayerImpl::AppendQuads(viz::CompositorRenderPass* render_pass,
AppendQuadsData* append_quads_data) {
if (tilings_.empty()) {
return;
}
const float max_contents_scale =
tilings_.empty() ? 1.0f : tilings_.back()->contents_scale_key();
viz::SharedQuadState* shared_quad_state =
render_pass->CreateAndAppendSharedQuadState();
PopulateScaledSharedQuadState(shared_quad_state, max_contents_scale,
contents_opaque());
const Occlusion scaled_occlusion =
draw_properties()
.occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
shared_quad_state->quad_to_target_transform);
// If we're doing a regular AppendQuads (ie, not solid color or resourceless
// software draw, and if the visible rect is scrolled far enough away, then we
// may run into a floating point precision in AA calculations in the renderer.
// See crbug.com/765297. In order to avoid this, we shift the quads up from
// where they logically reside and adjust the shared_quad_state's transform
// instead. We only do this in a scale/translate matrices to ensure the math
// is correct.
gfx::Vector2d quad_offset;
if (shared_quad_state->quad_to_target_transform.IsScaleOrTranslation()) {
const auto& visible_rect = shared_quad_state->visible_quad_layer_rect;
quad_offset = gfx::Vector2d(-visible_rect.x(), -visible_rect.y());
}
std::optional<gfx::Rect> scaled_cull_rect;
const ScrollTree& scroll_tree =
layer_tree_impl()->property_trees()->scroll_tree();
if (const ScrollNode* scroll_node = scroll_tree.Node(scroll_tree_index())) {
if (transform_tree_index() == scroll_node->transform_id) {
if (const gfx::Rect* cull_rect =
scroll_tree.ScrollingContentsCullRect(scroll_node->element_id)) {
scaled_cull_rect =
gfx::ScaleToEnclosingRect(*cull_rect, max_contents_scale);
}
}
}
std::vector<viz::TransferableResource> used_resources;
const auto ideal_scale = GetIdealContentsScale();
const float ideal_scale_key = std::max(ideal_scale.x(), ideal_scale.y());
for (auto iter = TilingSetCoverageIterator<Tiling>(
tilings_, shared_quad_state->visible_quad_layer_rect,
max_contents_scale, ideal_scale_key);
iter; ++iter) {
const gfx::Rect geometry_rect = iter.geometry_rect();
const gfx::Rect visible_geometry_rect =
scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
if (visible_geometry_rect.IsEmpty()) {
continue;
}
const gfx::Rect offset_geometry_rect = geometry_rect + quad_offset;
const gfx::Rect offset_visible_geometry_rect =
visible_geometry_rect + quad_offset;
const bool needs_blending = !contents_opaque();
const uint64_t visible_geometry_area =
visible_geometry_rect.size().Area64();
append_quads_data->visible_layer_area += visible_geometry_area;
bool has_draw_quad = false;
if (*iter) {
if (auto resource = iter->resource()) {
const gfx::RectF texture_rect = iter.texture_rect();
auto* quad = render_pass->CreateAndAppendDrawQuad<viz::TileDrawQuad>();
quad->SetNew(shared_quad_state, offset_geometry_rect,
offset_visible_geometry_rect, needs_blending,
resource->resource.id, texture_rect,
iter.CurrentTiling()->tile_size(),
resource->is_premultiplied, /*nearest_neighbor=*/false,
/*enable_edge_aa=*/false);
used_resources.push_back(resource->resource);
has_draw_quad = true;
} else if (auto color = iter->solid_color()) {
has_draw_quad = true;
const float alpha = color->fA * shared_quad_state->opacity;
if (alpha >= std::numeric_limits<float>::epsilon()) {
auto* quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
quad->SetNew(shared_quad_state, offset_geometry_rect,
offset_visible_geometry_rect, *color,
/*enable_edge_aa=*/false);
}
}
}
if (!has_draw_quad) {
// Checkerboard due to missing raster.
SkColor4f color = safe_opaque_background_color();
auto* quad =
render_pass->CreateAndAppendDrawQuad<viz::SolidColorDrawQuad>();
quad->SetNew(shared_quad_state, offset_geometry_rect,
offset_visible_geometry_rect, color, false);
}
}
// Adjust shared_quad_state with the quad_offset, since we've adjusted each
// quad we've appended by it.
shared_quad_state->quad_to_target_transform.Translate(-quad_offset);
shared_quad_state->quad_layer_rect.Offset(quad_offset);
shared_quad_state->visible_quad_layer_rect.Offset(quad_offset);
client_->DidAppendQuadsWithResources(used_resources);
}
} // namespace cc