base/threading: remove ScopedTracker placed for experiments
[chromium-blink-merge.git] / content / common / gpu / client / webgraphicscontext3d_command_buffer_impl.h
blob0eca015974d65235fcf7d69be6ce6d195abc3b87
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_
8 #include <string>
9 #include <vector>
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"
23 #include "url/gurl.h"
25 namespace gpu {
27 class ContextSupport;
28 class TransferBuffer;
30 namespace gles2 {
31 class GLES2CmdHelper;
32 class GLES2Implementation;
33 class GLES2Interface;
37 namespace content {
38 class GpuChannelHost;
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 {
47 public:
48 enum MappedMemoryReclaimLimit {
49 kNoLimit = 0,
52 struct CONTENT_EXPORT SharedMemoryLimits {
53 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> {
63 public:
64 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
69 // function.
70 lock_.AssertAcquired();
71 if (contexts_.empty())
72 return NULL;
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),
84 contexts_.end());
87 void RemoveAllContexts() {
88 base::AutoLock auto_lock(lock_);
89 contexts_.clear();
92 base::Lock& lock() {
93 return lock_;
96 private:
97 friend class base::RefCountedThreadSafe<ShareGroup>;
98 virtual ~ShareGroup();
100 std::vector<WebGraphicsContext3DCommandBufferImpl*> contexts_;
101 base::Lock lock_;
103 DISALLOW_COPY_AND_ASSIGN(ShareGroup);
106 WebGraphicsContext3DCommandBufferImpl(
107 int surface_id,
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
132 // on any failure.
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;
151 private:
152 // These are the same error codes as used by EGL.
153 enum Error {
154 SUCCESS = 0x3000,
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
162 // thread).
163 bool MaybeInitializeGL();
165 bool InitializeCommandBuffer(bool onscreen,
166 WebGraphicsContext3DCommandBufferImpl* share_context);
168 void Destroy();
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_;
190 bool visible_;
192 // State needed by MaybeInitializeGL.
193 scoped_refptr<GpuChannelHost> host_;
194 int32 surface_id_;
195 GURL active_url_;
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_;
205 Error last_error_;
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_