| 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 "gpu/ipc/service/image_transport_surface_overlay_mac.h" | 5 #include "gpu/ipc/service/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> |
| 11 #include <OpenGL/gl.h> | 11 #include <OpenGL/gl.h> |
| 12 #include <stddef.h> | 12 #include <stddef.h> |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 | 15 |
| 16 // This type consistently causes problem on Mac, and needs to be dealt with | 16 // This type consistently causes problem on Mac, and needs to be dealt with |
| 17 // in a systemic way. | 17 // in a systemic way. |
| 18 // http://crbug.com/517208 | 18 // http://crbug.com/517208 |
| 19 #ifndef GL_OES_EGL_image | 19 #ifndef GL_OES_EGL_image |
| 20 typedef void* GLeglImageOES; | 20 typedef void* GLeglImageOES; |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 #include "base/bind.h" | 23 #include "base/bind.h" |
| 24 #include "base/bind_helpers.h" | 24 #include "base/bind_helpers.h" |
| 25 #include "base/mac/scoped_cftyperef.h" | 25 #include "base/mac/scoped_cftyperef.h" |
| 26 #include "base/trace_event/trace_event.h" | 26 #include "base/trace_event/trace_event.h" |
| 27 #include "gpu/ipc/service/gpu_channel_manager.h" | 27 #include "gpu/ipc/service/gpu_channel_manager.h" |
| 28 #include "gpu/ipc/service/gpu_channel_manager_delegate.h" | 28 #include "gpu/ipc/service/gpu_channel_manager_delegate.h" |
| 29 #include "ui/accelerated_widget_mac/ca_layer_partial_damage_tree_mac.h" | 29 #include "ui/accelerated_widget_mac/ca_layer_tree_host.h" |
| 30 #include "ui/accelerated_widget_mac/ca_layer_tree_mac.h" | |
| 31 #include "ui/accelerated_widget_mac/io_surface_context.h" | 30 #include "ui/accelerated_widget_mac/io_surface_context.h" |
| 32 #include "ui/base/cocoa/animation_utils.h" | 31 #include "ui/base/cocoa/animation_utils.h" |
| 33 #include "ui/base/cocoa/remote_layer_api.h" | 32 #include "ui/base/cocoa/remote_layer_api.h" |
| 34 #include "ui/gfx/geometry/rect_conversions.h" | 33 #include "ui/gfx/geometry/rect_conversions.h" |
| 35 #include "ui/gfx/transform.h" | 34 #include "ui/gfx/transform.h" |
| 36 #include "ui/gl/gl_context.h" | 35 #include "ui/gl/gl_context.h" |
| 37 #include "ui/gl/gl_fence.h" | 36 #include "ui/gl/gl_fence.h" |
| 38 #include "ui/gl/gl_image_io_surface.h" | 37 #include "ui/gl/gl_image_io_surface.h" |
| 39 #include "ui/gl/gpu_switching_manager.h" | 38 #include "ui/gl/gpu_switching_manager.h" |
| 40 #include "ui/gl/scoped_api.h" | 39 #include "ui/gl/scoped_api.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 71 stub_(stub->AsWeakPtr()), | 70 stub_(stub->AsWeakPtr()), |
| 72 handle_(handle), | 71 handle_(handle), |
| 73 use_remote_layer_api_(ui::RemoteLayerAPISupported()), | 72 use_remote_layer_api_(ui::RemoteLayerAPISupported()), |
| 74 scale_factor_(1), | 73 scale_factor_(1), |
| 75 gl_renderer_id_(0), | 74 gl_renderer_id_(0), |
| 76 vsync_parameters_valid_(false) { | 75 vsync_parameters_valid_(false) { |
| 77 manager_->AddBufferPresentedCallback( | 76 manager_->AddBufferPresentedCallback( |
| 78 handle_, base::Bind(&ImageTransportSurfaceOverlayMac::BufferPresented, | 77 handle_, base::Bind(&ImageTransportSurfaceOverlayMac::BufferPresented, |
| 79 base::Unretained(this))); | 78 base::Unretained(this))); |
| 80 ui::GpuSwitchingManager::GetInstance()->AddObserver(this); | 79 ui::GpuSwitchingManager::GetInstance()->AddObserver(this); |
| 80 ca_layer_tree_host_.reset(new ui::CALayerTreeHost(use_remote_layer_api_)); |
| 81 } | 81 } |
| 82 | 82 |
| 83 ImageTransportSurfaceOverlayMac::~ImageTransportSurfaceOverlayMac() { | 83 ImageTransportSurfaceOverlayMac::~ImageTransportSurfaceOverlayMac() { |
| 84 ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this); | 84 ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this); |
| 85 if (stub_.get()) { | 85 if (stub_.get()) { |
| 86 stub_->SetLatencyInfoCallback( | 86 stub_->SetLatencyInfoCallback( |
| 87 base::Callback<void(const std::vector<ui::LatencyInfo>&)>()); | 87 base::Callback<void(const std::vector<ui::LatencyInfo>&)>()); |
| 88 } | 88 } |
| 89 Destroy(); | 89 Destroy(); |
| 90 manager_->RemoveBufferPresentedCallback(handle_); | 90 manager_->RemoveBufferPresentedCallback(handle_); |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool ImageTransportSurfaceOverlayMac::Initialize( | 93 bool ImageTransportSurfaceOverlayMac::Initialize( |
| 94 gfx::GLSurface::Format format) { | 94 gfx::GLSurface::Format format) { |
| 95 if (!stub_.get() || !stub_->decoder()) | 95 if (!stub_.get() || !stub_->decoder()) |
| 96 return false; | 96 return false; |
| 97 | 97 |
| 98 stub_->SetLatencyInfoCallback( | 98 stub_->SetLatencyInfoCallback( |
| 99 base::Bind(&ImageTransportSurfaceOverlayMac::SetLatencyInfo, | 99 base::Bind(&ImageTransportSurfaceOverlayMac::SetLatencyInfo, |
| 100 base::Unretained(this))); | 100 base::Unretained(this))); |
| 101 | 101 |
| 102 // Create the CAContext to send this to the GPU process, and the layer for | 102 // Create the CAContext to send this to the GPU process, and the layer for |
| 103 // the context. | 103 // the context. |
| 104 if (use_remote_layer_api_) { | 104 if (use_remote_layer_api_) { |
| 105 CGSConnectionID connection_id = CGSMainConnectionID(); | 105 CGSConnectionID connection_id = CGSMainConnectionID(); |
| 106 ca_context_.reset([ | 106 ca_context_.reset([ |
| 107 [CAContext contextWithCGSConnection:connection_id options:@{}] retain]); | 107 [CAContext contextWithCGSConnection:connection_id options:@{}] retain]); |
| 108 ca_root_layer_.reset([[CALayer alloc] init]); | 108 [ca_context_ setLayer:ca_layer_tree_host_->GetRootCALayer()]; |
| 109 [ca_root_layer_ setGeometryFlipped:YES]; | |
| 110 [ca_root_layer_ setOpaque:YES]; | |
| 111 [ca_context_ setLayer:ca_root_layer_]; | |
| 112 } | 109 } |
| 113 return true; | 110 return true; |
| 114 } | 111 } |
| 115 | 112 |
| 116 void ImageTransportSurfaceOverlayMac::Destroy() { | 113 void ImageTransportSurfaceOverlayMac::Destroy() { |
| 117 current_partial_damage_tree_.reset(); | 114 ca_layer_tree_host_.reset(); |
| 118 current_ca_layer_tree_.reset(); | |
| 119 } | 115 } |
| 120 | 116 |
| 121 bool ImageTransportSurfaceOverlayMac::IsOffscreen() { | 117 bool ImageTransportSurfaceOverlayMac::IsOffscreen() { |
| 122 return false; | 118 return false; |
| 123 } | 119 } |
| 124 | 120 |
| 125 void ImageTransportSurfaceOverlayMac::SetLatencyInfo( | 121 void ImageTransportSurfaceOverlayMac::SetLatencyInfo( |
| 126 const std::vector<ui::LatencyInfo>& latency_info) { | 122 const std::vector<ui::LatencyInfo>& latency_info) { |
| 127 latency_info_.insert(latency_info_.end(), latency_info.begin(), | 123 latency_info_.insert(latency_info_.end(), latency_info.begin(), |
| 128 latency_info.end()); | 124 latency_info.end()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 { | 168 { |
| 173 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; | 169 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api; |
| 174 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::glFinish"); | 170 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::glFinish"); |
| 175 CheckGLErrors("Before finish"); | 171 CheckGLErrors("Before finish"); |
| 176 glFinish(); | 172 glFinish(); |
| 177 CheckGLErrors("After finish"); | 173 CheckGLErrors("After finish"); |
| 178 } | 174 } |
| 179 | 175 |
| 180 base::TimeTicks finish_time = base::TimeTicks::Now(); | 176 base::TimeTicks finish_time = base::TimeTicks::Now(); |
| 181 | 177 |
| 182 // Update the CALayer hierarchy. | 178 ca_layer_tree_host_->CommitPendingTreesToCA(pixel_damage_rect); |
| 183 { | |
| 184 ScopedCAActionDisabler disabler; | |
| 185 if (pending_ca_layer_tree_) { | |
| 186 pending_ca_layer_tree_->CommitScheduledCALayers( | |
| 187 ca_root_layer_.get(), std::move(current_ca_layer_tree_), | |
| 188 scale_factor_); | |
| 189 current_ca_layer_tree_.swap(pending_ca_layer_tree_); | |
| 190 current_partial_damage_tree_.reset(); | |
| 191 } else if (pending_partial_damage_tree_) { | |
| 192 pending_partial_damage_tree_->CommitCALayers( | |
| 193 ca_root_layer_.get(), std::move(current_partial_damage_tree_), | |
| 194 scale_factor_, pixel_damage_rect); | |
| 195 current_partial_damage_tree_.swap(pending_partial_damage_tree_); | |
| 196 current_ca_layer_tree_.reset(); | |
| 197 } else { | |
| 198 TRACE_EVENT0("gpu", "Blank frame: No overlays or CALayers"); | |
| 199 [ca_root_layer_ setSublayers:nil]; | |
| 200 current_partial_damage_tree_.reset(); | |
| 201 current_ca_layer_tree_.reset(); | |
| 202 } | |
| 203 } | |
| 204 | 179 |
| 205 // Update the latency info to reflect the swap time. | 180 // Update the latency info to reflect the swap time. |
| 206 for (auto latency_info : latency_info_) { | 181 for (auto latency_info : latency_info_) { |
| 207 latency_info.AddLatencyNumberWithTimestamp( | 182 latency_info.AddLatencyNumberWithTimestamp( |
| 208 ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, 0, 0, finish_time, 1); | 183 ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, 0, 0, finish_time, 1); |
| 209 latency_info.AddLatencyNumberWithTimestamp( | 184 latency_info.AddLatencyNumberWithTimestamp( |
| 210 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, 0, 0, | 185 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, 0, 0, |
| 211 finish_time, 1); | 186 finish_time, 1); |
| 212 } | 187 } |
| 213 | 188 |
| 214 // Send acknowledgement to the browser. | 189 // Send acknowledgement to the browser. |
| 215 CAContextID ca_context_id = 0; | 190 CAContextID ca_context_id = 0; |
| 216 gfx::ScopedRefCountedIOSurfaceMachPort io_surface; | 191 gfx::ScopedRefCountedIOSurfaceMachPort io_surface_mach_port; |
| 217 if (use_remote_layer_api_) { | 192 if (use_remote_layer_api_) { |
| 218 ca_context_id = [ca_context_ contextId]; | 193 ca_context_id = [ca_context_ contextId]; |
| 219 } else if (current_partial_damage_tree_) { | 194 } else { |
| 220 io_surface.reset(IOSurfaceCreateMachPort( | 195 IOSurfaceRef io_surface = ca_layer_tree_host_->GetFrontbufferIOSurface(); |
| 221 current_partial_damage_tree_->RootLayerIOSurface())); | 196 if (io_surface) |
| 197 io_surface_mach_port.reset(IOSurfaceCreateMachPort(io_surface)); |
| 222 } | 198 } |
| 223 SendAcceleratedSurfaceBuffersSwapped(handle_, ca_context_id, io_surface, | 199 SendAcceleratedSurfaceBuffersSwapped(handle_, ca_context_id, |
| 224 pixel_size_, scale_factor_, | 200 io_surface_mach_port, pixel_size_, |
| 225 std::move(latency_info_)); | 201 scale_factor_, std::move(latency_info_)); |
| 226 | 202 |
| 227 // Reset all state for the next frame. | 203 // Reset all state for the next frame. |
| 228 latency_info_.clear(); | 204 latency_info_.clear(); |
| 229 pending_ca_layer_tree_.reset(); | |
| 230 pending_partial_damage_tree_.reset(); | |
| 231 return gfx::SwapResult::SWAP_ACK; | 205 return gfx::SwapResult::SWAP_ACK; |
| 232 } | 206 } |
| 233 | 207 |
| 234 gfx::SwapResult ImageTransportSurfaceOverlayMac::SwapBuffers() { | 208 gfx::SwapResult ImageTransportSurfaceOverlayMac::SwapBuffers() { |
| 235 return SwapBuffersInternal( | 209 return SwapBuffersInternal( |
| 236 gfx::Rect(0, 0, pixel_size_.width(), pixel_size_.height())); | 210 gfx::Rect(0, 0, pixel_size_.width(), pixel_size_.height())); |
| 237 } | 211 } |
| 238 | 212 |
| 239 gfx::SwapResult ImageTransportSurfaceOverlayMac::PostSubBuffer(int x, | 213 gfx::SwapResult ImageTransportSurfaceOverlayMac::PostSubBuffer(int x, |
| 240 int y, | 214 int y, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 270 const gfx::Rect& pixel_frame_rect, | 244 const gfx::Rect& pixel_frame_rect, |
| 271 const gfx::RectF& crop_rect) { | 245 const gfx::RectF& crop_rect) { |
| 272 if (transform != gfx::OVERLAY_TRANSFORM_NONE) { | 246 if (transform != gfx::OVERLAY_TRANSFORM_NONE) { |
| 273 DLOG(ERROR) << "Invalid overlay plane transform."; | 247 DLOG(ERROR) << "Invalid overlay plane transform."; |
| 274 return false; | 248 return false; |
| 275 } | 249 } |
| 276 if (z_order) { | 250 if (z_order) { |
| 277 DLOG(ERROR) << "Invalid non-zero Z order."; | 251 DLOG(ERROR) << "Invalid non-zero Z order."; |
| 278 return false; | 252 return false; |
| 279 } | 253 } |
| 280 if (pending_partial_damage_tree_) { | 254 return ca_layer_tree_host_->SetPendingBackbuffer( |
| 281 DLOG(ERROR) << "Only one overlay per swap is allowed."; | 255 static_cast<gl::GLImageIOSurface*>(image)->io_surface()); |
| 282 return false; | |
| 283 } | |
| 284 pending_partial_damage_tree_.reset(new ui::CALayerPartialDamageTree( | |
| 285 use_remote_layer_api_, | |
| 286 static_cast<gl::GLImageIOSurface*>(image)->io_surface(), | |
| 287 pixel_frame_rect)); | |
| 288 return true; | |
| 289 } | 256 } |
| 290 | 257 |
| 291 bool ImageTransportSurfaceOverlayMac::ScheduleCALayer( | 258 bool ImageTransportSurfaceOverlayMac::ScheduleCALayer( |
| 292 gl::GLImage* contents_image, | 259 gl::GLImage* contents_image, |
| 293 const gfx::RectF& contents_rect, | 260 const gfx::RectF& contents_rect, |
| 294 float opacity, | 261 float opacity, |
| 295 unsigned background_color, | 262 unsigned background_color, |
| 296 unsigned edge_aa_mask, | 263 unsigned edge_aa_mask, |
| 297 const gfx::RectF& rect, | 264 const gfx::RectF& rect, |
| 298 bool is_clipped, | 265 bool is_clipped, |
| 299 const gfx::RectF& clip_rect, | 266 const gfx::RectF& clip_rect, |
| 300 const gfx::Transform& transform, | 267 const gfx::Transform& transform, |
| 301 int sorting_context_id, | 268 int sorting_context_id, |
| 302 unsigned filter) { | 269 unsigned filter) { |
| 303 base::ScopedCFTypeRef<IOSurfaceRef> io_surface; | 270 base::ScopedCFTypeRef<IOSurfaceRef> io_surface; |
| 304 base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer; | 271 base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer; |
| 305 if (contents_image) { | 272 if (contents_image) { |
| 306 gl::GLImageIOSurface* io_surface_image = | 273 gl::GLImageIOSurface* io_surface_image = |
| 307 static_cast<gl::GLImageIOSurface*>(contents_image); | 274 static_cast<gl::GLImageIOSurface*>(contents_image); |
| 308 io_surface = io_surface_image->io_surface(); | 275 io_surface = io_surface_image->io_surface(); |
| 309 cv_pixel_buffer = io_surface_image->cv_pixel_buffer(); | 276 cv_pixel_buffer = io_surface_image->cv_pixel_buffer(); |
| 310 } | 277 } |
| 311 if (!pending_ca_layer_tree_) | 278 return ca_layer_tree_host_->GetPendingCALayerTree()->ScheduleCALayer( |
| 312 pending_ca_layer_tree_.reset(new ui::CALayerTree); | |
| 313 return pending_ca_layer_tree_->ScheduleCALayer( | |
| 314 is_clipped, gfx::ToEnclosingRect(clip_rect), sorting_context_id, | 279 is_clipped, gfx::ToEnclosingRect(clip_rect), sorting_context_id, |
| 315 transform, io_surface, cv_pixel_buffer, contents_rect, | 280 transform, io_surface, cv_pixel_buffer, contents_rect, |
| 316 gfx::ToEnclosingRect(rect), background_color, edge_aa_mask, opacity); | 281 gfx::ToEnclosingRect(rect), background_color, edge_aa_mask, opacity); |
| 317 } | 282 } |
| 318 | 283 |
| 319 bool ImageTransportSurfaceOverlayMac::IsSurfaceless() const { | 284 bool ImageTransportSurfaceOverlayMac::IsSurfaceless() const { |
| 320 return true; | 285 return true; |
| 321 } | 286 } |
| 322 | 287 |
| 323 bool ImageTransportSurfaceOverlayMac::Resize(const gfx::Size& pixel_size, | 288 bool ImageTransportSurfaceOverlayMac::Resize(const gfx::Size& pixel_size, |
| 324 float scale_factor, | 289 float scale_factor, |
| 325 bool has_alpha) { | 290 bool has_alpha) { |
| 326 // Flush through any pending frames. | |
| 327 pixel_size_ = pixel_size; | 291 pixel_size_ = pixel_size; |
| 328 scale_factor_ = scale_factor; | 292 scale_factor_ = scale_factor; |
| 293 ca_layer_tree_host_->Resize(pixel_size, scale_factor); |
| 329 return true; | 294 return true; |
| 330 } | 295 } |
| 331 | 296 |
| 332 void ImageTransportSurfaceOverlayMac::OnGpuSwitched() { | 297 void ImageTransportSurfaceOverlayMac::OnGpuSwitched() { |
| 333 // Create a new context, and use the GL renderer ID that the new context gets. | 298 // Create a new context, and use the GL renderer ID that the new context gets. |
| 334 scoped_refptr<ui::IOSurfaceContext> context_on_new_gpu = | 299 scoped_refptr<ui::IOSurfaceContext> context_on_new_gpu = |
| 335 ui::IOSurfaceContext::Get(ui::IOSurfaceContext::kCALayerContext); | 300 ui::IOSurfaceContext::Get(ui::IOSurfaceContext::kCALayerContext); |
| 336 if (!context_on_new_gpu) | 301 if (!context_on_new_gpu) |
| 337 return; | 302 return; |
| 338 GLint context_renderer_id = -1; | 303 GLint context_renderer_id = -1; |
| 339 if (CGLGetParameter(context_on_new_gpu->cgl_context(), | 304 if (CGLGetParameter(context_on_new_gpu->cgl_context(), |
| 340 kCGLCPCurrentRendererID, | 305 kCGLCPCurrentRendererID, |
| 341 &context_renderer_id) != kCGLNoError) { | 306 &context_renderer_id) != kCGLNoError) { |
| 342 LOG(ERROR) << "Failed to create test context after GPU switch"; | 307 LOG(ERROR) << "Failed to create test context after GPU switch"; |
| 343 return; | 308 return; |
| 344 } | 309 } |
| 345 gl_renderer_id_ = context_renderer_id & kCGLRendererIDMatchingMask; | 310 gl_renderer_id_ = context_renderer_id & kCGLRendererIDMatchingMask; |
| 346 | 311 |
| 347 // Post a task holding a reference to the new GL context. The reason for | 312 // Post a task holding a reference to the new GL context. The reason for |
| 348 // this is to avoid creating-then-destroying the context for every image | 313 // this is to avoid creating-then-destroying the context for every image |
| 349 // transport surface that is observing the GPU switch. | 314 // transport surface that is observing the GPU switch. |
| 350 base::MessageLoop::current()->PostTask( | 315 base::MessageLoop::current()->PostTask( |
| 351 FROM_HERE, base::Bind(&IOSurfaceContextNoOp, context_on_new_gpu)); | 316 FROM_HERE, base::Bind(&IOSurfaceContextNoOp, context_on_new_gpu)); |
| 352 } | 317 } |
| 353 | 318 |
| 354 } // namespace gpu | 319 } // namespace gpu |
| OLD | NEW |