[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 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | ac7c7f5 | 2012-11-08 06:26:50 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | 4456eee2 | 2012-10-19 18:16:38 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 8e61d4b | 2013-06-10 22:11:48 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
[email protected] | 681ccff | 2013-03-18 06:13:52 | [diff] [blame] | 13 | #include "cc/base/math_util.h" |
[email protected] | 6e84de2 | 2013-03-18 06:54:27 | [diff] [blame] | 14 | #include "cc/debug/debug_colors.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 15 | #include "cc/layers/layer_impl.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 16 | #include "cc/layers/render_pass_sink.h" |
[email protected] | 89e8267a | 2013-03-18 07:50:56 | [diff] [blame] | 17 | #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] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 21 | #include "cc/trees/damage_tracker.h" |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 22 | #include "cc/trees/draw_property_utils.h" |
| 23 | #include "cc/trees/layer_tree_impl.h" |
danakj | a1b92e4 | 2015-02-11 21:07:47 | [diff] [blame] | 24 | #include "cc/trees/occlusion.h" |
[email protected] | 55761e6 | 2012-11-21 18:55:58 | [diff] [blame] | 25 | #include "third_party/skia/include/core/SkImageFilter.h" |
heejin.r.chung | d28506ba | 2014-10-23 16:36:20 | [diff] [blame] | 26 | #include "ui/gfx/geometry/rect_conversions.h" |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 27 | #include "ui/gfx/transform.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 28 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 29 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 30 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 31 | RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl* owning_layer) |
| 32 | : owning_layer_(owning_layer), |
| 33 | surface_property_changed_(false), |
[email protected] | 30fe19ff | 2013-07-04 00:54:45 | [diff] [blame] | 34 | contributes_to_drawn_surface_(false), |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 35 | nearest_occlusion_immune_ancestor_(nullptr), |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 36 | target_render_surface_layer_index_history_(0), |
| 37 | current_layer_index_history_(0) { |
| 38 | damage_tracker_ = DamageTracker::Create(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 41 | RenderSurfaceImpl::~RenderSurfaceImpl() {} |
| 42 | |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 43 | RenderSurfaceImpl::DrawProperties::DrawProperties() { |
| 44 | draw_opacity = 1.f; |
| 45 | is_clipped = false; |
| 46 | } |
| 47 | |
| 48 | RenderSurfaceImpl::DrawProperties::~DrawProperties() {} |
| 49 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 50 | gfx::RectF RenderSurfaceImpl::DrawableContentRect() const { |
| 51 | gfx::RectF drawable_content_rect = |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 52 | MathUtil::MapClippedRect(draw_transform(), gfx::RectF(content_rect())); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 53 | if (owning_layer_->has_replica()) { |
danakj | 5e6ff6d | 2015-09-05 04:43:44 | [diff] [blame] | 54 | drawable_content_rect.Union(MathUtil::MapClippedRect( |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 55 | replica_draw_transform(), gfx::RectF(content_rect()))); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 56 | } |
senorblanco | 38858c5 | 2016-01-20 23:15:00 | [diff] [blame] | 57 | if (!owning_layer_->filters().IsEmpty()) { |
| 58 | int left, top, right, bottom; |
| 59 | owning_layer_->filters().GetOutsets(&top, &right, &bottom, &left); |
| 60 | drawable_content_rect.Inset(-left, -top, -right, -bottom); |
| 61 | } |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 62 | |
jaydasika | 5160e67 | 2015-10-15 15:25:14 | [diff] [blame] | 63 | // If the rect has a NaN coordinate, we return empty rect to avoid crashes in |
| 64 | // functions (for example, gfx::ToEnclosedRect) that are called on this rect. |
| 65 | if (std::isnan(drawable_content_rect.x()) || |
| 66 | std::isnan(drawable_content_rect.y()) || |
| 67 | std::isnan(drawable_content_rect.right()) || |
| 68 | std::isnan(drawable_content_rect.bottom())) |
| 69 | return gfx::RectF(); |
| 70 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 71 | return drawable_content_rect; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 72 | } |
| 73 | |
danakj | a1b92e4 | 2015-02-11 21:07:47 | [diff] [blame] | 74 | SkColor RenderSurfaceImpl::GetDebugBorderColor() const { |
| 75 | return DebugColors::SurfaceBorderColor(); |
| 76 | } |
| 77 | |
| 78 | SkColor RenderSurfaceImpl::GetReplicaDebugBorderColor() const { |
| 79 | return DebugColors::SurfaceReplicaBorderColor(); |
| 80 | } |
| 81 | |
| 82 | float RenderSurfaceImpl::GetDebugBorderWidth() const { |
| 83 | return DebugColors::SurfaceBorderWidth(owning_layer_->layer_tree_impl()); |
| 84 | } |
| 85 | |
| 86 | float RenderSurfaceImpl::GetReplicaDebugBorderWidth() const { |
| 87 | return DebugColors::SurfaceReplicaBorderWidth( |
| 88 | owning_layer_->layer_tree_impl()); |
| 89 | } |
| 90 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 91 | int RenderSurfaceImpl::OwningLayerId() const { |
| 92 | return owning_layer_ ? owning_layer_->id() : 0; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 93 | } |
| 94 | |
jaydasika | 7ba8f92 | 2015-08-21 05:39:42 | [diff] [blame] | 95 | bool RenderSurfaceImpl::HasReplica() const { |
| 96 | return owning_layer_->has_replica(); |
| 97 | } |
| 98 | |
jaydasika | 23fb382 | 2015-08-25 03:22:59 | [diff] [blame] | 99 | const LayerImpl* RenderSurfaceImpl::ReplicaLayer() const { |
| 100 | return owning_layer_->replica_layer(); |
| 101 | } |
| 102 | |
jaydasika | 504a050 | 2015-07-23 19:25:44 | [diff] [blame] | 103 | int RenderSurfaceImpl::TransformTreeIndex() const { |
| 104 | return owning_layer_->transform_tree_index(); |
| 105 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 106 | |
jaydasika | ebf9e4ea | 2015-08-14 21:29:12 | [diff] [blame] | 107 | int RenderSurfaceImpl::ClipTreeIndex() const { |
| 108 | return owning_layer_->clip_tree_index(); |
| 109 | } |
| 110 | |
jaydasika | 0f4b1a9 | 2015-08-18 23:19:02 | [diff] [blame] | 111 | int RenderSurfaceImpl::EffectTreeIndex() const { |
| 112 | return owning_layer_->effect_tree_index(); |
| 113 | } |
| 114 | |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 115 | void RenderSurfaceImpl::SetClipRect(const gfx::Rect& clip_rect) { |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 116 | if (clip_rect == draw_properties_.clip_rect) |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 117 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 118 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 119 | surface_property_changed_ = true; |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 120 | draw_properties_.clip_rect = clip_rect; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 123 | void RenderSurfaceImpl::SetContentRect(const gfx::Rect& content_rect) { |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 124 | if (content_rect == draw_properties_.content_rect) |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 125 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 126 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 127 | surface_property_changed_ = true; |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 128 | draw_properties_.content_rect = content_rect; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 129 | } |
| 130 | |
jaydasika | 7ba8f92 | 2015-08-21 05:39:42 | [diff] [blame] | 131 | void RenderSurfaceImpl::SetAccumulatedContentRect( |
| 132 | const gfx::Rect& content_rect) { |
| 133 | accumulated_content_rect_ = content_rect; |
| 134 | } |
| 135 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 136 | bool RenderSurfaceImpl::SurfacePropertyChanged() const { |
| 137 | // Surface property changes are tracked as follows: |
| 138 | // |
| 139 | // - surface_property_changed_ is flagged when the clip_rect or content_rect |
| 140 | // change. As of now, these are the only two properties that can be affected |
| 141 | // by descendant layers. |
| 142 | // |
| 143 | // - all other property changes come from the owning layer (or some ancestor |
| 144 | // layer that propagates its change to the owning layer). |
| 145 | // |
| 146 | DCHECK(owning_layer_); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 147 | return surface_property_changed_ || owning_layer_->LayerPropertyChanged(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 150 | bool RenderSurfaceImpl::SurfacePropertyChangedOnlyFromDescendant() const { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 151 | return surface_property_changed_ && !owning_layer_->LayerPropertyChanged(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 154 | void RenderSurfaceImpl::ClearLayerLists() { |
| 155 | layer_list_.clear(); |
[email protected] | 7d929c0 | 2012-09-20 17:26:57 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 0cd7d6f7 | 2014-08-22 14:50:51 | [diff] [blame] | 158 | RenderPassId RenderSurfaceImpl::GetRenderPassId() { |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 159 | int layer_id = owning_layer_->id(); |
| 160 | int sub_id = 0; |
| 161 | DCHECK_GT(layer_id, 0); |
[email protected] | 0cd7d6f7 | 2014-08-22 14:50:51 | [diff] [blame] | 162 | return RenderPassId(layer_id, sub_id); |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 165 | void RenderSurfaceImpl::AppendRenderPasses(RenderPassSink* pass_sink) { |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame^] | 166 | std::unique_ptr<RenderPass> pass = RenderPass::Create(layer_list_.size()); |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 167 | pass->SetNew(GetRenderPassId(), content_rect(), |
| 168 | gfx::IntersectRects(content_rect(), |
[email protected] | b4ead7b | 2014-04-07 18:12:18 | [diff] [blame] | 169 | damage_tracker_->current_damage_rect()), |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 170 | draw_properties_.screen_space_transform); |
danakj | a04855a | 2015-11-18 20:39:10 | [diff] [blame] | 171 | pass_sink->AppendRenderPass(std::move(pass)); |
[email protected] | 467b361 | 2012-08-28 07:41:16 | [diff] [blame] | 172 | } |
| 173 | |
danakj | a1b92e4 | 2015-02-11 21:07:47 | [diff] [blame] | 174 | void RenderSurfaceImpl::AppendQuads(RenderPass* render_pass, |
| 175 | const gfx::Transform& draw_transform, |
| 176 | const Occlusion& occlusion_in_content_space, |
| 177 | SkColor debug_border_color, |
| 178 | float debug_border_width, |
| 179 | LayerImpl* mask_layer, |
| 180 | AppendQuadsData* append_quads_data, |
| 181 | RenderPassId render_pass_id) { |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 182 | gfx::Rect visible_layer_rect = |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 183 | occlusion_in_content_space.GetUnoccludedContentRect(content_rect()); |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 184 | if (visible_layer_rect.IsEmpty()) |
[email protected] | ba0f8d9 | 2014-03-21 18:41:10 | [diff] [blame] | 185 | return; |
| 186 | |
[email protected] | c6707fd | 2014-06-23 05:50:36 | [diff] [blame] | 187 | SharedQuadState* shared_quad_state = |
| 188 | render_pass->CreateAndAppendSharedQuadState(); |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 189 | shared_quad_state->SetAll( |
| 190 | draw_transform, content_rect().size(), content_rect(), |
| 191 | draw_properties_.clip_rect, draw_properties_.is_clipped, |
| 192 | draw_properties_.draw_opacity, owning_layer_->blend_mode(), |
| 193 | owning_layer_->sorting_context_id()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 194 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 195 | if (owning_layer_->ShowDebugBorders()) { |
[email protected] | f7030c3 | 2014-07-03 18:54:34 | [diff] [blame] | 196 | DebugBorderDrawQuad* debug_border_quad = |
| 197 | render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>(); |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 198 | debug_border_quad->SetNew(shared_quad_state, content_rect(), |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 199 | visible_layer_rect, debug_border_color, |
danakj | a1b92e4 | 2015-02-11 21:07:47 | [diff] [blame] | 200 | debug_border_width); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 201 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 202 | |
jbauman | bbd425e | 2015-05-19 00:33:35 | [diff] [blame] | 203 | ResourceId mask_resource_id = 0; |
enne | f6f3fbba4 | 2014-10-16 18:16:49 | [diff] [blame] | 204 | gfx::Size mask_texture_size; |
| 205 | gfx::Vector2dF mask_uv_scale; |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 206 | gfx::Transform owning_layer_draw_transform = owning_layer_->DrawTransform(); |
danakj | a1b92e4 | 2015-02-11 21:07:47 | [diff] [blame] | 207 | if (mask_layer && mask_layer->DrawsContent() && |
| 208 | !mask_layer->bounds().IsEmpty()) { |
enne | f6f3fbba4 | 2014-10-16 18:16:49 | [diff] [blame] | 209 | mask_layer->GetContentsResourceId(&mask_resource_id, &mask_texture_size); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 210 | gfx::Vector2dF owning_layer_draw_scale = |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 211 | MathUtil::ComputeTransform2dScaleComponents(owning_layer_draw_transform, |
| 212 | 1.f); |
danakj | ddaec91 | 2015-09-25 19:38:40 | [diff] [blame] | 213 | gfx::SizeF unclipped_mask_target_size = gfx::ScaleSize( |
| 214 | gfx::SizeF(owning_layer_->bounds()), owning_layer_draw_scale.x(), |
| 215 | owning_layer_draw_scale.y()); |
senorblanco | dfcb362 | 2016-01-27 21:48:01 | [diff] [blame] | 216 | mask_uv_scale = gfx::Vector2dF(1.0f / unclipped_mask_target_size.width(), |
| 217 | 1.0f / unclipped_mask_target_size.height()); |
[email protected] | d2d915aa | 2013-03-08 20:18:12 | [diff] [blame] | 218 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 219 | |
robertn | ce99907 | 2016-01-05 15:06:13 | [diff] [blame] | 220 | DCHECK(owning_layer_draw_transform.IsScale2d()); |
[email protected] | 7ac3d49 | 2014-08-08 21:25:32 | [diff] [blame] | 221 | gfx::Vector2dF owning_layer_to_target_scale = |
ajuma | d9432e3 | 2015-11-30 19:43:44 | [diff] [blame] | 222 | owning_layer_draw_transform.Scale2d(); |
[email protected] | 7ac3d49 | 2014-08-08 21:25:32 | [diff] [blame] | 223 | |
[email protected] | f7030c3 | 2014-07-03 18:54:34 | [diff] [blame] | 224 | RenderPassDrawQuad* quad = |
| 225 | render_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>(); |
weiliangc | c97575c | 2016-03-03 18:34:27 | [diff] [blame] | 226 | quad->SetNew(shared_quad_state, content_rect(), visible_layer_rect, |
danakj | 64767d90 | 2015-06-19 00:10:43 | [diff] [blame] | 227 | render_pass_id, mask_resource_id, mask_uv_scale, |
| 228 | mask_texture_size, owning_layer_->filters(), |
[email protected] | 7ac3d49 | 2014-08-08 21:25:32 | [diff] [blame] | 229 | owning_layer_to_target_scale, |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 230 | owning_layer_->background_filters()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 233 | } // namespace cc |