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.
4 
5 #ifndef GPU_IPC_SERVICE_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_
6 #define GPU_IPC_SERVICE_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_
7 
8 #include <vector>
9 
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/memory/weak_ptr.h"
12 #include "gpu/ipc/service/command_buffer_stub.h"
13 #include "gpu/ipc/service/image_transport_surface.h"
14 #include "ui/gfx/presentation_feedback.h"
15 #include "ui/gl/gl_bindings.h"
16 #include "ui/gl/gl_surface.h"
17 #include "ui/gl/gpu_switching_observer.h"
18 
19 #if defined(USE_EGL)
20 #include "ui/gl/gl_surface_egl.h"
21 #endif
22 
23 @class CAContext;
24 @class CALayer;
25 
26 namespace ui {
27 class CALayerTreeCoordinator;
28 struct CARendererLayerParams;
29 }
30 
31 namespace gl {
32 class GLFence;
33 }
34 
35 namespace gpu {
36 
37 // Template ImageTransportSurfaceOverlayMac based on its base class so that it
38 // can be used by both the validating and passthrough command decoders by
39 // inheriting from GLSurface and GLSurfaceEGL respectively. Once the validating
40 // command decoder is removed, the template can be removed and
41 // ImageTransportSurfaceOverlayMac can always inherit from GLSurfaceEGL.
42 
43 template <typename BaseClass>
44 class ImageTransportSurfaceOverlayMacBase : public BaseClass,
45                                             public ui::GpuSwitchingObserver {
46  public:
47   explicit ImageTransportSurfaceOverlayMacBase(
48       base::WeakPtr<ImageTransportSurfaceDelegate> delegate);
49 
50   // GLSurface implementation
51   bool Initialize(gl::GLSurfaceFormat format) override;
52   void Destroy() override;
53   void PrepareToDestroy(bool have_context) override;
54   bool Resize(const gfx::Size& size,
55               float scale_factor,
56               const gfx::ColorSpace& color_space,
57               bool has_alpha) override;
58   bool IsOffscreen() override;
59   gfx::SwapResult SwapBuffers(
60       gl::GLSurface::PresentationCallback callback) override;
61   void SwapBuffersAsync(
62       gl::GLSurface::SwapCompletionCallback completion_callback,
63       gl::GLSurface::PresentationCallback presentation_callback) override;
64   gfx::SwapResult PostSubBuffer(
65       int x,
66       int y,
67       int width,
68       int height,
69       gl::GLSurface::PresentationCallback callback) override;
70   void PostSubBufferAsync(
71       int x,
72       int y,
73       int width,
74       int height,
75       gl::GLSurface::SwapCompletionCallback completion_callback,
76       gl::GLSurface::PresentationCallback presentation_callback) override;
77   gfx::SwapResult CommitOverlayPlanes(
78       gl::GLSurface::PresentationCallback callback) override;
79   void CommitOverlayPlanesAsync(
80       gl::GLSurface::SwapCompletionCallback completion_callback,
81       gl::GLSurface::PresentationCallback presentation_callback) override;
82 
83   bool SupportsPostSubBuffer() override;
84   bool SupportsCommitOverlayPlanes() override;
85   bool SupportsAsyncSwap() override;
86   gfx::Size GetSize() override;
87   void* GetHandle() override;
88   gl::GLSurfaceFormat GetFormat() override;
89   bool OnMakeCurrent(gl::GLContext* context) override;
90   bool ScheduleOverlayPlane(int z_order,
91                             gfx::OverlayTransform transform,
92                             gl::GLImage* image,
93                             const gfx::Rect& bounds_rect,
94                             const gfx::RectF& crop_rect,
95                             bool enable_blend,
96                             std::unique_ptr<gfx::GpuFence> gpu_fence) override;
97   bool ScheduleCALayer(const ui::CARendererLayerParams& params) override;
98   void ScheduleCALayerInUseQuery(
99       std::vector<gl::GLSurface::CALayerInUseQuery> queries) override;
100   bool IsSurfaceless() const override;
101   gfx::SurfaceOrigin GetOrigin() const override;
102 
103   // ui::GpuSwitchingObserver implementation.
104   void OnGpuSwitched(gl::GpuPreference active_gpu_heuristic) override;
105 
106  private:
107   ~ImageTransportSurfaceOverlayMacBase() override;
108 
109   gfx::SwapResult SwapBuffersInternal(
110       gl::GLSurface::SwapCompletionCallback completion_callback,
111       gl::GLSurface::PresentationCallback presentation_callback);
112   void ApplyBackpressure();
113   void BufferPresented(gl::GLSurface::PresentationCallback callback,
114                        const gfx::PresentationFeedback& feedback);
115 
116   base::WeakPtr<ImageTransportSurfaceDelegate> delegate_;
117 
118   bool use_remote_layer_api_;
119   base::scoped_nsobject<CAContext> ca_context_;
120   std::unique_ptr<ui::CALayerTreeCoordinator> ca_layer_tree_coordinator_;
121 
122   gfx::Size pixel_size_;
123   float scale_factor_;
124 
125   std::vector<gl::GLSurface::CALayerInUseQuery> ca_layer_in_use_queries_;
126 
127   // A GLFence marking the end of the previous frame, used for applying
128   // backpressure.
129   uint64_t previous_frame_fence_ = 0;
130 
131   // The renderer ID that all contexts made current to this surface should be
132   // targeting.
133   GLint gl_renderer_id_;
134   base::WeakPtrFactory<ImageTransportSurfaceOverlayMacBase<BaseClass>>
135       weak_ptr_factory_;
136 };
137 
138 using ImageTransportSurfaceOverlayMac =
139     ImageTransportSurfaceOverlayMacBase<gl::GLSurface>;
140 
141 #if defined(USE_EGL)
142 using ImageTransportSurfaceOverlayMacEGL =
143     ImageTransportSurfaceOverlayMacBase<gl::GLSurfaceEGL>;
144 #endif
145 
146 }  // namespace gpu
147 
148 #endif  // GPU_IPC_SERVICE_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_
149