| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/common/gpu/image_transport_surface_overlay_mac.h" | 5 #include "content/common/gpu/image_transport_surface_overlay_mac.h" |
| 6 | 6 |
| 7 #include <CoreGraphics/CoreGraphics.h> | 7 #include <CoreGraphics/CoreGraphics.h> |
| 8 #include <IOSurface/IOSurface.h> | 8 #include <IOSurface/IOSurface.h> |
| 9 #include <OpenGL/CGLRenderers.h> | 9 #include <OpenGL/CGLRenderers.h> |
| 10 #include <OpenGL/CGLTypes.h> | 10 #include <OpenGL/CGLTypes.h> |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 namespace content { | 88 namespace content { |
| 89 | 89 |
| 90 scoped_refptr<gfx::GLSurface> ImageTransportSurfaceCreateNativeSurface( | 90 scoped_refptr<gfx::GLSurface> ImageTransportSurfaceCreateNativeSurface( |
| 91 GpuChannelManager* manager, | 91 GpuChannelManager* manager, |
| 92 GpuCommandBufferStub* stub, | 92 GpuCommandBufferStub* stub, |
| 93 gfx::PluginWindowHandle handle) { | 93 gfx::PluginWindowHandle handle) { |
| 94 return new ImageTransportSurfaceOverlayMac(manager, stub, handle); | 94 return new ImageTransportSurfaceOverlayMac(manager, stub, handle); |
| 95 } | 95 } |
| 96 | 96 |
| 97 class ImageTransportSurfaceOverlayMac::OverlayPlane { | 97 class CALayerPartialDamageTree { |
| 98 public: |
| 99 CALayerPartialDamageTree(bool allow_partial_swap, |
| 100 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 101 const gfx::Rect& pixel_frame_rect); |
| 102 ~CALayerPartialDamageTree(); |
| 103 |
| 104 base::ScopedCFTypeRef<IOSurfaceRef> RootLayerIOSurface(); |
| 105 void CommitCALayers(CALayer* superlayer, |
| 106 scoped_ptr<CALayerPartialDamageTree> old_tree, |
| 107 float scale_factor, |
| 108 const gfx::Rect& pixel_damage_rect); |
| 109 |
| 110 private: |
| 111 class OverlayPlane; |
| 112 |
| 113 void UpdateRootAndPartialDamagePlanes(CALayerPartialDamageTree* old_tree, |
| 114 const gfx::RectF& pixel_damage_rect); |
| 115 void UpdateRootAndPartialDamageCALayers(CALayer* superlayer, |
| 116 float scale_factor); |
| 117 |
| 118 const bool allow_partial_swap_; |
| 119 linked_ptr<OverlayPlane> root_plane_; |
| 120 std::list<linked_ptr<OverlayPlane>> partial_damage_planes_; |
| 121 }; |
| 122 |
| 123 class CALayerPartialDamageTree::OverlayPlane { |
| 98 public: | 124 public: |
| 99 static linked_ptr<OverlayPlane> CreateWithFrameRect( | 125 static linked_ptr<OverlayPlane> CreateWithFrameRect( |
| 100 int z_order, | 126 int z_order, |
| 101 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, | 127 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 102 const gfx::RectF& pixel_frame_rect, | 128 const gfx::RectF& pixel_frame_rect, |
| 103 const gfx::RectF& contents_rect) { | 129 const gfx::RectF& contents_rect) { |
| 104 gfx::Transform transform; | 130 gfx::Transform transform; |
| 105 transform.Translate(pixel_frame_rect.x(), pixel_frame_rect.y()); | 131 transform.Translate(pixel_frame_rect.x(), pixel_frame_rect.y()); |
| 106 return linked_ptr<OverlayPlane>( | 132 return linked_ptr<OverlayPlane>( |
| 107 new OverlayPlane(z_order, io_surface, contents_rect, pixel_frame_rect)); | 133 new OverlayPlane(z_order, io_surface, contents_rect, pixel_frame_rect)); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 204 |
| 179 class ImageTransportSurfaceOverlayMac::PendingSwap { | 205 class ImageTransportSurfaceOverlayMac::PendingSwap { |
| 180 public: | 206 public: |
| 181 PendingSwap() {} | 207 PendingSwap() {} |
| 182 ~PendingSwap() { DCHECK(!gl_fence); } | 208 ~PendingSwap() { DCHECK(!gl_fence); } |
| 183 | 209 |
| 184 gfx::Size pixel_size; | 210 gfx::Size pixel_size; |
| 185 float scale_factor; | 211 float scale_factor; |
| 186 gfx::Rect pixel_damage_rect; | 212 gfx::Rect pixel_damage_rect; |
| 187 | 213 |
| 188 linked_ptr<OverlayPlane> root_plane; | 214 scoped_ptr<CALayerPartialDamageTree> partial_damage_tree; |
| 189 scoped_ptr<CALayerTree> ca_layer_tree; | 215 scoped_ptr<CALayerTree> ca_layer_tree; |
| 190 std::vector<ui::LatencyInfo> latency_info; | 216 std::vector<ui::LatencyInfo> latency_info; |
| 191 | 217 |
| 192 // A fence object, and the CGL context it was issued in. | 218 // A fence object, and the CGL context it was issued in. |
| 193 base::ScopedTypeRef<CGLContextObj> cgl_context; | 219 base::ScopedTypeRef<CGLContextObj> cgl_context; |
| 194 scoped_ptr<gfx::GLFence> gl_fence; | 220 scoped_ptr<gfx::GLFence> gl_fence; |
| 195 | 221 |
| 196 // The earliest time that this frame may be drawn. A frame is not allowed | 222 // The earliest time that this frame may be drawn. A frame is not allowed |
| 197 // to draw until a fraction of the way through the vsync interval after its | 223 // to draw until a fraction of the way through the vsync interval after its |
| 198 // This extra latency is to allow wiggle-room for smoothness. | 224 // This extra latency is to allow wiggle-room for smoothness. |
| 199 base::TimeTicks earliest_display_time_allowed; | 225 base::TimeTicks earliest_display_time_allowed; |
| 200 | 226 |
| 201 // The time that this will wake up and draw, if a following swap does not | 227 // The time that this will wake up and draw, if a following swap does not |
| 202 // cause it to draw earlier. | 228 // cause it to draw earlier. |
| 203 base::TimeTicks target_display_time; | 229 base::TimeTicks target_display_time; |
| 204 }; | 230 }; |
| 205 | 231 |
| 206 ImageTransportSurfaceOverlayMac::ImageTransportSurfaceOverlayMac( | 232 ImageTransportSurfaceOverlayMac::ImageTransportSurfaceOverlayMac( |
| 207 GpuChannelManager* manager, | 233 GpuChannelManager* manager, |
| 208 GpuCommandBufferStub* stub, | 234 GpuCommandBufferStub* stub, |
| 209 gfx::PluginWindowHandle handle) | 235 gfx::PluginWindowHandle handle) |
| 210 : use_remote_layer_api_(ui::RemoteLayerAPISupported()), | 236 : use_remote_layer_api_(ui::RemoteLayerAPISupported()), |
| 211 scale_factor_(1), | 237 scale_factor_(1), |
| 212 gl_renderer_id_(0), | 238 gl_renderer_id_(0), |
| 213 vsync_parameters_valid_(false), | 239 vsync_parameters_valid_(false), |
| 214 next_ca_layer_z_order_(1), | |
| 215 display_pending_swap_timer_(true, false), | 240 display_pending_swap_timer_(true, false), |
| 216 weak_factory_(this) { | 241 weak_factory_(this) { |
| 217 helper_.reset(new ImageTransportHelper(this, manager, stub, handle)); | 242 helper_.reset(new ImageTransportHelper(this, manager, stub, handle)); |
| 218 ui::GpuSwitchingManager::GetInstance()->AddObserver(this); | 243 ui::GpuSwitchingManager::GetInstance()->AddObserver(this); |
| 219 } | 244 } |
| 220 | 245 |
| 221 ImageTransportSurfaceOverlayMac::~ImageTransportSurfaceOverlayMac() { | 246 ImageTransportSurfaceOverlayMac::~ImageTransportSurfaceOverlayMac() { |
| 222 ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this); | 247 ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this); |
| 223 Destroy(); | 248 Destroy(); |
| 224 } | 249 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 237 ca_root_layer_.reset([[CALayer alloc] init]); | 262 ca_root_layer_.reset([[CALayer alloc] init]); |
| 238 [ca_root_layer_ setGeometryFlipped:YES]; | 263 [ca_root_layer_ setGeometryFlipped:YES]; |
| 239 [ca_root_layer_ setOpaque:YES]; | 264 [ca_root_layer_ setOpaque:YES]; |
| 240 [ca_context_ setLayer:ca_root_layer_]; | 265 [ca_context_ setLayer:ca_root_layer_]; |
| 241 } | 266 } |
| 242 return true; | 267 return true; |
| 243 } | 268 } |
| 244 | 269 |
| 245 void ImageTransportSurfaceOverlayMac::Destroy() { | 270 void ImageTransportSurfaceOverlayMac::Destroy() { |
| 246 DisplayAndClearAllPendingSwaps(); | 271 DisplayAndClearAllPendingSwaps(); |
| 247 current_partial_damage_planes_.clear(); | 272 |
| 248 current_root_plane_.reset(); | 273 current_partial_damage_tree_.reset(); |
| 274 current_ca_layer_tree_.reset(); |
| 249 } | 275 } |
| 250 | 276 |
| 251 bool ImageTransportSurfaceOverlayMac::IsOffscreen() { | 277 bool ImageTransportSurfaceOverlayMac::IsOffscreen() { |
| 252 return false; | 278 return false; |
| 253 } | 279 } |
| 254 | 280 |
| 255 gfx::SwapResult ImageTransportSurfaceOverlayMac::SwapBuffersInternal( | 281 gfx::SwapResult ImageTransportSurfaceOverlayMac::SwapBuffersInternal( |
| 256 const gfx::Rect& pixel_damage_rect) { | 282 const gfx::Rect& pixel_damage_rect) { |
| 257 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::SwapBuffersInternal"); | 283 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::SwapBuffersInternal"); |
| 258 next_ca_layer_z_order_ = 1; | |
| 259 | 284 |
| 260 // Use the same concept of 'now' for the entire function. The duration of | 285 // Use the same concept of 'now' for the entire function. The duration of |
| 261 // this function only affect the result if this function lasts across a vsync | 286 // this function only affect the result if this function lasts across a vsync |
| 262 // boundary, in which case smooth animation is out the window anyway. | 287 // boundary, in which case smooth animation is out the window anyway. |
| 263 const base::TimeTicks now = base::TimeTicks::Now(); | 288 const base::TimeTicks now = base::TimeTicks::Now(); |
| 264 | 289 |
| 265 // Decide if the frame should be drawn immediately, or if we should wait until | 290 // Decide if the frame should be drawn immediately, or if we should wait until |
| 266 // its work finishes before drawing immediately. | 291 // its work finishes before drawing immediately. |
| 267 bool display_immediately = false; | 292 bool display_immediately = false; |
| 268 if (vsync_parameters_valid_ && | 293 if (vsync_parameters_valid_ && |
| (...skipping 10 matching lines...) Expand all Loading... |
| 279 if (IsFirstPendingSwapReadyToDisplay(now)) | 304 if (IsFirstPendingSwapReadyToDisplay(now)) |
| 280 DisplayFirstPendingSwapImmediately(); | 305 DisplayFirstPendingSwapImmediately(); |
| 281 } | 306 } |
| 282 | 307 |
| 283 // The remainder of the function will populate the PendingSwap structure and | 308 // The remainder of the function will populate the PendingSwap structure and |
| 284 // then enqueue it. | 309 // then enqueue it. |
| 285 linked_ptr<PendingSwap> new_swap(new PendingSwap); | 310 linked_ptr<PendingSwap> new_swap(new PendingSwap); |
| 286 new_swap->pixel_size = pixel_size_; | 311 new_swap->pixel_size = pixel_size_; |
| 287 new_swap->scale_factor = scale_factor_; | 312 new_swap->scale_factor = scale_factor_; |
| 288 new_swap->pixel_damage_rect = pixel_damage_rect; | 313 new_swap->pixel_damage_rect = pixel_damage_rect; |
| 289 new_swap->root_plane = pending_root_plane_; | 314 new_swap->partial_damage_tree.swap(pending_partial_damage_tree_); |
| 290 pending_root_plane_ = linked_ptr<OverlayPlane>(); | |
| 291 new_swap->ca_layer_tree.swap(pending_ca_layer_tree_); | 315 new_swap->ca_layer_tree.swap(pending_ca_layer_tree_); |
| 292 new_swap->latency_info.swap(latency_info_); | 316 new_swap->latency_info.swap(latency_info_); |
| 293 | 317 |
| 294 // A flush is required to ensure that all content appears in the layer. | 318 // A flush is required to ensure that all content appears in the layer. |
| 295 { | 319 { |
| 296 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; | 320 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; |
| 297 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::glFlush"); | 321 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::glFlush"); |
| 298 CheckGLErrors("before flushing frame"); | 322 CheckGLErrors("before flushing frame"); |
| 299 new_swap->cgl_context.reset(CGLGetCurrentContext(), | 323 new_swap->cgl_context.reset(CGLGetCurrentContext(), |
| 300 base::scoped_policy::RETAIN); | 324 base::scoped_policy::RETAIN); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 // If there is a fence for this object, delete it. | 384 // If there is a fence for this object, delete it. |
| 361 if (swap->gl_fence) { | 385 if (swap->gl_fence) { |
| 362 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; | 386 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; |
| 363 gfx::ScopedCGLSetCurrentContext scoped_set_current(swap->cgl_context); | 387 gfx::ScopedCGLSetCurrentContext scoped_set_current(swap->cgl_context); |
| 364 | 388 |
| 365 CheckGLErrors("before deleting active fence"); | 389 CheckGLErrors("before deleting active fence"); |
| 366 swap->gl_fence.reset(); | 390 swap->gl_fence.reset(); |
| 367 CheckGLErrors("while deleting active fence"); | 391 CheckGLErrors("while deleting active fence"); |
| 368 } | 392 } |
| 369 | 393 |
| 370 // Update the plane lists. | 394 // Update the CALayer hierarchy. |
| 371 { | 395 { |
| 372 // Sort the input planes by z-index, and remove any overlays from the | |
| 373 // damage rect. | |
| 374 gfx::RectF pixel_damage_rect = gfx::RectF(swap->pixel_damage_rect); | 396 gfx::RectF pixel_damage_rect = gfx::RectF(swap->pixel_damage_rect); |
| 375 ScopedCAActionDisabler disabler; | 397 ScopedCAActionDisabler disabler; |
| 376 UpdateRootAndPartialDamagePlanes(swap->root_plane, pixel_damage_rect); | 398 if (swap->ca_layer_tree) { |
| 377 UpdateRootAndPartialDamageCALayers(swap->scale_factor); | 399 swap->ca_layer_tree->CommitScheduledCALayers( |
| 378 UpdateCALayerTree(std::move(swap->ca_layer_tree), swap->scale_factor); | 400 ca_root_layer_.get(), std::move(current_ca_layer_tree_), |
| 401 swap->scale_factor); |
| 402 current_ca_layer_tree_.swap(swap->ca_layer_tree); |
| 403 current_partial_damage_tree_.reset(); |
| 404 } else if (swap->partial_damage_tree) { |
| 405 swap->partial_damage_tree->CommitCALayers( |
| 406 ca_root_layer_.get(), std::move(current_partial_damage_tree_), |
| 407 swap->scale_factor, swap->pixel_damage_rect); |
| 408 current_partial_damage_tree_.swap(swap->partial_damage_tree); |
| 409 current_ca_layer_tree_.reset(); |
| 410 } else { |
| 411 [ca_root_layer_ setSublayers:nil]; |
| 412 } |
| 413 swap->ca_layer_tree.reset(); |
| 414 swap->partial_damage_tree.reset(); |
| 379 } | 415 } |
| 380 | 416 |
| 381 // Update the latency info to reflect the swap time. | 417 // Update the latency info to reflect the swap time. |
| 382 base::TimeTicks swap_time = base::TimeTicks::Now(); | 418 base::TimeTicks swap_time = base::TimeTicks::Now(); |
| 383 for (auto latency_info : swap->latency_info) { | 419 for (auto latency_info : swap->latency_info) { |
| 384 latency_info.AddLatencyNumberWithTimestamp( | 420 latency_info.AddLatencyNumberWithTimestamp( |
| 385 ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, 0, 0, swap_time, 1); | 421 ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, 0, 0, swap_time, 1); |
| 386 latency_info.AddLatencyNumberWithTimestamp( | 422 latency_info.AddLatencyNumberWithTimestamp( |
| 387 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, 0, 0, | 423 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, 0, 0, |
| 388 swap_time, 1); | 424 swap_time, 1); |
| 389 } | 425 } |
| 390 | 426 |
| 391 // Send acknowledgement to the browser. | 427 // Send acknowledgement to the browser. |
| 392 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params; | 428 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params; |
| 393 if (use_remote_layer_api_) { | 429 if (use_remote_layer_api_) { |
| 394 params.ca_context_id = [ca_context_ contextId]; | 430 params.ca_context_id = [ca_context_ contextId]; |
| 395 } else if (current_root_plane_.get()) { | 431 } else if (current_partial_damage_tree_) { |
| 396 params.io_surface.reset( | 432 params.io_surface.reset(IOSurfaceCreateMachPort( |
| 397 IOSurfaceCreateMachPort(current_root_plane_->io_surface)); | 433 current_partial_damage_tree_->RootLayerIOSurface())); |
| 398 } | 434 } |
| 399 params.size = swap->pixel_size; | 435 params.size = swap->pixel_size; |
| 400 params.scale_factor = swap->scale_factor; | 436 params.scale_factor = swap->scale_factor; |
| 401 params.latency_info.swap(swap->latency_info); | 437 params.latency_info.swap(swap->latency_info); |
| 402 helper_->SendAcceleratedSurfaceBuffersSwapped(params); | 438 helper_->SendAcceleratedSurfaceBuffersSwapped(params); |
| 403 | 439 |
| 404 // Remove this from the queue, and reset any callback timers. | 440 // Remove this from the queue, and reset any callback timers. |
| 405 pending_swaps_.pop_front(); | 441 pending_swaps_.pop_front(); |
| 406 } | 442 } |
| 407 | 443 |
| 408 void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamagePlanes( | 444 void CALayerPartialDamageTree::UpdateRootAndPartialDamagePlanes( |
| 409 const linked_ptr<OverlayPlane>& new_root_plane, | 445 CALayerPartialDamageTree* old_tree, |
| 410 const gfx::RectF& pixel_damage_rect) { | 446 const gfx::RectF& pixel_damage_rect) { |
| 411 std::list<linked_ptr<OverlayPlane>> old_partial_damage_planes; | 447 // This is the plane that will be updated this frame. It may be the root plane |
| 412 old_partial_damage_planes.swap(current_partial_damage_planes_); | 448 // or a child plane. |
| 413 linked_ptr<OverlayPlane> plane_for_swap; | 449 linked_ptr<OverlayPlane> plane_for_swap; |
| 414 | 450 |
| 415 // If there is no new root plane, remove everything. | |
| 416 if (!new_root_plane.get()) { | |
| 417 old_partial_damage_planes.clear(); | |
| 418 current_root_plane_.reset(); | |
| 419 return; | |
| 420 } | |
| 421 | |
| 422 // If the frame's size changed, if we haven't updated the root layer, if | 451 // If the frame's size changed, if we haven't updated the root layer, if |
| 423 // we have full damage, or if we don't support remote layers, then use the | 452 // we have full damage, or if we don't support remote layers, then use the |
| 424 // root layer directly. | 453 // root layer directly. |
| 425 if (!use_remote_layer_api_ || !current_root_plane_.get() || | 454 if (!allow_partial_swap_ || !old_tree || |
| 426 current_root_plane_->pixel_frame_rect != | 455 old_tree->root_plane_->pixel_frame_rect != |
| 427 new_root_plane->pixel_frame_rect || | 456 root_plane_->pixel_frame_rect || |
| 428 pixel_damage_rect == new_root_plane->pixel_frame_rect) { | 457 pixel_damage_rect == root_plane_->pixel_frame_rect) { |
| 429 plane_for_swap = new_root_plane; | 458 plane_for_swap = root_plane_; |
| 430 } | 459 } |
| 431 | 460 |
| 432 // Walk though the existing partial damage layers and see if there is one that | 461 // Walk though the old tree's partial damage layers and see if there is one |
| 433 // is appropriate to re-use. | 462 // that is appropriate to re-use. |
| 434 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) { | 463 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) { |
| 435 gfx::RectF plane_to_reuse_dip_enlarged_rect; | 464 gfx::RectF plane_to_reuse_dip_enlarged_rect; |
| 436 | 465 |
| 437 // Find the last partial damage plane to re-use the CALayer from. Grow the | 466 // Find the last partial damage plane to re-use the CALayer from. Grow the |
| 438 // new rect for this layer to include this damage, and all nearby partial | 467 // new rect for this layer to include this damage, and all nearby partial |
| 439 // damage layers. | 468 // damage layers. |
| 440 linked_ptr<OverlayPlane> plane_to_reuse; | 469 linked_ptr<OverlayPlane> plane_to_reuse; |
| 441 for (auto& old_plane : old_partial_damage_planes) { | 470 for (auto& old_plane : old_tree->partial_damage_planes_) { |
| 442 gfx::RectF dip_enlarged_rect = old_plane->pixel_frame_rect; | 471 gfx::RectF dip_enlarged_rect = old_plane->pixel_frame_rect; |
| 443 dip_enlarged_rect.Union(pixel_damage_rect); | 472 dip_enlarged_rect.Union(pixel_damage_rect); |
| 444 | 473 |
| 445 // Compute the fraction of the pixels that would not be updated by this | 474 // Compute the fraction of the pixels that would not be updated by this |
| 446 // swap. If it is too big, try another layer. | 475 // swap. If it is too big, try another layer. |
| 447 float waste_fraction = dip_enlarged_rect.size().GetArea() * 1.f / | 476 float waste_fraction = dip_enlarged_rect.size().GetArea() * 1.f / |
| 448 pixel_damage_rect.size().GetArea(); | 477 pixel_damage_rect.size().GetArea(); |
| 449 if (waste_fraction > kMaximumPartialDamageWasteFraction) | 478 if (waste_fraction > kMaximumPartialDamageWasteFraction) |
| 450 continue; | 479 continue; |
| 451 | 480 |
| 452 plane_to_reuse = old_plane; | 481 plane_to_reuse = old_plane; |
| 453 plane_to_reuse_dip_enlarged_rect.Union(dip_enlarged_rect); | 482 plane_to_reuse_dip_enlarged_rect.Union(dip_enlarged_rect); |
| 454 } | 483 } |
| 455 | 484 |
| 456 if (plane_to_reuse.get()) { | 485 if (plane_to_reuse.get()) { |
| 457 gfx::RectF enlarged_contents_rect = plane_to_reuse_dip_enlarged_rect; | 486 gfx::RectF enlarged_contents_rect = plane_to_reuse_dip_enlarged_rect; |
| 458 enlarged_contents_rect.Scale( | 487 enlarged_contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(), |
| 459 1. / new_root_plane->pixel_frame_rect.width(), | 488 1. / root_plane_->pixel_frame_rect.height()); |
| 460 1. / new_root_plane->pixel_frame_rect.height()); | |
| 461 | 489 |
| 462 plane_for_swap = OverlayPlane::CreateWithFrameRect( | 490 plane_for_swap = OverlayPlane::CreateWithFrameRect( |
| 463 0, new_root_plane->io_surface, plane_to_reuse_dip_enlarged_rect, | 491 0, root_plane_->io_surface, plane_to_reuse_dip_enlarged_rect, |
| 464 enlarged_contents_rect); | 492 enlarged_contents_rect); |
| 465 | 493 |
| 466 plane_for_swap->TakeCALayerFrom(plane_to_reuse.get()); | 494 plane_for_swap->TakeCALayerFrom(plane_to_reuse.get()); |
| 467 if (plane_to_reuse != old_partial_damage_planes.back()) | 495 if (plane_to_reuse != old_tree->partial_damage_planes_.back()) |
| 468 [plane_for_swap->ca_layer removeFromSuperlayer]; | 496 [plane_for_swap->ca_layer removeFromSuperlayer]; |
| 469 } | 497 } |
| 470 } | 498 } |
| 471 | 499 |
| 472 // If we haven't found an appropriate layer to re-use, create a new one, if | 500 // If we haven't found an appropriate layer to re-use, create a new one, if |
| 473 // we haven't already created too many. | 501 // we haven't already created too many. |
| 474 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty() && | 502 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty() && |
| 475 old_partial_damage_planes.size() < kMaximumPartialDamageLayers) { | 503 old_tree->partial_damage_planes_.size() < kMaximumPartialDamageLayers) { |
| 476 gfx::RectF contents_rect = gfx::RectF(pixel_damage_rect); | 504 gfx::RectF contents_rect = gfx::RectF(pixel_damage_rect); |
| 477 contents_rect.Scale(1. / new_root_plane->pixel_frame_rect.width(), | 505 contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(), |
| 478 1. / new_root_plane->pixel_frame_rect.height()); | 506 1. / root_plane_->pixel_frame_rect.height()); |
| 479 plane_for_swap = OverlayPlane::CreateWithFrameRect( | 507 plane_for_swap = OverlayPlane::CreateWithFrameRect( |
| 480 0, new_root_plane->io_surface, pixel_damage_rect, contents_rect); | 508 0, root_plane_->io_surface, pixel_damage_rect, contents_rect); |
| 481 } | 509 } |
| 482 | 510 |
| 483 // And if we still don't have a layer, use the root layer. | 511 // And if we still don't have a layer, use the root layer. |
| 484 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) | 512 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) |
| 485 plane_for_swap = new_root_plane; | 513 plane_for_swap = root_plane_; |
| 486 | 514 |
| 487 // Walk all old partial damage planes. Remove anything that is now completely | 515 // Walk all old partial damage planes. Remove anything that is now completely |
| 488 // covered, and move everything else into the new | 516 // covered, and move everything else into the new |partial_damage_planes_|. |
| 489 // |current_partial_damage_planes_|. | 517 if (old_tree) { |
| 490 for (auto& old_plane : old_partial_damage_planes) { | 518 for (auto& old_plane : old_tree->partial_damage_planes_) { |
| 491 // Intersect the planes' frames with the new root plane to ensure that | 519 // Intersect the planes' frames with the new root plane to ensure that |
| 492 // they don't get kept alive inappropriately. | 520 // they don't get kept alive inappropriately. |
| 493 gfx::RectF old_plane_frame_rect = old_plane->pixel_frame_rect; | 521 gfx::RectF old_plane_frame_rect = old_plane->pixel_frame_rect; |
| 494 old_plane_frame_rect.Intersect(new_root_plane->pixel_frame_rect); | 522 old_plane_frame_rect.Intersect(root_plane_->pixel_frame_rect); |
| 495 | 523 |
| 496 bool old_plane_covered_by_swap = false; | 524 bool old_plane_covered_by_swap = false; |
| 497 if (plane_for_swap.get() && | 525 if (plane_for_swap.get() && |
| 498 plane_for_swap->pixel_frame_rect.Contains(old_plane_frame_rect)) { | 526 plane_for_swap->pixel_frame_rect.Contains(old_plane_frame_rect)) { |
| 499 old_plane_covered_by_swap = true; | 527 old_plane_covered_by_swap = true; |
| 528 } |
| 529 if (!old_plane_covered_by_swap) { |
| 530 DCHECK(old_plane->ca_layer); |
| 531 partial_damage_planes_.push_back(old_plane); |
| 532 } |
| 500 } | 533 } |
| 501 if (!old_plane_covered_by_swap) { | 534 if (plane_for_swap != root_plane_) |
| 502 DCHECK(old_plane->ca_layer); | 535 root_plane_ = old_tree->root_plane_; |
| 503 current_partial_damage_planes_.push_back(old_plane); | |
| 504 } | |
| 505 } | 536 } |
| 506 | 537 |
| 507 // Finally, add the new swap's plane at the back of the list, if it exists. | 538 // Finally, add the new swap's plane at the back of the list, if it exists. |
| 508 if (plane_for_swap == new_root_plane) { | 539 if (plane_for_swap.get() && plane_for_swap != root_plane_) { |
| 509 current_root_plane_ = new_root_plane; | 540 partial_damage_planes_.push_back(plane_for_swap); |
| 510 } else if (plane_for_swap.get()) { | |
| 511 current_partial_damage_planes_.push_back(plane_for_swap); | |
| 512 } | 541 } |
| 513 } | 542 } |
| 514 | 543 |
| 515 void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamageCALayers( | 544 void CALayerPartialDamageTree::UpdateRootAndPartialDamageCALayers( |
| 545 CALayer* superlayer, |
| 516 float scale_factor) { | 546 float scale_factor) { |
| 517 if (!use_remote_layer_api_) { | 547 if (!allow_partial_swap_) { |
| 518 DCHECK(current_partial_damage_planes_.empty()); | 548 DCHECK(partial_damage_planes_.empty()); |
| 519 return; | 549 return; |
| 520 } | 550 } |
| 521 | 551 |
| 522 // Allocate and update CALayers for the backbuffer and partial damage layers. | 552 // Allocate and update CALayers for the backbuffer and partial damage layers. |
| 523 if (current_root_plane_.get()) { | 553 if (!root_plane_->ca_layer) { |
| 524 if (!current_root_plane_->ca_layer) { | 554 root_plane_->ca_layer.reset([[CALayer alloc] init]); |
| 525 current_root_plane_->ca_layer.reset([[CALayer alloc] init]); | 555 [superlayer setSublayers:nil]; |
| 526 [ca_root_layer_ setSublayers:nil]; | 556 [superlayer addSublayer:root_plane_->ca_layer]; |
| 527 [ca_root_layer_ addSublayer:current_root_plane_->ca_layer]; | |
| 528 } | |
| 529 } | 557 } |
| 530 for (auto& plane : current_partial_damage_planes_) { | 558 for (auto& plane : partial_damage_planes_) { |
| 531 if (!plane->ca_layer) { | 559 if (!plane->ca_layer) { |
| 532 DCHECK(plane == current_partial_damage_planes_.back()); | 560 DCHECK(plane == partial_damage_planes_.back()); |
| 533 plane->ca_layer.reset([[CALayer alloc] init]); | 561 plane->ca_layer.reset([[CALayer alloc] init]); |
| 534 } | 562 } |
| 535 if (![plane->ca_layer superlayer]) { | 563 if (![plane->ca_layer superlayer]) { |
| 536 DCHECK(plane == current_partial_damage_planes_.back()); | 564 DCHECK(plane == partial_damage_planes_.back()); |
| 537 [ca_root_layer_ addSublayer:plane->ca_layer]; | 565 [superlayer addSublayer:plane->ca_layer]; |
| 538 } | 566 } |
| 539 } | 567 } |
| 540 if (current_root_plane_.get()) | 568 root_plane_->UpdateProperties(scale_factor); |
| 541 current_root_plane_->UpdateProperties(scale_factor); | 569 for (auto& plane : partial_damage_planes_) |
| 542 for (auto& plane : current_partial_damage_planes_) | |
| 543 plane->UpdateProperties(scale_factor); | 570 plane->UpdateProperties(scale_factor); |
| 544 } | 571 } |
| 545 | 572 |
| 546 void ImageTransportSurfaceOverlayMac::UpdateCALayerTree( | |
| 547 scoped_ptr<CALayerTree> ca_layer_tree, | |
| 548 float scale_factor) { | |
| 549 if (ca_layer_tree) { | |
| 550 ca_layer_tree->CommitScheduledCALayers( | |
| 551 ca_root_layer_.get(), std::move(current_ca_layer_tree_), scale_factor); | |
| 552 current_ca_layer_tree_.swap(ca_layer_tree); | |
| 553 ca_layer_tree.reset(); | |
| 554 } else { | |
| 555 current_ca_layer_tree_.reset(); | |
| 556 } | |
| 557 } | |
| 558 | |
| 559 void ImageTransportSurfaceOverlayMac::DisplayAndClearAllPendingSwaps() { | 573 void ImageTransportSurfaceOverlayMac::DisplayAndClearAllPendingSwaps() { |
| 560 TRACE_EVENT0("gpu", | 574 TRACE_EVENT0("gpu", |
| 561 "ImageTransportSurfaceOverlayMac::DisplayAndClearAllPendingSwaps"); | 575 "ImageTransportSurfaceOverlayMac::DisplayAndClearAllPendingSwaps"); |
| 562 while (!pending_swaps_.empty()) | 576 while (!pending_swaps_.empty()) |
| 563 DisplayFirstPendingSwapImmediately(); | 577 DisplayFirstPendingSwapImmediately(); |
| 564 } | 578 } |
| 565 | 579 |
| 566 void ImageTransportSurfaceOverlayMac::CheckPendingSwapsCallback() { | 580 void ImageTransportSurfaceOverlayMac::CheckPendingSwapsCallback() { |
| 567 TRACE_EVENT0("gpu", | 581 TRACE_EVENT0("gpu", |
| 568 "ImageTransportSurfaceOverlayMac::CheckPendingSwapsCallback"); | 582 "ImageTransportSurfaceOverlayMac::CheckPendingSwapsCallback"); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 const gfx::Rect& pixel_frame_rect, | 653 const gfx::Rect& pixel_frame_rect, |
| 640 const gfx::RectF& crop_rect) { | 654 const gfx::RectF& crop_rect) { |
| 641 if (transform != gfx::OVERLAY_TRANSFORM_NONE) { | 655 if (transform != gfx::OVERLAY_TRANSFORM_NONE) { |
| 642 DLOG(ERROR) << "Invalid overlay plane transform."; | 656 DLOG(ERROR) << "Invalid overlay plane transform."; |
| 643 return false; | 657 return false; |
| 644 } | 658 } |
| 645 if (z_order) { | 659 if (z_order) { |
| 646 DLOG(ERROR) << "Invalid non-zero Z order."; | 660 DLOG(ERROR) << "Invalid non-zero Z order."; |
| 647 return false; | 661 return false; |
| 648 } | 662 } |
| 649 | 663 if (pending_partial_damage_tree_) { |
| 650 pending_root_plane_ = OverlayPlane::CreateWithFrameRect( | 664 DLOG(ERROR) << "Only one overlay per swap is allowed."; |
| 651 z_order, static_cast<gl::GLImageIOSurface*>(image)->io_surface(), | 665 return false; |
| 652 gfx::RectF(pixel_frame_rect), crop_rect); | 666 } |
| 667 pending_partial_damage_tree_.reset(new CALayerPartialDamageTree( |
| 668 use_remote_layer_api_, |
| 669 static_cast<gl::GLImageIOSurface*>(image)->io_surface(), |
| 670 pixel_frame_rect)); |
| 653 return true; | 671 return true; |
| 654 } | 672 } |
| 655 | 673 |
| 656 bool ImageTransportSurfaceOverlayMac::ScheduleCALayer( | 674 bool ImageTransportSurfaceOverlayMac::ScheduleCALayer( |
| 657 gl::GLImage* contents_image, | 675 gl::GLImage* contents_image, |
| 658 const gfx::RectF& contents_rect, | 676 const gfx::RectF& contents_rect, |
| 659 float opacity, | 677 float opacity, |
| 660 unsigned background_color, | 678 unsigned background_color, |
| 661 unsigned edge_aa_mask, | 679 unsigned edge_aa_mask, |
| 662 const gfx::RectF& rect, | 680 const gfx::RectF& rect, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 | 758 |
| 741 // Compute the previous vsync time. | 759 // Compute the previous vsync time. |
| 742 base::TimeTicks previous_vsync = | 760 base::TimeTicks previous_vsync = |
| 743 vsync_interval_ * ((from - vsync_timebase_) / vsync_interval_) + | 761 vsync_interval_ * ((from - vsync_timebase_) / vsync_interval_) + |
| 744 vsync_timebase_; | 762 vsync_timebase_; |
| 745 | 763 |
| 746 // Return |interval_fraction| through the next vsync. | 764 // Return |interval_fraction| through the next vsync. |
| 747 return previous_vsync + (1 + interval_fraction) * vsync_interval_; | 765 return previous_vsync + (1 + interval_fraction) * vsync_interval_; |
| 748 } | 766 } |
| 749 | 767 |
| 768 CALayerPartialDamageTree::CALayerPartialDamageTree( |
| 769 bool allow_partial_swap, |
| 770 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 771 const gfx::Rect& pixel_frame_rect) |
| 772 : allow_partial_swap_(allow_partial_swap) { |
| 773 root_plane_ = OverlayPlane::CreateWithFrameRect( |
| 774 0, io_surface, gfx::RectF(pixel_frame_rect), gfx::RectF(0, 0, 1, 1)); |
| 775 } |
| 776 |
| 777 CALayerPartialDamageTree::~CALayerPartialDamageTree() {} |
| 778 |
| 779 base::ScopedCFTypeRef<IOSurfaceRef> |
| 780 CALayerPartialDamageTree::RootLayerIOSurface() { |
| 781 return root_plane_->io_surface; |
| 782 } |
| 783 |
| 784 void CALayerPartialDamageTree::CommitCALayers( |
| 785 CALayer* superlayer, |
| 786 scoped_ptr<CALayerPartialDamageTree> old_tree, |
| 787 float scale_factor, |
| 788 const gfx::Rect& pixel_damage_rect) { |
| 789 UpdateRootAndPartialDamagePlanes(old_tree.get(), |
| 790 gfx::RectF(pixel_damage_rect)); |
| 791 UpdateRootAndPartialDamageCALayers(superlayer, scale_factor); |
| 792 } |
| 793 |
| 750 } // namespace content | 794 } // namespace content |
| OLD | NEW |