blob: ed036f3933ea188c7d371e2f5244c6d9ef2f1809 [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"
[email protected]89e8267a2013-03-18 07:50:5617#include "cc/quads/debug_border_draw_quad.h"
18#include "cc/quads/render_pass.h"
19#include "cc/quads/render_pass_draw_quad.h"
20#include "cc/quads/shared_quad_state.h"
[email protected]556fd292013-03-18 08:03:0421#include "cc/trees/damage_tracker.h"
ajumad9432e32015-11-30 19:43:4422#include "cc/trees/draw_property_utils.h"
23#include "cc/trees/layer_tree_impl.h"
danakja1b92e42015-02-11 21:07:4724#include "cc/trees/occlusion.h"
[email protected]55761e62012-11-21 18:55:5825#include "third_party/skia/include/core/SkImageFilter.h"
heejin.r.chungd28506ba2014-10-23 16:36:2026#include "ui/gfx/geometry/rect_conversions.h"
[email protected]c8686a02012-11-27 08:29:0027#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1428
[email protected]9c88e562012-09-14 22:21:3029namespace cc {
[email protected]94f206c12012-08-25 00:09:1430
[email protected]d2d915aa2013-03-08 20:18:1231RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl* owning_layer)
32 : owning_layer_(owning_layer),
33 surface_property_changed_(false),
[email protected]30fe19ff2013-07-04 00:54:4534 contributes_to_drawn_surface_(false),
kulkarni.a4015690f12014-10-10 13:50:0635 nearest_occlusion_immune_ancestor_(nullptr),
[email protected]d2d915aa2013-03-08 20:18:1236 target_render_surface_layer_index_history_(0),
37 current_layer_index_history_(0) {
38 damage_tracker_ = DamageTracker::Create();
[email protected]94f206c12012-08-25 00:09:1439}
40
[email protected]d2d915aa2013-03-08 20:18:1241RenderSurfaceImpl::~RenderSurfaceImpl() {}
42
weiliangc189c1a12016-04-11 16:16:2543RenderSurfaceImpl* RenderSurfaceImpl::render_target() {
44 EffectTree& effect_tree =
45 owning_layer_->layer_tree_impl()->property_trees()->effect_tree;
46 EffectNode* node = effect_tree.Node(EffectTreeIndex());
47 EffectNode* target_node = effect_tree.Node(node->data.target_id);
48 if (target_node->id != 0)
49 return target_node->data.render_surface;
50 else
51 return this;
52}
53
54const RenderSurfaceImpl* RenderSurfaceImpl::render_target() const {
55 const EffectTree& effect_tree =
56 owning_layer_->layer_tree_impl()->property_trees()->effect_tree;
57 const EffectNode* node = effect_tree.Node(EffectTreeIndex());
58 const EffectNode* target_node = effect_tree.Node(node->data.target_id);
59 if (target_node->id != 0)
60 return target_node->data.render_surface;
61 else
62 return this;
63}
64
weiliangcc97575c2016-03-03 18:34:2765RenderSurfaceImpl::DrawProperties::DrawProperties() {
66 draw_opacity = 1.f;
67 is_clipped = false;
68}
69
70RenderSurfaceImpl::DrawProperties::~DrawProperties() {}
71
[email protected]d2d915aa2013-03-08 20:18:1272gfx::RectF RenderSurfaceImpl::DrawableContentRect() const {
weiliangcd68cf712016-05-02 23:29:0773 if (content_rect().IsEmpty())
74 return gfx::RectF();
75
76 gfx::Rect surface_content_rect = content_rect();
senorblanco38858c52016-01-20 23:15:0077 if (!owning_layer_->filters().IsEmpty()) {
78 int left, top, right, bottom;
79 owning_layer_->filters().GetOutsets(&top, &right, &bottom, &left);
weiliangcd68cf712016-05-02 23:29:0780 surface_content_rect.Inset(-left, -top, -right, -bottom);
81 }
82 gfx::RectF drawable_content_rect = MathUtil::MapClippedRect(
83 draw_transform(), gfx::RectF(surface_content_rect));
84 if (owning_layer_->has_replica()) {
85 drawable_content_rect.Union(MathUtil::MapClippedRect(
86 replica_draw_transform(), gfx::RectF(surface_content_rect)));
87 } else if (!owning_layer_->filters().IsEmpty() && is_clipped()) {
88 // Filter could move pixels around, but still need to be clipped.
89 drawable_content_rect.Intersect(gfx::RectF(clip_rect()));
senorblanco38858c52016-01-20 23:15:0090 }
[email protected]d2d915aa2013-03-08 20:18:1291
jaydasika5160e672015-10-15 15:25:1492 // If the rect has a NaN coordinate, we return empty rect to avoid crashes in
93 // functions (for example, gfx::ToEnclosedRect) that are called on this rect.
94 if (std::isnan(drawable_content_rect.x()) ||
95 std::isnan(drawable_content_rect.y()) ||
96 std::isnan(drawable_content_rect.right()) ||
97 std::isnan(drawable_content_rect.bottom()))
98 return gfx::RectF();
99
[email protected]d2d915aa2013-03-08 20:18:12100 return drawable_content_rect;
[email protected]94f206c12012-08-25 00:09:14101}
102
danakja1b92e42015-02-11 21:07:47103SkColor RenderSurfaceImpl::GetDebugBorderColor() const {
104 return DebugColors::SurfaceBorderColor();
105}
106
107SkColor RenderSurfaceImpl::GetReplicaDebugBorderColor() const {
108 return DebugColors::SurfaceReplicaBorderColor();
109}
110
111float RenderSurfaceImpl::GetDebugBorderWidth() const {
112 return DebugColors::SurfaceBorderWidth(owning_layer_->layer_tree_impl());
113}
114
115float RenderSurfaceImpl::GetReplicaDebugBorderWidth() const {
116 return DebugColors::SurfaceReplicaBorderWidth(
117 owning_layer_->layer_tree_impl());
118}
119
[email protected]d2d915aa2013-03-08 20:18:12120int RenderSurfaceImpl::OwningLayerId() const {
121 return owning_layer_ ? owning_layer_->id() : 0;
[email protected]94f206c12012-08-25 00:09:14122}
123
jaydasika7ba8f922015-08-21 05:39:42124bool RenderSurfaceImpl::HasReplica() const {
125 return owning_layer_->has_replica();
126}
127
jaydasika23fb3822015-08-25 03:22:59128const LayerImpl* RenderSurfaceImpl::ReplicaLayer() const {
129 return owning_layer_->replica_layer();
130}
131
jaydasika504a0502015-07-23 19:25:44132int RenderSurfaceImpl::TransformTreeIndex() const {
133 return owning_layer_->transform_tree_index();
134}
[email protected]94f206c12012-08-25 00:09:14135
jaydasikaebf9e4ea2015-08-14 21:29:12136int RenderSurfaceImpl::ClipTreeIndex() const {
137 return owning_layer_->clip_tree_index();
138}
139
jaydasika0f4b1a92015-08-18 23:19:02140int RenderSurfaceImpl::EffectTreeIndex() const {
141 return owning_layer_->effect_tree_index();
142}
143
[email protected]0023fc72014-01-10 20:05:06144void RenderSurfaceImpl::SetClipRect(const gfx::Rect& clip_rect) {
weiliangcc97575c2016-03-03 18:34:27145 if (clip_rect == draw_properties_.clip_rect)
[email protected]d2d915aa2013-03-08 20:18:12146 return;
[email protected]94f206c12012-08-25 00:09:14147
[email protected]d2d915aa2013-03-08 20:18:12148 surface_property_changed_ = true;
weiliangcc97575c2016-03-03 18:34:27149 draw_properties_.clip_rect = clip_rect;
[email protected]94f206c12012-08-25 00:09:14150}
151
[email protected]0023fc72014-01-10 20:05:06152void RenderSurfaceImpl::SetContentRect(const gfx::Rect& content_rect) {
weiliangcc97575c2016-03-03 18:34:27153 if (content_rect == draw_properties_.content_rect)
[email protected]d2d915aa2013-03-08 20:18:12154 return;
[email protected]94f206c12012-08-25 00:09:14155
[email protected]d2d915aa2013-03-08 20:18:12156 surface_property_changed_ = true;
weiliangcc97575c2016-03-03 18:34:27157 draw_properties_.content_rect = content_rect;
[email protected]94f206c12012-08-25 00:09:14158}
159
weiliangc6da32862016-04-20 16:40:11160void RenderSurfaceImpl::SetContentRectForTesting(const gfx::Rect& rect) {
161 SetContentRect(rect);
162}
163
164gfx::Rect RenderSurfaceImpl::CalculateClippedAccumulatedContentRect() {
165 if (owning_layer_->replica_layer() || owning_layer_->HasCopyRequest() ||
166 !is_clipped())
167 return accumulated_content_rect();
168
169 if (accumulated_content_rect().IsEmpty())
170 return gfx::Rect();
171
172 // Calculate projection from the target surface rect to local
173 // space. Non-invertible draw transforms means no able to bring clipped rect
174 // in target space back to local space, early out without clip.
175 gfx::Transform target_to_surface(gfx::Transform::kSkipInitialization);
176 if (!draw_transform().GetInverse(&target_to_surface))
177 return accumulated_content_rect();
178
179 // Clip rect is in target space. Bring accumulated content rect to
180 // target space in preparation for clipping.
181 gfx::Rect accumulated_rect_in_target_space =
182 MathUtil::MapEnclosingClippedRect(draw_transform(),
183 accumulated_content_rect());
184 // If accumulated content rect is contained within clip rect, early out
185 // without clipping.
186 if (clip_rect().Contains(accumulated_rect_in_target_space))
187 return accumulated_content_rect();
188
189 gfx::Rect clipped_accumulated_rect_in_target_space = clip_rect();
190 clipped_accumulated_rect_in_target_space.Intersect(
191 accumulated_rect_in_target_space);
192
193 if (clipped_accumulated_rect_in_target_space.IsEmpty())
194 return gfx::Rect();
195
196 gfx::Rect clipped_accumulated_rect_in_local_space =
197 MathUtil::ProjectEnclosingClippedRect(
198 target_to_surface, clipped_accumulated_rect_in_target_space);
199 // Bringing clipped accumulated rect back to local space may result
200 // in inflation due to axis-alignment.
201 clipped_accumulated_rect_in_local_space.Intersect(accumulated_content_rect());
202 return clipped_accumulated_rect_in_local_space;
203}
204
205void RenderSurfaceImpl::CalculateContentRectFromAccumulatedContentRect(
206 int max_texture_size) {
207 // Root render surface use viewport, and does not calculate content rect.
208 DCHECK_NE(render_target(), this);
209
210 // Surface's content rect is the clipped accumulated content rect. By default
211 // use accumulated content rect, and then try to clip it.
212 gfx::Rect surface_content_rect = CalculateClippedAccumulatedContentRect();
213
214 // The RenderSurfaceImpl backing texture cannot exceed the maximum
215 // supported texture size.
216 surface_content_rect.set_width(
217 std::min(surface_content_rect.width(), max_texture_size));
218 surface_content_rect.set_height(
219 std::min(surface_content_rect.height(), max_texture_size));
220
221 SetContentRect(surface_content_rect);
222}
223
224void RenderSurfaceImpl::SetContentRectToViewport() {
225 // Only root render surface use viewport as content rect.
226 DCHECK_EQ(render_target(), this);
227 gfx::Rect viewport = gfx::ToEnclosingRect(owning_layer_->layer_tree_impl()
228 ->property_trees()
229 ->clip_tree.ViewportClip());
230 SetContentRect(viewport);
231}
232
weiliangc189c1a12016-04-11 16:16:25233void RenderSurfaceImpl::ClearAccumulatedContentRect() {
234 accumulated_content_rect_ = gfx::Rect();
235}
236
237void RenderSurfaceImpl::AccumulateContentRectFromContributingLayer(
238 LayerImpl* layer) {
239 DCHECK(layer->DrawsContent());
240 DCHECK_EQ(this, layer->render_target());
241
242 // Root render surface doesn't accumulate content rect, it always uses
243 // viewport for content rect.
244 if (render_target() == this)
245 return;
246
247 accumulated_content_rect_.Union(layer->drawable_content_rect());
248}
249
250void RenderSurfaceImpl::AccumulateContentRectFromContributingRenderSurface(
251 RenderSurfaceImpl* contributing_surface) {
252 DCHECK_NE(this, contributing_surface);
253 DCHECK_EQ(this, contributing_surface->render_target());
254
255 // Root render surface doesn't accumulate content rect, it always uses
256 // viewport for content rect.
257 if (render_target() == this)
258 return;
259
260 // The content rect of contributing surface is in its own space. Instead, we
261 // will use contributing surface's DrawableContentRect which is in target
262 // space (local space for this render surface) as required.
263 accumulated_content_rect_.Union(
264 gfx::ToEnclosedRect(contributing_surface->DrawableContentRect()));
jaydasika7ba8f922015-08-21 05:39:42265}
266
[email protected]d2d915aa2013-03-08 20:18:12267bool RenderSurfaceImpl::SurfacePropertyChanged() const {
268 // Surface property changes are tracked as follows:
269 //
270 // - surface_property_changed_ is flagged when the clip_rect or content_rect
271 // change. As of now, these are the only two properties that can be affected
272 // by descendant layers.
273 //
274 // - all other property changes come from the owning layer (or some ancestor
275 // layer that propagates its change to the owning layer).
276 //
277 DCHECK(owning_layer_);
[email protected]7aba6662013-03-12 10:17:34278 return surface_property_changed_ || owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14279}
280
[email protected]d2d915aa2013-03-08 20:18:12281bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const {
[email protected]7aba6662013-03-12 10:17:34282 return surface_property_changed_ && !owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14283}
284
[email protected]d2d915aa2013-03-08 20:18:12285void RenderSurfaceImpl::ClearLayerLists() {
286 layer_list_.clear();
[email protected]7d929c02012-09-20 17:26:57287}
288
[email protected]0cd7d6f72014-08-22 14:50:51289RenderPassId RenderSurfaceImpl::GetRenderPassId() {
[email protected]d2d915aa2013-03-08 20:18:12290 int layer_id = owning_layer_->id();
291 int sub_id = 0;
292 DCHECK_GT(layer_id, 0);
[email protected]0cd7d6f72014-08-22 14:50:51293 return RenderPassId(layer_id, sub_id);
[email protected]0f077a52012-09-08 01:45:24294}
295
[email protected]d2d915aa2013-03-08 20:18:12296void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink* pass_sink) {
danakj60bc3bc2016-04-09 00:24:48297 std::unique_ptr<RenderPass> pass = RenderPass::Create(layer_list_.size());
weiliangcc97575c2016-03-03 18:34:27298 pass->SetNew(GetRenderPassId(), content_rect(),
299 gfx::IntersectRects(content_rect(),
[email protected]b4ead7b2014-04-07 18:12:18300 damage_tracker_->current_damage_rect()),
weiliangcc97575c2016-03-03 18:34:27301 draw_properties_.screen_space_transform);
danakja04855a2015-11-18 20:39:10302 pass_sink->AppendRenderPass(std::move(pass));
[email protected]467b3612012-08-28 07:41:16303}
304
danakja1b92e42015-02-11 21:07:47305void RenderSurfaceImpl::AppendQuads(RenderPass* render_pass,
306 const gfx::Transform& draw_transform,
307 const Occlusion& occlusion_in_content_space,
308 SkColor debug_border_color,
309 float debug_border_width,
310 LayerImpl* mask_layer,
311 AppendQuadsData* append_quads_data,
312 RenderPassId render_pass_id) {
danakj64767d902015-06-19 00:10:43313 gfx::Rect visible_layer_rect =
weiliangcc97575c2016-03-03 18:34:27314 occlusion_in_content_space.GetUnoccludedContentRect(content_rect());
danakj64767d902015-06-19 00:10:43315 if (visible_layer_rect.IsEmpty())
[email protected]ba0f8d92014-03-21 18:41:10316 return;
317
[email protected]c6707fd2014-06-23 05:50:36318 SharedQuadState* shared_quad_state =
319 render_pass->CreateAndAppendSharedQuadState();
weiliangcc97575c2016-03-03 18:34:27320 shared_quad_state->SetAll(
321 draw_transform, content_rect().size(), content_rect(),
322 draw_properties_.clip_rect, draw_properties_.is_clipped,
323 draw_properties_.draw_opacity, owning_layer_->blend_mode(),
324 owning_layer_->sorting_context_id());
[email protected]94f206c12012-08-25 00:09:14325
[email protected]7aba6662013-03-12 10:17:34326 if (owning_layer_->ShowDebugBorders()) {
[email protected]f7030c32014-07-03 18:54:34327 DebugBorderDrawQuad* debug_border_quad =
328 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
weiliangcc97575c2016-03-03 18:34:27329 debug_border_quad->SetNew(shared_quad_state, content_rect(),
danakj64767d902015-06-19 00:10:43330 visible_layer_rect, debug_border_color,
danakja1b92e42015-02-11 21:07:47331 debug_border_width);
[email protected]d2d915aa2013-03-08 20:18:12332 }
[email protected]94f206c12012-08-25 00:09:14333
jbaumanbbd425e2015-05-19 00:33:35334 ResourceId mask_resource_id = 0;
ennef6f3fbba42014-10-16 18:16:49335 gfx::Size mask_texture_size;
336 gfx::Vector2dF mask_uv_scale;
ajumad9432e32015-11-30 19:43:44337 gfx::Transform owning_layer_draw_transform = owning_layer_->DrawTransform();
danakja1b92e42015-02-11 21:07:47338 if (mask_layer && mask_layer->DrawsContent() &&
339 !mask_layer->bounds().IsEmpty()) {
ennef6f3fbba42014-10-16 18:16:49340 mask_layer->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
[email protected]d2d915aa2013-03-08 20:18:12341 gfx::Vector2dF owning_layer_draw_scale =
ajumad9432e32015-11-30 19:43:44342 MathUtil::ComputeTransform2dScaleComponents(owning_layer_draw_transform,
343 1.f);
danakjddaec912015-09-25 19:38:40344 gfx::SizeF unclipped_mask_target_size = gfx::ScaleSize(
345 gfx::SizeF(owning_layer_->bounds()), owning_layer_draw_scale.x(),
346 owning_layer_draw_scale.y());
senorblancodfcb3622016-01-27 21:48:01347 mask_uv_scale = gfx::Vector2dF(1.0f / unclipped_mask_target_size.width(),
348 1.0f / unclipped_mask_target_size.height());
[email protected]d2d915aa2013-03-08 20:18:12349 }
[email protected]94f206c12012-08-25 00:09:14350
robertnce999072016-01-05 15:06:13351 DCHECK(owning_layer_draw_transform.IsScale2d());
[email protected]7ac3d492014-08-08 21:25:32352 gfx::Vector2dF owning_layer_to_target_scale =
ajumad9432e32015-11-30 19:43:44353 owning_layer_draw_transform.Scale2d();
[email protected]7ac3d492014-08-08 21:25:32354
[email protected]f7030c32014-07-03 18:54:34355 RenderPassDrawQuad* quad =
356 render_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
weiliangcc97575c2016-03-03 18:34:27357 quad->SetNew(shared_quad_state, content_rect(), visible_layer_rect,
danakj64767d902015-06-19 00:10:43358 render_pass_id, mask_resource_id, mask_uv_scale,
359 mask_texture_size, owning_layer_->filters(),
[email protected]7ac3d492014-08-08 21:25:32360 owning_layer_to_target_scale,
[email protected]7aba6662013-03-12 10:17:34361 owning_layer_->background_filters());
[email protected]94f206c12012-08-25 00:09:14362}
363
[email protected]bc5e77c2012-11-05 20:00:49364} // namespace cc