Temporarily make lastchange use only the git hash.
[chromium-blink-merge.git] / ui / gl / gl_surface.h
blobed029add26c6992111dcb446ba6cfe2523deb3da
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/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"
20 namespace gfx {
22 class GLContext;
23 class GLImage;
24 class VSyncProvider;
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> {
29 public:
30 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
48 // rescheduled.
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
55 // contexts.
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
84 // on error.
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
96 // available.
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
114 // composition.
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
118 // |bounds_rect|.
119 virtual bool ScheduleOverlayPlane(int z_order,
120 OverlayTransform transform,
121 GLImage* image,
122 const Rect& bounds_rect,
123 const RectF& crop_rect);
125 // Create a GL surface that renders directly to a view.
126 static scoped_refptr<GLSurface> CreateViewGLSurface(
127 gfx::AcceleratedWidget window);
129 // Create a GL surface used for offscreen rendering.
130 static scoped_refptr<GLSurface> CreateOffscreenGLSurface(
131 const gfx::Size& size);
133 static GLSurface* GetCurrent();
135 protected:
136 virtual ~GLSurface();
137 static bool InitializeOneOffImplementation(GLImplementation impl,
138 bool fallback_to_osmesa,
139 bool gpu_service_logging,
140 bool disable_gl_drawing);
141 static bool InitializeOneOffInternal();
142 static void SetCurrent(GLSurface* surface);
144 static bool ExtensionsContain(const char* extensions, const char* name);
146 private:
147 friend class base::RefCounted<GLSurface>;
148 friend class GLContext;
150 DISALLOW_COPY_AND_ASSIGN(GLSurface);
153 // Implementation of GLSurface that forwards all calls through to another
154 // GLSurface.
155 class GL_EXPORT GLSurfaceAdapter : public GLSurface {
156 public:
157 explicit GLSurfaceAdapter(GLSurface* surface);
159 virtual bool Initialize() OVERRIDE;
160 virtual void Destroy() OVERRIDE;
161 virtual bool Resize(const gfx::Size& size) OVERRIDE;
162 virtual bool Recreate() OVERRIDE;
163 virtual bool DeferDraws() OVERRIDE;
164 virtual bool IsOffscreen() OVERRIDE;
165 virtual bool SwapBuffers() OVERRIDE;
166 virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
167 virtual bool SupportsPostSubBuffer() OVERRIDE;
168 virtual gfx::Size GetSize() OVERRIDE;
169 virtual void* GetHandle() OVERRIDE;
170 virtual unsigned int GetBackingFrameBufferObject() OVERRIDE;
171 virtual bool OnMakeCurrent(GLContext* context) OVERRIDE;
172 virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE;
173 virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE;
174 virtual void* GetShareHandle() OVERRIDE;
175 virtual void* GetDisplay() OVERRIDE;
176 virtual void* GetConfig() OVERRIDE;
177 virtual unsigned GetFormat() OVERRIDE;
178 virtual VSyncProvider* GetVSyncProvider() OVERRIDE;
179 virtual bool ScheduleOverlayPlane(int z_order,
180 OverlayTransform transform,
181 GLImage* image,
182 const Rect& bounds_rect,
183 const RectF& crop_rect) OVERRIDE;
185 GLSurface* surface() const { return surface_.get(); }
187 protected:
188 virtual ~GLSurfaceAdapter();
190 private:
191 scoped_refptr<GLSurface> surface_;
193 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter);
196 } // namespace gfx
198 #endif // UI_GL_GL_SURFACE_H_