| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/accelerated_widget_mac/ca_layer_partial_damage_tree_mac.h" | 5 #include "ui/accelerated_widget_mac/gl_renderer_layer_tree.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/mac/sdk_forward_declarations.h" | 9 #include "base/mac/sdk_forward_declarations.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| 11 #include "ui/base/ui_base_switches.h" | 11 #include "ui/base/ui_base_switches.h" |
| 12 #include "ui/gfx/transform.h" | 12 #include "ui/gfx/transform.h" |
| 13 | 13 |
| 14 @interface CALayer(Private) | 14 @interface CALayer(Private) |
| 15 -(void)setContentsChanged; | 15 -(void)setContentsChanged; |
| 16 @end | 16 @end |
| 17 | 17 |
| 18 namespace ui { | 18 namespace ui { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // When selecting a CALayer to re-use for partial damage, this is the maximum | 21 // When selecting a CALayer to re-use for partial damage, this is the maximum |
| 22 // fraction of the merged layer's pixels that may be not-updated by the swap | 22 // fraction of the merged layer's pixels that may be not-updated by the swap |
| 23 // before we consider the CALayer to not be a good enough match, and create a | 23 // before we consider the CALayer to not be a good enough match, and create a |
| 24 // new one. | 24 // new one. |
| 25 const float kMaximumPartialDamageWasteFraction = 1.2f; | 25 const float kMaximumPartialDamageWasteFraction = 1.2f; |
| 26 | 26 |
| 27 // The maximum number of partial damage layers that may be created before we | 27 // The maximum number of partial damage layers that may be created before we |
| 28 // give up and remove them all (doing full damage in the process). | 28 // give up and remove them all (doing full damage in the process). |
| 29 const size_t kMaximumPartialDamageLayers = 8; | 29 const size_t kMaximumPartialDamageLayers = 8; |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 class CALayerPartialDamageTree::OverlayPlane { | 33 class GLRendererLayerTree::OverlayPlane { |
| 34 public: | 34 public: |
| 35 OverlayPlane(base::ScopedCFTypeRef<IOSurfaceRef> io_surface, | 35 OverlayPlane(base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 36 const gfx::Rect& pixel_frame_rect, | 36 const gfx::Rect& pixel_frame_rect, |
| 37 const gfx::RectF& contents_rect) | 37 const gfx::RectF& contents_rect) |
| 38 : io_surface(io_surface), | 38 : io_surface(io_surface), |
| 39 contents_rect(contents_rect), | 39 contents_rect(contents_rect), |
| 40 pixel_frame_rect(pixel_frame_rect), | 40 pixel_frame_rect(pixel_frame_rect), |
| 41 layer_needs_update(true) {} | 41 layer_needs_update(true) {} |
| 42 | 42 |
| 43 ~OverlayPlane() { | 43 ~OverlayPlane() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 92 } |
| 93 [ca_layer setBorderWidth:1]; | 93 [ca_layer setBorderWidth:1]; |
| 94 [ca_layer setBorderColor:color]; | 94 [ca_layer setBorderColor:color]; |
| 95 } | 95 } |
| 96 layer_needs_update = false; | 96 layer_needs_update = false; |
| 97 } | 97 } |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 void CALayerPartialDamageTree::UpdatePartialDamagePlanes( | 102 void GLRendererLayerTree::UpdatePartialDamagePlanes( |
| 103 CALayerPartialDamageTree* old_tree, | 103 GLRendererLayerTree* old_tree, |
| 104 const gfx::Rect& pixel_damage_rect) { | 104 const gfx::Rect& pixel_damage_rect) { |
| 105 // Don't create partial damage layers if partial swap is disabled. | 105 // Don't create partial damage layers if partial swap is disabled. |
| 106 if (!allow_partial_swap_) | 106 if (!allow_partial_swap_) |
| 107 return; | 107 return; |
| 108 // Only create partial damage layers when building on top of an existing tree. | 108 // Only create partial damage layers when building on top of an existing tree. |
| 109 if (!old_tree) | 109 if (!old_tree) |
| 110 return; | 110 return; |
| 111 // If the frame size has changed, discard all of the old partial damage | 111 // If the frame size has changed, discard all of the old partial damage |
| 112 // layers. | 112 // layers. |
| 113 if (old_tree->root_plane_->pixel_frame_rect != root_plane_->pixel_frame_rect) | 113 if (old_tree->root_plane_->pixel_frame_rect != root_plane_->pixel_frame_rect) |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 } | 199 } |
| 200 if (!old_plane_covered_by_swap) { | 200 if (!old_plane_covered_by_swap) { |
| 201 DCHECK(old_plane->ca_layer); | 201 DCHECK(old_plane->ca_layer); |
| 202 partial_damage_planes_.push_back(std::move(old_plane)); | 202 partial_damage_planes_.push_back(std::move(old_plane)); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 partial_damage_planes_.push_back(std::move(plane_for_swap)); | 206 partial_damage_planes_.push_back(std::move(plane_for_swap)); |
| 207 } | 207 } |
| 208 | 208 |
| 209 void CALayerPartialDamageTree::UpdateRootAndPartialDamagePlanes( | 209 void GLRendererLayerTree::UpdateRootAndPartialDamagePlanes( |
| 210 std::unique_ptr<CALayerPartialDamageTree> old_tree, | 210 std::unique_ptr<GLRendererLayerTree> old_tree, |
| 211 const gfx::Rect& pixel_damage_rect) { | 211 const gfx::Rect& pixel_damage_rect) { |
| 212 // First update the partial damage tree. | 212 // First update the partial damage tree. |
| 213 UpdatePartialDamagePlanes(old_tree.get(), pixel_damage_rect); | 213 UpdatePartialDamagePlanes(old_tree.get(), pixel_damage_rect); |
| 214 if (old_tree) { | 214 if (old_tree) { |
| 215 if (partial_damage_planes_.empty()) { | 215 if (partial_damage_planes_.empty()) { |
| 216 // If there are no partial damage planes, then we will be updating the | 216 // If there are no partial damage planes, then we will be updating the |
| 217 // root layer. Take the CALayer from the old tree. | 217 // root layer. Take the CALayer from the old tree. |
| 218 root_plane_->TakeCALayerFrom(old_tree->root_plane_.get()); | 218 root_plane_->TakeCALayerFrom(old_tree->root_plane_.get()); |
| 219 } else { | 219 } else { |
| 220 // If there is a partial damage tree, then just take the old plane | 220 // If there is a partial damage tree, then just take the old plane |
| 221 // from the previous frame, so that there is no update to it. | 221 // from the previous frame, so that there is no update to it. |
| 222 root_plane_.swap(old_tree->root_plane_); | 222 root_plane_.swap(old_tree->root_plane_); |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 | 226 |
| 227 void CALayerPartialDamageTree::UpdateCALayers(CALayer* superlayer, | 227 void GLRendererLayerTree::UpdateCALayers(CALayer* superlayer, |
| 228 float scale_factor) { | 228 float scale_factor) { |
| 229 if (!allow_partial_swap_) { | 229 if (!allow_partial_swap_) { |
| 230 DCHECK(partial_damage_planes_.empty()); | 230 DCHECK(partial_damage_planes_.empty()); |
| 231 return; | 231 return; |
| 232 } | 232 } |
| 233 | 233 |
| 234 // Allocate and update CALayers for the backbuffer and partial damage layers. | 234 // Allocate and update CALayers for the backbuffer and partial damage layers. |
| 235 if (!root_plane_->ca_layer) { | 235 if (!root_plane_->ca_layer) { |
| 236 DCHECK(partial_damage_planes_.empty()); | 236 DCHECK(partial_damage_planes_.empty()); |
| 237 root_plane_->ca_layer.reset([[CALayer alloc] init]); | 237 root_plane_->ca_layer.reset([[CALayer alloc] init]); |
| 238 [superlayer setSublayers:nil]; | 238 [superlayer setSublayers:nil]; |
| 239 [superlayer addSublayer:root_plane_->ca_layer]; | 239 [superlayer addSublayer:root_plane_->ca_layer]; |
| 240 } | 240 } |
| 241 // Excessive logging to debug white screens (crbug.com/583805). | 241 // Excessive logging to debug white screens (crbug.com/583805). |
| 242 // TODO(ccameron): change this back to a DLOG. | 242 // TODO(ccameron): change this back to a DLOG. |
| 243 if ([root_plane_->ca_layer superlayer] != superlayer) { | 243 if ([root_plane_->ca_layer superlayer] != superlayer) { |
| 244 LOG(ERROR) << "CALayerPartialDamageTree root layer not attached to tree."; | 244 LOG(ERROR) << "GLRendererLayerTree root layer not attached to tree."; |
| 245 } | 245 } |
| 246 for (auto& plane : partial_damage_planes_) { | 246 for (auto& plane : partial_damage_planes_) { |
| 247 if (!plane->ca_layer) { | 247 if (!plane->ca_layer) { |
| 248 DCHECK(plane == partial_damage_planes_.back()); | 248 DCHECK(plane == partial_damage_planes_.back()); |
| 249 plane->ca_layer.reset([[CALayer alloc] init]); | 249 plane->ca_layer.reset([[CALayer alloc] init]); |
| 250 } | 250 } |
| 251 if (![plane->ca_layer superlayer]) { | 251 if (![plane->ca_layer superlayer]) { |
| 252 DCHECK(plane == partial_damage_planes_.back()); | 252 DCHECK(plane == partial_damage_planes_.back()); |
| 253 [superlayer addSublayer:plane->ca_layer]; | 253 [superlayer addSublayer:plane->ca_layer]; |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 root_plane_->UpdateProperties(scale_factor); | 256 root_plane_->UpdateProperties(scale_factor); |
| 257 for (auto& plane : partial_damage_planes_) | 257 for (auto& plane : partial_damage_planes_) |
| 258 plane->UpdateProperties(scale_factor); | 258 plane->UpdateProperties(scale_factor); |
| 259 } | 259 } |
| 260 | 260 |
| 261 CALayerPartialDamageTree::CALayerPartialDamageTree( | 261 GLRendererLayerTree::GLRendererLayerTree( |
| 262 bool allow_partial_swap, | 262 bool allow_partial_swap, |
| 263 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, | 263 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 264 const gfx::Rect& pixel_frame_rect) | 264 const gfx::Rect& pixel_frame_rect) |
| 265 : allow_partial_swap_(allow_partial_swap) { | 265 : allow_partial_swap_(allow_partial_swap) { |
| 266 root_plane_.reset( | 266 root_plane_.reset( |
| 267 new OverlayPlane(io_surface, pixel_frame_rect, gfx::RectF(0, 0, 1, 1))); | 267 new OverlayPlane(io_surface, pixel_frame_rect, gfx::RectF(0, 0, 1, 1))); |
| 268 } | 268 } |
| 269 | 269 |
| 270 CALayerPartialDamageTree::~CALayerPartialDamageTree() {} | 270 GLRendererLayerTree::~GLRendererLayerTree() {} |
| 271 | 271 |
| 272 base::ScopedCFTypeRef<IOSurfaceRef> | 272 base::ScopedCFTypeRef<IOSurfaceRef> |
| 273 CALayerPartialDamageTree::RootLayerIOSurface() { | 273 GLRendererLayerTree::RootLayerIOSurface() { |
| 274 return root_plane_->io_surface; | 274 return root_plane_->io_surface; |
| 275 } | 275 } |
| 276 | 276 |
| 277 void CALayerPartialDamageTree::CommitCALayers( | 277 void GLRendererLayerTree::CommitCALayers( |
| 278 CALayer* superlayer, | 278 CALayer* superlayer, |
| 279 std::unique_ptr<CALayerPartialDamageTree> old_tree, | 279 std::unique_ptr<GLRendererLayerTree> old_tree, |
| 280 float scale_factor, | 280 float scale_factor, |
| 281 const gfx::Rect& pixel_damage_rect) { | 281 const gfx::Rect& pixel_damage_rect) { |
| 282 TRACE_EVENT0("gpu", "CALayerPartialDamageTree::CommitCALayers"); | 282 TRACE_EVENT0("gpu", "GLRendererLayerTree::CommitCALayers"); |
| 283 UpdateRootAndPartialDamagePlanes(std::move(old_tree), pixel_damage_rect); | 283 UpdateRootAndPartialDamagePlanes(std::move(old_tree), pixel_damage_rect); |
| 284 UpdateCALayers(superlayer, scale_factor); | 284 UpdateCALayers(superlayer, scale_factor); |
| 285 } | 285 } |
| 286 | 286 |
| 287 } // namespace ui | 287 } // namespace ui |
| OLD | NEW |