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 #include "gpu/command_buffer/client/gl_in_process_context.h" | 5 #include "gpu/command_buffer/client/gl_in_process_context.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 class GLInProcessContextImpl | 50 class GLInProcessContextImpl |
51 : public GLInProcessContext, | 51 : public GLInProcessContext, |
52 public base::SupportsWeakPtr<GLInProcessContextImpl> { | 52 public base::SupportsWeakPtr<GLInProcessContextImpl> { |
53 public: | 53 public: |
54 explicit GLInProcessContextImpl( | 54 explicit GLInProcessContextImpl( |
55 const GLInProcessContextSharedMemoryLimits& mem_limits); | 55 const GLInProcessContextSharedMemoryLimits& mem_limits); |
56 ~GLInProcessContextImpl() override; | 56 ~GLInProcessContextImpl() override; |
57 | 57 |
58 bool Initialize(scoped_refptr<gfx::GLSurface> surface, | 58 bool Initialize(scoped_refptr<gfx::GLSurface> surface, |
59 bool is_offscreen, | 59 bool is_offscreen, |
60 bool use_global_share_group, | |
61 GLInProcessContext* share_context, | 60 GLInProcessContext* share_context, |
62 gfx::AcceleratedWidget window, | 61 gfx::AcceleratedWidget window, |
63 const gfx::Size& size, | 62 const gfx::Size& size, |
64 const gpu::gles2::ContextCreationAttribHelper& attribs, | 63 const gpu::gles2::ContextCreationAttribHelper& attribs, |
65 gfx::GpuPreference gpu_preference, | 64 gfx::GpuPreference gpu_preference, |
66 const scoped_refptr<InProcessCommandBuffer::Service>& service, | 65 const scoped_refptr<InProcessCommandBuffer::Service>& service, |
67 GpuMemoryBufferManager* gpu_memory_buffer_manager, | 66 GpuMemoryBufferManager* gpu_memory_buffer_manager, |
68 ImageFactory* image_factory); | 67 ImageFactory* image_factory); |
69 | 68 |
70 // GLInProcessContext implementation: | 69 // GLInProcessContext implementation: |
(...skipping 12 matching lines...) Expand all Loading... |
83 void Destroy(); | 82 void Destroy(); |
84 void OnContextLost(); | 83 void OnContextLost(); |
85 void OnSignalSyncPoint(const base::Closure& callback); | 84 void OnSignalSyncPoint(const base::Closure& callback); |
86 | 85 |
87 scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_; | 86 scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_; |
88 scoped_ptr<TransferBuffer> transfer_buffer_; | 87 scoped_ptr<TransferBuffer> transfer_buffer_; |
89 scoped_ptr<gles2::GLES2Implementation> gles2_implementation_; | 88 scoped_ptr<gles2::GLES2Implementation> gles2_implementation_; |
90 scoped_ptr<InProcessCommandBuffer> command_buffer_; | 89 scoped_ptr<InProcessCommandBuffer> command_buffer_; |
91 | 90 |
92 const GLInProcessContextSharedMemoryLimits mem_limits_; | 91 const GLInProcessContextSharedMemoryLimits mem_limits_; |
93 bool context_lost_; | |
94 base::Closure context_lost_callback_; | 92 base::Closure context_lost_callback_; |
95 base::Lock* lock_; | |
96 | 93 |
97 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl); | 94 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl); |
98 }; | 95 }; |
99 | 96 |
100 base::LazyInstance<base::Lock> g_all_shared_contexts_lock = | |
101 LAZY_INSTANCE_INITIALIZER; | |
102 base::LazyInstance<std::set<GLInProcessContextImpl*> > g_all_shared_contexts = | |
103 LAZY_INSTANCE_INITIALIZER; | |
104 | |
105 GLInProcessContextImpl::GLInProcessContextImpl( | 97 GLInProcessContextImpl::GLInProcessContextImpl( |
106 const GLInProcessContextSharedMemoryLimits& mem_limits) | 98 const GLInProcessContextSharedMemoryLimits& mem_limits) |
107 : mem_limits_(mem_limits), context_lost_(false), lock_(nullptr) { | 99 : mem_limits_(mem_limits) {} |
108 } | |
109 | 100 |
110 GLInProcessContextImpl::~GLInProcessContextImpl() { | 101 GLInProcessContextImpl::~GLInProcessContextImpl() { |
111 { | |
112 base::AutoLock lock(g_all_shared_contexts_lock.Get()); | |
113 g_all_shared_contexts.Get().erase(this); | |
114 } | |
115 Destroy(); | 102 Destroy(); |
116 } | 103 } |
117 | 104 |
118 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() { | 105 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() { |
119 return gles2_implementation_.get(); | 106 return gles2_implementation_.get(); |
120 } | 107 } |
121 | 108 |
122 size_t GLInProcessContextImpl::GetMappedMemoryLimit() { | 109 size_t GLInProcessContextImpl::GetMappedMemoryLimit() { |
123 return mem_limits_.mapped_memory_reclaim_limit; | 110 return mem_limits_.mapped_memory_reclaim_limit; |
124 } | 111 } |
125 | 112 |
126 void GLInProcessContextImpl::SetLock(base::Lock* lock) { | 113 void GLInProcessContextImpl::SetLock(base::Lock* lock) { |
127 command_buffer_->SetLock(lock); | 114 NOTREACHED(); |
128 lock_ = lock; | |
129 } | 115 } |
130 | 116 |
131 void GLInProcessContextImpl::SetContextLostCallback( | 117 void GLInProcessContextImpl::SetContextLostCallback( |
132 const base::Closure& callback) { | 118 const base::Closure& callback) { |
133 context_lost_callback_ = callback; | 119 context_lost_callback_ = callback; |
134 } | 120 } |
135 | 121 |
136 void GLInProcessContextImpl::OnContextLost() { | 122 void GLInProcessContextImpl::OnContextLost() { |
137 scoped_ptr<base::AutoLock> lock; | 123 if (!context_lost_callback_.is_null()) |
138 if (lock_) | |
139 lock.reset(new base::AutoLock(*lock_)); | |
140 context_lost_ = true; | |
141 if (!context_lost_callback_.is_null()) { | |
142 context_lost_callback_.Run(); | 124 context_lost_callback_.Run(); |
143 } | |
144 } | 125 } |
145 | 126 |
146 bool GLInProcessContextImpl::Initialize( | 127 bool GLInProcessContextImpl::Initialize( |
147 scoped_refptr<gfx::GLSurface> surface, | 128 scoped_refptr<gfx::GLSurface> surface, |
148 bool is_offscreen, | 129 bool is_offscreen, |
149 bool use_global_share_group, | |
150 GLInProcessContext* share_context, | 130 GLInProcessContext* share_context, |
151 gfx::AcceleratedWidget window, | 131 gfx::AcceleratedWidget window, |
152 const gfx::Size& size, | 132 const gfx::Size& size, |
153 const gles2::ContextCreationAttribHelper& attribs, | 133 const gles2::ContextCreationAttribHelper& attribs, |
154 gfx::GpuPreference gpu_preference, | 134 gfx::GpuPreference gpu_preference, |
155 const scoped_refptr<InProcessCommandBuffer::Service>& service, | 135 const scoped_refptr<InProcessCommandBuffer::Service>& service, |
156 GpuMemoryBufferManager* gpu_memory_buffer_manager, | 136 GpuMemoryBufferManager* gpu_memory_buffer_manager, |
157 ImageFactory* image_factory) { | 137 ImageFactory* image_factory) { |
158 DCHECK(!use_global_share_group || !share_context); | |
159 DCHECK(size.width() >= 0 && size.height() >= 0); | 138 DCHECK(size.width() >= 0 && size.height() >= 0); |
160 | 139 |
161 std::vector<int32_t> attrib_vector; | 140 std::vector<int32_t> attrib_vector; |
162 attribs.Serialize(&attrib_vector); | 141 attribs.Serialize(&attrib_vector); |
163 | 142 |
164 base::Closure wrapped_callback = | 143 base::Closure wrapped_callback = |
165 base::Bind(&GLInProcessContextImpl::OnContextLost, AsWeakPtr()); | 144 base::Bind(&GLInProcessContextImpl::OnContextLost, AsWeakPtr()); |
166 command_buffer_.reset(new InProcessCommandBuffer(service)); | 145 command_buffer_.reset(new InProcessCommandBuffer(service)); |
167 | 146 |
168 scoped_ptr<base::AutoLock> scoped_shared_context_lock; | |
169 scoped_refptr<gles2::ShareGroup> share_group; | 147 scoped_refptr<gles2::ShareGroup> share_group; |
170 InProcessCommandBuffer* share_command_buffer = NULL; | 148 InProcessCommandBuffer* share_command_buffer = nullptr; |
171 if (use_global_share_group) { | 149 if (share_context) { |
172 scoped_shared_context_lock.reset( | |
173 new base::AutoLock(g_all_shared_contexts_lock.Get())); | |
174 for (std::set<GLInProcessContextImpl*>::const_iterator it = | |
175 g_all_shared_contexts.Get().begin(); | |
176 it != g_all_shared_contexts.Get().end(); | |
177 it++) { | |
178 const GLInProcessContextImpl* context = *it; | |
179 if (!context->context_lost_) { | |
180 share_group = context->gles2_implementation_->share_group(); | |
181 share_command_buffer = context->command_buffer_.get(); | |
182 DCHECK(share_group.get()); | |
183 DCHECK(share_command_buffer); | |
184 break; | |
185 } | |
186 } | |
187 } else if (share_context) { | |
188 GLInProcessContextImpl* impl = | 150 GLInProcessContextImpl* impl = |
189 static_cast<GLInProcessContextImpl*>(share_context); | 151 static_cast<GLInProcessContextImpl*>(share_context); |
190 share_group = impl->gles2_implementation_->share_group(); | 152 share_group = impl->gles2_implementation_->share_group(); |
191 share_command_buffer = impl->command_buffer_.get(); | 153 share_command_buffer = impl->command_buffer_.get(); |
192 DCHECK(share_group.get()); | 154 DCHECK(share_group); |
193 DCHECK(share_command_buffer); | 155 DCHECK(share_command_buffer); |
194 } | 156 } |
195 | 157 |
196 if (!command_buffer_->Initialize(surface, | 158 if (!command_buffer_->Initialize(surface, |
197 is_offscreen, | 159 is_offscreen, |
198 window, | 160 window, |
199 size, | 161 size, |
200 attrib_vector, | 162 attrib_vector, |
201 gpu_preference, | 163 gpu_preference, |
202 wrapped_callback, | 164 wrapped_callback, |
203 share_command_buffer, | 165 share_command_buffer, |
204 gpu_memory_buffer_manager, | 166 gpu_memory_buffer_manager, |
205 image_factory)) { | 167 image_factory)) { |
206 LOG(ERROR) << "Failed to initialize InProcessCommmandBuffer"; | 168 DLOG(ERROR) << "Failed to initialize InProcessCommmandBuffer"; |
207 return false; | 169 return false; |
208 } | 170 } |
209 | 171 |
210 // Create the GLES2 helper, which writes the command buffer protocol. | 172 // Create the GLES2 helper, which writes the command buffer protocol. |
211 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get())); | 173 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get())); |
212 if (!gles2_helper_->Initialize(mem_limits_.command_buffer_size)) { | 174 if (!gles2_helper_->Initialize(mem_limits_.command_buffer_size)) { |
213 LOG(ERROR) << "Failed to initialize GLES2CmdHelper"; | 175 LOG(ERROR) << "Failed to initialize GLES2CmdHelper"; |
214 Destroy(); | 176 Destroy(); |
215 return false; | 177 return false; |
216 } | 178 } |
217 | 179 |
218 // Create a transfer buffer. | 180 // Create a transfer buffer. |
219 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); | 181 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); |
220 | 182 |
221 // Check for consistency. | 183 // Check for consistency. |
222 DCHECK(!attribs.bind_generates_resource); | 184 DCHECK(!attribs.bind_generates_resource); |
223 const bool bind_generates_resource = false; | 185 const bool bind_generates_resource = false; |
224 const bool support_client_side_arrays = false; | 186 const bool support_client_side_arrays = false; |
225 | 187 |
226 // Create the object exposing the OpenGL API. | 188 // Create the object exposing the OpenGL API. |
227 gles2_implementation_.reset( | 189 gles2_implementation_.reset( |
228 new gles2::GLES2Implementation(gles2_helper_.get(), | 190 new gles2::GLES2Implementation(gles2_helper_.get(), |
229 share_group.get(), | 191 share_group.get(), |
230 transfer_buffer_.get(), | 192 transfer_buffer_.get(), |
231 bind_generates_resource, | 193 bind_generates_resource, |
232 attribs.lose_context_when_out_of_memory, | 194 attribs.lose_context_when_out_of_memory, |
233 support_client_side_arrays, | 195 support_client_side_arrays, |
234 command_buffer_.get())); | 196 command_buffer_.get())); |
235 | 197 |
236 if (use_global_share_group) { | |
237 g_all_shared_contexts.Get().insert(this); | |
238 scoped_shared_context_lock.reset(); | |
239 } | |
240 | |
241 if (!gles2_implementation_->Initialize( | 198 if (!gles2_implementation_->Initialize( |
242 mem_limits_.start_transfer_buffer_size, | 199 mem_limits_.start_transfer_buffer_size, |
243 mem_limits_.min_transfer_buffer_size, | 200 mem_limits_.min_transfer_buffer_size, |
244 mem_limits_.max_transfer_buffer_size, | 201 mem_limits_.max_transfer_buffer_size, |
245 mem_limits_.mapped_memory_reclaim_limit)) { | 202 mem_limits_.mapped_memory_reclaim_limit)) { |
246 return false; | 203 return false; |
247 } | 204 } |
248 | 205 |
249 return true; | 206 return true; |
250 } | 207 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 245 } |
289 | 246 |
290 // static | 247 // static |
291 GLInProcessContext* GLInProcessContext::Create( | 248 GLInProcessContext* GLInProcessContext::Create( |
292 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, | 249 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, |
293 scoped_refptr<gfx::GLSurface> surface, | 250 scoped_refptr<gfx::GLSurface> surface, |
294 bool is_offscreen, | 251 bool is_offscreen, |
295 gfx::AcceleratedWidget window, | 252 gfx::AcceleratedWidget window, |
296 const gfx::Size& size, | 253 const gfx::Size& size, |
297 GLInProcessContext* share_context, | 254 GLInProcessContext* share_context, |
298 bool use_global_share_group, | |
299 const ::gpu::gles2::ContextCreationAttribHelper& attribs, | 255 const ::gpu::gles2::ContextCreationAttribHelper& attribs, |
300 gfx::GpuPreference gpu_preference, | 256 gfx::GpuPreference gpu_preference, |
301 const GLInProcessContextSharedMemoryLimits& memory_limits, | 257 const GLInProcessContextSharedMemoryLimits& memory_limits, |
302 GpuMemoryBufferManager* gpu_memory_buffer_manager, | 258 GpuMemoryBufferManager* gpu_memory_buffer_manager, |
303 ImageFactory* image_factory) { | 259 ImageFactory* image_factory) { |
304 DCHECK(!use_global_share_group || !share_context); | |
305 if (surface.get()) { | 260 if (surface.get()) { |
306 DCHECK_EQ(surface->IsOffscreen(), is_offscreen); | 261 DCHECK_EQ(surface->IsOffscreen(), is_offscreen); |
307 DCHECK(surface->GetSize() == size); | 262 DCHECK(surface->GetSize() == size); |
308 DCHECK_EQ(gfx::kNullAcceleratedWidget, window); | 263 DCHECK_EQ(gfx::kNullAcceleratedWidget, window); |
309 } | 264 } |
310 | 265 |
311 scoped_ptr<GLInProcessContextImpl> context( | 266 scoped_ptr<GLInProcessContextImpl> context( |
312 new GLInProcessContextImpl(memory_limits)); | 267 new GLInProcessContextImpl(memory_limits)); |
313 if (!context->Initialize(surface, | 268 if (!context->Initialize(surface, |
314 is_offscreen, | 269 is_offscreen, |
315 use_global_share_group, | |
316 share_context, | 270 share_context, |
317 window, | 271 window, |
318 size, | 272 size, |
319 attribs, | 273 attribs, |
320 gpu_preference, | 274 gpu_preference, |
321 service, | 275 service, |
322 gpu_memory_buffer_manager, | 276 gpu_memory_buffer_manager, |
323 image_factory)) | 277 image_factory)) |
324 return NULL; | 278 return NULL; |
325 | 279 |
326 return context.release(); | 280 return context.release(); |
327 } | 281 } |
328 | 282 |
329 } // namespace gpu | 283 } // namespace gpu |
OLD | NEW |