1 // 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.
5 #include "cc/layers/render_surface_impl.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "cc/base/math_util.h"
12 #include "cc/debug/debug_colors.h"
13 #include "cc/layers/delegated_renderer_layer_impl.h"
14 #include "cc/layers/layer_impl.h"
15 #include "cc/layers/render_pass_sink.h"
16 #include "cc/quads/debug_border_draw_quad.h"
17 #include "cc/quads/render_pass.h"
18 #include "cc/quads/render_pass_draw_quad.h"
19 #include "cc/quads/shared_quad_state.h"
20 #include "cc/trees/damage_tracker.h"
21 #include "cc/trees/occlusion.h"
22 #include "third_party/skia/include/core/SkImageFilter.h"
23 #include "ui/gfx/geometry/rect_conversions.h"
24 #include "ui/gfx/transform.h"
28 RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl
* owning_layer
)
29 : owning_layer_(owning_layer
),
30 surface_property_changed_(false),
32 contributes_to_drawn_surface_(false),
34 nearest_occlusion_immune_ancestor_(nullptr),
35 target_render_surface_layer_index_history_(0),
36 current_layer_index_history_(0) {
37 damage_tracker_
= DamageTracker::Create();
40 RenderSurfaceImpl::~RenderSurfaceImpl() {}
42 gfx::RectF
RenderSurfaceImpl::DrawableContentRect() const {
43 gfx::RectF drawable_content_rect
=
44 MathUtil::MapClippedRect(draw_transform_
, content_rect_
);
45 if (owning_layer_
->has_replica()) {
46 drawable_content_rect
.Union(
47 MathUtil::MapClippedRect(replica_draw_transform_
, content_rect_
));
50 return drawable_content_rect
;
53 SkColor
RenderSurfaceImpl::GetDebugBorderColor() const {
54 return DebugColors::SurfaceBorderColor();
57 SkColor
RenderSurfaceImpl::GetReplicaDebugBorderColor() const {
58 return DebugColors::SurfaceReplicaBorderColor();
61 float RenderSurfaceImpl::GetDebugBorderWidth() const {
62 return DebugColors::SurfaceBorderWidth(owning_layer_
->layer_tree_impl());
65 float RenderSurfaceImpl::GetReplicaDebugBorderWidth() const {
66 return DebugColors::SurfaceReplicaBorderWidth(
67 owning_layer_
->layer_tree_impl());
70 int RenderSurfaceImpl::OwningLayerId() const {
71 return owning_layer_
? owning_layer_
->id() : 0;
74 bool RenderSurfaceImpl::HasReplica() const {
75 return owning_layer_
->has_replica();
78 gfx::Transform
RenderSurfaceImpl::ReplicaDrawTransform() const {
79 return replica_draw_transform_
;
82 int RenderSurfaceImpl::TransformTreeIndex() const {
83 return owning_layer_
->transform_tree_index();
86 int RenderSurfaceImpl::ClipTreeIndex() const {
87 return owning_layer_
->clip_tree_index();
90 int RenderSurfaceImpl::EffectTreeIndex() const {
91 return owning_layer_
->effect_tree_index();
94 int RenderSurfaceImpl::TargetEffectTreeIndex() const {
95 if (!owning_layer_
->parent() || !owning_layer_
->parent()->render_target())
97 return owning_layer_
->parent()->render_target()->effect_tree_index();
100 void RenderSurfaceImpl::SetClipRect(const gfx::Rect
& clip_rect
) {
101 if (clip_rect_
== clip_rect
)
104 surface_property_changed_
= true;
105 clip_rect_
= clip_rect
;
108 void RenderSurfaceImpl::SetContentRect(const gfx::Rect
& content_rect
) {
109 if (content_rect_
== content_rect
)
112 surface_property_changed_
= true;
113 content_rect_
= content_rect
;
116 void RenderSurfaceImpl::SetContentRectFromPropertyTrees(
117 const gfx::Rect
& content_rect
) {
118 if (content_rect_from_property_trees_
== content_rect
)
121 surface_property_changed_
= true;
122 content_rect_from_property_trees_
= content_rect
;
125 void RenderSurfaceImpl::SetAccumulatedContentRect(
126 const gfx::Rect
& content_rect
) {
127 accumulated_content_rect_
= content_rect
;
130 bool RenderSurfaceImpl::SurfacePropertyChanged() const {
131 // Surface property changes are tracked as follows:
133 // - surface_property_changed_ is flagged when the clip_rect or content_rect
134 // change. As of now, these are the only two properties that can be affected
135 // by descendant layers.
137 // - all other property changes come from the owning layer (or some ancestor
138 // layer that propagates its change to the owning layer).
140 DCHECK(owning_layer_
);
141 return surface_property_changed_
|| owning_layer_
->LayerPropertyChanged();
144 bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const {
145 return surface_property_changed_
&& !owning_layer_
->LayerPropertyChanged();
148 void RenderSurfaceImpl::AddContributingDelegatedRenderPassLayer(
150 DCHECK(std::find(layer_list_
.begin(), layer_list_
.end(), layer
) !=
152 DelegatedRendererLayerImpl
* delegated_renderer_layer
=
153 static_cast<DelegatedRendererLayerImpl
*>(layer
);
154 contributing_delegated_render_pass_layer_list_
.push_back(
155 delegated_renderer_layer
);
158 void RenderSurfaceImpl::ClearLayerLists() {
160 contributing_delegated_render_pass_layer_list_
.clear();
163 RenderPassId
RenderSurfaceImpl::GetRenderPassId() {
164 int layer_id
= owning_layer_
->id();
166 DCHECK_GT(layer_id
, 0);
167 return RenderPassId(layer_id
, sub_id
);
170 void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink
* pass_sink
) {
172 i
< contributing_delegated_render_pass_layer_list_
.size();
174 DelegatedRendererLayerImpl
* delegated_renderer_layer
=
175 contributing_delegated_render_pass_layer_list_
[i
];
176 delegated_renderer_layer
->AppendContributingRenderPasses(pass_sink
);
179 scoped_ptr
<RenderPass
> pass
= RenderPass::Create(layer_list_
.size());
180 pass
->SetNew(GetRenderPassId(),
182 gfx::IntersectRects(content_rect_
,
183 damage_tracker_
->current_damage_rect()),
184 screen_space_transform_
);
185 pass_sink
->AppendRenderPass(pass
.Pass());
188 void RenderSurfaceImpl::AppendQuads(RenderPass
* render_pass
,
189 const gfx::Transform
& draw_transform
,
190 const Occlusion
& occlusion_in_content_space
,
191 SkColor debug_border_color
,
192 float debug_border_width
,
193 LayerImpl
* mask_layer
,
194 AppendQuadsData
* append_quads_data
,
195 RenderPassId render_pass_id
) {
196 gfx::Rect visible_layer_rect
=
197 occlusion_in_content_space
.GetUnoccludedContentRect(content_rect_
);
198 if (visible_layer_rect
.IsEmpty())
201 SharedQuadState
* shared_quad_state
=
202 render_pass
->CreateAndAppendSharedQuadState();
203 shared_quad_state
->SetAll(draw_transform
, content_rect_
.size(), content_rect_
,
204 clip_rect_
, is_clipped_
, draw_opacity_
,
205 owning_layer_
->blend_mode(),
206 owning_layer_
->sorting_context_id());
208 if (owning_layer_
->ShowDebugBorders()) {
209 DebugBorderDrawQuad
* debug_border_quad
=
210 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
211 debug_border_quad
->SetNew(shared_quad_state
, content_rect_
,
212 visible_layer_rect
, debug_border_color
,
216 ResourceId mask_resource_id
= 0;
217 gfx::Size mask_texture_size
;
218 gfx::Vector2dF mask_uv_scale
;
219 if (mask_layer
&& mask_layer
->DrawsContent() &&
220 !mask_layer
->bounds().IsEmpty()) {
221 mask_layer
->GetContentsResourceId(&mask_resource_id
, &mask_texture_size
);
222 gfx::Vector2dF owning_layer_draw_scale
=
223 MathUtil::ComputeTransform2dScaleComponents(
224 owning_layer_
->draw_transform(), 1.f
);
225 gfx::SizeF unclipped_mask_target_size
=
226 gfx::ScaleSize(owning_layer_
->bounds(), owning_layer_draw_scale
.x(),
227 owning_layer_draw_scale
.y());
228 mask_uv_scale
= gfx::Vector2dF(
229 content_rect_
.width() / unclipped_mask_target_size
.width(),
230 content_rect_
.height() / unclipped_mask_target_size
.height());
233 DCHECK(owning_layer_
->draw_properties().target_space_transform
.IsScale2d());
234 gfx::Vector2dF owning_layer_to_target_scale
=
235 owning_layer_
->draw_properties().target_space_transform
.Scale2d();
237 RenderPassDrawQuad
* quad
=
238 render_pass
->CreateAndAppendDrawQuad
<RenderPassDrawQuad
>();
239 quad
->SetNew(shared_quad_state
, content_rect_
, visible_layer_rect
,
240 render_pass_id
, mask_resource_id
, mask_uv_scale
,
241 mask_texture_size
, owning_layer_
->filters(),
242 owning_layer_to_target_scale
,
243 owning_layer_
->background_filters());