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_tracker.h"
22 #include "third_party/skia/include/core/SkImageFilter.h"
23 #include "ui/gfx/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),
31 draw_opacity_is_animating_(false),
32 target_surface_transforms_are_animating_(false),
33 screen_space_transforms_are_animating_(false),
35 contributes_to_drawn_surface_(false),
37 nearest_occlusion_immune_ancestor_(NULL
),
38 target_render_surface_layer_index_history_(0),
39 current_layer_index_history_(0) {
40 damage_tracker_
= DamageTracker::Create();
43 RenderSurfaceImpl::~RenderSurfaceImpl() {}
45 gfx::RectF
RenderSurfaceImpl::DrawableContentRect() const {
46 gfx::RectF drawable_content_rect
=
47 MathUtil::MapClippedRect(draw_transform_
, content_rect_
);
48 if (owning_layer_
->has_replica()) {
49 drawable_content_rect
.Union(
50 MathUtil::MapClippedRect(replica_draw_transform_
, content_rect_
));
53 return drawable_content_rect
;
56 int RenderSurfaceImpl::OwningLayerId() const {
57 return owning_layer_
? owning_layer_
->id() : 0;
61 void RenderSurfaceImpl::SetClipRect(const gfx::Rect
& clip_rect
) {
62 if (clip_rect_
== clip_rect
)
65 surface_property_changed_
= true;
66 clip_rect_
= clip_rect
;
69 bool RenderSurfaceImpl::ContentsChanged() const {
70 return !damage_tracker_
->current_damage_rect().IsEmpty();
73 void RenderSurfaceImpl::SetContentRect(const gfx::Rect
& content_rect
) {
74 if (content_rect_
== content_rect
)
77 surface_property_changed_
= true;
78 content_rect_
= content_rect
;
81 bool RenderSurfaceImpl::SurfacePropertyChanged() const {
82 // Surface property changes are tracked as follows:
84 // - surface_property_changed_ is flagged when the clip_rect or content_rect
85 // change. As of now, these are the only two properties that can be affected
86 // by descendant layers.
88 // - all other property changes come from the owning layer (or some ancestor
89 // layer that propagates its change to the owning layer).
91 DCHECK(owning_layer_
);
92 return surface_property_changed_
|| owning_layer_
->LayerPropertyChanged();
95 bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const {
96 return surface_property_changed_
&& !owning_layer_
->LayerPropertyChanged();
99 void RenderSurfaceImpl::AddContributingDelegatedRenderPassLayer(
101 DCHECK(std::find(layer_list_
.begin(), layer_list_
.end(), layer
) !=
103 DelegatedRendererLayerImpl
* delegated_renderer_layer
=
104 static_cast<DelegatedRendererLayerImpl
*>(layer
);
105 contributing_delegated_render_pass_layer_list_
.push_back(
106 delegated_renderer_layer
);
109 void RenderSurfaceImpl::ClearLayerLists() {
111 contributing_delegated_render_pass_layer_list_
.clear();
114 RenderPass::Id
RenderSurfaceImpl::RenderPassId() {
115 int layer_id
= owning_layer_
->id();
117 DCHECK_GT(layer_id
, 0);
118 return RenderPass::Id(layer_id
, sub_id
);
121 void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink
* pass_sink
) {
123 i
< contributing_delegated_render_pass_layer_list_
.size();
125 DelegatedRendererLayerImpl
* delegated_renderer_layer
=
126 contributing_delegated_render_pass_layer_list_
[i
];
127 delegated_renderer_layer
->AppendContributingRenderPasses(pass_sink
);
130 scoped_ptr
<RenderPass
> pass
= RenderPass::Create(layer_list_
.size());
131 pass
->SetNew(RenderPassId(),
133 gfx::IntersectRects(content_rect_
,
134 damage_tracker_
->current_damage_rect()),
135 screen_space_transform_
);
136 pass_sink
->AppendRenderPass(pass
.Pass());
139 void RenderSurfaceImpl::AppendQuads(
140 RenderPass
* render_pass
,
141 const OcclusionTracker
<LayerImpl
>& occlusion_tracker
,
142 AppendQuadsData
* append_quads_data
,
144 RenderPass::Id render_pass_id
) {
145 DCHECK(!for_replica
|| owning_layer_
->has_replica());
147 const gfx::Transform
& draw_transform
=
148 for_replica
? replica_draw_transform_
: draw_transform_
;
149 gfx::Rect visible_content_rect
=
150 occlusion_tracker
.UnoccludedContributingSurfaceContentRect(
151 content_rect_
, draw_transform
);
152 if (visible_content_rect
.IsEmpty())
155 SharedQuadState
* shared_quad_state
=
156 render_pass
->CreateAndAppendSharedQuadState();
157 shared_quad_state
->SetAll(draw_transform
,
158 content_rect_
.size(),
163 owning_layer_
->blend_mode(),
164 owning_layer_
->sorting_context_id());
166 if (owning_layer_
->ShowDebugBorders()) {
167 SkColor color
= for_replica
?
168 DebugColors::SurfaceReplicaBorderColor() :
169 DebugColors::SurfaceBorderColor();
170 float width
= for_replica
?
171 DebugColors::SurfaceReplicaBorderWidth(
172 owning_layer_
->layer_tree_impl()) :
173 DebugColors::SurfaceBorderWidth(
174 owning_layer_
->layer_tree_impl());
175 DebugBorderDrawQuad
* debug_border_quad
=
176 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
177 debug_border_quad
->SetNew(
178 shared_quad_state
, content_rect_
, visible_content_rect
, color
, width
);
181 // TODO(shawnsingh): By using the same RenderSurfaceImpl for both the content
182 // and its reflection, it's currently not possible to apply a separate mask to
183 // the reflection layer or correctly handle opacity in reflections (opacity
184 // must be applied after drawing both the layer and its reflection). The
185 // solution is to introduce yet another RenderSurfaceImpl to draw the layer
186 // and its reflection in. For now we only apply a separate reflection mask if
187 // the contents don't have a mask of their own.
188 LayerImpl
* mask_layer
= owning_layer_
->mask_layer();
190 (!mask_layer
->DrawsContent() || mask_layer
->bounds().IsEmpty()))
193 if (!mask_layer
&& for_replica
) {
194 mask_layer
= owning_layer_
->replica_layer()->mask_layer();
196 (!mask_layer
->DrawsContent() || mask_layer
->bounds().IsEmpty()))
200 gfx::RectF
mask_uv_rect(0.f
, 0.f
, 1.f
, 1.f
);
202 gfx::Vector2dF owning_layer_draw_scale
=
203 MathUtil::ComputeTransform2dScaleComponents(
204 owning_layer_
->draw_transform(), 1.f
);
205 gfx::SizeF unclipped_mask_target_size
= gfx::ScaleSize(
206 owning_layer_
->content_bounds(),
207 owning_layer_draw_scale
.x(),
208 owning_layer_draw_scale
.y());
211 content_rect_
.width() / unclipped_mask_target_size
.width();
213 content_rect_
.height() / unclipped_mask_target_size
.height();
215 mask_uv_rect
= gfx::RectF(
216 uv_scale_x
* content_rect_
.x() / content_rect_
.width(),
217 uv_scale_y
* content_rect_
.y() / content_rect_
.height(),
222 ResourceProvider::ResourceId mask_resource_id
=
223 mask_layer
? mask_layer
->ContentsResourceId() : 0;
224 gfx::Rect contents_changed_since_last_frame
=
225 ContentsChanged() ? content_rect_
: gfx::Rect();
227 RenderPassDrawQuad
* quad
=
228 render_pass
->CreateAndAppendDrawQuad
<RenderPassDrawQuad
>();
229 quad
->SetNew(shared_quad_state
,
231 visible_content_rect
,
235 contents_changed_since_last_frame
,
237 owning_layer_
->filters(),
238 owning_layer_
->background_filters());