blob: 493adf9f70b539cf4a3d6d11e283219220f4b8e4 [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/delegated_renderer_layer_impl.h"
16#include "cc/layers/layer_impl.h"
[email protected]cc3cfaa2013-03-18 09:05:5217#include "cc/layers/render_pass_sink.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]d2d915aa2013-03-08 20:18:1235 is_clipped_(false),
[email protected]30fe19ff2013-07-04 00:54:4536 contributes_to_drawn_surface_(false),
[email protected]c55f3fc2013-12-10 05:48:4837 draw_opacity_(1),
kulkarni.a4015690f12014-10-10 13:50:0638 nearest_occlusion_immune_ancestor_(nullptr),
[email protected]d2d915aa2013-03-08 20:18:1239 target_render_surface_layer_index_history_(0),
40 current_layer_index_history_(0) {
41 damage_tracker_ = DamageTracker::Create();
[email protected]94f206c12012-08-25 00:09:1442}
43
[email protected]d2d915aa2013-03-08 20:18:1244RenderSurfaceImpl::~RenderSurfaceImpl() {}
45
46gfx::RectF RenderSurfaceImpl::DrawableContentRect() const {
47 gfx::RectF drawable_content_rect =
danakj5e6ff6d2015-09-05 04:43:4448 MathUtil::MapClippedRect(draw_transform_, gfx::RectF(content_rect_));
[email protected]7aba6662013-03-12 10:17:3449 if (owning_layer_->has_replica()) {
danakj5e6ff6d2015-09-05 04:43:4450 drawable_content_rect.Union(MathUtil::MapClippedRect(
51 replica_draw_transform_, gfx::RectF(content_rect_)));
[email protected]d2d915aa2013-03-08 20:18:1252 }
senorblanco38858c52016-01-20 23:15:0053 if (!owning_layer_->filters().IsEmpty()) {
54 int left, top, right, bottom;
55 owning_layer_->filters().GetOutsets(&top, &right, &bottom, &left);
56 drawable_content_rect.Inset(-left, -top, -right, -bottom);
57 }
[email protected]d2d915aa2013-03-08 20:18:1258
jaydasika5160e672015-10-15 15:25:1459 // If the rect has a NaN coordinate, we return empty rect to avoid crashes in
60 // functions (for example, gfx::ToEnclosedRect) that are called on this rect.
61 if (std::isnan(drawable_content_rect.x()) ||
62 std::isnan(drawable_content_rect.y()) ||
63 std::isnan(drawable_content_rect.right()) ||
64 std::isnan(drawable_content_rect.bottom()))
65 return gfx::RectF();
66
[email protected]d2d915aa2013-03-08 20:18:1267 return drawable_content_rect;
[email protected]94f206c12012-08-25 00:09:1468}
69
danakja1b92e42015-02-11 21:07:4770SkColor RenderSurfaceImpl::GetDebugBorderColor() const {
71 return DebugColors::SurfaceBorderColor();
72}
73
74SkColor RenderSurfaceImpl::GetReplicaDebugBorderColor() const {
75 return DebugColors::SurfaceReplicaBorderColor();
76}
77
78float RenderSurfaceImpl::GetDebugBorderWidth() const {
79 return DebugColors::SurfaceBorderWidth(owning_layer_->layer_tree_impl());
80}
81
82float RenderSurfaceImpl::GetReplicaDebugBorderWidth() const {
83 return DebugColors::SurfaceReplicaBorderWidth(
84 owning_layer_->layer_tree_impl());
85}
86
[email protected]d2d915aa2013-03-08 20:18:1287int RenderSurfaceImpl::OwningLayerId() const {
88 return owning_layer_ ? owning_layer_->id() : 0;
[email protected]94f206c12012-08-25 00:09:1489}
90
jaydasika7ba8f922015-08-21 05:39:4291bool RenderSurfaceImpl::HasReplica() const {
92 return owning_layer_->has_replica();
93}
94
jaydasika23fb3822015-08-25 03:22:5995const LayerImpl* RenderSurfaceImpl::ReplicaLayer() const {
96 return owning_layer_->replica_layer();
97}
98
jaydasika504a0502015-07-23 19:25:4499int RenderSurfaceImpl::TransformTreeIndex() const {
100 return owning_layer_->transform_tree_index();
101}
[email protected]94f206c12012-08-25 00:09:14102
jaydasikaebf9e4ea2015-08-14 21:29:12103int RenderSurfaceImpl::ClipTreeIndex() const {
104 return owning_layer_->clip_tree_index();
105}
106
jaydasika0f4b1a92015-08-18 23:19:02107int RenderSurfaceImpl::EffectTreeIndex() const {
108 return owning_layer_->effect_tree_index();
109}
110
111int RenderSurfaceImpl::TargetEffectTreeIndex() const {
112 if (!owning_layer_->parent() || !owning_layer_->parent()->render_target())
113 return -1;
114 return owning_layer_->parent()->render_target()->effect_tree_index();
115}
116
[email protected]0023fc72014-01-10 20:05:06117void RenderSurfaceImpl::SetClipRect(const gfx::Rect& clip_rect) {
[email protected]d2d915aa2013-03-08 20:18:12118 if (clip_rect_ == clip_rect)
119 return;
[email protected]94f206c12012-08-25 00:09:14120
[email protected]d2d915aa2013-03-08 20:18:12121 surface_property_changed_ = true;
122 clip_rect_ = clip_rect;
[email protected]94f206c12012-08-25 00:09:14123}
124
[email protected]0023fc72014-01-10 20:05:06125void RenderSurfaceImpl::SetContentRect(const gfx::Rect& content_rect) {
[email protected]d2d915aa2013-03-08 20:18:12126 if (content_rect_ == content_rect)
127 return;
[email protected]94f206c12012-08-25 00:09:14128
[email protected]d2d915aa2013-03-08 20:18:12129 surface_property_changed_ = true;
130 content_rect_ = content_rect;
[email protected]94f206c12012-08-25 00:09:14131}
132
jaydasika7ba8f922015-08-21 05:39:42133void RenderSurfaceImpl::SetContentRectFromPropertyTrees(
134 const gfx::Rect& content_rect) {
135 if (content_rect_from_property_trees_ == content_rect)
136 return;
137
138 surface_property_changed_ = true;
139 content_rect_from_property_trees_ = content_rect;
140}
141
142void RenderSurfaceImpl::SetAccumulatedContentRect(
143 const gfx::Rect& content_rect) {
144 accumulated_content_rect_ = content_rect;
145}
146
[email protected]d2d915aa2013-03-08 20:18:12147bool RenderSurfaceImpl::SurfacePropertyChanged() const {
148 // Surface property changes are tracked as follows:
149 //
150 // - surface_property_changed_ is flagged when the clip_rect or content_rect
151 // change. As of now, these are the only two properties that can be affected
152 // by descendant layers.
153 //
154 // - all other property changes come from the owning layer (or some ancestor
155 // layer that propagates its change to the owning layer).
156 //
157 DCHECK(owning_layer_);
[email protected]7aba6662013-03-12 10:17:34158 return surface_property_changed_ || owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14159}
160
[email protected]d2d915aa2013-03-08 20:18:12161bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const {
[email protected]7aba6662013-03-12 10:17:34162 return surface_property_changed_ && !owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14163}
164
[email protected]d2d915aa2013-03-08 20:18:12165void RenderSurfaceImpl::AddContributingDelegatedRenderPassLayer(
166 LayerImpl* layer) {
167 DCHECK(std::find(layer_list_.begin(), layer_list_.end(), layer) !=
168 layer_list_.end());
169 DelegatedRendererLayerImpl* delegated_renderer_layer =
170 static_cast<DelegatedRendererLayerImpl*>(layer);
171 contributing_delegated_render_pass_layer_list_.push_back(
172 delegated_renderer_layer);
[email protected]7d929c02012-09-20 17:26:57173}
174
[email protected]d2d915aa2013-03-08 20:18:12175void RenderSurfaceImpl::ClearLayerLists() {
176 layer_list_.clear();
177 contributing_delegated_render_pass_layer_list_.clear();
[email protected]7d929c02012-09-20 17:26:57178}
179
[email protected]0cd7d6f72014-08-22 14:50:51180RenderPassId RenderSurfaceImpl::GetRenderPassId() {
[email protected]d2d915aa2013-03-08 20:18:12181 int layer_id = owning_layer_->id();
182 int sub_id = 0;
183 DCHECK_GT(layer_id, 0);
[email protected]0cd7d6f72014-08-22 14:50:51184 return RenderPassId(layer_id, sub_id);
[email protected]0f077a52012-09-08 01:45:24185}
186
[email protected]d2d915aa2013-03-08 20:18:12187void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink* pass_sink) {
188 for (size_t i = 0;
189 i < contributing_delegated_render_pass_layer_list_.size();
190 ++i) {
191 DelegatedRendererLayerImpl* delegated_renderer_layer =
192 contributing_delegated_render_pass_layer_list_[i];
193 delegated_renderer_layer->AppendContributingRenderPasses(pass_sink);
194 }
[email protected]7d929c02012-09-20 17:26:57195
[email protected]b27511f2013-11-28 05:39:04196 scoped_ptr<RenderPass> pass = RenderPass::Create(layer_list_.size());
[email protected]0cd7d6f72014-08-22 14:50:51197 pass->SetNew(GetRenderPassId(),
[email protected]d2d915aa2013-03-08 20:18:12198 content_rect_,
[email protected]b4ead7b2014-04-07 18:12:18199 gfx::IntersectRects(content_rect_,
200 damage_tracker_->current_damage_rect()),
[email protected]d2d915aa2013-03-08 20:18:12201 screen_space_transform_);
danakja04855a2015-11-18 20:39:10202 pass_sink->AppendRenderPass(std::move(pass));
[email protected]467b3612012-08-28 07:41:16203}
204
danakja1b92e42015-02-11 21:07:47205void RenderSurfaceImpl::AppendQuads(RenderPass* render_pass,
206 const gfx::Transform& draw_transform,
207 const Occlusion& occlusion_in_content_space,
208 SkColor debug_border_color,
209 float debug_border_width,
210 LayerImpl* mask_layer,
211 AppendQuadsData* append_quads_data,
212 RenderPassId render_pass_id) {
danakj64767d902015-06-19 00:10:43213 gfx::Rect visible_layer_rect =
danakja1b92e42015-02-11 21:07:47214 occlusion_in_content_space.GetUnoccludedContentRect(content_rect_);
danakj64767d902015-06-19 00:10:43215 if (visible_layer_rect.IsEmpty())
[email protected]ba0f8d92014-03-21 18:41:10216 return;
217
[email protected]c6707fd2014-06-23 05:50:36218 SharedQuadState* shared_quad_state =
219 render_pass->CreateAndAppendSharedQuadState();
danakja1b92e42015-02-11 21:07:47220 shared_quad_state->SetAll(draw_transform, content_rect_.size(), content_rect_,
221 clip_rect_, is_clipped_, draw_opacity_,
[email protected]a9d4d4f2014-06-19 06:49:28222 owning_layer_->blend_mode(),
223 owning_layer_->sorting_context_id());
[email protected]94f206c12012-08-25 00:09:14224
[email protected]7aba6662013-03-12 10:17:34225 if (owning_layer_->ShowDebugBorders()) {
[email protected]f7030c32014-07-03 18:54:34226 DebugBorderDrawQuad* debug_border_quad =
227 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
danakja1b92e42015-02-11 21:07:47228 debug_border_quad->SetNew(shared_quad_state, content_rect_,
danakj64767d902015-06-19 00:10:43229 visible_layer_rect, debug_border_color,
danakja1b92e42015-02-11 21:07:47230 debug_border_width);
[email protected]d2d915aa2013-03-08 20:18:12231 }
[email protected]94f206c12012-08-25 00:09:14232
jbaumanbbd425e2015-05-19 00:33:35233 ResourceId mask_resource_id = 0;
ennef6f3fbba42014-10-16 18:16:49234 gfx::Size mask_texture_size;
235 gfx::Vector2dF mask_uv_scale;
ajumad9432e32015-11-30 19:43:44236 gfx::Transform owning_layer_draw_transform = owning_layer_->DrawTransform();
danakja1b92e42015-02-11 21:07:47237 if (mask_layer && mask_layer->DrawsContent() &&
238 !mask_layer->bounds().IsEmpty()) {
ennef6f3fbba42014-10-16 18:16:49239 mask_layer->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
[email protected]d2d915aa2013-03-08 20:18:12240 gfx::Vector2dF owning_layer_draw_scale =
ajumad9432e32015-11-30 19:43:44241 MathUtil::ComputeTransform2dScaleComponents(owning_layer_draw_transform,
242 1.f);
danakjddaec912015-09-25 19:38:40243 gfx::SizeF unclipped_mask_target_size = gfx::ScaleSize(
244 gfx::SizeF(owning_layer_->bounds()), owning_layer_draw_scale.x(),
245 owning_layer_draw_scale.y());
ennef6f3fbba42014-10-16 18:16:49246 mask_uv_scale = gfx::Vector2dF(
247 content_rect_.width() / unclipped_mask_target_size.width(),
248 content_rect_.height() / unclipped_mask_target_size.height());
[email protected]d2d915aa2013-03-08 20:18:12249 }
[email protected]94f206c12012-08-25 00:09:14250
robertnce999072016-01-05 15:06:13251 DCHECK(owning_layer_draw_transform.IsScale2d());
[email protected]7ac3d492014-08-08 21:25:32252 gfx::Vector2dF owning_layer_to_target_scale =
ajumad9432e32015-11-30 19:43:44253 owning_layer_draw_transform.Scale2d();
[email protected]7ac3d492014-08-08 21:25:32254
[email protected]f7030c32014-07-03 18:54:34255 RenderPassDrawQuad* quad =
256 render_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
danakj64767d902015-06-19 00:10:43257 quad->SetNew(shared_quad_state, content_rect_, visible_layer_rect,
258 render_pass_id, mask_resource_id, mask_uv_scale,
259 mask_texture_size, owning_layer_->filters(),
[email protected]7ac3d492014-08-08 21:25:32260 owning_layer_to_target_scale,
[email protected]7aba6662013-03-12 10:17:34261 owning_layer_->background_filters());
[email protected]94f206c12012-08-25 00:09:14262}
263
[email protected]bc5e77c2012-11-05 20:00:49264} // namespace cc