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
3 // found in the LICENSE file.
5 #include "content/common/gpu/image_transport_surface_overlay_mac.h"
9 #include "content/common/gpu/gpu_messages.h"
10 #include "ui/accelerated_widget_mac/surface_handle_types.h"
11 #include "ui/base/cocoa/animation_utils.h"
12 #include "ui/base/cocoa/remote_layer_api.h"
13 #include "ui/gfx/geometry/dip_util.h"
14 #include "ui/gl/gl_image_io_surface.h"
18 ImageTransportSurfaceOverlayMac::ImageTransportSurfaceOverlayMac(
19 GpuChannelManager* manager,
20 GpuCommandBufferStub* stub,
21 gfx::PluginWindowHandle handle)
22 : scale_factor_(1), pending_overlay_image_(nullptr) {
23 helper_.reset(new ImageTransportHelper(this, manager, stub, handle));
26 ImageTransportSurfaceOverlayMac::~ImageTransportSurfaceOverlayMac() {
27 gfx::GLImageIOSurface::SetLayerForWidget(widget_, nil);
30 bool ImageTransportSurfaceOverlayMac::Initialize() {
31 if (!helper_->Initialize())
34 // Create the CAContext to send this to the GPU process, and the layer for
36 CGSConnectionID connection_id = CGSMainConnectionID();
38 [[CAContext contextWithCGSConnection:connection_id options:@{}] retain]);
39 layer_.reset([[CALayer alloc] init]);
40 [ca_context_ setLayer:layer_];
42 // Register the CALayer so that it can be picked up in GLImageIOSurface.
43 static intptr_t previous_widget = 0;
45 widget_ = reinterpret_cast<gfx::AcceleratedWidget>(previous_widget);
46 gfx::GLImageIOSurface::SetLayerForWidget(widget_, layer_);
51 void ImageTransportSurfaceOverlayMac::Destroy() {}
53 bool ImageTransportSurfaceOverlayMac::IsOffscreen() {
57 gfx::SwapResult ImageTransportSurfaceOverlayMac::SwapBuffers() {
58 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::SwapBuffers");
60 // A flush is required to ensure that all content appears in the layer.
62 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::glFlush");
66 // There should exist only one overlay image, and it should cover the whole
68 DCHECK(pending_overlay_image_);
69 if (pending_overlay_image_) {
70 TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::setContents");
71 ScopedCAActionDisabler disabler;
72 gfx::Rect dip_bounds = gfx::ConvertRectToDIP(
73 scale_factor_, gfx::Rect(pixel_size_));
74 gfx::RectF crop_rect(0, 0, 1, 1);
75 pending_overlay_image_->ScheduleOverlayPlane(
76 widget_, 0, gfx::OVERLAY_TRANSFORM_NONE, dip_bounds, crop_rect);
77 pending_overlay_image_ = nullptr;
80 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params;
81 params.surface_handle =
82 ui::SurfaceHandleFromCAContextID([ca_context_ contextId]);
83 params.size = pixel_size_;
84 params.scale_factor = scale_factor_;
85 params.latency_info.swap(latency_info_);
86 helper_->SendAcceleratedSurfaceBuffersSwapped(params);
87 return gfx::SwapResult::SWAP_ACK;
90 gfx::SwapResult ImageTransportSurfaceOverlayMac::PostSubBuffer(int x,
97 bool ImageTransportSurfaceOverlayMac::SupportsPostSubBuffer() {
101 gfx::Size ImageTransportSurfaceOverlayMac::GetSize() {
105 void* ImageTransportSurfaceOverlayMac::GetHandle() {
109 bool ImageTransportSurfaceOverlayMac::ScheduleOverlayPlane(
111 gfx::OverlayTransform transform,
113 const gfx::Rect& bounds_rect,
114 const gfx::RectF& crop_rect) {
115 // For now we allow only the one full-surface overlay plane.
116 // TODO(ccameron): This will need to be updated when support for multiple
117 // planes is enabled.
118 DCHECK_EQ(z_order, 0);
119 DCHECK_EQ(bounds_rect.ToString(), gfx::Rect(pixel_size_).ToString());
120 DCHECK_EQ(crop_rect.ToString(), gfx::RectF(0, 0, 1, 1).ToString());
121 DCHECK_EQ(transform, gfx::OVERLAY_TRANSFORM_NONE);
122 DCHECK(!pending_overlay_image_);
123 pending_overlay_image_ = image;
127 bool ImageTransportSurfaceOverlayMac::IsSurfaceless() const {
131 void ImageTransportSurfaceOverlayMac::OnBufferPresented(
132 const AcceleratedSurfaceMsg_BufferPresented_Params& params) {}
134 void ImageTransportSurfaceOverlayMac::OnResize(gfx::Size pixel_size,
135 float scale_factor) {
136 pixel_size_ = pixel_size;
137 scale_factor_ = scale_factor;
140 void ImageTransportSurfaceOverlayMac::SetLatencyInfo(
141 const std::vector<ui::LatencyInfo>& latency_info) {
142 latency_info_.insert(
143 latency_info_.end(), latency_info.begin(), latency_info.end());
146 void ImageTransportSurfaceOverlayMac::WakeUpGpu() {}
148 } // namespace content