blob: 600206faa081522f815ead7ae6d41d0e912123d6 [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 }
53
jaydasika5160e672015-10-15 15:25:1454 // If the rect has a NaN coordinate, we return empty rect to avoid crashes in
55 // functions (for example, gfx::ToEnclosedRect) that are called on this rect.
56 if (std::isnan(drawable_content_rect.x()) ||
57 std::isnan(drawable_content_rect.y()) ||
58 std::isnan(drawable_content_rect.right()) ||
59 std::isnan(drawable_content_rect.bottom()))
60 return gfx::RectF();
61
[email protected]d2d915aa2013-03-08 20:18:1262 return drawable_content_rect;
[email protected]94f206c12012-08-25 00:09:1463}
64
danakja1b92e42015-02-11 21:07:4765SkColor RenderSurfaceImpl::GetDebugBorderColor() const {
66 return DebugColors::SurfaceBorderColor();
67}
68
69SkColor RenderSurfaceImpl::GetReplicaDebugBorderColor() const {
70 return DebugColors::SurfaceReplicaBorderColor();
71}
72
73float RenderSurfaceImpl::GetDebugBorderWidth() const {
74 return DebugColors::SurfaceBorderWidth(owning_layer_->layer_tree_impl());
75}
76
77float RenderSurfaceImpl::GetReplicaDebugBorderWidth() const {
78 return DebugColors::SurfaceReplicaBorderWidth(
79 owning_layer_->layer_tree_impl());
80}
81
[email protected]d2d915aa2013-03-08 20:18:1282int RenderSurfaceImpl::OwningLayerId() const {
83 return owning_layer_ ? owning_layer_->id() : 0;
[email protected]94f206c12012-08-25 00:09:1484}
85
jaydasika7ba8f922015-08-21 05:39:4286bool RenderSurfaceImpl::HasReplica() const {
87 return owning_layer_->has_replica();
88}
89
jaydasika23fb3822015-08-25 03:22:5990const LayerImpl* RenderSurfaceImpl::ReplicaLayer() const {
91 return owning_layer_->replica_layer();
92}
93
jaydasika504a0502015-07-23 19:25:4494int RenderSurfaceImpl::TransformTreeIndex() const {
95 return owning_layer_->transform_tree_index();
96}
[email protected]94f206c12012-08-25 00:09:1497
jaydasikaebf9e4ea2015-08-14 21:29:1298int RenderSurfaceImpl::ClipTreeIndex() const {
99 return owning_layer_->clip_tree_index();
100}
101
jaydasika0f4b1a92015-08-18 23:19:02102int RenderSurfaceImpl::EffectTreeIndex() const {
103 return owning_layer_->effect_tree_index();
104}
105
106int RenderSurfaceImpl::TargetEffectTreeIndex() const {
107 if (!owning_layer_->parent() || !owning_layer_->parent()->render_target())
108 return -1;
109 return owning_layer_->parent()->render_target()->effect_tree_index();
110}
111
[email protected]0023fc72014-01-10 20:05:06112void RenderSurfaceImpl::SetClipRect(const gfx::Rect& clip_rect) {
[email protected]d2d915aa2013-03-08 20:18:12113 if (clip_rect_ == clip_rect)
114 return;
[email protected]94f206c12012-08-25 00:09:14115
[email protected]d2d915aa2013-03-08 20:18:12116 surface_property_changed_ = true;
117 clip_rect_ = clip_rect;
[email protected]94f206c12012-08-25 00:09:14118}
119
[email protected]0023fc72014-01-10 20:05:06120void RenderSurfaceImpl::SetContentRect(const gfx::Rect& content_rect) {
[email protected]d2d915aa2013-03-08 20:18:12121 if (content_rect_ == content_rect)
122 return;
[email protected]94f206c12012-08-25 00:09:14123
[email protected]d2d915aa2013-03-08 20:18:12124 surface_property_changed_ = true;
125 content_rect_ = content_rect;
[email protected]94f206c12012-08-25 00:09:14126}
127
jaydasika7ba8f922015-08-21 05:39:42128void RenderSurfaceImpl::SetContentRectFromPropertyTrees(
129 const gfx::Rect& content_rect) {
130 if (content_rect_from_property_trees_ == content_rect)
131 return;
132
133 surface_property_changed_ = true;
134 content_rect_from_property_trees_ = content_rect;
135}
136
137void RenderSurfaceImpl::SetAccumulatedContentRect(
138 const gfx::Rect& content_rect) {
139 accumulated_content_rect_ = content_rect;
140}
141
[email protected]d2d915aa2013-03-08 20:18:12142bool RenderSurfaceImpl::SurfacePropertyChanged() const {
143 // Surface property changes are tracked as follows:
144 //
145 // - surface_property_changed_ is flagged when the clip_rect or content_rect
146 // change. As of now, these are the only two properties that can be affected
147 // by descendant layers.
148 //
149 // - all other property changes come from the owning layer (or some ancestor
150 // layer that propagates its change to the owning layer).
151 //
152 DCHECK(owning_layer_);
[email protected]7aba6662013-03-12 10:17:34153 return surface_property_changed_ || owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14154}
155
[email protected]d2d915aa2013-03-08 20:18:12156bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const {
[email protected]7aba6662013-03-12 10:17:34157 return surface_property_changed_ && !owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14158}
159
[email protected]d2d915aa2013-03-08 20:18:12160void RenderSurfaceImpl::AddContributingDelegatedRenderPassLayer(
161 LayerImpl* layer) {
162 DCHECK(std::find(layer_list_.begin(), layer_list_.end(), layer) !=
163 layer_list_.end());
164 DelegatedRendererLayerImpl* delegated_renderer_layer =
165 static_cast<DelegatedRendererLayerImpl*>(layer);
166 contributing_delegated_render_pass_layer_list_.push_back(
167 delegated_renderer_layer);
[email protected]7d929c02012-09-20 17:26:57168}
169
[email protected]d2d915aa2013-03-08 20:18:12170void RenderSurfaceImpl::ClearLayerLists() {
171 layer_list_.clear();
172 contributing_delegated_render_pass_layer_list_.clear();
[email protected]7d929c02012-09-20 17:26:57173}
174
[email protected]0cd7d6f72014-08-22 14:50:51175RenderPassId RenderSurfaceImpl::GetRenderPassId() {
[email protected]d2d915aa2013-03-08 20:18:12176 int layer_id = owning_layer_->id();
177 int sub_id = 0;
178 DCHECK_GT(layer_id, 0);
[email protected]0cd7d6f72014-08-22 14:50:51179 return RenderPassId(layer_id, sub_id);
[email protected]0f077a52012-09-08 01:45:24180}
181
[email protected]d2d915aa2013-03-08 20:18:12182void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink* pass_sink) {
183 for (size_t i = 0;
184 i < contributing_delegated_render_pass_layer_list_.size();
185 ++i) {
186 DelegatedRendererLayerImpl* delegated_renderer_layer =
187 contributing_delegated_render_pass_layer_list_[i];
188 delegated_renderer_layer->AppendContributingRenderPasses(pass_sink);
189 }
[email protected]7d929c02012-09-20 17:26:57190
[email protected]b27511f2013-11-28 05:39:04191 scoped_ptr<RenderPass> pass = RenderPass::Create(layer_list_.size());
[email protected]0cd7d6f72014-08-22 14:50:51192 pass->SetNew(GetRenderPassId(),
[email protected]d2d915aa2013-03-08 20:18:12193 content_rect_,
[email protected]b4ead7b2014-04-07 18:12:18194 gfx::IntersectRects(content_rect_,
195 damage_tracker_->current_damage_rect()),
[email protected]d2d915aa2013-03-08 20:18:12196 screen_space_transform_);
danakja04855a2015-11-18 20:39:10197 pass_sink->AppendRenderPass(std::move(pass));
[email protected]467b3612012-08-28 07:41:16198}
199
danakja1b92e42015-02-11 21:07:47200void RenderSurfaceImpl::AppendQuads(RenderPass* render_pass,
201 const gfx::Transform& draw_transform,
202 const Occlusion& occlusion_in_content_space,
203 SkColor debug_border_color,
204 float debug_border_width,
205 LayerImpl* mask_layer,
206 AppendQuadsData* append_quads_data,
207 RenderPassId render_pass_id) {
danakj64767d902015-06-19 00:10:43208 gfx::Rect visible_layer_rect =
danakja1b92e42015-02-11 21:07:47209 occlusion_in_content_space.GetUnoccludedContentRect(content_rect_);
danakj64767d902015-06-19 00:10:43210 if (visible_layer_rect.IsEmpty())
[email protected]ba0f8d92014-03-21 18:41:10211 return;
212
[email protected]c6707fd2014-06-23 05:50:36213 SharedQuadState* shared_quad_state =
214 render_pass->CreateAndAppendSharedQuadState();
danakja1b92e42015-02-11 21:07:47215 shared_quad_state->SetAll(draw_transform, content_rect_.size(), content_rect_,
216 clip_rect_, is_clipped_, draw_opacity_,
[email protected]a9d4d4f2014-06-19 06:49:28217 owning_layer_->blend_mode(),
218 owning_layer_->sorting_context_id());
[email protected]94f206c12012-08-25 00:09:14219
[email protected]7aba6662013-03-12 10:17:34220 if (owning_layer_->ShowDebugBorders()) {
[email protected]f7030c32014-07-03 18:54:34221 DebugBorderDrawQuad* debug_border_quad =
222 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
danakja1b92e42015-02-11 21:07:47223 debug_border_quad->SetNew(shared_quad_state, content_rect_,
danakj64767d902015-06-19 00:10:43224 visible_layer_rect, debug_border_color,
danakja1b92e42015-02-11 21:07:47225 debug_border_width);
[email protected]d2d915aa2013-03-08 20:18:12226 }
[email protected]94f206c12012-08-25 00:09:14227
jbaumanbbd425e2015-05-19 00:33:35228 ResourceId mask_resource_id = 0;
ennef6f3fbba42014-10-16 18:16:49229 gfx::Size mask_texture_size;
230 gfx::Vector2dF mask_uv_scale;
ajumad9432e32015-11-30 19:43:44231 gfx::Transform owning_layer_draw_transform = owning_layer_->DrawTransform();
danakja1b92e42015-02-11 21:07:47232 if (mask_layer && mask_layer->DrawsContent() &&
233 !mask_layer->bounds().IsEmpty()) {
ennef6f3fbba42014-10-16 18:16:49234 mask_layer->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
[email protected]d2d915aa2013-03-08 20:18:12235 gfx::Vector2dF owning_layer_draw_scale =
ajumad9432e32015-11-30 19:43:44236 MathUtil::ComputeTransform2dScaleComponents(owning_layer_draw_transform,
237 1.f);
danakjddaec912015-09-25 19:38:40238 gfx::SizeF unclipped_mask_target_size = gfx::ScaleSize(
239 gfx::SizeF(owning_layer_->bounds()), owning_layer_draw_scale.x(),
240 owning_layer_draw_scale.y());
ennef6f3fbba42014-10-16 18:16:49241 mask_uv_scale = gfx::Vector2dF(
242 content_rect_.width() / unclipped_mask_target_size.width(),
243 content_rect_.height() / unclipped_mask_target_size.height());
[email protected]d2d915aa2013-03-08 20:18:12244 }
[email protected]94f206c12012-08-25 00:09:14245
robertnce999072016-01-05 15:06:13246 DCHECK(owning_layer_draw_transform.IsScale2d());
[email protected]7ac3d492014-08-08 21:25:32247 gfx::Vector2dF owning_layer_to_target_scale =
ajumad9432e32015-11-30 19:43:44248 owning_layer_draw_transform.Scale2d();
[email protected]7ac3d492014-08-08 21:25:32249
[email protected]f7030c32014-07-03 18:54:34250 RenderPassDrawQuad* quad =
251 render_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
danakj64767d902015-06-19 00:10:43252 quad->SetNew(shared_quad_state, content_rect_, visible_layer_rect,
253 render_pass_id, mask_resource_id, mask_uv_scale,
254 mask_texture_size, owning_layer_->filters(),
[email protected]7ac3d492014-08-08 21:25:32255 owning_layer_to_target_scale,
[email protected]7aba6662013-03-12 10:17:34256 owning_layer_->background_filters());
[email protected]94f206c12012-08-25 00:09:14257}
258
[email protected]bc5e77c2012-11-05 20:00:49259} // namespace cc