blob: 1a742f981796de7b0e68dacde11c82d6cae26860 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cc3cfaa2013-03-18 09:05:525#include "cc/layers/render_surface_impl.h"
[email protected]94f206c12012-08-25 00:09:146
avi02a4d172015-12-21 06:14:367#include <stddef.h>
8
[email protected]ac7c7f52012-11-08 06:26:509#include <algorithm>
10
[email protected]4456eee22012-10-19 18:16:3811#include "base/logging.h"
[email protected]8e61d4b2013-06-10 22:11:4812#include "base/strings/stringprintf.h"
[email protected]681ccff2013-03-18 06:13:5213#include "cc/base/math_util.h"
[email protected]6e84de22013-03-18 06:54:2714#include "cc/debug/debug_colors.h"
[email protected]cc3cfaa2013-03-18 09:05:5215#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5216#include "cc/layers/render_pass_sink.h"
ajuma50bce7e2016-06-24 20:56:0417#include "cc/output/filter_operations.h"
[email protected]89e8267a2013-03-18 07:50:5618#include "cc/quads/debug_border_draw_quad.h"
19#include "cc/quads/render_pass.h"
20#include "cc/quads/render_pass_draw_quad.h"
21#include "cc/quads/shared_quad_state.h"
[email protected]556fd292013-03-18 08:03:0422#include "cc/trees/damage_tracker.h"
ajumad9432e32015-11-30 19:43:4423#include "cc/trees/draw_property_utils.h"
24#include "cc/trees/layer_tree_impl.h"
danakja1b92e42015-02-11 21:07:4725#include "cc/trees/occlusion.h"
[email protected]55761e62012-11-21 18:55:5826#include "third_party/skia/include/core/SkImageFilter.h"
heejin.r.chungd28506ba2014-10-23 16:36:2027#include "ui/gfx/geometry/rect_conversions.h"
[email protected]c8686a02012-11-27 08:29:0028#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1429
[email protected]9c88e562012-09-14 22:21:3030namespace cc {
[email protected]94f206c12012-08-25 00:09:1431
[email protected]d2d915aa2013-03-08 20:18:1232RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl* owning_layer)
33 : owning_layer_(owning_layer),
34 surface_property_changed_(false),
[email protected]30fe19ff2013-07-04 00:54:4535 contributes_to_drawn_surface_(false),
kulkarni.a4015690f12014-10-10 13:50:0636 nearest_occlusion_immune_ancestor_(nullptr),
[email protected]d2d915aa2013-03-08 20:18:1237 target_render_surface_layer_index_history_(0),
38 current_layer_index_history_(0) {
39 damage_tracker_ = DamageTracker::Create();
[email protected]94f206c12012-08-25 00:09:1440}
41
[email protected]d2d915aa2013-03-08 20:18:1242RenderSurfaceImpl::~RenderSurfaceImpl() {}
43
weiliangc189c1a12016-04-11 16:16:2544RenderSurfaceImpl* RenderSurfaceImpl::render_target() {
45 EffectTree& effect_tree =
46 owning_layer_->layer_tree_impl()->property_trees()->effect_tree;
47 EffectNode* node = effect_tree.Node(EffectTreeIndex());
48 EffectNode* target_node = effect_tree.Node(node->data.target_id);
49 if (target_node->id != 0)
50 return target_node->data.render_surface;
51 else
52 return this;
53}
54
55const RenderSurfaceImpl* RenderSurfaceImpl::render_target() const {
56 const EffectTree& effect_tree =
57 owning_layer_->layer_tree_impl()->property_trees()->effect_tree;
58 const EffectNode* node = effect_tree.Node(EffectTreeIndex());
59 const EffectNode* target_node = effect_tree.Node(node->data.target_id);
60 if (target_node->id != 0)
61 return target_node->data.render_surface;
62 else
63 return this;
64}
65
weiliangcc97575c2016-03-03 18:34:2766RenderSurfaceImpl::DrawProperties::DrawProperties() {
67 draw_opacity = 1.f;
68 is_clipped = false;
69}
70
71RenderSurfaceImpl::DrawProperties::~DrawProperties() {}
72
[email protected]d2d915aa2013-03-08 20:18:1273gfx::RectF RenderSurfaceImpl::DrawableContentRect() const {
weiliangcd68cf712016-05-02 23:29:0774 if (content_rect().IsEmpty())
75 return gfx::RectF();
76
77 gfx::Rect surface_content_rect = content_rect();
senorblanco38858c52016-01-20 23:15:0078 if (!owning_layer_->filters().IsEmpty()) {
jbroman1c44d5b52016-06-06 21:19:3079 const gfx::Transform& owning_layer_draw_transform =
80 owning_layer_->DrawTransform();
81 DCHECK(owning_layer_draw_transform.IsScale2d());
82 surface_content_rect = owning_layer_->filters().MapRect(
83 surface_content_rect, owning_layer_draw_transform.matrix());
weiliangcd68cf712016-05-02 23:29:0784 }
85 gfx::RectF drawable_content_rect = MathUtil::MapClippedRect(
86 draw_transform(), gfx::RectF(surface_content_rect));
ajuma1d4026a32016-06-14 13:18:5087 if (HasReplica()) {
weiliangcd68cf712016-05-02 23:29:0788 drawable_content_rect.Union(MathUtil::MapClippedRect(
89 replica_draw_transform(), gfx::RectF(surface_content_rect)));
90 } else if (!owning_layer_->filters().IsEmpty() && is_clipped()) {
91 // Filter could move pixels around, but still need to be clipped.
92 drawable_content_rect.Intersect(gfx::RectF(clip_rect()));
senorblanco38858c52016-01-20 23:15:0093 }
[email protected]d2d915aa2013-03-08 20:18:1294
jaydasika5160e672015-10-15 15:25:1495 // If the rect has a NaN coordinate, we return empty rect to avoid crashes in
96 // functions (for example, gfx::ToEnclosedRect) that are called on this rect.
97 if (std::isnan(drawable_content_rect.x()) ||
98 std::isnan(drawable_content_rect.y()) ||
99 std::isnan(drawable_content_rect.right()) ||
100 std::isnan(drawable_content_rect.bottom()))
101 return gfx::RectF();
102
[email protected]d2d915aa2013-03-08 20:18:12103 return drawable_content_rect;
[email protected]94f206c12012-08-25 00:09:14104}
105
danakja1b92e42015-02-11 21:07:47106SkColor RenderSurfaceImpl::GetDebugBorderColor() const {
107 return DebugColors::SurfaceBorderColor();
108}
109
110SkColor RenderSurfaceImpl::GetReplicaDebugBorderColor() const {
111 return DebugColors::SurfaceReplicaBorderColor();
112}
113
114float RenderSurfaceImpl::GetDebugBorderWidth() const {
115 return DebugColors::SurfaceBorderWidth(owning_layer_->layer_tree_impl());
116}
117
118float RenderSurfaceImpl::GetReplicaDebugBorderWidth() const {
119 return DebugColors::SurfaceReplicaBorderWidth(
120 owning_layer_->layer_tree_impl());
121}
122
[email protected]d2d915aa2013-03-08 20:18:12123int RenderSurfaceImpl::OwningLayerId() const {
124 return owning_layer_ ? owning_layer_->id() : 0;
[email protected]94f206c12012-08-25 00:09:14125}
126
jaydasika7ba8f922015-08-21 05:39:42127bool RenderSurfaceImpl::HasReplica() const {
ajuma1d4026a32016-06-14 13:18:50128 return OwningEffectNode()->data.replica_layer_id != -1;
jaydasika7ba8f922015-08-21 05:39:42129}
130
jaydasika23fb3822015-08-25 03:22:59131const LayerImpl* RenderSurfaceImpl::ReplicaLayer() const {
ajuma1d4026a32016-06-14 13:18:50132 int replica_layer_id = OwningEffectNode()->data.replica_layer_id;
133 return owning_layer_->layer_tree_impl()->LayerById(replica_layer_id);
134}
135
136LayerImpl* RenderSurfaceImpl::ReplicaLayer() {
137 int replica_layer_id = OwningEffectNode()->data.replica_layer_id;
138 return owning_layer_->layer_tree_impl()->LayerById(replica_layer_id);
139}
140
141LayerImpl* RenderSurfaceImpl::MaskLayer() {
142 int mask_layer_id = OwningEffectNode()->data.mask_layer_id;
143 return owning_layer_->layer_tree_impl()->LayerById(mask_layer_id);
144}
145
146bool RenderSurfaceImpl::HasMask() const {
147 return OwningEffectNode()->data.mask_layer_id != -1;
148}
149
150LayerImpl* RenderSurfaceImpl::ReplicaMaskLayer() {
151 int replica_mask_layer_id = OwningEffectNode()->data.replica_mask_layer_id;
152 return owning_layer_->layer_tree_impl()->LayerById(replica_mask_layer_id);
153}
154
155bool RenderSurfaceImpl::HasReplicaMask() const {
156 return OwningEffectNode()->data.replica_mask_layer_id != -1;
jaydasika23fb3822015-08-25 03:22:59157}
158
ajuma50bce7e2016-06-24 20:56:04159const FilterOperations& RenderSurfaceImpl::BackgroundFilters() const {
160 return OwningEffectNode()->data.background_filters;
161}
162
ajumae6f541b2016-05-31 16:50:50163bool RenderSurfaceImpl::HasCopyRequest() const {
ajuma1d4026a32016-06-14 13:18:50164 return OwningEffectNode()->data.has_copy_request;
ajumae6f541b2016-05-31 16:50:50165}
166
jaydasika504a0502015-07-23 19:25:44167int RenderSurfaceImpl::TransformTreeIndex() const {
168 return owning_layer_->transform_tree_index();
169}
[email protected]94f206c12012-08-25 00:09:14170
jaydasikaebf9e4ea2015-08-14 21:29:12171int RenderSurfaceImpl::ClipTreeIndex() const {
172 return owning_layer_->clip_tree_index();
173}
174
jaydasika0f4b1a92015-08-18 23:19:02175int RenderSurfaceImpl::EffectTreeIndex() const {
176 return owning_layer_->effect_tree_index();
177}
178
ajuma1d4026a32016-06-14 13:18:50179const EffectNode* RenderSurfaceImpl::OwningEffectNode() const {
180 return owning_layer_->layer_tree_impl()->property_trees()->effect_tree.Node(
181 EffectTreeIndex());
182}
183
[email protected]0023fc72014-01-10 20:05:06184void RenderSurfaceImpl::SetClipRect(const gfx::Rect& clip_rect) {
weiliangcc97575c2016-03-03 18:34:27185 if (clip_rect == draw_properties_.clip_rect)
[email protected]d2d915aa2013-03-08 20:18:12186 return;
[email protected]94f206c12012-08-25 00:09:14187
[email protected]d2d915aa2013-03-08 20:18:12188 surface_property_changed_ = true;
weiliangcc97575c2016-03-03 18:34:27189 draw_properties_.clip_rect = clip_rect;
[email protected]94f206c12012-08-25 00:09:14190}
191
[email protected]0023fc72014-01-10 20:05:06192void RenderSurfaceImpl::SetContentRect(const gfx::Rect& content_rect) {
weiliangcc97575c2016-03-03 18:34:27193 if (content_rect == draw_properties_.content_rect)
[email protected]d2d915aa2013-03-08 20:18:12194 return;
[email protected]94f206c12012-08-25 00:09:14195
[email protected]d2d915aa2013-03-08 20:18:12196 surface_property_changed_ = true;
weiliangcc97575c2016-03-03 18:34:27197 draw_properties_.content_rect = content_rect;
[email protected]94f206c12012-08-25 00:09:14198}
199
weiliangc6da32862016-04-20 16:40:11200void RenderSurfaceImpl::SetContentRectForTesting(const gfx::Rect& rect) {
201 SetContentRect(rect);
202}
203
204gfx::Rect RenderSurfaceImpl::CalculateClippedAccumulatedContentRect() {
ajuma1d4026a32016-06-14 13:18:50205 if (ReplicaLayer() || HasCopyRequest() || !is_clipped())
weiliangc6da32862016-04-20 16:40:11206 return accumulated_content_rect();
207
208 if (accumulated_content_rect().IsEmpty())
209 return gfx::Rect();
210
211 // Calculate projection from the target surface rect to local
212 // space. Non-invertible draw transforms means no able to bring clipped rect
213 // in target space back to local space, early out without clip.
214 gfx::Transform target_to_surface(gfx::Transform::kSkipInitialization);
215 if (!draw_transform().GetInverse(&target_to_surface))
216 return accumulated_content_rect();
217
218 // Clip rect is in target space. Bring accumulated content rect to
219 // target space in preparation for clipping.
220 gfx::Rect accumulated_rect_in_target_space =
221 MathUtil::MapEnclosingClippedRect(draw_transform(),
222 accumulated_content_rect());
223 // If accumulated content rect is contained within clip rect, early out
224 // without clipping.
225 if (clip_rect().Contains(accumulated_rect_in_target_space))
226 return accumulated_content_rect();
227
228 gfx::Rect clipped_accumulated_rect_in_target_space = clip_rect();
229 clipped_accumulated_rect_in_target_space.Intersect(
230 accumulated_rect_in_target_space);
231
232 if (clipped_accumulated_rect_in_target_space.IsEmpty())
233 return gfx::Rect();
234
235 gfx::Rect clipped_accumulated_rect_in_local_space =
236 MathUtil::ProjectEnclosingClippedRect(
237 target_to_surface, clipped_accumulated_rect_in_target_space);
238 // Bringing clipped accumulated rect back to local space may result
239 // in inflation due to axis-alignment.
240 clipped_accumulated_rect_in_local_space.Intersect(accumulated_content_rect());
241 return clipped_accumulated_rect_in_local_space;
242}
243
244void RenderSurfaceImpl::CalculateContentRectFromAccumulatedContentRect(
245 int max_texture_size) {
246 // Root render surface use viewport, and does not calculate content rect.
247 DCHECK_NE(render_target(), this);
248
249 // Surface's content rect is the clipped accumulated content rect. By default
250 // use accumulated content rect, and then try to clip it.
251 gfx::Rect surface_content_rect = CalculateClippedAccumulatedContentRect();
252
253 // The RenderSurfaceImpl backing texture cannot exceed the maximum
254 // supported texture size.
255 surface_content_rect.set_width(
256 std::min(surface_content_rect.width(), max_texture_size));
257 surface_content_rect.set_height(
258 std::min(surface_content_rect.height(), max_texture_size));
259
260 SetContentRect(surface_content_rect);
261}
262
263void RenderSurfaceImpl::SetContentRectToViewport() {
264 // Only root render surface use viewport as content rect.
265 DCHECK_EQ(render_target(), this);
266 gfx::Rect viewport = gfx::ToEnclosingRect(owning_layer_->layer_tree_impl()
267 ->property_trees()
268 ->clip_tree.ViewportClip());
269 SetContentRect(viewport);
270}
271
weiliangc189c1a12016-04-11 16:16:25272void RenderSurfaceImpl::ClearAccumulatedContentRect() {
273 accumulated_content_rect_ = gfx::Rect();
274}
275
276void RenderSurfaceImpl::AccumulateContentRectFromContributingLayer(
277 LayerImpl* layer) {
278 DCHECK(layer->DrawsContent());
279 DCHECK_EQ(this, layer->render_target());
280
281 // Root render surface doesn't accumulate content rect, it always uses
282 // viewport for content rect.
283 if (render_target() == this)
284 return;
285
286 accumulated_content_rect_.Union(layer->drawable_content_rect());
287}
288
289void RenderSurfaceImpl::AccumulateContentRectFromContributingRenderSurface(
290 RenderSurfaceImpl* contributing_surface) {
291 DCHECK_NE(this, contributing_surface);
292 DCHECK_EQ(this, contributing_surface->render_target());
293
294 // Root render surface doesn't accumulate content rect, it always uses
295 // viewport for content rect.
296 if (render_target() == this)
297 return;
298
299 // The content rect of contributing surface is in its own space. Instead, we
300 // will use contributing surface's DrawableContentRect which is in target
301 // space (local space for this render surface) as required.
302 accumulated_content_rect_.Union(
303 gfx::ToEnclosedRect(contributing_surface->DrawableContentRect()));
jaydasika7ba8f922015-08-21 05:39:42304}
305
[email protected]d2d915aa2013-03-08 20:18:12306bool RenderSurfaceImpl::SurfacePropertyChanged() const {
307 // Surface property changes are tracked as follows:
308 //
309 // - surface_property_changed_ is flagged when the clip_rect or content_rect
310 // change. As of now, these are the only two properties that can be affected
311 // by descendant layers.
312 //
313 // - all other property changes come from the owning layer (or some ancestor
314 // layer that propagates its change to the owning layer).
315 //
316 DCHECK(owning_layer_);
[email protected]7aba6662013-03-12 10:17:34317 return surface_property_changed_ || owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14318}
319
[email protected]d2d915aa2013-03-08 20:18:12320bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const {
[email protected]7aba6662013-03-12 10:17:34321 return surface_property_changed_ && !owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14322}
323
[email protected]d2d915aa2013-03-08 20:18:12324void RenderSurfaceImpl::ClearLayerLists() {
325 layer_list_.clear();
[email protected]7d929c02012-09-20 17:26:57326}
327
[email protected]0cd7d6f72014-08-22 14:50:51328RenderPassId RenderSurfaceImpl::GetRenderPassId() {
[email protected]d2d915aa2013-03-08 20:18:12329 int layer_id = owning_layer_->id();
330 int sub_id = 0;
331 DCHECK_GT(layer_id, 0);
[email protected]0cd7d6f72014-08-22 14:50:51332 return RenderPassId(layer_id, sub_id);
[email protected]0f077a52012-09-08 01:45:24333}
334
[email protected]d2d915aa2013-03-08 20:18:12335void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink* pass_sink) {
danakj60bc3bc2016-04-09 00:24:48336 std::unique_ptr<RenderPass> pass = RenderPass::Create(layer_list_.size());
weiliangcc97575c2016-03-03 18:34:27337 pass->SetNew(GetRenderPassId(), content_rect(),
338 gfx::IntersectRects(content_rect(),
[email protected]b4ead7b2014-04-07 18:12:18339 damage_tracker_->current_damage_rect()),
weiliangcc97575c2016-03-03 18:34:27340 draw_properties_.screen_space_transform);
danakja04855a2015-11-18 20:39:10341 pass_sink->AppendRenderPass(std::move(pass));
[email protected]467b3612012-08-28 07:41:16342}
343
danakja1b92e42015-02-11 21:07:47344void RenderSurfaceImpl::AppendQuads(RenderPass* render_pass,
345 const gfx::Transform& draw_transform,
346 const Occlusion& occlusion_in_content_space,
347 SkColor debug_border_color,
348 float debug_border_width,
349 LayerImpl* mask_layer,
350 AppendQuadsData* append_quads_data,
351 RenderPassId render_pass_id) {
danakj64767d902015-06-19 00:10:43352 gfx::Rect visible_layer_rect =
weiliangcc97575c2016-03-03 18:34:27353 occlusion_in_content_space.GetUnoccludedContentRect(content_rect());
danakj64767d902015-06-19 00:10:43354 if (visible_layer_rect.IsEmpty())
[email protected]ba0f8d92014-03-21 18:41:10355 return;
356
[email protected]c6707fd2014-06-23 05:50:36357 SharedQuadState* shared_quad_state =
358 render_pass->CreateAndAppendSharedQuadState();
weiliangcc97575c2016-03-03 18:34:27359 shared_quad_state->SetAll(
360 draw_transform, content_rect().size(), content_rect(),
361 draw_properties_.clip_rect, draw_properties_.is_clipped,
362 draw_properties_.draw_opacity, owning_layer_->blend_mode(),
363 owning_layer_->sorting_context_id());
[email protected]94f206c12012-08-25 00:09:14364
[email protected]7aba6662013-03-12 10:17:34365 if (owning_layer_->ShowDebugBorders()) {
[email protected]f7030c32014-07-03 18:54:34366 DebugBorderDrawQuad* debug_border_quad =
367 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
weiliangcc97575c2016-03-03 18:34:27368 debug_border_quad->SetNew(shared_quad_state, content_rect(),
danakj64767d902015-06-19 00:10:43369 visible_layer_rect, debug_border_color,
danakja1b92e42015-02-11 21:07:47370 debug_border_width);
[email protected]d2d915aa2013-03-08 20:18:12371 }
[email protected]94f206c12012-08-25 00:09:14372
jbaumanbbd425e2015-05-19 00:33:35373 ResourceId mask_resource_id = 0;
ennef6f3fbba42014-10-16 18:16:49374 gfx::Size mask_texture_size;
375 gfx::Vector2dF mask_uv_scale;
ajumad9432e32015-11-30 19:43:44376 gfx::Transform owning_layer_draw_transform = owning_layer_->DrawTransform();
danakja1b92e42015-02-11 21:07:47377 if (mask_layer && mask_layer->DrawsContent() &&
378 !mask_layer->bounds().IsEmpty()) {
ennef6f3fbba42014-10-16 18:16:49379 mask_layer->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
[email protected]d2d915aa2013-03-08 20:18:12380 gfx::Vector2dF owning_layer_draw_scale =
ajumad9432e32015-11-30 19:43:44381 MathUtil::ComputeTransform2dScaleComponents(owning_layer_draw_transform,
382 1.f);
danakjddaec912015-09-25 19:38:40383 gfx::SizeF unclipped_mask_target_size = gfx::ScaleSize(
384 gfx::SizeF(owning_layer_->bounds()), owning_layer_draw_scale.x(),
385 owning_layer_draw_scale.y());
senorblancodfcb3622016-01-27 21:48:01386 mask_uv_scale = gfx::Vector2dF(1.0f / unclipped_mask_target_size.width(),
387 1.0f / unclipped_mask_target_size.height());
[email protected]d2d915aa2013-03-08 20:18:12388 }
[email protected]94f206c12012-08-25 00:09:14389
robertnce999072016-01-05 15:06:13390 DCHECK(owning_layer_draw_transform.IsScale2d());
[email protected]7ac3d492014-08-08 21:25:32391 gfx::Vector2dF owning_layer_to_target_scale =
ajumad9432e32015-11-30 19:43:44392 owning_layer_draw_transform.Scale2d();
[email protected]7ac3d492014-08-08 21:25:32393
[email protected]f7030c32014-07-03 18:54:34394 RenderPassDrawQuad* quad =
395 render_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
weiliangcc97575c2016-03-03 18:34:27396 quad->SetNew(shared_quad_state, content_rect(), visible_layer_rect,
danakj64767d902015-06-19 00:10:43397 render_pass_id, mask_resource_id, mask_uv_scale,
398 mask_texture_size, owning_layer_->filters(),
ajuma50bce7e2016-06-24 20:56:04399 owning_layer_to_target_scale, BackgroundFilters());
[email protected]94f206c12012-08-25 00:09:14400}
401
[email protected]bc5e77c2012-11-05 20:00:49402} // namespace cc