blob: 660f5e417e7ba8724698626010df6b9614c3d08e [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
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
8
[email protected]4456eee22012-10-19 18:16:389#include "base/logging.h"
[email protected]8e61d4b2013-06-10 22:11:4810#include "base/strings/stringprintf.h"
[email protected]681ccff2013-03-18 06:13:5211#include "cc/base/math_util.h"
[email protected]6e84de22013-03-18 06:54:2712#include "cc/debug/debug_colors.h"
[email protected]cc3cfaa2013-03-18 09:05:5213#include "cc/layers/delegated_renderer_layer_impl.h"
14#include "cc/layers/layer_impl.h"
15#include "cc/layers/quad_sink.h"
16#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"
[email protected]55761e62012-11-21 18:55:5822#include "third_party/skia/include/core/SkImageFilter.h"
[email protected]aad0a0072012-11-01 18:15:5823#include "ui/gfx/rect_conversions.h"
[email protected]c8686a02012-11-27 08:29:0024#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1425
[email protected]9c88e562012-09-14 22:21:3026namespace cc {
[email protected]94f206c12012-08-25 00:09:1427
[email protected]d2d915aa2013-03-08 20:18:1228RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl* owning_layer)
29 : owning_layer_(owning_layer),
30 surface_property_changed_(false),
31 draw_opacity_(1),
32 draw_opacity_is_animating_(false),
33 target_surface_transforms_are_animating_(false),
34 screen_space_transforms_are_animating_(false),
35 is_clipped_(false),
[email protected]30fe19ff2013-07-04 00:54:4536 contributes_to_drawn_surface_(false),
[email protected]66b52e12013-11-17 15:53:1837 nearest_occlusion_immune_ancestor_(NULL),
[email protected]d2d915aa2013-03-08 20:18:1238 target_render_surface_layer_index_history_(0),
39 current_layer_index_history_(0) {
40 damage_tracker_ = DamageTracker::Create();
[email protected]94f206c12012-08-25 00:09:1441}
42
[email protected]d2d915aa2013-03-08 20:18:1243RenderSurfaceImpl::~RenderSurfaceImpl() {}
44
45gfx::RectF RenderSurfaceImpl::DrawableContentRect() const {
46 gfx::RectF drawable_content_rect =
[email protected]fa816c62013-03-18 04:24:2147 MathUtil::MapClippedRect(draw_transform_, content_rect_);
[email protected]7aba6662013-03-12 10:17:3448 if (owning_layer_->has_replica()) {
[email protected]d2d915aa2013-03-08 20:18:1249 drawable_content_rect.Union(
[email protected]fa816c62013-03-18 04:24:2150 MathUtil::MapClippedRect(replica_draw_transform_, content_rect_));
[email protected]d2d915aa2013-03-08 20:18:1251 }
52
53 return drawable_content_rect;
[email protected]94f206c12012-08-25 00:09:1454}
55
[email protected]d2d915aa2013-03-08 20:18:1256std::string RenderSurfaceImpl::Name() const {
57 return base::StringPrintf("RenderSurfaceImpl(id=%i,owner=%s)",
58 owning_layer_->id(),
[email protected]7aba6662013-03-12 10:17:3459 owning_layer_->debug_name().data());
[email protected]94f206c12012-08-25 00:09:1460}
61
[email protected]d2d915aa2013-03-08 20:18:1262int RenderSurfaceImpl::OwningLayerId() const {
63 return owning_layer_ ? owning_layer_->id() : 0;
[email protected]94f206c12012-08-25 00:09:1464}
65
66
[email protected]d2d915aa2013-03-08 20:18:1267void RenderSurfaceImpl::SetClipRect(gfx::Rect clip_rect) {
68 if (clip_rect_ == clip_rect)
69 return;
[email protected]94f206c12012-08-25 00:09:1470
[email protected]d2d915aa2013-03-08 20:18:1271 surface_property_changed_ = true;
72 clip_rect_ = clip_rect;
[email protected]94f206c12012-08-25 00:09:1473}
74
[email protected]d2d915aa2013-03-08 20:18:1275bool RenderSurfaceImpl::ContentsChanged() const {
76 return !damage_tracker_->current_damage_rect().IsEmpty();
[email protected]94f206c12012-08-25 00:09:1477}
78
[email protected]d2d915aa2013-03-08 20:18:1279void RenderSurfaceImpl::SetContentRect(gfx::Rect content_rect) {
80 if (content_rect_ == content_rect)
81 return;
[email protected]94f206c12012-08-25 00:09:1482
[email protected]d2d915aa2013-03-08 20:18:1283 surface_property_changed_ = true;
84 content_rect_ = content_rect;
[email protected]94f206c12012-08-25 00:09:1485}
86
[email protected]d2d915aa2013-03-08 20:18:1287bool RenderSurfaceImpl::SurfacePropertyChanged() const {
88 // Surface property changes are tracked as follows:
89 //
90 // - surface_property_changed_ is flagged when the clip_rect or content_rect
91 // change. As of now, these are the only two properties that can be affected
92 // by descendant layers.
93 //
94 // - all other property changes come from the owning layer (or some ancestor
95 // layer that propagates its change to the owning layer).
96 //
97 DCHECK(owning_layer_);
[email protected]7aba6662013-03-12 10:17:3498 return surface_property_changed_ || owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:1499}
100
[email protected]d2d915aa2013-03-08 20:18:12101bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const {
[email protected]7aba6662013-03-12 10:17:34102 return surface_property_changed_ && !owning_layer_->LayerPropertyChanged();
[email protected]94f206c12012-08-25 00:09:14103}
104
[email protected]d2d915aa2013-03-08 20:18:12105void RenderSurfaceImpl::AddContributingDelegatedRenderPassLayer(
106 LayerImpl* layer) {
107 DCHECK(std::find(layer_list_.begin(), layer_list_.end(), layer) !=
108 layer_list_.end());
109 DelegatedRendererLayerImpl* delegated_renderer_layer =
110 static_cast<DelegatedRendererLayerImpl*>(layer);
111 contributing_delegated_render_pass_layer_list_.push_back(
112 delegated_renderer_layer);
[email protected]7d929c02012-09-20 17:26:57113}
114
[email protected]d2d915aa2013-03-08 20:18:12115void RenderSurfaceImpl::ClearLayerLists() {
116 layer_list_.clear();
117 contributing_delegated_render_pass_layer_list_.clear();
[email protected]7d929c02012-09-20 17:26:57118}
119
[email protected]d2d915aa2013-03-08 20:18:12120RenderPass::Id RenderSurfaceImpl::RenderPassId() {
121 int layer_id = owning_layer_->id();
122 int sub_id = 0;
123 DCHECK_GT(layer_id, 0);
124 return RenderPass::Id(layer_id, sub_id);
[email protected]0f077a52012-09-08 01:45:24125}
126
[email protected]d2d915aa2013-03-08 20:18:12127void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink* pass_sink) {
128 for (size_t i = 0;
129 i < contributing_delegated_render_pass_layer_list_.size();
130 ++i) {
131 DelegatedRendererLayerImpl* delegated_renderer_layer =
132 contributing_delegated_render_pass_layer_list_[i];
133 delegated_renderer_layer->AppendContributingRenderPasses(pass_sink);
134 }
[email protected]7d929c02012-09-20 17:26:57135
[email protected]d2d915aa2013-03-08 20:18:12136 scoped_ptr<RenderPass> pass = RenderPass::Create();
137 pass->SetNew(RenderPassId(),
138 content_rect_,
139 damage_tracker_->current_damage_rect(),
140 screen_space_transform_);
[email protected]c1bb5af2013-03-13 19:06:27141 pass_sink->AppendRenderPass(pass.Pass());
[email protected]467b3612012-08-28 07:41:16142}
143
[email protected]d2d915aa2013-03-08 20:18:12144void RenderSurfaceImpl::AppendQuads(QuadSink* quad_sink,
145 AppendQuadsData* append_quads_data,
146 bool for_replica,
147 RenderPass::Id render_pass_id) {
[email protected]7aba6662013-03-12 10:17:34148 DCHECK(!for_replica || owning_layer_->has_replica());
[email protected]94f206c12012-08-25 00:09:14149
[email protected]d2d915aa2013-03-08 20:18:12150 const gfx::Transform& draw_transform =
151 for_replica ? replica_draw_transform_ : draw_transform_;
152 SharedQuadState* shared_quad_state =
[email protected]c7e95b42013-03-18 01:13:49153 quad_sink->UseSharedQuadState(SharedQuadState::Create());
[email protected]d2d915aa2013-03-08 20:18:12154 shared_quad_state->SetAll(draw_transform,
155 content_rect_.size(),
156 content_rect_,
157 clip_rect_,
158 is_clipped_,
159 draw_opacity_);
[email protected]94f206c12012-08-25 00:09:14160
[email protected]7aba6662013-03-12 10:17:34161 if (owning_layer_->ShowDebugBorders()) {
[email protected]d2d915aa2013-03-08 20:18:12162 SkColor color = for_replica ?
163 DebugColors::SurfaceReplicaBorderColor() :
164 DebugColors::SurfaceBorderColor();
165 float width = for_replica ?
166 DebugColors::SurfaceReplicaBorderWidth(
[email protected]7aba6662013-03-12 10:17:34167 owning_layer_->layer_tree_impl()) :
[email protected]d2d915aa2013-03-08 20:18:12168 DebugColors::SurfaceBorderWidth(
[email protected]7aba6662013-03-12 10:17:34169 owning_layer_->layer_tree_impl());
[email protected]d2d915aa2013-03-08 20:18:12170 scoped_ptr<DebugBorderDrawQuad> debug_border_quad =
171 DebugBorderDrawQuad::Create();
172 debug_border_quad->SetNew(shared_quad_state, content_rect_, color, width);
[email protected]c7e95b42013-03-18 01:13:49173 quad_sink->Append(debug_border_quad.PassAs<DrawQuad>(), append_quads_data);
[email protected]d2d915aa2013-03-08 20:18:12174 }
[email protected]94f206c12012-08-25 00:09:14175
[email protected]27d851ab2013-06-28 01:09:50176 // TODO(shawnsingh): By using the same RenderSurfaceImpl for both the content
177 // and its reflection, it's currently not possible to apply a separate mask to
178 // the reflection layer or correctly handle opacity in reflections (opacity
179 // must be applied after drawing both the layer and its reflection). The
180 // solution is to introduce yet another RenderSurfaceImpl to draw the layer
181 // and its reflection in. For now we only apply a separate reflection mask if
182 // the contents don't have a mask of their own.
[email protected]7aba6662013-03-12 10:17:34183 LayerImpl* mask_layer = owning_layer_->mask_layer();
[email protected]d2d915aa2013-03-08 20:18:12184 if (mask_layer &&
[email protected]7aba6662013-03-12 10:17:34185 (!mask_layer->DrawsContent() || mask_layer->bounds().IsEmpty()))
[email protected]d2d915aa2013-03-08 20:18:12186 mask_layer = NULL;
[email protected]94f206c12012-08-25 00:09:14187
[email protected]d2d915aa2013-03-08 20:18:12188 if (!mask_layer && for_replica) {
[email protected]7aba6662013-03-12 10:17:34189 mask_layer = owning_layer_->replica_layer()->mask_layer();
[email protected]d2d915aa2013-03-08 20:18:12190 if (mask_layer &&
[email protected]7aba6662013-03-12 10:17:34191 (!mask_layer->DrawsContent() || mask_layer->bounds().IsEmpty()))
[email protected]d2d915aa2013-03-08 20:18:12192 mask_layer = NULL;
193 }
[email protected]94f206c12012-08-25 00:09:14194
[email protected]d2d915aa2013-03-08 20:18:12195 gfx::RectF mask_uv_rect(0.f, 0.f, 1.f, 1.f);
196 if (mask_layer) {
197 gfx::Vector2dF owning_layer_draw_scale =
[email protected]fa816c62013-03-18 04:24:21198 MathUtil::ComputeTransform2dScaleComponents(
[email protected]7aba6662013-03-12 10:17:34199 owning_layer_->draw_transform(), 1.f);
[email protected]8591a642013-04-17 18:48:56200 gfx::SizeF unclipped_mask_target_size = gfx::ScaleSize(
[email protected]7aba6662013-03-12 10:17:34201 owning_layer_->content_bounds(),
[email protected]d2d915aa2013-03-08 20:18:12202 owning_layer_draw_scale.x(),
203 owning_layer_draw_scale.y());
[email protected]458af1e2012-12-13 05:12:18204
[email protected]8591a642013-04-17 18:48:56205 float uv_scale_x =
206 content_rect_.width() / unclipped_mask_target_size.width();
207 float uv_scale_y =
208 content_rect_.height() / unclipped_mask_target_size.height();
[email protected]8611f012013-01-17 03:44:55209
[email protected]d2d915aa2013-03-08 20:18:12210 mask_uv_rect = gfx::RectF(
211 uv_scale_x * content_rect_.x() / content_rect_.width(),
212 uv_scale_y * content_rect_.y() / content_rect_.height(),
213 uv_scale_x,
214 uv_scale_y);
215 }
[email protected]94f206c12012-08-25 00:09:14216
[email protected]d2d915aa2013-03-08 20:18:12217 ResourceProvider::ResourceId mask_resource_id =
[email protected]7aba6662013-03-12 10:17:34218 mask_layer ? mask_layer->ContentsResourceId() : 0;
[email protected]d2d915aa2013-03-08 20:18:12219 gfx::Rect contents_changed_since_last_frame =
220 ContentsChanged() ? content_rect_ : gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14221
[email protected]d2d915aa2013-03-08 20:18:12222 scoped_ptr<RenderPassDrawQuad> quad = RenderPassDrawQuad::Create();
223 quad->SetNew(shared_quad_state,
224 content_rect_,
225 render_pass_id,
226 for_replica,
227 mask_resource_id,
228 contents_changed_since_last_frame,
229 mask_uv_rect,
230 owning_layer_->filters(),
[email protected]7aba6662013-03-12 10:17:34231 owning_layer_->background_filters());
[email protected]c7e95b42013-03-18 01:13:49232 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
[email protected]94f206c12012-08-25 00:09:14233}
234
[email protected]bc5e77c2012-11-05 20:00:49235} // namespace cc