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 <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include <GLES2/gl2.h> | 10 #include <GLES2/gl2.h> |
11 #ifndef GL_GLEXT_PROTOTYPES | 11 #ifndef GL_GLEXT_PROTOTYPES |
12 #define GL_GLEXT_PROTOTYPES 1 | 12 #define GL_GLEXT_PROTOTYPES 1 |
13 #endif | 13 #endif |
14 #include <GLES2/gl2ext.h> | 14 #include <GLES2/gl2ext.h> |
15 #include <GLES2/gl2extchromium.h> | 15 #include <GLES2/gl2extchromium.h> |
16 | 16 |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/bind_helpers.h" | 18 #include "base/bind_helpers.h" |
19 #include "base/callback.h" | 19 #include "base/callback.h" |
20 #include "base/lazy_instance.h" | 20 #include "base/lazy_instance.h" |
21 #include "base/logging.h" | 21 #include "base/logging.h" |
22 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
23 #include "base/memory/weak_ptr.h" | 23 #include "base/memory/weak_ptr.h" |
24 #include "base/message_loop/message_loop.h" | 24 #include "base/message_loop/message_loop.h" |
25 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
26 #include "gpu/command_buffer/client/gles2_implementation.h" | 26 #include "gpu/command_buffer/client/gles2_implementation.h" |
27 #include "gpu/command_buffer/client/gpu_memory_buffer_factory.h" | |
28 #include "gpu/command_buffer/client/image_factory.h" | |
29 #include "gpu/command_buffer/client/transfer_buffer.h" | 27 #include "gpu/command_buffer/client/transfer_buffer.h" |
30 #include "gpu/command_buffer/common/constants.h" | 28 #include "gpu/command_buffer/common/constants.h" |
31 #include "gpu/command_buffer/common/id_allocator.h" | |
32 #include "gpu/command_buffer/service/command_buffer_service.h" | 29 #include "gpu/command_buffer/service/command_buffer_service.h" |
33 #include "gpu/command_buffer/service/context_group.h" | 30 #include "gpu/command_buffer/service/context_group.h" |
34 #include "gpu/command_buffer/service/gl_context_virtual.h" | 31 #include "gpu/command_buffer/service/gl_context_virtual.h" |
35 #include "gpu/command_buffer/service/gpu_scheduler.h" | 32 #include "gpu/command_buffer/service/gpu_scheduler.h" |
36 #include "gpu/command_buffer/service/image_manager.h" | 33 #include "gpu/command_buffer/service/image_manager.h" |
37 #include "gpu/command_buffer/service/transfer_buffer_manager.h" | 34 #include "gpu/command_buffer/service/transfer_buffer_manager.h" |
38 #include "ui/gfx/gpu_memory_buffer.h" | |
39 #include "ui/gfx/size.h" | 35 #include "ui/gfx/size.h" |
40 #include "ui/gl/gl_context.h" | 36 #include "ui/gl/gl_context.h" |
41 #include "ui/gl/gl_image.h" | 37 #include "ui/gl/gl_image.h" |
42 #include "ui/gl/gl_share_group.h" | 38 #include "ui/gl/gl_share_group.h" |
43 #include "ui/gl/gl_surface.h" | 39 #include "ui/gl/gl_surface.h" |
44 | 40 |
45 namespace gpu { | 41 namespace gpu { |
46 | 42 |
47 using gles2::ImageManager; | |
48 | |
49 namespace { | 43 namespace { |
50 | 44 |
51 const int32 kCommandBufferSize = 1024 * 1024; | 45 const int32 kCommandBufferSize = 1024 * 1024; |
52 // TODO(kbr): make the transfer buffer size configurable via context | 46 // TODO(kbr): make the transfer buffer size configurable via context |
53 // creation attributes. | 47 // creation attributes. |
54 const size_t kStartTransferBufferSize = 4 * 1024 * 1024; | 48 const size_t kStartTransferBufferSize = 4 * 1024 * 1024; |
55 const size_t kMinTransferBufferSize = 1 * 256 * 1024; | 49 const size_t kMinTransferBufferSize = 1 * 256 * 1024; |
56 const size_t kMaxTransferBufferSize = 16 * 1024 * 1024; | 50 const size_t kMaxTransferBufferSize = 16 * 1024 * 1024; |
57 | 51 |
58 // In the normal command buffer implementation, all commands are passed over IPC | 52 // In the normal command buffer implementation, all commands are passed over IPC |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 }; | 85 }; |
92 | 86 |
93 size_t SharedContextCount() { | 87 size_t SharedContextCount() { |
94 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(), | 88 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(), |
95 g_all_shared_contexts.Get()); | 89 g_all_shared_contexts.Get()); |
96 return g_all_shared_contexts.Get().size(); | 90 return g_all_shared_contexts.Get().size(); |
97 } | 91 } |
98 | 92 |
99 class GLInProcessContextImpl | 93 class GLInProcessContextImpl |
100 : public GLInProcessContext, | 94 : public GLInProcessContext, |
101 public gles2::ImageFactory, | |
102 public base::SupportsWeakPtr<GLInProcessContextImpl> { | 95 public base::SupportsWeakPtr<GLInProcessContextImpl> { |
103 public: | 96 public: |
104 explicit GLInProcessContextImpl(bool share_resources); | 97 explicit GLInProcessContextImpl(bool share_resources); |
105 virtual ~GLInProcessContextImpl(); | 98 virtual ~GLInProcessContextImpl(); |
106 | 99 |
107 bool Initialize(bool is_offscreen, | 100 bool Initialize(bool is_offscreen, |
108 gfx::AcceleratedWidget window, | 101 gfx::AcceleratedWidget window, |
109 const gfx::Size& size, | 102 const gfx::Size& size, |
110 const char* allowed_extensions, | 103 const char* allowed_extensions, |
111 const int32* attrib_list, | 104 const int32* attrib_list, |
112 gfx::GpuPreference gpu_preference, | 105 gfx::GpuPreference gpu_preference, |
113 const base::Closure& context_lost_callback); | 106 const base::Closure& context_lost_callback); |
114 | 107 |
115 // GLInProcessContext implementation: | 108 // GLInProcessContext implementation: |
116 virtual void SignalSyncPoint(unsigned sync_point, | 109 virtual void SignalSyncPoint(unsigned sync_point, |
117 const base::Closure& callback) OVERRIDE; | 110 const base::Closure& callback) OVERRIDE; |
118 virtual void SignalQuery(unsigned query, const base::Closure& callback) | 111 virtual void SignalQuery(unsigned query, const base::Closure& callback) |
119 OVERRIDE; | 112 OVERRIDE; |
120 virtual gles2::GLES2Implementation* GetImplementation() OVERRIDE; | 113 virtual gles2::GLES2Implementation* GetImplementation() OVERRIDE; |
121 | 114 |
122 // ImageFactory implementation: | |
123 virtual scoped_ptr<gfx::GpuMemoryBuffer> CreateGpuMemoryBuffer( | |
124 int width, int height, GLenum internalformat, | |
125 unsigned* image_id) OVERRIDE; | |
126 virtual void DeleteGpuMemoryBuffer(unsigned image_id) OVERRIDE; | |
127 | |
128 // Other methods: | 115 // Other methods: |
129 gles2::GLES2Decoder* GetDecoder(); | 116 gles2::GLES2Decoder* GetDecoder(); |
130 bool GetBufferChanged(int32 transfer_buffer_id); | 117 bool GetBufferChanged(int32 transfer_buffer_id); |
131 void PumpCommands(); | 118 void PumpCommands(); |
132 void OnResizeView(gfx::Size size, float scale_factor); | 119 void OnResizeView(gfx::Size size, float scale_factor); |
133 void OnContextLost(); | 120 void OnContextLost(); |
134 | 121 |
135 private: | 122 private: |
136 void Destroy(); | 123 void Destroy(); |
137 bool IsCommandBufferContextLost(); | 124 bool IsCommandBufferContextLost(); |
138 void PollQueryCallbacks(); | 125 void PollQueryCallbacks(); |
139 void CallQueryCallback(size_t index); | 126 void CallQueryCallback(size_t index); |
140 bool MakeCurrent(); | 127 bool MakeCurrent(); |
141 | 128 |
142 gles2::ImageManager* GetImageManager(); | |
143 | |
144 base::Closure context_lost_callback_; | 129 base::Closure context_lost_callback_; |
145 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; | 130 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; |
| 131 scoped_refptr<gles2::ImageManager> image_manager_; |
146 scoped_ptr<CommandBuffer> command_buffer_; | 132 scoped_ptr<CommandBuffer> command_buffer_; |
147 scoped_ptr<GpuScheduler> gpu_scheduler_; | 133 scoped_ptr<GpuScheduler> gpu_scheduler_; |
148 scoped_ptr<gles2::GLES2Decoder> decoder_; | 134 scoped_ptr<gles2::GLES2Decoder> decoder_; |
149 scoped_refptr<gfx::GLContext> context_; | 135 scoped_refptr<gfx::GLContext> context_; |
150 scoped_refptr<gfx::GLSurface> surface_; | 136 scoped_refptr<gfx::GLSurface> surface_; |
151 scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_; | 137 scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_; |
152 scoped_ptr<TransferBuffer> transfer_buffer_; | 138 scoped_ptr<TransferBuffer> transfer_buffer_; |
153 scoped_ptr<gles2::GLES2Implementation> gles2_implementation_; | 139 scoped_ptr<gles2::GLES2Implementation> gles2_implementation_; |
154 bool share_resources_; | 140 bool share_resources_; |
155 bool context_lost_; | 141 bool context_lost_; |
(...skipping 17 matching lines...) Expand all Loading... |
173 if (context->GetDecoder()) | 159 if (context->GetDecoder()) |
174 context->GetDecoder()->DetachFromThreadHack(); | 160 context->GetDecoder()->DetachFromThreadHack(); |
175 } | 161 } |
176 | 162 |
177 AutoLockAndDecoderDetachThread::~AutoLockAndDecoderDetachThread() { | 163 AutoLockAndDecoderDetachThread::~AutoLockAndDecoderDetachThread() { |
178 std::for_each(contexts_.begin(), | 164 std::for_each(contexts_.begin(), |
179 contexts_.end(), | 165 contexts_.end(), |
180 &DetachThread); | 166 &DetachThread); |
181 } | 167 } |
182 | 168 |
183 scoped_ptr<gfx::GpuMemoryBuffer> GLInProcessContextImpl::CreateGpuMemoryBuffer( | |
184 int width, int height, GLenum internalformat, unsigned int* image_id) { | |
185 // We're taking the lock here because we're accessing the ContextGroup's | |
186 // shared IdManager. | |
187 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(), | |
188 g_all_shared_contexts.Get()); | |
189 scoped_ptr<gfx::GpuMemoryBuffer> buffer( | |
190 g_gpu_memory_buffer_factory->CreateGpuMemoryBuffer(width, | |
191 height, | |
192 internalformat)); | |
193 if (!buffer) | |
194 return scoped_ptr<gfx::GpuMemoryBuffer>(); | |
195 | |
196 scoped_refptr<gfx::GLImage> gl_image = | |
197 gfx::GLImage::CreateGLImageForGpuMemoryBuffer(buffer->GetHandle(), | |
198 gfx::Size(width, height)); | |
199 *image_id = decoder_->GetContextGroup() | |
200 ->GetIdAllocator(gles2::id_namespaces::kImages)->AllocateID(); | |
201 GetImageManager()->AddImage(gl_image.get(), *image_id); | |
202 return buffer.Pass(); | |
203 } | |
204 | |
205 void GLInProcessContextImpl::DeleteGpuMemoryBuffer(unsigned int image_id) { | |
206 // We're taking the lock here because we're accessing the ContextGroup's | |
207 // shared ImageManager. | |
208 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(), | |
209 g_all_shared_contexts.Get()); | |
210 GetImageManager()->RemoveImage(image_id); | |
211 decoder_->GetContextGroup()->GetIdAllocator(gles2::id_namespaces::kImages) | |
212 ->FreeID(image_id); | |
213 } | |
214 | |
215 GLInProcessContextImpl::GLInProcessContextImpl(bool share_resources) | 169 GLInProcessContextImpl::GLInProcessContextImpl(bool share_resources) |
216 : share_resources_(share_resources), | 170 : share_resources_(share_resources), |
217 context_lost_(false) { | 171 context_lost_(false) { |
218 } | 172 } |
219 | 173 |
220 GLInProcessContextImpl::~GLInProcessContextImpl() { | 174 GLInProcessContextImpl::~GLInProcessContextImpl() { |
221 Destroy(); | 175 Destroy(); |
222 } | 176 } |
223 | 177 |
224 bool GLInProcessContextImpl::MakeCurrent() { | 178 bool GLInProcessContextImpl::MakeCurrent() { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 229 |
276 void GLInProcessContextImpl::OnResizeView(gfx::Size size, float scale_factor) { | 230 void GLInProcessContextImpl::OnResizeView(gfx::Size size, float scale_factor) { |
277 DCHECK(!surface_->IsOffscreen()); | 231 DCHECK(!surface_->IsOffscreen()); |
278 surface_->Resize(size); | 232 surface_->Resize(size); |
279 } | 233 } |
280 | 234 |
281 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() { | 235 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() { |
282 return gles2_implementation_.get(); | 236 return gles2_implementation_.get(); |
283 } | 237 } |
284 | 238 |
285 gles2::ImageManager* GLInProcessContextImpl::GetImageManager() { | |
286 return decoder_->GetContextGroup()->image_manager(); | |
287 } | |
288 | |
289 bool GLInProcessContextImpl::Initialize( | 239 bool GLInProcessContextImpl::Initialize( |
290 bool is_offscreen, | 240 bool is_offscreen, |
291 gfx::AcceleratedWidget window, | 241 gfx::AcceleratedWidget window, |
292 const gfx::Size& size, | 242 const gfx::Size& size, |
293 const char* allowed_extensions, | 243 const char* allowed_extensions, |
294 const int32* attrib_list, | 244 const int32* attrib_list, |
295 gfx::GpuPreference gpu_preference, | 245 gfx::GpuPreference gpu_preference, |
296 const base::Closure& context_lost_callback) { | 246 const base::Closure& context_lost_callback) { |
297 // Use one share group for all contexts. | 247 // Use one share group for all contexts. |
298 CR_DEFINE_STATIC_LOCAL(scoped_refptr<gfx::GLShareGroup>, share_group, | 248 CR_DEFINE_STATIC_LOCAL(scoped_refptr<gfx::GLShareGroup>, share_group, |
(...skipping 27 matching lines...) Expand all Loading... |
326 break; | 276 break; |
327 } | 277 } |
328 } | 278 } |
329 | 279 |
330 { | 280 { |
331 TransferBufferManager* manager = new TransferBufferManager(); | 281 TransferBufferManager* manager = new TransferBufferManager(); |
332 transfer_buffer_manager_.reset(manager); | 282 transfer_buffer_manager_.reset(manager); |
333 manager->Initialize(); | 283 manager->Initialize(); |
334 } | 284 } |
335 | 285 |
| 286 image_manager_ = new gles2::ImageManager(); |
| 287 |
336 scoped_ptr<CommandBufferService> command_buffer( | 288 scoped_ptr<CommandBufferService> command_buffer( |
337 new CommandBufferService(transfer_buffer_manager_.get())); | 289 new CommandBufferService(transfer_buffer_manager_.get(), |
| 290 image_manager_.get(), |
| 291 g_gpu_memory_buffer_factory)); |
338 command_buffer->SetPutOffsetChangeCallback(base::Bind( | 292 command_buffer->SetPutOffsetChangeCallback(base::Bind( |
339 &GLInProcessContextImpl::PumpCommands, base::Unretained(this))); | 293 &GLInProcessContextImpl::PumpCommands, base::Unretained(this))); |
340 command_buffer->SetGetBufferChangeCallback(base::Bind( | 294 command_buffer->SetGetBufferChangeCallback(base::Bind( |
341 &GLInProcessContextImpl::GetBufferChanged, base::Unretained(this))); | 295 &GLInProcessContextImpl::GetBufferChanged, base::Unretained(this))); |
342 command_buffer->SetParseErrorCallback(base::Bind( | 296 command_buffer->SetParseErrorCallback(base::Bind( |
343 &GLInProcessContextImpl::OnContextLost, base::Unretained(this))); | 297 &GLInProcessContextImpl::OnContextLost, base::Unretained(this))); |
344 | 298 |
345 command_buffer_ = command_buffer.Pass(); | 299 command_buffer_ = command_buffer.Pass(); |
346 if (!command_buffer_->Initialize()) { | 300 if (!command_buffer_->Initialize()) { |
347 LOG(ERROR) << "Could not initialize command buffer."; | 301 LOG(ERROR) << "Could not initialize command buffer."; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 } | 408 } |
455 | 409 |
456 // Create a transfer buffer. | 410 // Create a transfer buffer. |
457 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); | 411 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); |
458 | 412 |
459 // Create the object exposing the OpenGL API. | 413 // Create the object exposing the OpenGL API. |
460 gles2_implementation_.reset(new gles2::GLES2Implementation( | 414 gles2_implementation_.reset(new gles2::GLES2Implementation( |
461 gles2_helper_.get(), | 415 gles2_helper_.get(), |
462 context_group ? context_group->GetImplementation()->share_group() : NULL, | 416 context_group ? context_group->GetImplementation()->share_group() : NULL, |
463 transfer_buffer_.get(), | 417 transfer_buffer_.get(), |
464 false, | 418 false)); |
465 this)); | |
466 | 419 |
467 if (!gles2_implementation_->Initialize( | 420 if (!gles2_implementation_->Initialize( |
468 kStartTransferBufferSize, | 421 kStartTransferBufferSize, |
469 kMinTransferBufferSize, | 422 kMinTransferBufferSize, |
470 kMaxTransferBufferSize)) { | 423 kMaxTransferBufferSize)) { |
471 return false; | 424 return false; |
472 } | 425 } |
473 | 426 |
474 if (share_resources_) { | 427 if (share_resources_) { |
475 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(), | 428 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(), |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 g_gpu_memory_buffer_factory = factory; | 556 g_gpu_memory_buffer_factory = factory; |
604 } | 557 } |
605 | 558 |
606 // static | 559 // static |
607 void GLInProcessContext::EnableVirtualizedContext() { | 560 void GLInProcessContext::EnableVirtualizedContext() { |
608 DCHECK_EQ(0u, SharedContextCount()); | 561 DCHECK_EQ(0u, SharedContextCount()); |
609 g_use_virtualized_gl_context = true; | 562 g_use_virtualized_gl_context = true; |
610 } | 563 } |
611 | 564 |
612 } // namespace gpu | 565 } // namespace gpu |
OLD | NEW |