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_metrics.h"
17 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
18 #include "gpu/blink/webgraphicscontext3d_impl.h"
19 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gl/gpu_preference.h"
32 class GLES2Implementation
;
40 const size_t kDefaultCommandBufferSize
= 1024 * 1024;
41 const size_t kDefaultStartTransferBufferSize
= 1 * 1024 * 1024;
42 const size_t kDefaultMinTransferBufferSize
= 1 * 256 * 1024;
43 const size_t kDefaultMaxTransferBufferSize
= 16 * 1024 * 1024;
45 class WebGraphicsContext3DCommandBufferImpl
46 : public gpu_blink::WebGraphicsContext3DImpl
{
48 enum MappedMemoryReclaimLimit
{
52 struct CONTENT_EXPORT SharedMemoryLimits
{
55 size_t command_buffer_size
;
56 size_t start_transfer_buffer_size
;
57 size_t min_transfer_buffer_size
;
58 size_t max_transfer_buffer_size
;
59 size_t mapped_memory_reclaim_limit
;
62 class ShareGroup
: public base::RefCountedThreadSafe
<ShareGroup
> {
66 WebGraphicsContext3DCommandBufferImpl
* GetAnyContextLocked() {
67 // In order to ensure that the context returned is not removed while
68 // in use, the share group's lock should be aquired before calling this
70 lock_
.AssertAcquired();
71 if (contexts_
.empty())
73 return contexts_
.front();
76 void AddContextLocked(WebGraphicsContext3DCommandBufferImpl
* context
) {
77 lock_
.AssertAcquired();
78 contexts_
.push_back(context
);
81 void RemoveContext(WebGraphicsContext3DCommandBufferImpl
* context
) {
82 base::AutoLock
auto_lock(lock_
);
83 contexts_
.erase(std::remove(contexts_
.begin(), contexts_
.end(), context
),
87 void RemoveAllContexts() {
88 base::AutoLock
auto_lock(lock_
);
97 friend class base::RefCountedThreadSafe
<ShareGroup
>;
98 virtual ~ShareGroup();
100 std::vector
<WebGraphicsContext3DCommandBufferImpl
*> contexts_
;
103 DISALLOW_COPY_AND_ASSIGN(ShareGroup
);
106 WebGraphicsContext3DCommandBufferImpl(
108 const GURL
& active_url
,
109 GpuChannelHost
* host
,
110 const Attributes
& attributes
,
111 bool lose_context_when_out_of_memory
,
112 const SharedMemoryLimits
& limits
,
113 WebGraphicsContext3DCommandBufferImpl
* share_context
);
115 virtual ~WebGraphicsContext3DCommandBufferImpl();
117 CommandBufferProxyImpl
* GetCommandBufferProxy() {
118 return command_buffer_
.get();
121 CONTENT_EXPORT
gpu::ContextSupport
* GetContextSupport();
123 gpu::gles2::GLES2Implementation
* GetImplementation() {
124 return real_gl_
.get();
127 // Return true if GPU process reported context lost or there was a
128 // problem communicating with the GPU process.
129 bool IsCommandBufferContextLost();
131 // Create & initialize a WebGraphicsContext3DCommandBufferImpl. Return NULL
133 static CONTENT_EXPORT WebGraphicsContext3DCommandBufferImpl
*
134 CreateOffscreenContext(
135 GpuChannelHost
* host
,
136 const WebGraphicsContext3D::Attributes
& attributes
,
137 bool lose_context_when_out_of_memory
,
138 const GURL
& active_url
,
139 const SharedMemoryLimits
& limits
,
140 WebGraphicsContext3DCommandBufferImpl
* share_context
);
142 size_t GetMappedMemoryLimit() {
143 return mem_limits_
.mapped_memory_reclaim_limit
;
146 CONTENT_EXPORT
bool InitializeOnCurrentThread();
148 void SetContextType(CommandBufferContextType type
) {
149 context_type_
= type
;
152 // These are the same error codes as used by EGL.
155 BAD_ATTRIBUTE
= 0x3004,
156 CONTEXT_LOST
= 0x300E
159 // Initialize the underlying GL context. May be called multiple times; second
160 // and subsequent calls are ignored. Must be called from the thread that is
161 // going to use this object to issue GL commands (which might not be the main
163 bool MaybeInitializeGL();
165 bool InitializeCommandBuffer(bool onscreen
,
166 WebGraphicsContext3DCommandBufferImpl
* share_context
);
170 // Create a CommandBufferProxy that renders directly to a view. The view and
171 // the associated window must not be destroyed until the returned
172 // CommandBufferProxy has been destroyed, otherwise the GPU process might
173 // attempt to render to an invalid window handle.
175 // NOTE: on Mac OS X, this entry point is only used to set up the
176 // accelerated compositor's output. On this platform, we actually pass
177 // a gfx::PluginWindowHandle in place of the gfx::NativeViewId,
178 // because the facility to allocate a fake PluginWindowHandle is
179 // already in place. We could add more entry points and messages to
180 // allocate both fake PluginWindowHandles and NativeViewIds and map
181 // from fake NativeViewIds to PluginWindowHandles, but this seems like
182 // unnecessary complexity at the moment.
183 bool CreateContext(bool onscreen
);
185 virtual void OnContextLost();
187 bool lose_context_when_out_of_memory_
;
188 blink::WebGraphicsContext3D::Attributes attributes_
;
192 // State needed by MaybeInitializeGL.
193 scoped_refptr
<GpuChannelHost
> host_
;
196 CommandBufferContextType context_type_
;
198 gfx::GpuPreference gpu_preference_
;
200 scoped_ptr
<CommandBufferProxyImpl
> command_buffer_
;
201 scoped_ptr
<gpu::gles2::GLES2CmdHelper
> gles2_helper_
;
202 scoped_ptr
<gpu::TransferBuffer
> transfer_buffer_
;
203 scoped_ptr
<gpu::gles2::GLES2Implementation
> real_gl_
;
204 scoped_ptr
<gpu::gles2::GLES2Interface
> trace_gl_
;
206 SharedMemoryLimits mem_limits_
;
207 scoped_refptr
<ShareGroup
> share_group_
;
209 // Member variables should appear before the WeakPtrFactory, to ensure
210 // that any WeakPtrs to Controller are invalidated before its members
211 // variable's destructors are executed, rendering them invalid.
212 base::WeakPtrFactory
<WebGraphicsContext3DCommandBufferImpl
> weak_ptr_factory_
;
215 } // namespace content
217 #endif // CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_