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 CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_
6 #define CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "content/common/content_export.h"
16 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
17 #include "gpu/blink/webgraphicscontext3d_impl.h"
18 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "ui/gfx/native_widget_types.h"
21 #include "ui/gl/gpu_preference.h"
31 class GLES2Implementation
;
39 const size_t kDefaultCommandBufferSize
= 1024 * 1024;
40 const size_t kDefaultStartTransferBufferSize
= 1 * 1024 * 1024;
41 const size_t kDefaultMinTransferBufferSize
= 1 * 256 * 1024;
42 const size_t kDefaultMaxTransferBufferSize
= 16 * 1024 * 1024;
44 class WebGraphicsContext3DCommandBufferImpl
45 : public gpu_blink::WebGraphicsContext3DImpl
{
47 enum MappedMemoryReclaimLimit
{
51 struct CONTENT_EXPORT SharedMemoryLimits
{
54 size_t command_buffer_size
;
55 size_t start_transfer_buffer_size
;
56 size_t min_transfer_buffer_size
;
57 size_t max_transfer_buffer_size
;
58 size_t mapped_memory_reclaim_limit
;
61 class ShareGroup
: public base::RefCountedThreadSafe
<ShareGroup
> {
65 WebGraphicsContext3DCommandBufferImpl
* GetAnyContextLocked() {
66 // In order to ensure that the context returned is not removed while
67 // in use, the share group's lock should be aquired before calling this
69 lock_
.AssertAcquired();
70 if (contexts_
.empty())
72 return contexts_
.front();
75 void AddContextLocked(WebGraphicsContext3DCommandBufferImpl
* context
) {
76 lock_
.AssertAcquired();
77 contexts_
.push_back(context
);
80 void RemoveContext(WebGraphicsContext3DCommandBufferImpl
* context
) {
81 base::AutoLock
auto_lock(lock_
);
82 contexts_
.erase(std::remove(contexts_
.begin(), contexts_
.end(), context
),
86 void RemoveAllContexts() {
87 base::AutoLock
auto_lock(lock_
);
96 friend class base::RefCountedThreadSafe
<ShareGroup
>;
97 virtual ~ShareGroup();
99 std::vector
<WebGraphicsContext3DCommandBufferImpl
*> contexts_
;
102 DISALLOW_COPY_AND_ASSIGN(ShareGroup
);
105 WebGraphicsContext3DCommandBufferImpl(
107 const GURL
& active_url
,
108 GpuChannelHost
* host
,
109 const Attributes
& attributes
,
110 bool lose_context_when_out_of_memory
,
111 const SharedMemoryLimits
& limits
,
112 WebGraphicsContext3DCommandBufferImpl
* share_context
);
114 virtual ~WebGraphicsContext3DCommandBufferImpl();
116 CommandBufferProxyImpl
* GetCommandBufferProxy() {
117 return command_buffer_
.get();
120 CONTENT_EXPORT
gpu::ContextSupport
* GetContextSupport();
122 gpu::gles2::GLES2Implementation
* GetImplementation() {
123 return real_gl_
.get();
126 // Return true if GPU process reported context lost or there was a
127 // problem communicating with the GPU process.
128 bool IsCommandBufferContextLost();
130 // Create & initialize a WebGraphicsContext3DCommandBufferImpl. Return NULL
132 static CONTENT_EXPORT WebGraphicsContext3DCommandBufferImpl
*
133 CreateOffscreenContext(
134 GpuChannelHost
* host
,
135 const WebGraphicsContext3D::Attributes
& attributes
,
136 bool lose_context_when_out_of_memory
,
137 const GURL
& active_url
,
138 const SharedMemoryLimits
& limits
,
139 WebGraphicsContext3DCommandBufferImpl
* share_context
);
141 size_t GetMappedMemoryLimit() {
142 return mem_limits_
.mapped_memory_reclaim_limit
;
145 CONTENT_EXPORT
bool InitializeOnCurrentThread();
147 //----------------------------------------------------------------------
148 // WebGraphicsContext3D methods
149 virtual bool isContextLost();
151 virtual blink::WGC3Denum
getGraphicsResetStatusARB();
154 // These are the same error codes as used by EGL.
157 BAD_ATTRIBUTE
= 0x3004,
158 CONTEXT_LOST
= 0x300E
161 // Initialize the underlying GL context. May be called multiple times; second
162 // and subsequent calls are ignored. Must be called from the thread that is
163 // going to use this object to issue GL commands (which might not be the main
165 bool MaybeInitializeGL();
167 bool InitializeCommandBuffer(bool onscreen
,
168 WebGraphicsContext3DCommandBufferImpl
* share_context
);
172 // Create a CommandBufferProxy that renders directly to a view. The view and
173 // the associated window must not be destroyed until the returned
174 // CommandBufferProxy has been destroyed, otherwise the GPU process might
175 // attempt to render to an invalid window handle.
177 // NOTE: on Mac OS X, this entry point is only used to set up the
178 // accelerated compositor's output. On this platform, we actually pass
179 // a gfx::PluginWindowHandle in place of the gfx::NativeViewId,
180 // because the facility to allocate a fake PluginWindowHandle is
181 // already in place. We could add more entry points and messages to
182 // allocate both fake PluginWindowHandles and NativeViewIds and map
183 // from fake NativeViewIds to PluginWindowHandles, but this seems like
184 // unnecessary complexity at the moment.
185 bool CreateContext(bool onscreen
);
187 virtual void OnGpuChannelLost();
189 bool lose_context_when_out_of_memory_
;
190 blink::WebGraphicsContext3D::Attributes attributes_
;
194 // State needed by MaybeInitializeGL.
195 scoped_refptr
<GpuChannelHost
> host_
;
199 gfx::GpuPreference gpu_preference_
;
201 scoped_ptr
<CommandBufferProxyImpl
> command_buffer_
;
202 scoped_ptr
<gpu::gles2::GLES2CmdHelper
> gles2_helper_
;
203 scoped_ptr
<gpu::TransferBuffer
> transfer_buffer_
;
204 scoped_ptr
<gpu::gles2::GLES2Implementation
> real_gl_
;
205 scoped_ptr
<gpu::gles2::GLES2Interface
> trace_gl_
;
207 SharedMemoryLimits mem_limits_
;
208 scoped_refptr
<ShareGroup
> share_group_
;
210 // Member variables should appear before the WeakPtrFactory, to ensure
211 // that any WeakPtrs to Controller are invalidated before its members
212 // variable's destructors are executed, rendering them invalid.
213 base::WeakPtrFactory
<WebGraphicsContext3DCommandBufferImpl
> weak_ptr_factory_
;
216 } // namespace content
218 #endif // CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_