1 // Copyright (c) 2012 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 #ifndef UI_GL_GL_SURFACE_H_
6 #define UI_GL_GL_SURFACE_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "build/build_config.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/rect_f.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gfx/native_widget_types.h"
17 #include "ui/gfx/overlay_transform.h"
18 #include "ui/gfx/swap_result.h"
19 #include "ui/gl/gl_export.h"
20 #include "ui/gl/gl_implementation.h"
28 // Encapsulates a surface that can be rendered to with GL, hiding platform
29 // specific management.
30 class GL_EXPORT GLSurface
: public base::RefCounted
<GLSurface
> {
34 // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
35 // EGL surface associated to be recreated without destroying the associated
36 // context. The implementation of this function for other GLSurface derived
37 // classes is in a pending changelist.
38 virtual bool Initialize();
40 // Destroys the surface.
41 virtual void Destroy() = 0;
43 // Destroys the surface and terminates its underlying display. This must be
44 // the last surface which uses the display.
45 virtual void DestroyAndTerminateDisplay();
47 virtual bool Resize(const gfx::Size
& size
);
49 // Recreate the surface without changing the size.
50 virtual bool Recreate();
52 // Unschedule the GpuScheduler and return true to abort the processing of
53 // a GL draw call to this surface and defer it until the GpuScheduler is
55 virtual bool DeferDraws();
57 // Returns true if this surface is offscreen.
58 virtual bool IsOffscreen() = 0;
60 // Swaps front and back buffers. This has no effect for off-screen
62 virtual gfx::SwapResult
SwapBuffers() = 0;
64 // Get the size of the surface.
65 virtual gfx::Size
GetSize() = 0;
67 // Get the underlying platform specific surface "handle".
68 virtual void* GetHandle() = 0;
70 // Returns whether or not the surface supports PostSubBuffer.
71 virtual bool SupportsPostSubBuffer();
73 // Returns the internal frame buffer object name if the surface is backed by
74 // FBO. Otherwise returns 0.
75 virtual unsigned int GetBackingFrameBufferObject();
77 typedef base::Callback
<void(SwapResult
)> SwapCompletionCallback
;
78 // Swaps front and back buffers. This has no effect for off-screen
79 // contexts. On some platforms, we want to send SwapBufferAck only after the
80 // surface is displayed on screen. The callback can be used to delay sending
81 // SwapBufferAck till that data is available. The callback should be run on
82 // the calling thread (i.e. same thread SwapBuffersAsync is called)
83 virtual bool SwapBuffersAsync(const SwapCompletionCallback
& callback
);
85 // Copy part of the backbuffer to the frontbuffer.
86 virtual gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
);
88 // Copy part of the backbuffer to the frontbuffer. On some platforms, we want
89 // to send SwapBufferAck only after the surface is displayed on screen. The
90 // callback can be used to delay sending SwapBufferAck till that data is
91 // available. The callback should be run on the calling thread (i.e. same
92 // thread PostSubBufferAsync is called)
93 virtual bool PostSubBufferAsync(int x
,
97 const SwapCompletionCallback
& callback
);
99 // Initialize GL bindings.
100 static bool InitializeOneOff();
102 // Called after a context is made current with this surface. Returns false
104 virtual bool OnMakeCurrent(GLContext
* context
);
106 // Called when the surface is bound as the current framebuffer for the
108 virtual void NotifyWasBound();
110 // Used for explicit buffer management.
111 virtual bool SetBackbufferAllocation(bool allocated
);
112 virtual void SetFrontbufferAllocation(bool allocated
);
114 // Get a handle used to share the surface with another process. Returns null
115 // if this is not possible.
116 virtual void* GetShareHandle();
118 // Get the platform specific display on which this surface resides, if
120 virtual void* GetDisplay();
122 // Get the platfrom specific configuration for this surface, if available.
123 virtual void* GetConfig();
125 // Get the GL pixel format of the surface, if available.
126 virtual unsigned GetFormat();
128 // Get access to a helper providing time of recent refresh and period
129 // of screen refresh. If unavailable, returns NULL.
130 virtual VSyncProvider
* GetVSyncProvider();
132 // Schedule an overlay plane to be shown at swap time.
133 // |z_order| specifies the stacking order of the plane relative to the
134 // main framebuffer located at index 0. For the case where there is no
135 // main framebuffer, overlays may be scheduled at 0, taking its place.
136 // |transform| specifies how the buffer is to be transformed during
138 // |image| to be presented by the overlay.
139 // |bounds_rect| specify where it is supposed to be on the screen in pixels.
140 // |crop_rect| specifies the region within the buffer to be placed inside
142 virtual bool ScheduleOverlayPlane(int z_order
,
143 OverlayTransform transform
,
145 const Rect
& bounds_rect
,
146 const RectF
& crop_rect
);
148 virtual bool IsSurfaceless() const;
150 // Create a GL surface that renders directly to a view.
151 static scoped_refptr
<GLSurface
> CreateViewGLSurface(
152 gfx::AcceleratedWidget window
);
154 #if defined(USE_OZONE)
155 // Create a GL surface that renders directly into a window with surfaceless
156 // semantics - there is no default framebuffer and the primary surface must
157 // be presented as an overlay. If surfaceless mode is not supported or
158 // enabled it will return a null pointer.
159 static scoped_refptr
<GLSurface
> CreateSurfacelessViewGLSurface(
160 gfx::AcceleratedWidget window
);
161 #endif // defined(USE_OZONE)
163 // Create a GL surface used for offscreen rendering.
164 static scoped_refptr
<GLSurface
> CreateOffscreenGLSurface(
165 const gfx::Size
& size
);
167 static GLSurface
* GetCurrent();
169 // Called when the swap interval for the associated context changes.
170 virtual void OnSetSwapInterval(int interval
);
173 virtual ~GLSurface();
174 static bool InitializeOneOffImplementation(GLImplementation impl
,
175 bool fallback_to_osmesa
,
176 bool gpu_service_logging
,
177 bool disable_gl_drawing
);
178 static bool InitializeOneOffInternal();
179 static void SetCurrent(GLSurface
* surface
);
181 static bool ExtensionsContain(const char* extensions
, const char* name
);
184 friend class base::RefCounted
<GLSurface
>;
185 friend class GLContext
;
186 friend class GLSurfaceTestSupport
;
188 DISALLOW_COPY_AND_ASSIGN(GLSurface
);
191 // Implementation of GLSurface that forwards all calls through to another
193 class GL_EXPORT GLSurfaceAdapter
: public GLSurface
{
195 explicit GLSurfaceAdapter(GLSurface
* surface
);
197 bool Initialize() override
;
198 void Destroy() override
;
199 bool Resize(const gfx::Size
& size
) override
;
200 bool Recreate() override
;
201 bool DeferDraws() override
;
202 bool IsOffscreen() override
;
203 gfx::SwapResult
SwapBuffers() override
;
204 bool SwapBuffersAsync(const SwapCompletionCallback
& callback
) override
;
205 gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
) override
;
206 bool PostSubBufferAsync(int x
,
210 const SwapCompletionCallback
& callback
) override
;
211 bool SupportsPostSubBuffer() override
;
212 gfx::Size
GetSize() override
;
213 void* GetHandle() override
;
214 unsigned int GetBackingFrameBufferObject() override
;
215 bool OnMakeCurrent(GLContext
* context
) override
;
216 bool SetBackbufferAllocation(bool allocated
) override
;
217 void SetFrontbufferAllocation(bool allocated
) override
;
218 void* GetShareHandle() override
;
219 void* GetDisplay() override
;
220 void* GetConfig() override
;
221 unsigned GetFormat() override
;
222 VSyncProvider
* GetVSyncProvider() override
;
223 bool ScheduleOverlayPlane(int z_order
,
224 OverlayTransform transform
,
226 const Rect
& bounds_rect
,
227 const RectF
& crop_rect
) override
;
228 bool IsSurfaceless() const override
;
230 GLSurface
* surface() const { return surface_
.get(); }
233 ~GLSurfaceAdapter() override
;
236 scoped_refptr
<GLSurface
> surface_
;
238 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter
);
243 #endif // UI_GL_GL_SURFACE_H_