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/memory/ref_counted.h"
11 #include "build/build_config.h"
12 #include "ui/gfx/native_widget_types.h"
13 #include "ui/gfx/overlay_transform.h"
14 #include "ui/gfx/rect.h"
15 #include "ui/gfx/rect_f.h"
16 #include "ui/gfx/size.h"
17 #include "ui/gl/gl_export.h"
18 #include "ui/gl/gl_implementation.h"
26 // Encapsulates a surface that can be rendered to with GL, hiding platform
27 // specific management.
28 class GL_EXPORT GLSurface
: public base::RefCounted
<GLSurface
> {
32 // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
33 // EGL surface associated to be recreated without destroying the associated
34 // context. The implementation of this function for other GLSurface derived
35 // classes is in a pending changelist.
36 virtual bool Initialize();
38 // Destroys the surface.
39 virtual void Destroy() = 0;
41 virtual bool Resize(const gfx::Size
& size
);
43 // Recreate the surface without changing the size.
44 virtual bool Recreate();
46 // Unschedule the GpuScheduler and return true to abort the processing of
47 // a GL draw call to this surface and defer it until the GpuScheduler is
49 virtual bool DeferDraws();
51 // Returns true if this surface is offscreen.
52 virtual bool IsOffscreen() = 0;
54 // Swaps front and back buffers. This has no effect for off-screen
56 virtual bool SwapBuffers() = 0;
58 // Get the size of the surface.
59 virtual gfx::Size
GetSize() = 0;
61 // Get the underlying platform specific surface "handle".
62 virtual void* GetHandle() = 0;
64 // Returns whether or not the surface supports PostSubBuffer.
65 virtual bool SupportsPostSubBuffer();
67 // Returns the internal frame buffer object name if the surface is backed by
68 // FBO. Otherwise returns 0.
69 virtual unsigned int GetBackingFrameBufferObject();
71 // Copy part of the backbuffer to the frontbuffer.
72 virtual bool PostSubBuffer(int x
, int y
, int width
, int height
);
74 // Initialize GL bindings.
75 static bool InitializeOneOff();
77 // Unit tests should call these instead of InitializeOneOff() to set up
78 // GL bindings appropriate for tests.
79 static void InitializeOneOffForTests();
80 static void InitializeOneOffWithMockBindingsForTests();
81 static void InitializeDynamicMockBindingsForTests(GLContext
* context
);
83 // Called after a context is made current with this surface. Returns false
85 virtual bool OnMakeCurrent(GLContext
* context
);
87 // Used for explicit buffer management.
88 virtual bool SetBackbufferAllocation(bool allocated
);
89 virtual void SetFrontbufferAllocation(bool allocated
);
91 // Get a handle used to share the surface with another process. Returns null
92 // if this is not possible.
93 virtual void* GetShareHandle();
95 // Get the platform specific display on which this surface resides, if
97 virtual void* GetDisplay();
99 // Get the platfrom specific configuration for this surface, if available.
100 virtual void* GetConfig();
102 // Get the GL pixel format of the surface, if available.
103 virtual unsigned GetFormat();
105 // Get access to a helper providing time of recent refresh and period
106 // of screen refresh. If unavailable, returns NULL.
107 virtual VSyncProvider
* GetVSyncProvider();
109 // Schedule an overlay plane to be shown at swap time.
110 // |z_order| specifies the stacking order of the plane relative to the
111 // main framebuffer located at index 0. For the case where there is no
112 // main framebuffer, overlays may be scheduled at 0, taking its place.
113 // |transform| specifies how the buffer is to be transformed during
115 // |image| to be presented by the overlay.
116 // |bounds_rect| specify where it is supposed to be on the screen in pixels.
117 // |crop_rect| specifies the region within the buffer to be placed inside
119 virtual bool ScheduleOverlayPlane(int z_order
,
120 OverlayTransform transform
,
122 const Rect
& bounds_rect
,
123 const RectF
& crop_rect
);
125 virtual bool IsSurfaceless() const;
127 // Create a GL surface that renders directly to a view.
128 static scoped_refptr
<GLSurface
> CreateViewGLSurface(
129 gfx::AcceleratedWidget window
);
131 // Create a GL surface used for offscreen rendering.
132 static scoped_refptr
<GLSurface
> CreateOffscreenGLSurface(
133 const gfx::Size
& size
);
135 static GLSurface
* GetCurrent();
138 virtual ~GLSurface();
139 static bool InitializeOneOffImplementation(GLImplementation impl
,
140 bool fallback_to_osmesa
,
141 bool gpu_service_logging
,
142 bool disable_gl_drawing
);
143 static bool InitializeOneOffInternal();
144 static void SetCurrent(GLSurface
* surface
);
146 static bool ExtensionsContain(const char* extensions
, const char* name
);
149 friend class base::RefCounted
<GLSurface
>;
150 friend class GLContext
;
152 DISALLOW_COPY_AND_ASSIGN(GLSurface
);
155 // Implementation of GLSurface that forwards all calls through to another
157 class GL_EXPORT GLSurfaceAdapter
: public GLSurface
{
159 explicit GLSurfaceAdapter(GLSurface
* surface
);
161 bool Initialize() override
;
162 void Destroy() override
;
163 bool Resize(const gfx::Size
& size
) override
;
164 bool Recreate() override
;
165 bool DeferDraws() override
;
166 bool IsOffscreen() override
;
167 bool SwapBuffers() override
;
168 bool PostSubBuffer(int x
, int y
, int width
, int height
) override
;
169 bool SupportsPostSubBuffer() override
;
170 gfx::Size
GetSize() override
;
171 void* GetHandle() override
;
172 unsigned int GetBackingFrameBufferObject() override
;
173 bool OnMakeCurrent(GLContext
* context
) override
;
174 bool SetBackbufferAllocation(bool allocated
) override
;
175 void SetFrontbufferAllocation(bool allocated
) override
;
176 void* GetShareHandle() override
;
177 void* GetDisplay() override
;
178 void* GetConfig() override
;
179 unsigned GetFormat() override
;
180 VSyncProvider
* GetVSyncProvider() override
;
181 bool ScheduleOverlayPlane(int z_order
,
182 OverlayTransform transform
,
184 const Rect
& bounds_rect
,
185 const RectF
& crop_rect
) override
;
186 bool IsSurfaceless() const override
;
188 GLSurface
* surface() const { return surface_
.get(); }
191 ~GLSurfaceAdapter() override
;
194 scoped_refptr
<GLSurface
> surface_
;
196 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter
);
201 #endif // UI_GL_GL_SURFACE_H_