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 virtual bool Resize(const gfx::Size
& size
);
45 // Recreate the surface without changing the size.
46 virtual bool Recreate();
48 // Unschedule the GpuScheduler and return true to abort the processing of
49 // a GL draw call to this surface and defer it until the GpuScheduler is
51 virtual bool DeferDraws();
53 // Returns true if this surface is offscreen.
54 virtual bool IsOffscreen() = 0;
56 // Swaps front and back buffers. This has no effect for off-screen
58 virtual gfx::SwapResult
SwapBuffers() = 0;
60 // Get the size of the surface.
61 virtual gfx::Size
GetSize() = 0;
63 // Get the underlying platform specific surface "handle".
64 virtual void* GetHandle() = 0;
66 // Returns whether or not the surface supports PostSubBuffer.
67 virtual bool SupportsPostSubBuffer();
69 // Returns the internal frame buffer object name if the surface is backed by
70 // FBO. Otherwise returns 0.
71 virtual unsigned int GetBackingFrameBufferObject();
73 typedef base::Callback
<void(SwapResult
)> SwapCompletionCallback
;
74 // Swaps front and back buffers. This has no effect for off-screen
75 // contexts. On some platforms, we want to send SwapBufferAck only after the
76 // surface is displayed on screen. The callback can be used to delay sending
77 // SwapBufferAck till that data is available. The callback should be run on
78 // the calling thread (i.e. same thread SwapBuffersAsync is called)
79 virtual bool SwapBuffersAsync(const SwapCompletionCallback
& callback
);
81 // Copy part of the backbuffer to the frontbuffer.
82 virtual gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
);
84 // Copy part of the backbuffer to the frontbuffer. On some platforms, we want
85 // to send SwapBufferAck only after the surface is displayed on screen. The
86 // callback can be used to delay sending SwapBufferAck till that data is
87 // available. The callback should be run on the calling thread (i.e. same
88 // thread PostSubBufferAsync is called)
89 virtual bool PostSubBufferAsync(int x
,
93 const SwapCompletionCallback
& callback
);
95 // Initialize GL bindings.
96 static bool InitializeOneOff();
98 // Called after a context is made current with this surface. Returns false
100 virtual bool OnMakeCurrent(GLContext
* context
);
102 // Called when the surface is bound as the current framebuffer for the
104 virtual void NotifyWasBound();
106 // Used for explicit buffer management.
107 virtual bool SetBackbufferAllocation(bool allocated
);
108 virtual void SetFrontbufferAllocation(bool allocated
);
110 // Get a handle used to share the surface with another process. Returns null
111 // if this is not possible.
112 virtual void* GetShareHandle();
114 // Get the platform specific display on which this surface resides, if
116 virtual void* GetDisplay();
118 // Get the platfrom specific configuration for this surface, if available.
119 virtual void* GetConfig();
121 // Get the GL pixel format of the surface, if available.
122 virtual unsigned GetFormat();
124 // Get access to a helper providing time of recent refresh and period
125 // of screen refresh. If unavailable, returns NULL.
126 virtual VSyncProvider
* GetVSyncProvider();
128 // Schedule an overlay plane to be shown at swap time.
129 // |z_order| specifies the stacking order of the plane relative to the
130 // main framebuffer located at index 0. For the case where there is no
131 // main framebuffer, overlays may be scheduled at 0, taking its place.
132 // |transform| specifies how the buffer is to be transformed during
134 // |image| to be presented by the overlay.
135 // |bounds_rect| specify where it is supposed to be on the screen in pixels.
136 // |crop_rect| specifies the region within the buffer to be placed inside
138 virtual bool ScheduleOverlayPlane(int z_order
,
139 OverlayTransform transform
,
141 const Rect
& bounds_rect
,
142 const RectF
& crop_rect
);
144 virtual bool IsSurfaceless() const;
146 // Create a GL surface that renders directly to a view.
147 static scoped_refptr
<GLSurface
> CreateViewGLSurface(
148 gfx::AcceleratedWidget window
);
150 #if defined(USE_OZONE)
151 // Create a GL surface that renders directly into a window with surfaceless
152 // semantics - there is no default framebuffer and the primary surface must
153 // be presented as an overlay. If surfaceless mode is not supported or
154 // enabled it will return a null pointer.
155 static scoped_refptr
<GLSurface
> CreateSurfacelessViewGLSurface(
156 gfx::AcceleratedWidget window
);
157 #endif // defined(USE_OZONE)
159 // Create a GL surface used for offscreen rendering.
160 static scoped_refptr
<GLSurface
> CreateOffscreenGLSurface(
161 const gfx::Size
& size
);
163 static GLSurface
* GetCurrent();
165 // Called when the swap interval for the associated context changes.
166 virtual void OnSetSwapInterval(int interval
);
169 virtual ~GLSurface();
170 static bool InitializeOneOffImplementation(GLImplementation impl
,
171 bool fallback_to_osmesa
,
172 bool gpu_service_logging
,
173 bool disable_gl_drawing
);
174 static bool InitializeOneOffInternal();
175 static void SetCurrent(GLSurface
* surface
);
177 static bool ExtensionsContain(const char* extensions
, const char* name
);
180 friend class base::RefCounted
<GLSurface
>;
181 friend class GLContext
;
182 friend class GLSurfaceTestSupport
;
184 DISALLOW_COPY_AND_ASSIGN(GLSurface
);
187 // Implementation of GLSurface that forwards all calls through to another
189 class GL_EXPORT GLSurfaceAdapter
: public GLSurface
{
191 explicit GLSurfaceAdapter(GLSurface
* surface
);
193 bool Initialize() override
;
194 void Destroy() override
;
195 bool Resize(const gfx::Size
& size
) override
;
196 bool Recreate() override
;
197 bool DeferDraws() override
;
198 bool IsOffscreen() override
;
199 gfx::SwapResult
SwapBuffers() override
;
200 bool SwapBuffersAsync(const SwapCompletionCallback
& callback
) override
;
201 gfx::SwapResult
PostSubBuffer(int x
, int y
, int width
, int height
) override
;
202 bool PostSubBufferAsync(int x
,
206 const SwapCompletionCallback
& callback
) override
;
207 bool SupportsPostSubBuffer() override
;
208 gfx::Size
GetSize() override
;
209 void* GetHandle() override
;
210 unsigned int GetBackingFrameBufferObject() override
;
211 bool OnMakeCurrent(GLContext
* context
) override
;
212 bool SetBackbufferAllocation(bool allocated
) override
;
213 void SetFrontbufferAllocation(bool allocated
) override
;
214 void* GetShareHandle() override
;
215 void* GetDisplay() override
;
216 void* GetConfig() override
;
217 unsigned GetFormat() override
;
218 VSyncProvider
* GetVSyncProvider() override
;
219 bool ScheduleOverlayPlane(int z_order
,
220 OverlayTransform transform
,
222 const Rect
& bounds_rect
,
223 const RectF
& crop_rect
) override
;
224 bool IsSurfaceless() const override
;
226 GLSurface
* surface() const { return surface_
.get(); }
229 ~GLSurfaceAdapter() override
;
232 scoped_refptr
<GLSurface
> surface_
;
234 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter
);
239 #endif // UI_GL_GL_SURFACE_H_