| OLD | NEW |
| (Empty) |
| 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. | |
| 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_GPU_RENDERER_GL_CONTEXT_H_ | |
| 10 #define CONTENT_RENDERER_GPU_RENDERER_GL_CONTEXT_H_ | |
| 11 #pragma once | |
| 12 | |
| 13 #include "base/callback.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "base/threading/non_thread_safe.h" | |
| 18 #include "build/build_config.h" | |
| 19 #include "ui/gfx/gl/gpu_preference.h" | |
| 20 #include "ui/gfx/native_widget_types.h" | |
| 21 #include "ui/gfx/size.h" | |
| 22 | |
| 23 class GpuChannelHost; | |
| 24 class CommandBufferProxy; | |
| 25 class GURL; | |
| 26 | |
| 27 namespace gpu { | |
| 28 class TransferBuffer; | |
| 29 namespace gles2 { | |
| 30 class GLES2CmdHelper; | |
| 31 class GLES2Implementation; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 class RendererGLContext : public base::SupportsWeakPtr<RendererGLContext>, | |
| 36 public base::NonThreadSafe { | |
| 37 public: | |
| 38 // These are the same error codes as used by EGL. | |
| 39 enum Error { | |
| 40 SUCCESS = 0x3000, | |
| 41 NOT_INITIALIZED = 0x3001, | |
| 42 BAD_ATTRIBUTE = 0x3004, | |
| 43 BAD_RendererGLContext = 0x3006, | |
| 44 CONTEXT_LOST = 0x300E | |
| 45 }; | |
| 46 | |
| 47 // RendererGLContext configuration attributes. Those in the 16-bit range are | |
| 48 // the same as used by EGL. Those outside the 16-bit range are unique to | |
| 49 // Chromium. Attributes are matched using a closest fit algorithm. | |
| 50 enum Attribute { | |
| 51 ALPHA_SIZE = 0x3021, | |
| 52 BLUE_SIZE = 0x3022, | |
| 53 GREEN_SIZE = 0x3023, | |
| 54 RED_SIZE = 0x3024, | |
| 55 DEPTH_SIZE = 0x3025, | |
| 56 STENCIL_SIZE = 0x3026, | |
| 57 SAMPLES = 0x3031, | |
| 58 SAMPLE_BUFFERS = 0x3032, | |
| 59 HEIGHT = 0x3056, | |
| 60 WIDTH = 0x3057, | |
| 61 NONE = 0x3038, // Attrib list = terminator | |
| 62 SHARE_RESOURCES = 0x10000, | |
| 63 BIND_GENERATES_RESOURCES = 0x10001 | |
| 64 }; | |
| 65 | |
| 66 // Reasons that a lost context might have been provoked. | |
| 67 enum ContextLostReason { | |
| 68 // This context definitely provoked the loss of context. | |
| 69 kGuilty, | |
| 70 | |
| 71 // This context definitely did not provoke the loss of context. | |
| 72 kInnocent, | |
| 73 | |
| 74 // It is unknown whether this context provoked the loss of context. | |
| 75 kUnknown | |
| 76 }; | |
| 77 | |
| 78 // Initialize the library. This must have completed before any other | |
| 79 // functions are invoked. | |
| 80 static bool Initialize(); | |
| 81 | |
| 82 // Terminate the library. This must be called after any other functions | |
| 83 // have completed. | |
| 84 static bool Terminate(); | |
| 85 | |
| 86 ~RendererGLContext(); | |
| 87 | |
| 88 // Create a RendererGLContext that renders directly to a view. The view and | |
| 89 // the associated window must not be destroyed until the returned | |
| 90 // RendererGLContext has been destroyed, otherwise the GPU process might | |
| 91 // attempt to render to an invalid window handle. | |
| 92 // | |
| 93 // NOTE: on Mac OS X, this entry point is only used to set up the | |
| 94 // accelerated compositor's output. On this platform, we actually pass | |
| 95 // a gfx::PluginWindowHandle in place of the gfx::NativeViewId, | |
| 96 // because the facility to allocate a fake PluginWindowHandle is | |
| 97 // already in place. We could add more entry points and messages to | |
| 98 // allocate both fake PluginWindowHandles and NativeViewIds and map | |
| 99 // from fake NativeViewIds to PluginWindowHandles, but this seems like | |
| 100 // unnecessary complexity at the moment. | |
| 101 // | |
| 102 // The render_view_id is currently also only used on Mac OS X. | |
| 103 // TODO(kbr): clean up the arguments to this function and make them | |
| 104 // more cross-platform. | |
| 105 static RendererGLContext* CreateViewContext( | |
| 106 GpuChannelHost* channel, | |
| 107 int32 surface_id, | |
| 108 RendererGLContext* share_group, | |
| 109 const char* allowed_extensions, | |
| 110 const int32* attrib_list, | |
| 111 const GURL& active_url, | |
| 112 gfx::GpuPreference gpu_preference); | |
| 113 | |
| 114 // Create a RendererGLContext that renders to an offscreen frame buffer. If | |
| 115 // parent is not NULL, that RendererGLContext can access a copy of the created | |
| 116 // RendererGLContext's frame buffer that is updated every time SwapBuffers is | |
| 117 // called. It is not as general as shared RendererGLContexts in other | |
| 118 // implementations of OpenGL. If parent is not NULL, it must be used on the | |
| 119 // same thread as the parent. A child RendererGLContext may not outlive its | |
| 120 // parent. attrib_list must be NULL or a NONE-terminated list of | |
| 121 // attribute/value pairs. | |
| 122 static RendererGLContext* CreateOffscreenContext( | |
| 123 GpuChannelHost* channel, | |
| 124 const gfx::Size& size, | |
| 125 RendererGLContext* share_group, | |
| 126 const char* allowed_extensions, | |
| 127 const int32* attrib_list, | |
| 128 const GURL& active_url, | |
| 129 gfx::GpuPreference gpu_preference); | |
| 130 | |
| 131 // Sets the parent context. If any parent textures have been created for | |
| 132 // another parent, it is important to delete them before changing the parent. | |
| 133 bool SetParent(RendererGLContext* parent); | |
| 134 | |
| 135 // For an offscreen frame buffer RendererGLContext, return the texture ID with | |
| 136 // respect to the parent RendererGLContext. Returns zero if RendererGLContext | |
| 137 // does not have a parent. | |
| 138 uint32 GetParentTextureId(); | |
| 139 | |
| 140 // Create a new texture in the parent's RendererGLContext. Returns zero if | |
| 141 // RendererGLContext does not have a parent. | |
| 142 uint32 CreateParentTexture(const gfx::Size& size); | |
| 143 | |
| 144 // Deletes a texture in the parent's RendererGLContext. | |
| 145 void DeleteParentTexture(uint32 texture); | |
| 146 | |
| 147 void SetContextLostCallback( | |
| 148 const base::Callback<void(ContextLostReason)>& callback); | |
| 149 | |
| 150 // Set the current RendererGLContext for the calling thread. | |
| 151 static bool MakeCurrent(RendererGLContext* context); | |
| 152 | |
| 153 // For a view RendererGLContext, display everything that has been rendered | |
| 154 // since the last call. For an offscreen RendererGLContext, resolve everything | |
| 155 // that has been rendered since the last call to a copy that can be accessed | |
| 156 // by the parent RendererGLContext. | |
| 157 bool SwapBuffers(); | |
| 158 | |
| 159 // Run the task once the channel has been flushed. Takes care of deleting the | |
| 160 // task whether the echo succeeds or not. | |
| 161 bool Echo(const base::Closure& task); | |
| 162 | |
| 163 // Sends an IPC message with the new state of surface visibility | |
| 164 bool SetSurfaceVisible(bool visibility); | |
| 165 | |
| 166 // TODO(gman): Remove this | |
| 167 void DisableShaderTranslation(); | |
| 168 | |
| 169 // Allows direct access to the GLES2 implementation so a RendererGLContext | |
| 170 // can be used without making it current. | |
| 171 gpu::gles2::GLES2Implementation* GetImplementation(); | |
| 172 | |
| 173 // Return the current error. | |
| 174 Error GetError(); | |
| 175 | |
| 176 // Return true if GPU process reported RendererGLContext lost or there was a | |
| 177 // problem communicating with the GPU process. | |
| 178 bool IsCommandBufferContextLost(); | |
| 179 | |
| 180 CommandBufferProxy* GetCommandBufferProxy(); | |
| 181 | |
| 182 private: | |
| 183 explicit RendererGLContext(GpuChannelHost* channel); | |
| 184 | |
| 185 bool Initialize(bool onscreen, | |
| 186 int32 surface_id, | |
| 187 const gfx::Size& size, | |
| 188 RendererGLContext* share_group, | |
| 189 const char* allowed_extensions, | |
| 190 const int32* attrib_list, | |
| 191 const GURL& active_url, | |
| 192 gfx::GpuPreference gpu_preference); | |
| 193 void Destroy(); | |
| 194 | |
| 195 void OnContextLost(); | |
| 196 | |
| 197 scoped_refptr<GpuChannelHost> channel_; | |
| 198 base::WeakPtr<RendererGLContext> parent_; | |
| 199 base::Callback<void(ContextLostReason)> context_lost_callback_; | |
| 200 uint32 parent_texture_id_; | |
| 201 CommandBufferProxy* command_buffer_; | |
| 202 gpu::gles2::GLES2CmdHelper* gles2_helper_; | |
| 203 gpu::TransferBuffer* transfer_buffer_; | |
| 204 gpu::gles2::GLES2Implementation* gles2_implementation_; | |
| 205 Error last_error_; | |
| 206 int frame_number_; | |
| 207 | |
| 208 DISALLOW_COPY_AND_ASSIGN(RendererGLContext); | |
| 209 }; | |
| 210 | |
| 211 #endif // CONTENT_RENDERER_GPU_RENDERER_GL_CONTEXT_H_ | |
| OLD | NEW |