OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_ | 5 #ifndef CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_ |
6 #define CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_ | 6 #define CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" |
17 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
19 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
20 #include "content/common/gpu/client/command_buffer_metrics.h" | 21 #include "content/common/gpu/client/command_buffer_metrics.h" |
21 #include "gpu/command_buffer/common/gles2_cmd_utils.h" | 22 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
22 #include "gpu/ipc/client/command_buffer_proxy_impl.h" | 23 #include "gpu/ipc/client/command_buffer_proxy_impl.h" |
23 #include "gpu/ipc/common/surface_handle.h" | 24 #include "gpu/ipc/common/surface_handle.h" |
24 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | 25 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
25 #include "third_party/WebKit/public/platform/WebString.h" | 26 #include "third_party/WebKit/public/platform/WebString.h" |
26 #include "ui/gl/gpu_preference.h" | 27 #include "ui/gl/gpu_preference.h" |
27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
28 | 29 |
29 namespace gpu { | 30 namespace gpu { |
30 | 31 |
31 class ContextSupport; | 32 class ContextSupport; |
32 class GpuChannelHost; | 33 class GpuChannelHost; |
33 struct SharedMemoryLimits; | 34 struct SharedMemoryLimits; |
34 class TransferBuffer; | 35 class TransferBuffer; |
35 | 36 |
36 namespace gles2 { | 37 namespace gles2 { |
37 class GLES2CmdHelper; | 38 class GLES2CmdHelper; |
38 class GLES2Implementation; | 39 class GLES2Implementation; |
39 class GLES2Interface; | 40 class GLES2Interface; |
| 41 class ShareGroup; |
40 } | 42 } |
41 } | 43 } |
42 | 44 |
43 namespace content { | 45 namespace content { |
44 | 46 |
45 class WebGraphicsContext3DCommandBufferImpl | 47 class WebGraphicsContext3DCommandBufferImpl |
46 : public NON_EXPORTED_BASE(blink::WebGraphicsContext3D) { | 48 : public NON_EXPORTED_BASE(blink::WebGraphicsContext3D) { |
47 public: | 49 public: |
48 enum MappedMemoryReclaimLimit { | 50 enum MappedMemoryReclaimLimit { |
49 kNoLimit = 0, | 51 kNoLimit = 0, |
50 }; | 52 }; |
51 | 53 |
52 class ShareGroup : public base::RefCountedThreadSafe<ShareGroup> { | |
53 public: | |
54 ShareGroup(); | |
55 | |
56 WebGraphicsContext3DCommandBufferImpl* GetAnyContextLocked() { | |
57 // In order to ensure that the context returned is not removed while | |
58 // in use, the share group's lock should be aquired before calling this | |
59 // function. | |
60 lock_.AssertAcquired(); | |
61 if (contexts_.empty()) | |
62 return NULL; | |
63 return contexts_.front(); | |
64 } | |
65 | |
66 void AddContextLocked(WebGraphicsContext3DCommandBufferImpl* context) { | |
67 lock_.AssertAcquired(); | |
68 contexts_.push_back(context); | |
69 } | |
70 | |
71 void RemoveContext(WebGraphicsContext3DCommandBufferImpl* context) { | |
72 base::AutoLock auto_lock(lock_); | |
73 contexts_.erase(std::remove(contexts_.begin(), contexts_.end(), context), | |
74 contexts_.end()); | |
75 } | |
76 | |
77 void RemoveAllContexts() { | |
78 base::AutoLock auto_lock(lock_); | |
79 contexts_.clear(); | |
80 } | |
81 | |
82 base::Lock& lock() { | |
83 return lock_; | |
84 } | |
85 | |
86 private: | |
87 friend class base::RefCountedThreadSafe<ShareGroup>; | |
88 virtual ~ShareGroup(); | |
89 | |
90 std::vector<WebGraphicsContext3DCommandBufferImpl*> contexts_; | |
91 base::Lock lock_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(ShareGroup); | |
94 }; | |
95 | |
96 class WebGraphicsContextLostCallback { | 54 class WebGraphicsContextLostCallback { |
97 public: | 55 public: |
98 virtual void onContextLost() = 0; | 56 virtual void onContextLost() = 0; |
99 | 57 |
100 protected: | 58 protected: |
101 virtual ~WebGraphicsContextLostCallback() {} | 59 virtual ~WebGraphicsContextLostCallback() {} |
102 }; | 60 }; |
103 | 61 |
104 // If surface_handle is not kNullSurfaceHandle, this creates a | 62 // If surface_handle is not kNullSurfaceHandle, this creates a |
105 // CommandBufferProxy that renders directly to a view. The view and | 63 // CommandBufferProxy that renders directly to a view. The view and |
106 // the associated window must not be destroyed until the returned | 64 // the associated window must not be destroyed until the returned |
107 // CommandBufferProxy has been destroyed, otherwise the GPU process might | 65 // CommandBufferProxy has been destroyed, otherwise the GPU process might |
108 // attempt to render to an invalid window handle. | 66 // attempt to render to an invalid window handle. |
109 CONTENT_EXPORT WebGraphicsContext3DCommandBufferImpl( | 67 CONTENT_EXPORT WebGraphicsContext3DCommandBufferImpl( |
110 gpu::SurfaceHandle surface_handle, | 68 gpu::SurfaceHandle surface_handle, |
111 const GURL& active_url, | 69 const GURL& active_url, |
112 gpu::GpuChannelHost* host, | 70 gpu::GpuChannelHost* host, |
113 const gpu::gles2::ContextCreationAttribHelper& attributes, | 71 const gpu::gles2::ContextCreationAttribHelper& attributes, |
114 gfx::GpuPreference gpu_preference, | 72 gfx::GpuPreference gpu_preference, |
115 bool share_resources, | 73 bool automatic_flushes); |
116 bool automatic_flushes, | |
117 WebGraphicsContext3DCommandBufferImpl* share_context); | |
118 | 74 |
119 ~WebGraphicsContext3DCommandBufferImpl() override; | 75 ~WebGraphicsContext3DCommandBufferImpl() override; |
120 | 76 |
121 gpu::CommandBufferProxyImpl* GetCommandBufferProxy() { | 77 gpu::CommandBufferProxyImpl* GetCommandBufferProxy() { |
122 return command_buffer_.get(); | 78 return command_buffer_.get(); |
123 } | 79 } |
124 | 80 |
125 CONTENT_EXPORT gpu::ContextSupport* GetContextSupport(); | 81 CONTENT_EXPORT gpu::ContextSupport* GetContextSupport(); |
126 | 82 |
127 gpu::gles2::GLES2Implementation* GetImplementation() { | 83 gpu::gles2::GLES2Implementation* GetImplementation() { |
128 return real_gl_.get(); | 84 return real_gl_.get(); |
129 } | 85 } |
130 | 86 |
131 void SetContextLostCallback(WebGraphicsContextLostCallback* callback) { | 87 void SetContextLostCallback(WebGraphicsContextLostCallback* callback) { |
132 context_lost_callback_ = callback; | 88 context_lost_callback_ = callback; |
133 } | 89 } |
134 | 90 |
135 CONTENT_EXPORT bool InitializeOnCurrentThread( | 91 CONTENT_EXPORT bool InitializeOnCurrentThread( |
136 const gpu::SharedMemoryLimits& memory_limits); | 92 const gpu::SharedMemoryLimits& memory_limits, |
| 93 gpu::CommandBufferProxyImpl* shared_command_buffer, |
| 94 scoped_refptr<gpu::gles2::ShareGroup> share_group); |
137 | 95 |
138 void SetContextType(CommandBufferContextType type) { | 96 void SetContextType(CommandBufferContextType type) { |
139 context_type_ = type; | 97 context_type_ = type; |
140 } | 98 } |
141 private: | 99 private: |
142 // These are the same error codes as used by EGL. | 100 // These are the same error codes as used by EGL. |
143 enum Error { | 101 enum Error { |
144 SUCCESS = 0x3000, | 102 SUCCESS = 0x3000, |
145 BAD_ATTRIBUTE = 0x3004, | 103 BAD_ATTRIBUTE = 0x3004, |
146 CONTEXT_LOST = 0x300E | 104 CONTEXT_LOST = 0x300E |
147 }; | 105 }; |
148 | 106 |
149 // Initialize the underlying GL context. May be called multiple times; second | 107 // Initialize the underlying GL context. May be called multiple times; second |
150 // and subsequent calls are ignored. Must be called from the thread that is | 108 // and subsequent calls are ignored. Must be called from the thread that is |
151 // going to use this object to issue GL commands (which might not be the main | 109 // going to use this object to issue GL commands (which might not be the main |
152 // thread). | 110 // thread). |
153 bool MaybeInitializeGL(const gpu::SharedMemoryLimits& memory_limits); | 111 bool MaybeInitializeGL(const gpu::SharedMemoryLimits& memory_limits, |
| 112 gpu::CommandBufferProxyImpl* shared_command_buffer, |
| 113 scoped_refptr<gpu::gles2::ShareGroup> share_group); |
154 | 114 |
155 bool InitializeCommandBuffer( | 115 bool InitializeCommandBuffer( |
156 WebGraphicsContext3DCommandBufferImpl* share_context); | 116 gpu::CommandBufferProxyImpl* shared_command_buffer); |
157 | 117 |
158 void Destroy(); | 118 void Destroy(); |
159 | 119 |
160 bool CreateContext(const gpu::SharedMemoryLimits& memory_limits); | 120 bool CreateContext(const gpu::SharedMemoryLimits& memory_limits, |
| 121 gpu::CommandBufferProxyImpl* shared_command_buffer, |
| 122 scoped_refptr<gpu::gles2::ShareGroup> share_group); |
161 | 123 |
162 void OnContextLost(); | 124 void OnContextLost(); |
163 | 125 |
164 bool initialized_ = false; | 126 bool initialized_ = false; |
165 bool initialize_failed_ = false; | 127 bool initialize_failed_ = false; |
166 WebGraphicsContextLostCallback* context_lost_callback_ = nullptr; | 128 WebGraphicsContextLostCallback* context_lost_callback_ = nullptr; |
167 | 129 |
168 bool automatic_flushes_; | 130 bool automatic_flushes_; |
169 gpu::gles2::ContextCreationAttribHelper attributes_; | 131 gpu::gles2::ContextCreationAttribHelper attributes_; |
170 | 132 |
171 // State needed by MaybeInitializeGL. | 133 // State needed by MaybeInitializeGL. |
172 scoped_refptr<gpu::GpuChannelHost> host_; | 134 scoped_refptr<gpu::GpuChannelHost> host_; |
173 gpu::SurfaceHandle surface_handle_; | 135 gpu::SurfaceHandle surface_handle_; |
174 GURL active_url_; | 136 GURL active_url_; |
175 CommandBufferContextType context_type_; | 137 CommandBufferContextType context_type_; |
176 | 138 |
177 gfx::GpuPreference gpu_preference_; | 139 gfx::GpuPreference gpu_preference_; |
178 | 140 |
179 std::unique_ptr<gpu::CommandBufferProxyImpl> command_buffer_; | 141 std::unique_ptr<gpu::CommandBufferProxyImpl> command_buffer_; |
180 std::unique_ptr<gpu::gles2::GLES2CmdHelper> gles2_helper_; | 142 std::unique_ptr<gpu::gles2::GLES2CmdHelper> gles2_helper_; |
181 std::unique_ptr<gpu::TransferBuffer> transfer_buffer_; | 143 std::unique_ptr<gpu::TransferBuffer> transfer_buffer_; |
182 std::unique_ptr<gpu::gles2::GLES2Implementation> real_gl_; | 144 std::unique_ptr<gpu::gles2::GLES2Implementation> real_gl_; |
183 std::unique_ptr<gpu::gles2::GLES2Interface> trace_gl_; | 145 std::unique_ptr<gpu::gles2::GLES2Interface> trace_gl_; |
184 scoped_refptr<ShareGroup> share_group_; | |
185 | 146 |
186 // Member variables should appear before the WeakPtrFactory, to ensure | 147 // Member variables should appear before the WeakPtrFactory, to ensure |
187 // that any WeakPtrs to Controller are invalidated before its members | 148 // that any WeakPtrs to Controller are invalidated before its members |
188 // variable's destructors are executed, rendering them invalid. | 149 // variable's destructors are executed, rendering them invalid. |
189 base::WeakPtrFactory<WebGraphicsContext3DCommandBufferImpl> weak_ptr_factory_; | 150 base::WeakPtrFactory<WebGraphicsContext3DCommandBufferImpl> weak_ptr_factory_; |
190 }; | 151 }; |
191 | 152 |
192 } // namespace content | 153 } // namespace content |
193 | 154 |
194 #endif // CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_ | 155 #endif // CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_ |
OLD | NEW |