Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.cc

Issue 14456004: GPU client side changes for GpuMemoryBuffers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@glapi
Patch Set: Incorporate sievers' comments Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h" 5 #include "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
6 6
7 #include <GLES2/gl2.h> 7 #include <GLES2/gl2.h>
8 #ifndef GL_GLEXT_PROTOTYPES 8 #ifndef GL_GLEXT_PROTOTYPES
9 #define GL_GLEXT_PROTOTYPES 1 9 #define GL_GLEXT_PROTOTYPES 1
10 #endif 10 #endif
11 #include <GLES2/gl2ext.h> 11 #include <GLES2/gl2ext.h>
12 #include <GLES2/gl2extchromium.h> 12 #include <GLES2/gl2extchromium.h>
13 13
14 #include <algorithm> 14 #include <algorithm>
15 #include <set> 15 #include <set>
16 #include <string> 16 #include <string>
17 17
18 #include "base/bind.h" 18 #include "base/bind.h"
19 #include "base/bind_helpers.h" 19 #include "base/bind_helpers.h"
20 #include "base/callback.h" 20 #include "base/callback.h"
21 #include "base/command_line.h" 21 #include "base/command_line.h"
22 #include "base/lazy_instance.h" 22 #include "base/lazy_instance.h"
23 #include "base/logging.h" 23 #include "base/logging.h"
24 #include "base/memory/singleton.h" 24 #include "base/memory/singleton.h"
25 #include "base/message_loop.h" 25 #include "base/message_loop.h"
26 #include "base/metrics/histogram.h" 26 #include "base/metrics/histogram.h"
27 #include "base/synchronization/lock.h" 27 #include "base/synchronization/lock.h"
28 #include "gpu/command_buffer/client/gles2_implementation.h" 28 #include "gpu/command_buffer/client/gles2_implementation.h"
29 #include "gpu/command_buffer/client/gles2_lib.h" 29 #include "gpu/command_buffer/client/gles2_lib.h"
30 #include "gpu/command_buffer/client/gpu_memory_buffer_factory.h"
31 #include "gpu/command_buffer/client/image_factory.h"
30 #include "gpu/command_buffer/client/transfer_buffer.h" 32 #include "gpu/command_buffer/client/transfer_buffer.h"
31 #include "gpu/command_buffer/common/constants.h" 33 #include "gpu/command_buffer/common/constants.h"
32 #include "gpu/command_buffer/service/command_buffer_service.h" 34 #include "gpu/command_buffer/service/command_buffer_service.h"
33 #include "gpu/command_buffer/service/context_group.h" 35 #include "gpu/command_buffer/service/context_group.h"
34 #include "gpu/command_buffer/service/gl_context_virtual.h" 36 #include "gpu/command_buffer/service/gl_context_virtual.h"
35 #include "gpu/command_buffer/service/gpu_scheduler.h" 37 #include "gpu/command_buffer/service/gpu_scheduler.h"
38 #include "gpu/command_buffer/service/image_manager.h"
36 #include "gpu/command_buffer/service/transfer_buffer_manager.h" 39 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
37 #include "ui/gl/gl_context.h" 40 #include "ui/gl/gl_context.h"
41 #include "ui/gl/gl_image.h"
38 #include "ui/gl/gl_share_group.h" 42 #include "ui/gl/gl_share_group.h"
39 #include "ui/gl/gl_surface.h" 43 #include "ui/gl/gl_surface.h"
40 #include "webkit/gpu/gl_bindings_skia_cmd_buffer.h" 44 #include "webkit/gpu/gl_bindings_skia_cmd_buffer.h"
41 45
42 using gpu::Buffer; 46 using gpu::Buffer;
43 using gpu::CommandBuffer; 47 using gpu::CommandBuffer;
44 using gpu::CommandBufferService; 48 using gpu::CommandBufferService;
45 using gpu::gles2::GLES2CmdHelper; 49 using gpu::gles2::GLES2CmdHelper;
46 using gpu::gles2::GLES2Implementation; 50 using gpu::gles2::GLES2Implementation;
51 using gpu::gles2::ImageFactory;
52 using gpu::gles2::ImageManager;
53 using gpu::GpuMemoryBuffer;
47 using gpu::GpuScheduler; 54 using gpu::GpuScheduler;
48 using gpu::TransferBuffer; 55 using gpu::TransferBuffer;
49 using gpu::TransferBufferManager; 56 using gpu::TransferBufferManager;
50 using gpu::TransferBufferManagerInterface; 57 using gpu::TransferBufferManagerInterface;
51 58
52 namespace webkit { 59 namespace webkit {
53 namespace gpu { 60 namespace gpu {
54 61
55 class GLInProcessContext { 62 class GLInProcessContext {
56 public: 63 public:
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 void LoseContext(uint32 current, uint32 other); 149 void LoseContext(uint32 current, uint32 other);
143 150
144 void SetSignalSyncPointCallback( 151 void SetSignalSyncPointCallback(
145 scoped_ptr< 152 scoped_ptr<
146 WebKit::WebGraphicsContext3D::WebGraphicsSyncPointCallback> callback); 153 WebKit::WebGraphicsContext3D::WebGraphicsSyncPointCallback> callback);
147 154
148 CommandBufferService* GetCommandBufferService(); 155 CommandBufferService* GetCommandBufferService();
149 156
150 ::gpu::gles2::GLES2Decoder* GetDecoder(); 157 ::gpu::gles2::GLES2Decoder* GetDecoder();
151 158
159 ::gpu::gles2::ImageManager* GetImageManager();
160
152 private: 161 private:
153 explicit GLInProcessContext(bool share_resources); 162 explicit GLInProcessContext(bool share_resources);
154 163
155 bool Initialize(bool is_offscreen, 164 bool Initialize(bool is_offscreen,
156 gfx::AcceleratedWidget window, 165 gfx::AcceleratedWidget window,
157 const gfx::Size& size, 166 const gfx::Size& size,
158 const char* allowed_extensions, 167 const char* allowed_extensions,
159 const int32* attrib_list, 168 const int32* attrib_list,
160 gfx::GpuPreference gpu_preference); 169 gfx::GpuPreference gpu_preference);
161 void Destroy(); 170 void Destroy();
162 171
163 void OnContextLost(); 172 void OnContextLost();
164 173
165 base::Closure context_lost_callback_; 174 base::Closure context_lost_callback_;
166 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; 175 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
167 scoped_ptr<CommandBufferService> command_buffer_; 176 scoped_ptr<CommandBufferService> command_buffer_;
168 scoped_ptr< ::gpu::GpuScheduler> gpu_scheduler_; 177 scoped_ptr< ::gpu::GpuScheduler> gpu_scheduler_;
169 scoped_ptr< ::gpu::gles2::GLES2Decoder> decoder_; 178 scoped_ptr< ::gpu::gles2::GLES2Decoder> decoder_;
170 scoped_refptr<gfx::GLContext> context_; 179 scoped_refptr<gfx::GLContext> context_;
171 scoped_refptr<gfx::GLSurface> surface_; 180 scoped_refptr<gfx::GLSurface> surface_;
172 scoped_ptr<GLES2CmdHelper> gles2_helper_; 181 scoped_ptr<GLES2CmdHelper> gles2_helper_;
173 scoped_ptr<TransferBuffer> transfer_buffer_; 182 scoped_ptr<TransferBuffer> transfer_buffer_;
174 scoped_ptr<GLES2Implementation> gles2_implementation_; 183 scoped_ptr<GLES2Implementation> gles2_implementation_;
184 scoped_refptr< ::gpu::gles2::ImageFactory> image_factory_;
175 scoped_ptr<WebKit::WebGraphicsContext3D::WebGraphicsSyncPointCallback> 185 scoped_ptr<WebKit::WebGraphicsContext3D::WebGraphicsSyncPointCallback>
176 signal_sync_point_callback_; 186 signal_sync_point_callback_;
177 Error last_error_; 187 Error last_error_;
178 bool share_resources_; 188 bool share_resources_;
179 bool context_lost_; 189 bool context_lost_;
180 190
181 DISALLOW_COPY_AND_ASSIGN(GLInProcessContext); 191 DISALLOW_COPY_AND_ASSIGN(GLInProcessContext);
182 }; 192 };
183 193
184 namespace { 194 namespace {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 if (context->GetDecoder()) 290 if (context->GetDecoder())
281 context->GetDecoder()->DetachFromThread(); 291 context->GetDecoder()->DetachFromThread();
282 } 292 }
283 293
284 AutoLockAndDecoderDetachThread::~AutoLockAndDecoderDetachThread() { 294 AutoLockAndDecoderDetachThread::~AutoLockAndDecoderDetachThread() {
285 std::for_each(contexts_.begin(), 295 std::for_each(contexts_.begin(),
286 contexts_.end(), 296 contexts_.end(),
287 &DetachThread); 297 &DetachThread);
288 } 298 }
289 299
300 class ImageFactoryInProcess : public ImageFactory {
301 public:
302 explicit ImageFactoryInProcess(ImageManager* image_manager);
303
304 // methods from ImageFactory
305 virtual scoped_ptr<GpuMemoryBuffer> CreateGpuMemoryBuffer(
306 int width, int height, GLenum internalformat,
307 unsigned* image_id) OVERRIDE;
308 virtual void DeleteGpuMemoryBuffer(unsigned image_id) OVERRIDE;
309 private:
310 virtual ~ImageFactoryInProcess();
311
312 scoped_refptr<ImageManager> image_manager_;
313 unsigned next_id_;
314
315 DISALLOW_COPY_AND_ASSIGN(ImageFactoryInProcess);
316 };
317
318 ImageFactoryInProcess::ImageFactoryInProcess(
319 ImageManager* image_manager) : image_manager_(image_manager),
320 next_id_(0) {
321 }
322
323 ImageFactoryInProcess::~ImageFactoryInProcess() {
324 }
325
326 scoped_ptr<GpuMemoryBuffer> ImageFactoryInProcess::CreateGpuMemoryBuffer(
327 int width, int height, GLenum internalformat, unsigned int* image_id) {
328 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(),
329 g_all_shared_contexts.Get());
330 // TODO(kaanb): Check with reviewers if GL_RGBA8_OES is the right
331 // constant to use. If not which header should we include to make GL_RGBA8
332 // visible?
333 // For Android WebView we assume the |internalformat| will always be
334 // GL_RGBA8_OES.
335 DCHECK_EQ(GL_RGBA8_OES, internalformat);
336 const GpuMemoryBuffer::Creator& create_gpu_memory_buffer_callback =
337 ::gpu::gles2::GetProcessDefaultGpuMemoryBufferFactory();
338 scoped_ptr<GpuMemoryBuffer> buffer =
339 create_gpu_memory_buffer_callback.Run(width, height);
340 scoped_refptr<gfx::GLImage> gl_image =
341 gfx::GLImage::CreateGLImageForGpuMemoryBuffer(buffer->GetNativeBuffer(),
342 gfx::Size(width, height));
343 *image_id = ++next_id_; // Valid image_ids start from 1.
344 image_manager_->AddImage(gl_image, *image_id);
345 return buffer.Pass();
346 }
347
348 void ImageFactoryInProcess::DeleteGpuMemoryBuffer(unsigned int image_id) {
349 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(),
350 g_all_shared_contexts.Get());
351 image_manager_->RemoveImage(image_id);
352 }
353
290 } // namespace 354 } // namespace
291 355
292 static void CallAndDestroy( 356 static void CallAndDestroy(
293 scoped_ptr< 357 scoped_ptr<
294 WebKit::WebGraphicsContext3D::WebGraphicsSyncPointCallback> callback) { 358 WebKit::WebGraphicsContext3D::WebGraphicsSyncPointCallback> callback) {
295 callback->onSyncPointReached(); 359 callback->onSyncPointReached();
296 } 360 }
297 361
298 void GLInProcessContext::PumpCommands() { 362 void GLInProcessContext::PumpCommands() {
299 if (!context_lost_) { 363 if (!context_lost_) {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 474
411 // TODO(gman): Remove This 475 // TODO(gman): Remove This
412 void GLInProcessContext::DisableShaderTranslation() { 476 void GLInProcessContext::DisableShaderTranslation() {
413 NOTREACHED(); 477 NOTREACHED();
414 } 478 }
415 479
416 GLES2Implementation* GLInProcessContext::GetImplementation() { 480 GLES2Implementation* GLInProcessContext::GetImplementation() {
417 return gles2_implementation_.get(); 481 return gles2_implementation_.get();
418 } 482 }
419 483
484 ::gpu::gles2::ImageManager* GLInProcessContext::GetImageManager() {
485 return decoder_->GetContextGroup()->image_manager();
486 }
487
420 GLInProcessContext::GLInProcessContext(bool share_resources) 488 GLInProcessContext::GLInProcessContext(bool share_resources)
421 : last_error_(SUCCESS), 489 : last_error_(SUCCESS),
422 share_resources_(share_resources), 490 share_resources_(share_resources),
423 context_lost_(false) { 491 context_lost_(false) {
424 } 492 }
425 493
426 bool GLInProcessContext::Initialize( 494 bool GLInProcessContext::Initialize(
427 bool is_offscreen, 495 bool is_offscreen,
428 gfx::AcceleratedWidget window, 496 gfx::AcceleratedWidget window,
429 const gfx::Size& size, 497 const gfx::Size& size,
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 // Create the GLES2 helper, which writes the command buffer protocol. 654 // Create the GLES2 helper, which writes the command buffer protocol.
587 gles2_helper_.reset(new GLES2CmdHelper(command_buffer_.get())); 655 gles2_helper_.reset(new GLES2CmdHelper(command_buffer_.get()));
588 if (!gles2_helper_->Initialize(kCommandBufferSize)) { 656 if (!gles2_helper_->Initialize(kCommandBufferSize)) {
589 Destroy(); 657 Destroy();
590 return false; 658 return false;
591 } 659 }
592 660
593 // Create a transfer buffer. 661 // Create a transfer buffer.
594 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); 662 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get()));
595 663
664 if (share_resources_) {
665 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(),
666 g_all_shared_contexts.Get());
667 DCHECK(!g_all_shared_contexts.Get().empty());
668 GLInProcessContext* first_context = *g_all_shared_contexts.Get().begin();
669 image_factory_ = new ImageFactoryInProcess(
670 first_context->GetImageManager());
671 } else {
672 // Create the image factory, this object retains its ownership.
673 image_factory_ = new ImageFactoryInProcess(GetImageManager());
674 }
675
596 // Create the object exposing the OpenGL API. 676 // Create the object exposing the OpenGL API.
597 gles2_implementation_.reset(new GLES2Implementation( 677 gles2_implementation_.reset(new GLES2Implementation(
598 gles2_helper_.get(), 678 gles2_helper_.get(),
599 context_group ? context_group->GetImplementation()->share_group() : NULL, 679 context_group ? context_group->GetImplementation()->share_group() : NULL,
600 transfer_buffer_.get(), 680 transfer_buffer_.get(),
601 true, 681 true,
602 false)); 682 false,
683 image_factory_));
603 684
604 if (!gles2_implementation_->Initialize( 685 if (!gles2_implementation_->Initialize(
605 kStartTransferBufferSize, 686 kStartTransferBufferSize,
606 kMinTransferBufferSize, 687 kMinTransferBufferSize,
607 kMaxTransferBufferSize)) { 688 kMaxTransferBufferSize)) {
608 return false; 689 return false;
609 } 690 }
610 691
611 if (share_resources_) { 692 if (share_resources_) {
612 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(), 693 AutoLockAndDecoderDetachThread lock(g_decoder_lock.Get(),
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 return gl_->glname(a1, a2); \ 1170 return gl_->glname(a1, a2); \
1090 } 1171 }
1091 1172
1092 #define DELEGATE_TO_GL_3(name, glname, t1, t2, t3) \ 1173 #define DELEGATE_TO_GL_3(name, glname, t1, t2, t3) \
1093 void WebGraphicsContext3DInProcessCommandBufferImpl::name( \ 1174 void WebGraphicsContext3DInProcessCommandBufferImpl::name( \
1094 t1 a1, t2 a2, t3 a3) { \ 1175 t1 a1, t2 a2, t3 a3) { \
1095 ClearContext(); \ 1176 ClearContext(); \
1096 gl_->glname(a1, a2, a3); \ 1177 gl_->glname(a1, a2, a3); \
1097 } 1178 }
1098 1179
1180 #define DELEGATE_TO_GL_3R(name, glname, t1, t2, t3, rt) \
1181 rt WebGraphicsContext3DInProcessCommandBufferImpl::name( \
1182 t1 a1, t2 a2, t3 a3) { \
1183 ClearContext(); \
1184 return gl_->glname(a1, a2, a3); \
1185 }
1186
1099 #define DELEGATE_TO_GL_4(name, glname, t1, t2, t3, t4) \ 1187 #define DELEGATE_TO_GL_4(name, glname, t1, t2, t3, t4) \
1100 void WebGraphicsContext3DInProcessCommandBufferImpl::name( \ 1188 void WebGraphicsContext3DInProcessCommandBufferImpl::name( \
1101 t1 a1, t2 a2, t3 a3, t4 a4) { \ 1189 t1 a1, t2 a2, t3 a3, t4 a4) { \
1102 ClearContext(); \ 1190 ClearContext(); \
1103 gl_->glname(a1, a2, a3, a4); \ 1191 gl_->glname(a1, a2, a3, a4); \
1104 } 1192 }
1105 1193
1106 #define DELEGATE_TO_GL_5(name, glname, t1, t2, t3, t4, t5) \ 1194 #define DELEGATE_TO_GL_5(name, glname, t1, t2, t3, t4, t5) \
1107 void WebGraphicsContext3DInProcessCommandBufferImpl::name( \ 1195 void WebGraphicsContext3DInProcessCommandBufferImpl::name( \
1108 t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) { \ 1196 t1 a1, t2 a2, t3 a3, t4 a4, t5 a5) { \
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
1822 } 1910 }
1823 1911
1824 void WebGraphicsContext3DInProcessCommandBufferImpl::OnContextLost() { 1912 void WebGraphicsContext3DInProcessCommandBufferImpl::OnContextLost() {
1825 // TODO(kbr): improve the precision here. 1913 // TODO(kbr): improve the precision here.
1826 context_lost_reason_ = GL_UNKNOWN_CONTEXT_RESET_ARB; 1914 context_lost_reason_ = GL_UNKNOWN_CONTEXT_RESET_ARB;
1827 if (context_lost_callback_) { 1915 if (context_lost_callback_) {
1828 context_lost_callback_->onContextLost(); 1916 context_lost_callback_->onContextLost();
1829 } 1917 }
1830 } 1918 }
1831 1919
1920 DELEGATE_TO_GL_3R(createImageCHROMIUM, CreateImageCHROMIUM,
1921 WGC3Dsizei, WGC3Dsizei, WGC3Denum, WGC3Duint);
1922
1923 DELEGATE_TO_GL_1(destroyImageCHROMIUM, DestroyImageCHROMIUM, WGC3Duint);
1924
1925 DELEGATE_TO_GL_3(getImageParameterivCHROMIUM, GetImageParameterivCHROMIUM,
1926 WGC3Duint, WGC3Denum, GLint*);
1927
1928 DELEGATE_TO_GL_2R(mapImageCHROMIUM, MapImageCHROMIUM,
1929 WGC3Duint, WGC3Denum, void*);
1930
1931 DELEGATE_TO_GL_1(unmapImageCHROMIUM, UnmapImageCHROMIUM, WGC3Duint);
1932
1832 DELEGATE_TO_GL_3(bindUniformLocationCHROMIUM, BindUniformLocationCHROMIUM, 1933 DELEGATE_TO_GL_3(bindUniformLocationCHROMIUM, BindUniformLocationCHROMIUM,
1833 WebGLId, WGC3Dint, const WGC3Dchar*) 1934 WebGLId, WGC3Dint, const WGC3Dchar*)
1834 1935
1835 DELEGATE_TO_GL(shallowFlushCHROMIUM, ShallowFlushCHROMIUM) 1936 DELEGATE_TO_GL(shallowFlushCHROMIUM, ShallowFlushCHROMIUM)
1836 1937
1837 DELEGATE_TO_GL_1(genMailboxCHROMIUM, GenMailboxCHROMIUM, WGC3Dbyte*) 1938 DELEGATE_TO_GL_1(genMailboxCHROMIUM, GenMailboxCHROMIUM, WGC3Dbyte*)
1838 DELEGATE_TO_GL_2(produceTextureCHROMIUM, ProduceTextureCHROMIUM, 1939 DELEGATE_TO_GL_2(produceTextureCHROMIUM, ProduceTextureCHROMIUM,
1839 WGC3Denum, const WGC3Dbyte*) 1940 WGC3Denum, const WGC3Dbyte*)
1840 DELEGATE_TO_GL_2(consumeTextureCHROMIUM, ConsumeTextureCHROMIUM, 1941 DELEGATE_TO_GL_2(consumeTextureCHROMIUM, ConsumeTextureCHROMIUM,
1841 WGC3Denum, const WGC3Dbyte*) 1942 WGC3Denum, const WGC3Dbyte*)
(...skipping 26 matching lines...) Expand all
1868 1969
1869 DELEGATE_TO_GL_9(asyncTexSubImage2DCHROMIUM, AsyncTexSubImage2DCHROMIUM, 1970 DELEGATE_TO_GL_9(asyncTexSubImage2DCHROMIUM, AsyncTexSubImage2DCHROMIUM,
1870 WGC3Denum, WGC3Dint, WGC3Dint, WGC3Dint, WGC3Dsizei, WGC3Dsizei, 1971 WGC3Denum, WGC3Dint, WGC3Dint, WGC3Dint, WGC3Dsizei, WGC3Dsizei,
1871 WGC3Denum, WGC3Denum, const void*) 1972 WGC3Denum, WGC3Denum, const void*)
1872 1973
1873 DELEGATE_TO_GL_1(waitAsyncTexImage2DCHROMIUM, WaitAsyncTexImage2DCHROMIUM, 1974 DELEGATE_TO_GL_1(waitAsyncTexImage2DCHROMIUM, WaitAsyncTexImage2DCHROMIUM,
1874 WGC3Denum) 1975 WGC3Denum)
1875 1976
1876 } // namespace gpu 1977 } // namespace gpu
1877 } // namespace webkit 1978 } // namespace webkit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698