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/size.h"
14 #include "ui/gl/gl_export.h"
21 // Encapsulates a surface that can be rendered to with GL, hiding platform
22 // specific management.
23 class GL_EXPORT GLSurface
: public base::RefCounted
<GLSurface
> {
27 // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
28 // EGL surface associated to be recreated without destroying the associated
29 // context. The implementation of this function for other GLSurface derived
30 // classes is in a pending changelist.
31 virtual bool Initialize();
33 // Destroys the surface.
34 virtual void Destroy() = 0;
36 virtual bool Resize(const gfx::Size
& size
);
38 // Recreate the surface without changing the size.
39 virtual bool Recreate();
41 // Unschedule the GpuScheduler and return true to abort the processing of
42 // a GL draw call to this surface and defer it until the GpuScheduler is
44 virtual bool DeferDraws();
46 // Returns true if this surface is offscreen.
47 virtual bool IsOffscreen() = 0;
49 // Swaps front and back buffers. This has no effect for off-screen
51 virtual bool SwapBuffers() = 0;
53 // Get the size of the surface.
54 virtual gfx::Size
GetSize() = 0;
56 // Get the underlying platform specific surface "handle".
57 virtual void* GetHandle() = 0;
59 // Returns space separated list of surface specific extensions.
60 // The surface must be current.
61 virtual std::string
GetExtensions();
63 bool HasExtension(const char* name
);
65 // Returns the internal frame buffer object name if the surface is backed by
66 // FBO. Otherwise returns 0.
67 virtual unsigned int GetBackingFrameBufferObject();
69 // Copy part of the backbuffer to the frontbuffer.
70 virtual bool PostSubBuffer(int x
, int y
, int width
, int height
);
72 static bool InitializeOneOff();
74 // Called after a context is made current with this surface. Returns false
76 virtual bool OnMakeCurrent(GLContext
* context
);
78 // Used for explicit buffer management.
79 virtual bool SetBackbufferAllocation(bool allocated
);
80 virtual void SetFrontbufferAllocation(bool allocated
);
82 // Get a handle used to share the surface with another process. Returns null
83 // if this is not possible.
84 virtual void* GetShareHandle();
86 // Get the platform specific display on which this surface resides, if
88 virtual void* GetDisplay();
90 // Get the platfrom specific configuration for this surface, if available.
91 virtual void* GetConfig();
93 // Get the GL pixel format of the surface, if available.
94 virtual unsigned GetFormat();
96 // Get access to a helper providing time of recent refresh and period
97 // of screen refresh. If unavailable, returns NULL.
98 virtual VSyncProvider
* GetVSyncProvider();
100 // Create a GL surface that renders directly to a view.
101 static scoped_refptr
<GLSurface
> CreateViewGLSurface(
102 gfx::AcceleratedWidget window
);
104 // Create a GL surface used for offscreen rendering.
105 static scoped_refptr
<GLSurface
> CreateOffscreenGLSurface(
106 const gfx::Size
& size
);
108 static GLSurface
* GetCurrent();
111 virtual ~GLSurface();
112 static bool InitializeOneOffInternal();
113 static void SetCurrent(GLSurface
* surface
);
115 static bool ExtensionsContain(const char* extensions
, const char* name
);
118 friend class base::RefCounted
<GLSurface
>;
119 friend class GLContext
;
121 DISALLOW_COPY_AND_ASSIGN(GLSurface
);
124 // Implementation of GLSurface that forwards all calls through to another
126 class GL_EXPORT GLSurfaceAdapter
: public GLSurface
{
128 explicit GLSurfaceAdapter(GLSurface
* surface
);
130 virtual bool Initialize() OVERRIDE
;
131 virtual void Destroy() OVERRIDE
;
132 virtual bool Resize(const gfx::Size
& size
) OVERRIDE
;
133 virtual bool Recreate() OVERRIDE
;
134 virtual bool DeferDraws() OVERRIDE
;
135 virtual bool IsOffscreen() OVERRIDE
;
136 virtual bool SwapBuffers() OVERRIDE
;
137 virtual bool PostSubBuffer(int x
, int y
, int width
, int height
) OVERRIDE
;
138 virtual std::string
GetExtensions() OVERRIDE
;
139 virtual gfx::Size
GetSize() OVERRIDE
;
140 virtual void* GetHandle() OVERRIDE
;
141 virtual unsigned int GetBackingFrameBufferObject() OVERRIDE
;
142 virtual bool OnMakeCurrent(GLContext
* context
) OVERRIDE
;
143 virtual bool SetBackbufferAllocation(bool allocated
) OVERRIDE
;
144 virtual void SetFrontbufferAllocation(bool allocated
) OVERRIDE
;
145 virtual void* GetShareHandle() OVERRIDE
;
146 virtual void* GetDisplay() OVERRIDE
;
147 virtual void* GetConfig() OVERRIDE
;
148 virtual unsigned GetFormat() OVERRIDE
;
149 virtual VSyncProvider
* GetVSyncProvider() OVERRIDE
;
151 GLSurface
* surface() const { return surface_
.get(); }
154 virtual ~GLSurfaceAdapter();
157 scoped_refptr
<GLSurface
> surface_
;
159 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter
);
164 #endif // UI_GL_GL_SURFACE_H_