Roll src/third_party/WebKit 327fcc4:20c53c8 (svn 191269:191273)
[chromium-blink-merge.git] / ui / gl / gl_surface.h
blobb67ca69a63968758dd4e77024924d63015311856
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_
8 #include <string>
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/gl/gl_export.h"
19 #include "ui/gl/gl_implementation.h"
21 namespace gfx {
23 class GLContext;
24 class GLImage;
25 class VSyncProvider;
27 // Encapsulates a surface that can be rendered to with GL, hiding platform
28 // specific management.
29 class GL_EXPORT GLSurface : public base::RefCounted<GLSurface> {
30 public:
31 GLSurface();
33 // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
34 // EGL surface associated to be recreated without destroying the associated
35 // context. The implementation of this function for other GLSurface derived
36 // classes is in a pending changelist.
37 virtual bool Initialize();
39 // Destroys the surface.
40 virtual void Destroy() = 0;
42 // Destroys the surface and terminates its underlying display. This must be
43 // the last surface which uses the display.
44 virtual void DestroyAndTerminateDisplay();
46 virtual bool Resize(const gfx::Size& size);
48 // Recreate the surface without changing the size.
49 virtual bool Recreate();
51 // Unschedule the GpuScheduler and return true to abort the processing of
52 // a GL draw call to this surface and defer it until the GpuScheduler is
53 // rescheduled.
54 virtual bool DeferDraws();
56 // Returns true if this surface is offscreen.
57 virtual bool IsOffscreen() = 0;
59 // Swaps front and back buffers. This has no effect for off-screen
60 // contexts.
61 virtual bool SwapBuffers() = 0;
63 // Get the size of the surface.
64 virtual gfx::Size GetSize() = 0;
66 // Get the underlying platform specific surface "handle".
67 virtual void* GetHandle() = 0;
69 // Returns whether or not the surface supports PostSubBuffer.
70 virtual bool SupportsPostSubBuffer();
72 // Returns the internal frame buffer object name if the surface is backed by
73 // FBO. Otherwise returns 0.
74 virtual unsigned int GetBackingFrameBufferObject();
76 typedef base::Callback<void()> SwapCompletionCallback;
77 // Swaps front and back buffers. This has no effect for off-screen
78 // contexts. On some platforms, we want to send SwapBufferAck only after the
79 // surface is displayed on screen. The callback can be used to delay sending
80 // SwapBufferAck till that data is available. The callback should be run on
81 // the calling thread (i.e. same thread SwapBuffersAsync is called)
82 virtual bool SwapBuffersAsync(const SwapCompletionCallback& callback);
84 // Copy part of the backbuffer to the frontbuffer.
85 virtual bool PostSubBuffer(int x, int y, int width, int height);
87 // Copy part of the backbuffer to the frontbuffer. On some platforms, we want
88 // to send SwapBufferAck only after the surface is displayed on screen. The
89 // callback can be used to delay sending SwapBufferAck till that data is
90 // available. The callback should be run on the calling thread (i.e. same
91 // thread PostSubBufferAsync is called)
92 virtual bool PostSubBufferAsync(int x,
93 int y,
94 int width,
95 int height,
96 const SwapCompletionCallback& callback);
98 // Initialize GL bindings.
99 static bool InitializeOneOff();
101 // Unit tests should call these instead of InitializeOneOff() to set up
102 // GL bindings appropriate for tests.
103 static void InitializeOneOffForTests();
104 static void InitializeOneOffWithMockBindingsForTests();
105 static void InitializeDynamicMockBindingsForTests(GLContext* context);
107 // Called after a context is made current with this surface. Returns false
108 // on error.
109 virtual bool OnMakeCurrent(GLContext* context);
111 // Called when the surface is bound as the current framebuffer for the
112 // current context.
113 virtual void NotifyWasBound();
115 // Used for explicit buffer management.
116 virtual bool SetBackbufferAllocation(bool allocated);
117 virtual void SetFrontbufferAllocation(bool allocated);
119 // Get a handle used to share the surface with another process. Returns null
120 // if this is not possible.
121 virtual void* GetShareHandle();
123 // Get the platform specific display on which this surface resides, if
124 // available.
125 virtual void* GetDisplay();
127 // Get the platfrom specific configuration for this surface, if available.
128 virtual void* GetConfig();
130 // Get the GL pixel format of the surface, if available.
131 virtual unsigned GetFormat();
133 // Get access to a helper providing time of recent refresh and period
134 // of screen refresh. If unavailable, returns NULL.
135 virtual VSyncProvider* GetVSyncProvider();
137 // Schedule an overlay plane to be shown at swap time.
138 // |z_order| specifies the stacking order of the plane relative to the
139 // main framebuffer located at index 0. For the case where there is no
140 // main framebuffer, overlays may be scheduled at 0, taking its place.
141 // |transform| specifies how the buffer is to be transformed during
142 // composition.
143 // |image| to be presented by the overlay.
144 // |bounds_rect| specify where it is supposed to be on the screen in pixels.
145 // |crop_rect| specifies the region within the buffer to be placed inside
146 // |bounds_rect|.
147 virtual bool ScheduleOverlayPlane(int z_order,
148 OverlayTransform transform,
149 GLImage* image,
150 const Rect& bounds_rect,
151 const RectF& crop_rect);
153 virtual bool IsSurfaceless() const;
155 // Create a GL surface that renders directly to a view.
156 static scoped_refptr<GLSurface> CreateViewGLSurface(
157 gfx::AcceleratedWidget window);
159 #if defined(USE_OZONE)
160 // Create a GL surface that renders directly into a window with surfaceless
161 // semantics - there is no default framebuffer and the primary surface must
162 // be presented as an overlay. If surfaceless mode is not supported or
163 // enabled it will return a null pointer.
164 static scoped_refptr<GLSurface> CreateSurfacelessViewGLSurface(
165 gfx::AcceleratedWidget window);
166 #endif // defined(USE_OZONE)
168 // Create a GL surface used for offscreen rendering.
169 static scoped_refptr<GLSurface> CreateOffscreenGLSurface(
170 const gfx::Size& size);
172 static GLSurface* GetCurrent();
174 // Called when the swap interval for the associated context changes.
175 virtual void OnSetSwapInterval(int interval);
177 protected:
178 virtual ~GLSurface();
179 static bool InitializeOneOffImplementation(GLImplementation impl,
180 bool fallback_to_osmesa,
181 bool gpu_service_logging,
182 bool disable_gl_drawing);
183 static bool InitializeOneOffInternal();
184 static void SetCurrent(GLSurface* surface);
186 static bool ExtensionsContain(const char* extensions, const char* name);
188 private:
189 friend class base::RefCounted<GLSurface>;
190 friend class GLContext;
192 DISALLOW_COPY_AND_ASSIGN(GLSurface);
195 // Implementation of GLSurface that forwards all calls through to another
196 // GLSurface.
197 class GL_EXPORT GLSurfaceAdapter : public GLSurface {
198 public:
199 explicit GLSurfaceAdapter(GLSurface* surface);
201 bool Initialize() override;
202 void Destroy() override;
203 bool Resize(const gfx::Size& size) override;
204 bool Recreate() override;
205 bool DeferDraws() override;
206 bool IsOffscreen() override;
207 bool SwapBuffers() override;
208 bool SwapBuffersAsync(const SwapCompletionCallback& callback) override;
209 bool PostSubBuffer(int x, int y, int width, int height) override;
210 bool PostSubBufferAsync(int x,
211 int y,
212 int width,
213 int height,
214 const SwapCompletionCallback& callback) override;
215 bool SupportsPostSubBuffer() override;
216 gfx::Size GetSize() override;
217 void* GetHandle() override;
218 unsigned int GetBackingFrameBufferObject() override;
219 bool OnMakeCurrent(GLContext* context) override;
220 bool SetBackbufferAllocation(bool allocated) override;
221 void SetFrontbufferAllocation(bool allocated) override;
222 void* GetShareHandle() override;
223 void* GetDisplay() override;
224 void* GetConfig() override;
225 unsigned GetFormat() override;
226 VSyncProvider* GetVSyncProvider() override;
227 bool ScheduleOverlayPlane(int z_order,
228 OverlayTransform transform,
229 GLImage* image,
230 const Rect& bounds_rect,
231 const RectF& crop_rect) override;
232 bool IsSurfaceless() const override;
234 GLSurface* surface() const { return surface_.get(); }
236 protected:
237 ~GLSurfaceAdapter() override;
239 private:
240 scoped_refptr<GLSurface> surface_;
242 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter);
245 } // namespace gfx
247 #endif // UI_GL_GL_SURFACE_H_