Migrate TabUmaTest from ChromeShell to ChromePublic.
[chromium-blink-merge.git] / ui / gl / gl_surface.h
blob9fee0a37c2cc6d09b9ab6bd3253ea106c17c832a
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/gfx/swap_result.h"
19 #include "ui/gl/gl_export.h"
20 #include "ui/gl/gl_implementation.h"
22 namespace gfx {
24 class GLContext;
25 class GLImage;
26 class VSyncProvider;
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> {
31 public:
32 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
54 // rescheduled.
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
61 // contexts.
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,
94 int y,
95 int width,
96 int height,
97 const SwapCompletionCallback& callback);
99 // Initialize GL bindings.
100 static bool InitializeOneOff();
102 // Unit tests should call these instead of InitializeOneOff() to set up
103 // GL bindings appropriate for tests.
104 static void InitializeOneOffForTests();
105 static void InitializeOneOffWithMockBindingsForTests();
106 static void InitializeDynamicMockBindingsForTests(GLContext* context);
108 // Called after a context is made current with this surface. Returns false
109 // on error.
110 virtual bool OnMakeCurrent(GLContext* context);
112 // Called when the surface is bound as the current framebuffer for the
113 // current context.
114 virtual void NotifyWasBound();
116 // Used for explicit buffer management.
117 virtual bool SetBackbufferAllocation(bool allocated);
118 virtual void SetFrontbufferAllocation(bool allocated);
120 // Get a handle used to share the surface with another process. Returns null
121 // if this is not possible.
122 virtual void* GetShareHandle();
124 // Get the platform specific display on which this surface resides, if
125 // available.
126 virtual void* GetDisplay();
128 // Get the platfrom specific configuration for this surface, if available.
129 virtual void* GetConfig();
131 // Get the GL pixel format of the surface, if available.
132 virtual unsigned GetFormat();
134 // Get access to a helper providing time of recent refresh and period
135 // of screen refresh. If unavailable, returns NULL.
136 virtual VSyncProvider* GetVSyncProvider();
138 // Schedule an overlay plane to be shown at swap time.
139 // |z_order| specifies the stacking order of the plane relative to the
140 // main framebuffer located at index 0. For the case where there is no
141 // main framebuffer, overlays may be scheduled at 0, taking its place.
142 // |transform| specifies how the buffer is to be transformed during
143 // composition.
144 // |image| to be presented by the overlay.
145 // |bounds_rect| specify where it is supposed to be on the screen in pixels.
146 // |crop_rect| specifies the region within the buffer to be placed inside
147 // |bounds_rect|.
148 virtual bool ScheduleOverlayPlane(int z_order,
149 OverlayTransform transform,
150 GLImage* image,
151 const Rect& bounds_rect,
152 const RectF& crop_rect);
154 virtual bool IsSurfaceless() const;
156 // Create a GL surface that renders directly to a view.
157 static scoped_refptr<GLSurface> CreateViewGLSurface(
158 gfx::AcceleratedWidget window);
160 #if defined(USE_OZONE)
161 // Create a GL surface that renders directly into a window with surfaceless
162 // semantics - there is no default framebuffer and the primary surface must
163 // be presented as an overlay. If surfaceless mode is not supported or
164 // enabled it will return a null pointer.
165 static scoped_refptr<GLSurface> CreateSurfacelessViewGLSurface(
166 gfx::AcceleratedWidget window);
167 #endif // defined(USE_OZONE)
169 // Create a GL surface used for offscreen rendering.
170 static scoped_refptr<GLSurface> CreateOffscreenGLSurface(
171 const gfx::Size& size);
173 static GLSurface* GetCurrent();
175 // Called when the swap interval for the associated context changes.
176 virtual void OnSetSwapInterval(int interval);
178 protected:
179 virtual ~GLSurface();
180 static bool InitializeOneOffImplementation(GLImplementation impl,
181 bool fallback_to_osmesa,
182 bool gpu_service_logging,
183 bool disable_gl_drawing);
184 static bool InitializeOneOffInternal();
185 static void SetCurrent(GLSurface* surface);
187 static bool ExtensionsContain(const char* extensions, const char* name);
189 private:
190 friend class base::RefCounted<GLSurface>;
191 friend class GLContext;
193 DISALLOW_COPY_AND_ASSIGN(GLSurface);
196 // Implementation of GLSurface that forwards all calls through to another
197 // GLSurface.
198 class GL_EXPORT GLSurfaceAdapter : public GLSurface {
199 public:
200 explicit GLSurfaceAdapter(GLSurface* surface);
202 bool Initialize() override;
203 void Destroy() override;
204 bool Resize(const gfx::Size& size) override;
205 bool Recreate() override;
206 bool DeferDraws() override;
207 bool IsOffscreen() override;
208 gfx::SwapResult SwapBuffers() override;
209 bool SwapBuffersAsync(const SwapCompletionCallback& callback) override;
210 gfx::SwapResult PostSubBuffer(int x, int y, int width, int height) override;
211 bool PostSubBufferAsync(int x,
212 int y,
213 int width,
214 int height,
215 const SwapCompletionCallback& callback) override;
216 bool SupportsPostSubBuffer() override;
217 gfx::Size GetSize() override;
218 void* GetHandle() override;
219 unsigned int GetBackingFrameBufferObject() override;
220 bool OnMakeCurrent(GLContext* context) override;
221 bool SetBackbufferAllocation(bool allocated) override;
222 void SetFrontbufferAllocation(bool allocated) override;
223 void* GetShareHandle() override;
224 void* GetDisplay() override;
225 void* GetConfig() override;
226 unsigned GetFormat() override;
227 VSyncProvider* GetVSyncProvider() override;
228 bool ScheduleOverlayPlane(int z_order,
229 OverlayTransform transform,
230 GLImage* image,
231 const Rect& bounds_rect,
232 const RectF& crop_rect) override;
233 bool IsSurfaceless() const override;
235 GLSurface* surface() const { return surface_.get(); }
237 protected:
238 ~GLSurfaceAdapter() override;
240 private:
241 scoped_refptr<GLSurface> surface_;
243 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter);
246 } // namespace gfx
248 #endif // UI_GL_GL_SURFACE_H_