OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 // This API is consistent with other OpenGL setup APIs like window's WGL | |
6 // and pepper's PGL. This API is used to manage OpenGL RendererGLContexts in the | |
7 // Chrome renderer process in a way that is consistent with other platforms. | |
8 | |
9 #ifndef CONTENT_RENDERER_RENDERER_GL_CONTEXT_H_ | |
10 #define CONTENT_RENDERER_RENDERER_GL_CONTEXT_H_ | |
11 #pragma once | |
12 | |
13 #include "base/callback_old.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "build/build_config.h" | |
18 #include "ui/gfx/native_widget_types.h" | |
19 #include "ui/gfx/size.h" | |
20 | |
21 class GpuChannelHost; | |
22 class MessageLoop; | |
23 class CommandBufferProxy; | |
24 class GURL; | |
25 class TransportTextureHost; | |
26 | |
27 namespace gpu { | |
28 namespace gles2 { | |
29 class GLES2CmdHelper; | |
30 class GLES2Implementation; | |
31 } | |
32 } | |
33 | |
34 namespace media { | |
35 class VideoDecodeContext; | |
36 class VideoDecodeEngine; | |
37 class VideoDecodeRendererGLContext; | |
38 } | |
39 | |
40 class RendererGLContext : public base::SupportsWeakPtr<RendererGLContext> { | |
41 public: | |
42 // These are the same error codes as used by EGL. | |
43 enum Error { | |
44 SUCCESS = 0x3000, | |
45 NOT_INITIALIZED = 0x3001, | |
46 BAD_ATTRIBUTE = 0x3004, | |
47 BAD_RendererGLContext = 0x3006, | |
48 CONTEXT_LOST = 0x300E | |
49 }; | |
50 | |
51 // RendererGLContext configuration attributes. These are the same as used by | |
52 // EGL. Attributes are matched using a closest fit algorithm. | |
53 enum Attribute { | |
54 ALPHA_SIZE = 0x3021, | |
55 BLUE_SIZE = 0x3022, | |
56 GREEN_SIZE = 0x3023, | |
57 RED_SIZE = 0x3024, | |
58 DEPTH_SIZE = 0x3025, | |
59 STENCIL_SIZE = 0x3026, | |
60 SAMPLES = 0x3031, | |
61 SAMPLE_BUFFERS = 0x3032, | |
62 NONE = 0x3038 // Attrib list = terminator | |
63 }; | |
64 | |
65 // Initialize the library. This must have completed before any other | |
66 // functions are invoked. | |
67 static bool Initialize(); | |
68 | |
69 // Terminate the library. This must be called after any other functions | |
70 // have completed. | |
71 static bool Terminate(); | |
72 | |
73 ~RendererGLContext(); | |
74 | |
75 // Create a RendererGLContext that renders directly to a view. The view and | |
76 // the associated window must not be destroyed until the returned | |
77 // RendererGLContext has been destroyed, otherwise the GPU process might | |
78 // attempt to render to an invalid window handle. | |
79 // | |
80 // NOTE: on Mac OS X, this entry point is only used to set up the | |
81 // accelerated compositor's output. On this platform, we actually pass | |
82 // a gfx::PluginWindowHandle in place of the gfx::NativeViewId, | |
83 // because the facility to allocate a fake PluginWindowHandle is | |
84 // already in place. We could add more entry points and messages to | |
85 // allocate both fake PluginWindowHandles and NativeViewIds and map | |
86 // from fake NativeViewIds to PluginWindowHandles, but this seems like | |
87 // unnecessary complexity at the moment. | |
88 // | |
89 // The render_view_id is currently also only used on Mac OS X. | |
90 // TODO(kbr): clean up the arguments to this function and make them | |
91 // more cross-platform. | |
92 static RendererGLContext* CreateViewContext( | |
93 GpuChannelHost* channel, | |
94 gfx::PluginWindowHandle render_surface, | |
95 int render_view_id, | |
96 const char* allowed_extensions, | |
97 const int32* attrib_list, | |
98 const GURL& active_arl); | |
99 | |
100 #if defined(OS_MACOSX) | |
101 // On Mac OS X only, view RendererGLContexts actually behave like offscreen | |
102 // RendererGLContexts, and require an explicit resize operation which is | |
103 // slightly different from that of offscreen RendererGLContexts. | |
104 void ResizeOnscreen(const gfx::Size& size); | |
105 #endif | |
106 | |
107 // Create a RendererGLContext that renders to an offscreen frame buffer. If | |
108 // parent is not NULL, that RendererGLContext can access a copy of the created | |
109 // RendererGLContext's frame buffer that is updated every time SwapBuffers is | |
110 // called. It is not as general as shared RendererGLContexts in other | |
111 // implementations of OpenGL. If parent is not NULL, it must be used on the | |
112 // same thread as the parent. A child RendererGLContext may not outlive its | |
113 // parent. attrib_list must be NULL or a NONE-terminated list of | |
114 // attribute/value pairs. | |
115 static RendererGLContext* CreateOffscreenContext( | |
116 GpuChannelHost* channel, | |
117 RendererGLContext* parent, | |
118 const gfx::Size& size, | |
119 const char* allowed_extensions, | |
120 const int32* attrib_list, | |
121 const GURL& active_url); | |
122 | |
123 // Resize an offscreen frame buffer. The resize occurs on the next call to | |
124 // SwapBuffers. This is to avoid waiting until all pending GL calls have been | |
125 // executed by the GPU process. Everything rendered up to the call to | |
126 // SwapBuffers will be lost. A lost RendererGLContext will be reported if the | |
127 // resize fails. | |
128 void ResizeOffscreen(const gfx::Size& size); | |
129 | |
130 // For an offscreen frame buffer RendererGLContext, return the texture ID with | |
131 // respect to the parent RendererGLContext. Returns zero if RendererGLContext | |
132 // does not have a parent. | |
133 uint32 GetParentTextureId(); | |
134 | |
135 // Create a new texture in the parent's RendererGLContext. Returns zero if | |
136 // RendererGLContext does not have a parent. | |
137 uint32 CreateParentTexture(const gfx::Size& size); | |
138 | |
139 // Deletes a texture in the parent's RendererGLContext. | |
140 void DeleteParentTexture(uint32 texture); | |
141 | |
142 // Provides a callback that will be invoked when SwapBuffers has completed | |
143 // service side. | |
144 void SetSwapBuffersCallback(Callback0::Type* callback); | |
145 | |
146 void SetContextLostCallback(Callback0::Type* callback); | |
147 | |
148 // Set the current RendererGLContext for the calling thread. | |
149 static bool MakeCurrent(RendererGLContext* context); | |
150 | |
151 // For a view RendererGLContext, display everything that has been rendered | |
152 // since the last call. For an offscreen RendererGLContext, resolve everything | |
153 // that has been rendered since the last call to a copy that can be accessed | |
154 // by the parent RendererGLContext. | |
155 bool SwapBuffers(); | |
156 | |
157 // Create a hardware video decode engine corresponding to the | |
158 // RendererGLContext. | |
159 media::VideoDecodeEngine* CreateVideoDecodeEngine(); | |
160 | |
161 // Create a hardware video decode RendererGLContext to pair with the hardware | |
162 // video decode engine. It can also be used with a software decode engine. | |
163 // | |
164 // Set |hardware_decoder| to true if this RendererGLContext is for a hardware | |
165 // video engine. |message_loop| is where the decode RendererGLContext should | |
166 // run on. | |
167 media::VideoDecodeContext* CreateVideoDecodeContext(MessageLoop* message_loop, | |
168 bool hardware_decoder); | |
169 | |
170 // Create a TransportTextureHost object associated with the context. | |
171 scoped_refptr<TransportTextureHost> CreateTransportTextureHost(); | |
172 | |
173 // TODO(gman): Remove this | |
174 void DisableShaderTranslation(); | |
175 | |
176 // Allows direct access to the GLES2 implementation so a RendererGLContext | |
177 // can be used without making it current. | |
178 gpu::gles2::GLES2Implementation* GetImplementation(); | |
179 | |
180 // Return the current error. | |
181 Error GetError(); | |
182 | |
183 // Return true if GPU process reported RendererGLContext lost or there was a | |
184 // problem communicating with the GPU process. | |
185 bool IsCommandBufferContextLost(); | |
186 | |
187 CommandBufferProxy* GetCommandBufferProxy(); | |
188 | |
189 // Create a latch for synchronization between contexts using glSetLatch and | |
190 // glWaitLatch. | |
191 // CreateLatch will only fail if there is a generally unrecoverable | |
192 // error, in which case 0 is returned. Returns latch_id on success. | |
193 bool CreateLatch(uint32* ret_latch); | |
194 | |
195 // Destroy a latch. | |
196 bool DestroyLatch(uint32 latch); | |
197 | |
198 // All child contexts get a latch pair automatically. These latches are used | |
199 // for synchronization with parent context. If *this* context does not have a | |
200 // parent context, these methods will return false. | |
201 bool GetParentToChildLatch(uint32* parent_to_child_latch); | |
202 bool GetChildToParentLatch(uint32* child_to_parent_latch); | |
203 | |
204 private: | |
205 RendererGLContext(GpuChannelHost* channel, | |
206 RendererGLContext* parent); | |
207 | |
208 bool Initialize(bool onscreen, | |
209 gfx::PluginWindowHandle render_surface, | |
210 int render_view_id, | |
211 const gfx::Size& size, | |
212 const char* allowed_extensions, | |
213 const int32* attrib_list, | |
214 const GURL& active_url); | |
215 void Destroy(); | |
216 | |
217 void OnSwapBuffers(); | |
218 void OnContextLost(); | |
219 | |
220 scoped_refptr<GpuChannelHost> channel_; | |
221 base::WeakPtr<RendererGLContext> parent_; | |
222 scoped_ptr<Callback0::Type> swap_buffers_callback_; | |
223 scoped_ptr<Callback0::Type> context_lost_callback_; | |
224 uint32 parent_texture_id_; | |
225 uint32 child_to_parent_latch_; | |
226 uint32 parent_to_child_latch_; | |
227 int32 latch_transfer_buffer_id_; | |
228 CommandBufferProxy* command_buffer_; | |
229 gpu::gles2::GLES2CmdHelper* gles2_helper_; | |
230 int32 transfer_buffer_id_; | |
231 gpu::gles2::GLES2Implementation* gles2_implementation_; | |
232 gfx::Size size_; | |
233 Error last_error_; | |
234 int frame_number_; | |
235 | |
236 DISALLOW_COPY_AND_ASSIGN(RendererGLContext); | |
237 }; | |
238 | |
239 #endif // CONTENT_RENDERER_RENDERER_GL_CONTEXT_H_ | |
OLD | NEW |