[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 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. |
| 4 | |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 5 | #include "cc/layers/render_surface_impl.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 6 | |
[email protected] | ac7c7f5 | 2012-11-08 06:26:50 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | 4456eee2 | 2012-10-19 18:16:38 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 8e61d4b | 2013-06-10 22:11:48 | [diff] [blame] | 10 | #include "base/strings/stringprintf.h" |
[email protected] | 681ccff | 2013-03-18 06:13:52 | [diff] [blame] | 11 | #include "cc/base/math_util.h" |
[email protected] | 6e84de2 | 2013-03-18 06:54:27 | [diff] [blame] | 12 | #include "cc/debug/debug_colors.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 13 | #include "cc/layers/delegated_renderer_layer_impl.h" |
| 14 | #include "cc/layers/layer_impl.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 15 | #include "cc/layers/render_pass_sink.h" |
[email protected] | 89e8267a | 2013-03-18 07:50:56 | [diff] [blame] | 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" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 20 | #include "cc/trees/damage_tracker.h" |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 21 | #include "cc/trees/occlusion_tracker.h" |
[email protected] | 55761e6 | 2012-11-21 18:55:58 | [diff] [blame] | 22 | #include "third_party/skia/include/core/SkImageFilter.h" |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 23 | #include "ui/gfx/rect_conversions.h" |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 24 | #include "ui/gfx/transform.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 25 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 26 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 27 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 28 | RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl* owning_layer) |
| 29 | : owning_layer_(owning_layer), |
| 30 | surface_property_changed_(false), |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 31 | draw_opacity_is_animating_(false), |
| 32 | target_surface_transforms_are_animating_(false), |
| 33 | screen_space_transforms_are_animating_(false), |
| 34 | is_clipped_(false), |
[email protected] | 30fe19ff | 2013-07-04 00:54:45 | [diff] [blame] | 35 | contributes_to_drawn_surface_(false), |
[email protected] | c55f3fc | 2013-12-10 05:48:48 | [diff] [blame] | 36 | draw_opacity_(1), |
[email protected] | 66b52e1 | 2013-11-17 15:53:18 | [diff] [blame] | 37 | nearest_occlusion_immune_ancestor_(NULL), |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 38 | target_render_surface_layer_index_history_(0), |
| 39 | current_layer_index_history_(0) { |
| 40 | damage_tracker_ = DamageTracker::Create(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 43 | RenderSurfaceImpl::~RenderSurfaceImpl() {} |
| 44 | |
| 45 | gfx::RectF RenderSurfaceImpl::DrawableContentRect() const { |
| 46 | gfx::RectF drawable_content_rect = |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 47 | MathUtil::MapClippedRect(draw_transform_, content_rect_); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 48 | if (owning_layer_->has_replica()) { |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 49 | drawable_content_rect.Union( |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 50 | MathUtil::MapClippedRect(replica_draw_transform_, content_rect_)); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return drawable_content_rect; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 56 | int RenderSurfaceImpl::OwningLayerId() const { |
| 57 | return owning_layer_ ? owning_layer_->id() : 0; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 61 | void RenderSurfaceImpl::SetClipRect(const gfx::Rect& clip_rect) { |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 62 | if (clip_rect_ == clip_rect) |
| 63 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 64 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 65 | surface_property_changed_ = true; |
| 66 | clip_rect_ = clip_rect; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 69 | void RenderSurfaceImpl::SetContentRect(const gfx::Rect& content_rect) { |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 70 | if (content_rect_ == content_rect) |
| 71 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 72 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 73 | surface_property_changed_ = true; |
| 74 | content_rect_ = content_rect; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 77 | bool RenderSurfaceImpl::SurfacePropertyChanged() const { |
| 78 | // Surface property changes are tracked as follows: |
| 79 | // |
| 80 | // - surface_property_changed_ is flagged when the clip_rect or content_rect |
| 81 | // change. As of now, these are the only two properties that can be affected |
| 82 | // by descendant layers. |
| 83 | // |
| 84 | // - all other property changes come from the owning layer (or some ancestor |
| 85 | // layer that propagates its change to the owning layer). |
| 86 | // |
| 87 | DCHECK(owning_layer_); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 88 | return surface_property_changed_ || owning_layer_->LayerPropertyChanged(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 91 | bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 92 | return surface_property_changed_ && !owning_layer_->LayerPropertyChanged(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 95 | void RenderSurfaceImpl::AddContributingDelegatedRenderPassLayer( |
| 96 | LayerImpl* layer) { |
| 97 | DCHECK(std::find(layer_list_.begin(), layer_list_.end(), layer) != |
| 98 | layer_list_.end()); |
| 99 | DelegatedRendererLayerImpl* delegated_renderer_layer = |
| 100 | static_cast<DelegatedRendererLayerImpl*>(layer); |
| 101 | contributing_delegated_render_pass_layer_list_.push_back( |
| 102 | delegated_renderer_layer); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 105 | void RenderSurfaceImpl::ClearLayerLists() { |
| 106 | layer_list_.clear(); |
| 107 | contributing_delegated_render_pass_layer_list_.clear(); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 0cd7d6f7 | 2014-08-22 14:50:51 | [diff] [blame^] | 110 | RenderPassId RenderSurfaceImpl::GetRenderPassId() { |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 111 | int layer_id = owning_layer_->id(); |
| 112 | int sub_id = 0; |
| 113 | DCHECK_GT(layer_id, 0); |
[email protected] | 0cd7d6f7 | 2014-08-22 14:50:51 | [diff] [blame^] | 114 | return RenderPassId(layer_id, sub_id); |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 117 | void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink* pass_sink) { |
| 118 | for (size_t i = 0; |
| 119 | i < contributing_delegated_render_pass_layer_list_.size(); |
| 120 | ++i) { |
| 121 | DelegatedRendererLayerImpl* delegated_renderer_layer = |
| 122 | contributing_delegated_render_pass_layer_list_[i]; |
| 123 | delegated_renderer_layer->AppendContributingRenderPasses(pass_sink); |
| 124 | } |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 125 | |
[email protected] | b27511f | 2013-11-28 05:39:04 | [diff] [blame] | 126 | scoped_ptr<RenderPass> pass = RenderPass::Create(layer_list_.size()); |
[email protected] | 0cd7d6f7 | 2014-08-22 14:50:51 | [diff] [blame^] | 127 | pass->SetNew(GetRenderPassId(), |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 128 | content_rect_, |
[email protected] | b4ead7b | 2014-04-07 18:12:18 | [diff] [blame] | 129 | gfx::IntersectRects(content_rect_, |
| 130 | damage_tracker_->current_damage_rect()), |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 131 | screen_space_transform_); |
[email protected] | c1bb5af | 2013-03-13 19:06:27 | [diff] [blame] | 132 | pass_sink->AppendRenderPass(pass.Pass()); |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 135 | void RenderSurfaceImpl::AppendQuads( |
| 136 | RenderPass* render_pass, |
| 137 | const OcclusionTracker<LayerImpl>& occlusion_tracker, |
| 138 | AppendQuadsData* append_quads_data, |
| 139 | bool for_replica, |
[email protected] | 0cd7d6f7 | 2014-08-22 14:50:51 | [diff] [blame^] | 140 | RenderPassId render_pass_id) { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 141 | DCHECK(!for_replica || owning_layer_->has_replica()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 142 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 143 | const gfx::Transform& draw_transform = |
| 144 | for_replica ? replica_draw_transform_ : draw_transform_; |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 145 | gfx::Rect visible_content_rect = |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 146 | occlusion_tracker.UnoccludedContributingSurfaceContentRect( |
| 147 | content_rect_, draw_transform); |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 148 | if (visible_content_rect.IsEmpty()) |
| 149 | return; |
| 150 | |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 151 | SharedQuadState* shared_quad_state = |
| 152 | render_pass->CreateAndAppendSharedQuadState(); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 153 | shared_quad_state->SetAll(draw_transform, |
| 154 | content_rect_.size(), |
| 155 | content_rect_, |
| 156 | clip_rect_, |
| 157 | is_clipped_, |
[email protected] | 7bbeaf4e | 2013-11-26 10:27:22 | [diff] [blame] | 158 | draw_opacity_, |
[email protected] | a9d4d4f | 2014-06-19 06:49:28 | [diff] [blame] | 159 | owning_layer_->blend_mode(), |
| 160 | owning_layer_->sorting_context_id()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 161 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 162 | if (owning_layer_->ShowDebugBorders()) { |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 163 | SkColor color = for_replica ? |
| 164 | DebugColors::SurfaceReplicaBorderColor() : |
| 165 | DebugColors::SurfaceBorderColor(); |
| 166 | float width = for_replica ? |
| 167 | DebugColors::SurfaceReplicaBorderWidth( |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 168 | owning_layer_->layer_tree_impl()) : |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 169 | DebugColors::SurfaceBorderWidth( |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 170 | owning_layer_->layer_tree_impl()); |
[email protected] | f7030c3 | 2014-07-03 18:54:34 | [diff] [blame] | 171 | DebugBorderDrawQuad* debug_border_quad = |
| 172 | render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>(); |
[email protected] | 9bf06c7 | 2014-03-07 18:16:24 | [diff] [blame] | 173 | debug_border_quad->SetNew( |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 174 | shared_quad_state, content_rect_, visible_content_rect, color, width); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 175 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 176 | |
[email protected] | 27d851ab | 2013-06-28 01:09:50 | [diff] [blame] | 177 | // TODO(shawnsingh): By using the same RenderSurfaceImpl for both the content |
| 178 | // and its reflection, it's currently not possible to apply a separate mask to |
| 179 | // the reflection layer or correctly handle opacity in reflections (opacity |
| 180 | // must be applied after drawing both the layer and its reflection). The |
| 181 | // solution is to introduce yet another RenderSurfaceImpl to draw the layer |
| 182 | // and its reflection in. For now we only apply a separate reflection mask if |
| 183 | // the contents don't have a mask of their own. |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 184 | LayerImpl* mask_layer = owning_layer_->mask_layer(); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 185 | if (mask_layer && |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 186 | (!mask_layer->DrawsContent() || mask_layer->bounds().IsEmpty())) |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 187 | mask_layer = NULL; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 188 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 189 | if (!mask_layer && for_replica) { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 190 | mask_layer = owning_layer_->replica_layer()->mask_layer(); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 191 | if (mask_layer && |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 192 | (!mask_layer->DrawsContent() || mask_layer->bounds().IsEmpty())) |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 193 | mask_layer = NULL; |
| 194 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 195 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 196 | gfx::RectF mask_uv_rect(0.f, 0.f, 1.f, 1.f); |
| 197 | if (mask_layer) { |
| 198 | gfx::Vector2dF owning_layer_draw_scale = |
[email protected] | fa816c6 | 2013-03-18 04:24:21 | [diff] [blame] | 199 | MathUtil::ComputeTransform2dScaleComponents( |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 200 | owning_layer_->draw_transform(), 1.f); |
[email protected] | 8591a64 | 2013-04-17 18:48:56 | [diff] [blame] | 201 | gfx::SizeF unclipped_mask_target_size = gfx::ScaleSize( |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 202 | owning_layer_->content_bounds(), |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 203 | owning_layer_draw_scale.x(), |
| 204 | owning_layer_draw_scale.y()); |
[email protected] | 458af1e | 2012-12-13 05:12:18 | [diff] [blame] | 205 | |
[email protected] | 8591a64 | 2013-04-17 18:48:56 | [diff] [blame] | 206 | float uv_scale_x = |
| 207 | content_rect_.width() / unclipped_mask_target_size.width(); |
| 208 | float uv_scale_y = |
| 209 | content_rect_.height() / unclipped_mask_target_size.height(); |
[email protected] | 8611f01 | 2013-01-17 03:44:55 | [diff] [blame] | 210 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 211 | mask_uv_rect = gfx::RectF( |
| 212 | uv_scale_x * content_rect_.x() / content_rect_.width(), |
| 213 | uv_scale_y * content_rect_.y() / content_rect_.height(), |
| 214 | uv_scale_x, |
| 215 | uv_scale_y); |
| 216 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 217 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 218 | ResourceProvider::ResourceId mask_resource_id = |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 219 | mask_layer ? mask_layer->ContentsResourceId() : 0; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 220 | |
[email protected] | 7ac3d49 | 2014-08-08 21:25:32 | [diff] [blame] | 221 | DCHECK(owning_layer_->draw_properties().target_space_transform.IsScale2d()); |
| 222 | gfx::Vector2dF owning_layer_to_target_scale = |
| 223 | owning_layer_->draw_properties().target_space_transform.Scale2d(); |
| 224 | owning_layer_to_target_scale.Scale(owning_layer_->contents_scale_x(), |
| 225 | owning_layer_->contents_scale_y()); |
| 226 | |
[email protected] | f7030c3 | 2014-07-03 18:54:34 | [diff] [blame] | 227 | RenderPassDrawQuad* quad = |
| 228 | render_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>(); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 229 | quad->SetNew(shared_quad_state, |
| 230 | content_rect_, |
[email protected] | 9bf06c7 | 2014-03-07 18:16:24 | [diff] [blame] | 231 | visible_content_rect, |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 232 | render_pass_id, |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 233 | mask_resource_id, |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 234 | mask_uv_rect, |
| 235 | owning_layer_->filters(), |
[email protected] | 7ac3d49 | 2014-08-08 21:25:32 | [diff] [blame] | 236 | owning_layer_to_target_scale, |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 237 | owning_layer_->background_filters()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 240 | } // namespace cc |