Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ui / gl / gl_surface.h
blob70336ba6df89845717bd9c2957802168f91b36e4
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/size.h"
14 #include "ui/gl/gl_export.h"
15 #include "ui/gl/gl_implementation.h"
17 namespace gfx {
19 class GLContext;
20 class VSyncProvider;
22 // Encapsulates a surface that can be rendered to with GL, hiding platform
23 // specific management.
24 class GL_EXPORT GLSurface : public base::RefCounted<GLSurface> {
25 public:
26 GLSurface();
28 // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
29 // EGL surface associated to be recreated without destroying the associated
30 // context. The implementation of this function for other GLSurface derived
31 // classes is in a pending changelist.
32 virtual bool Initialize();
34 // Destroys the surface.
35 virtual void Destroy() = 0;
37 virtual bool Resize(const gfx::Size& size);
39 // Recreate the surface without changing the size.
40 virtual bool Recreate();
42 // Unschedule the GpuScheduler and return true to abort the processing of
43 // a GL draw call to this surface and defer it until the GpuScheduler is
44 // rescheduled.
45 virtual bool DeferDraws();
47 // Returns true if this surface is offscreen.
48 virtual bool IsOffscreen() = 0;
50 // Swaps front and back buffers. This has no effect for off-screen
51 // contexts.
52 virtual bool SwapBuffers() = 0;
54 // Get the size of the surface.
55 virtual gfx::Size GetSize() = 0;
57 // Get the underlying platform specific surface "handle".
58 virtual void* GetHandle() = 0;
60 // Returns whether or not the surface supports PostSubBuffer.
61 virtual bool SupportsPostSubBuffer();
63 // Returns the internal frame buffer object name if the surface is backed by
64 // FBO. Otherwise returns 0.
65 virtual unsigned int GetBackingFrameBufferObject();
67 // Copy part of the backbuffer to the frontbuffer.
68 virtual bool PostSubBuffer(int x, int y, int width, int height);
70 // Initialize GL bindings.
71 static bool InitializeOneOff();
73 // Unit tests should call these instead of InitializeOneOff() to set up
74 // GL bindings appropriate for tests.
75 static void InitializeOneOffForTests();
76 static void InitializeOneOffWithMockBindingsForTests();
77 static void InitializeDynamicMockBindingsForTests(GLContext* context);
79 // Called after a context is made current with this surface. Returns false
80 // on error.
81 virtual bool OnMakeCurrent(GLContext* context);
83 // Used for explicit buffer management.
84 virtual bool SetBackbufferAllocation(bool allocated);
85 virtual void SetFrontbufferAllocation(bool allocated);
87 // Get a handle used to share the surface with another process. Returns null
88 // if this is not possible.
89 virtual void* GetShareHandle();
91 // Get the platform specific display on which this surface resides, if
92 // available.
93 virtual void* GetDisplay();
95 // Get the platfrom specific configuration for this surface, if available.
96 virtual void* GetConfig();
98 // Get the GL pixel format of the surface, if available.
99 virtual unsigned GetFormat();
101 // Get access to a helper providing time of recent refresh and period
102 // of screen refresh. If unavailable, returns NULL.
103 virtual VSyncProvider* GetVSyncProvider();
105 // Create a GL surface that renders directly to a view.
106 static scoped_refptr<GLSurface> CreateViewGLSurface(
107 gfx::AcceleratedWidget window);
109 // Create a GL surface used for offscreen rendering.
110 static scoped_refptr<GLSurface> CreateOffscreenGLSurface(
111 const gfx::Size& size);
113 static GLSurface* GetCurrent();
115 protected:
116 virtual ~GLSurface();
117 static bool InitializeOneOffImplementation(GLImplementation impl,
118 bool fallback_to_osmesa,
119 bool gpu_service_logging,
120 bool disable_gl_drawing);
121 static bool InitializeOneOffInternal();
122 static void SetCurrent(GLSurface* surface);
124 static bool ExtensionsContain(const char* extensions, const char* name);
126 private:
127 friend class base::RefCounted<GLSurface>;
128 friend class GLContext;
130 DISALLOW_COPY_AND_ASSIGN(GLSurface);
133 // Implementation of GLSurface that forwards all calls through to another
134 // GLSurface.
135 class GL_EXPORT GLSurfaceAdapter : public GLSurface {
136 public:
137 explicit GLSurfaceAdapter(GLSurface* surface);
139 virtual bool Initialize() OVERRIDE;
140 virtual void Destroy() OVERRIDE;
141 virtual bool Resize(const gfx::Size& size) OVERRIDE;
142 virtual bool Recreate() OVERRIDE;
143 virtual bool DeferDraws() OVERRIDE;
144 virtual bool IsOffscreen() OVERRIDE;
145 virtual bool SwapBuffers() OVERRIDE;
146 virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
147 virtual bool SupportsPostSubBuffer() OVERRIDE;
148 virtual gfx::Size GetSize() OVERRIDE;
149 virtual void* GetHandle() OVERRIDE;
150 virtual unsigned int GetBackingFrameBufferObject() OVERRIDE;
151 virtual bool OnMakeCurrent(GLContext* context) OVERRIDE;
152 virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE;
153 virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE;
154 virtual void* GetShareHandle() OVERRIDE;
155 virtual void* GetDisplay() OVERRIDE;
156 virtual void* GetConfig() OVERRIDE;
157 virtual unsigned GetFormat() OVERRIDE;
158 virtual VSyncProvider* GetVSyncProvider() OVERRIDE;
160 GLSurface* surface() const { return surface_.get(); }
162 protected:
163 virtual ~GLSurfaceAdapter();
165 private:
166 scoped_refptr<GLSurface> surface_;
168 DISALLOW_COPY_AND_ASSIGN(GLSurfaceAdapter);
171 } // namespace gfx
173 #endif // UI_GL_GL_SURFACE_H_