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 "content/browser/renderer_host/compositor_impl_android.h" | 5 #include "content/browser/renderer_host/compositor_impl_android.h" |
6 | 6 |
7 #include <android/bitmap.h> | 7 #include <android/bitmap.h> |
8 #include <android/native_window_jni.h> | 8 #include <android/native_window_jni.h> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "cc/input/input_handler.h" | 20 #include "cc/input/input_handler.h" |
21 #include "cc/layers/layer.h" | 21 #include "cc/layers/layer.h" |
22 #include "cc/output/compositor_frame.h" | 22 #include "cc/output/compositor_frame.h" |
23 #include "cc/output/context_provider.h" | 23 #include "cc/output/context_provider.h" |
24 #include "cc/output/output_surface.h" | 24 #include "cc/output/output_surface.h" |
25 #include "cc/trees/layer_tree_host.h" | 25 #include "cc/trees/layer_tree_host.h" |
26 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | 26 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
27 #include "content/browser/gpu/gpu_surface_tracker.h" | 27 #include "content/browser/gpu/gpu_surface_tracker.h" |
28 #include "content/common/gpu/client/command_buffer_proxy_impl.h" | 28 #include "content/common/gpu/client/command_buffer_proxy_impl.h" |
| 29 #include "content/common/gpu/client/context_provider_command_buffer.h" |
29 #include "content/common/gpu/client/gl_helper.h" | 30 #include "content/common/gpu/client/gl_helper.h" |
30 #include "content/common/gpu/client/gpu_channel_host.h" | 31 #include "content/common/gpu/client/gpu_channel_host.h" |
31 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | 32 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" |
32 #include "content/common/gpu/gpu_process_launch_causes.h" | 33 #include "content/common/gpu/gpu_process_launch_causes.h" |
33 #include "content/public/browser/android/compositor_client.h" | 34 #include "content/public/browser/android/compositor_client.h" |
34 #include "content/public/common/content_switches.h" | 35 #include "content/public/common/content_switches.h" |
35 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | 36 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
36 #include "third_party/khronos/GLES2/gl2.h" | 37 #include "third_party/khronos/GLES2/gl2.h" |
37 #include "third_party/khronos/GLES2/gl2ext.h" | 38 #include "third_party/khronos/GLES2/gl2ext.h" |
38 #include "ui/gfx/android/device_display_info.h" | 39 #include "ui/gfx/android/device_display_info.h" |
39 #include "ui/gfx/android/java_bitmap.h" | 40 #include "ui/gfx/android/java_bitmap.h" |
| 41 #include "webkit/common/gpu/context_provider_in_process.h" |
40 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" | 42 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" |
41 | 43 |
42 namespace gfx { | 44 namespace gfx { |
43 class JavaBitmap; | 45 class JavaBitmap; |
44 } | 46 } |
45 | 47 |
46 namespace { | 48 namespace { |
47 | 49 |
48 // Used for drawing directly to the screen. Bypasses resizing and swaps. | 50 // Used for drawing directly to the screen. Bypasses resizing and swaps. |
49 class DirectOutputSurface : public cc::OutputSurface { | 51 class DirectOutputSurface : public cc::OutputSurface { |
50 public: | 52 public: |
51 DirectOutputSurface(scoped_ptr<WebKit::WebGraphicsContext3D> context3d) | 53 DirectOutputSurface( |
52 : cc::OutputSurface(context3d.Pass()) { | 54 const scoped_refptr<cc::ContextProvider>& context_provider) |
| 55 : cc::OutputSurface(context_provider) { |
53 capabilities_.adjust_deadline_for_parent = false; | 56 capabilities_.adjust_deadline_for_parent = false; |
54 } | 57 } |
55 | 58 |
56 virtual void Reshape(gfx::Size size, float scale_factor) OVERRIDE { | 59 virtual void Reshape(gfx::Size size, float scale_factor) OVERRIDE { |
57 surface_size_ = size; | 60 surface_size_ = size; |
58 } | 61 } |
59 virtual void SwapBuffers(cc::CompositorFrame*) OVERRIDE { | 62 virtual void SwapBuffers(cc::CompositorFrame*) OVERRIDE { |
60 context3d()->shallowFlushCHROMIUM(); | 63 context_provider_->Context3d()->shallowFlushCHROMIUM(); |
61 } | 64 } |
62 }; | 65 }; |
63 | 66 |
64 // Used to override capabilities_.adjust_deadline_for_parent to false | 67 // Used to override capabilities_.adjust_deadline_for_parent to false |
65 class OutputSurfaceWithoutParent : public cc::OutputSurface { | 68 class OutputSurfaceWithoutParent : public cc::OutputSurface { |
66 public: | 69 public: |
67 OutputSurfaceWithoutParent(scoped_ptr<WebKit::WebGraphicsContext3D> context3d) | 70 OutputSurfaceWithoutParent( |
68 : cc::OutputSurface(context3d.Pass()) { | 71 const scoped_refptr< |
| 72 content::ContextProviderCommandBuffer>& context_provider) |
| 73 : cc::OutputSurface(context_provider) { |
69 capabilities_.adjust_deadline_for_parent = false; | 74 capabilities_.adjust_deadline_for_parent = false; |
70 } | 75 } |
71 | 76 |
72 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE { | 77 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE { |
73 content::WebGraphicsContext3DCommandBufferImpl* command_buffer = | 78 content::WebGraphicsContext3DCommandBufferImpl* command_buffer_context = |
74 static_cast<content::WebGraphicsContext3DCommandBufferImpl*>(context3d()); | 79 static_cast<content::WebGraphicsContext3DCommandBufferImpl*>( |
| 80 context_provider_->Context3d()); |
75 content::CommandBufferProxyImpl* command_buffer_proxy = | 81 content::CommandBufferProxyImpl* command_buffer_proxy = |
76 command_buffer->GetCommandBufferProxy(); | 82 command_buffer_context->GetCommandBufferProxy(); |
77 DCHECK(command_buffer_proxy); | 83 DCHECK(command_buffer_proxy); |
78 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); | 84 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); |
79 | 85 |
80 OutputSurface::SwapBuffers(frame); | 86 OutputSurface::SwapBuffers(frame); |
81 } | 87 } |
82 }; | 88 }; |
83 | 89 |
84 static bool g_initialized = false; | 90 static bool g_initialized = false; |
85 static base::Thread* g_impl_thread = NULL; | 91 static base::Thread* g_impl_thread = NULL; |
86 static bool g_use_direct_gl = false; | 92 static bool g_use_direct_gl = false; |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 DCHECK(bitmap.size() == sub_rect.size()); | 357 DCHECK(bitmap.size() == sub_rect.size()); |
352 if (bitmap.size() != sub_rect.size() || texture_id == 0) return false; | 358 if (bitmap.size() != sub_rect.size() || texture_id == 0) return false; |
353 | 359 |
354 GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper(); | 360 GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper(); |
355 helper->ReadbackTextureSync(texture_id, | 361 helper->ReadbackTextureSync(texture_id, |
356 sub_rect, | 362 sub_rect, |
357 static_cast<unsigned char*> (bitmap.pixels())); | 363 static_cast<unsigned char*> (bitmap.pixels())); |
358 return true; | 364 return true; |
359 } | 365 } |
360 | 366 |
| 367 static scoped_ptr<webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl> |
| 368 CreateInProcessViewContext( |
| 369 const WebKit::WebGraphicsContext3D::Attributes attributes, |
| 370 ANativeWindow* window) { |
| 371 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; |
| 372 return WebGraphicsContext3DInProcessCommandBufferImpl::CreateViewContext( |
| 373 attributes, window).Pass(); |
| 374 } |
| 375 |
| 376 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> |
| 377 CreateGpuProcessViewContext( |
| 378 const WebKit::WebGraphicsContext3D::Attributes attributes, |
| 379 int surface_id, |
| 380 base::WeakPtr<CompositorImpl> compositor_impl) { |
| 381 GpuChannelHostFactory* factory = BrowserGpuChannelHostFactory::instance(); |
| 382 GURL url("chrome://gpu/Compositor::createContext3D"); |
| 383 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context( |
| 384 new WebGraphicsContext3DCommandBufferImpl(surface_id, |
| 385 url, |
| 386 factory, |
| 387 compositor_impl)); |
| 388 static const size_t kBytesPerPixel = 4; |
| 389 gfx::DeviceDisplayInfo display_info; |
| 390 size_t full_screen_texture_size_in_bytes = |
| 391 display_info.GetDisplayHeight() * |
| 392 display_info.GetDisplayWidth() * |
| 393 kBytesPerPixel; |
| 394 if (!context->Initialize( |
| 395 attributes, |
| 396 false, |
| 397 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE, |
| 398 64 * 1024, // command buffer size |
| 399 std::min(full_screen_texture_size_in_bytes, |
| 400 kDefaultStartTransferBufferSize), |
| 401 kDefaultMinTransferBufferSize, |
| 402 std::min(3 * full_screen_texture_size_in_bytes, |
| 403 kDefaultMaxTransferBufferSize))) { |
| 404 LOG(ERROR) << "Failed to create 3D context for compositor."; |
| 405 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>(); |
| 406 } |
| 407 return context.Pass(); |
| 408 } |
| 409 |
361 scoped_ptr<cc::OutputSurface> CompositorImpl::CreateOutputSurface( | 410 scoped_ptr<cc::OutputSurface> CompositorImpl::CreateOutputSurface( |
362 bool fallback) { | 411 bool fallback) { |
363 WebKit::WebGraphicsContext3D::Attributes attrs; | 412 WebKit::WebGraphicsContext3D::Attributes attrs; |
364 attrs.shareResources = true; | 413 attrs.shareResources = true; |
365 attrs.noAutomaticFlushes = true; | 414 attrs.noAutomaticFlushes = true; |
366 | 415 |
367 if (g_use_direct_gl) { | 416 if (g_use_direct_gl) { |
368 scoped_ptr<WebKit::WebGraphicsContext3D> context( | 417 scoped_refptr<webkit::gpu::ContextProviderInProcess> context_provider = |
369 webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl:: | 418 webkit::gpu::ContextProviderInProcess::Create( |
370 CreateViewContext(attrs, window_)); | 419 base::Bind(&CreateInProcessViewContext, attrs, window_)); |
371 if (!window_) { | |
372 return scoped_ptr<cc::OutputSurface>( | |
373 new DirectOutputSurface(context.Pass())); | |
374 } | |
375 | 420 |
376 return make_scoped_ptr(new cc::OutputSurface(context.Pass())); | 421 scoped_ptr<cc::OutputSurface> output_surface; |
377 } else { | 422 if (!window_) |
378 DCHECK(window_ && surface_id_); | 423 output_surface.reset(new DirectOutputSurface(context_provider)); |
379 GpuChannelHostFactory* factory = BrowserGpuChannelHostFactory::instance(); | 424 else |
380 GURL url("chrome://gpu/Compositor::createContext3D"); | 425 output_surface.reset(new cc::OutputSurface(context_provider)); |
381 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context( | 426 return output_surface.Pass(); |
382 new WebGraphicsContext3DCommandBufferImpl(surface_id_, | |
383 url, | |
384 factory, | |
385 weak_factory_.GetWeakPtr())); | |
386 static const size_t kBytesPerPixel = 4; | |
387 gfx::DeviceDisplayInfo display_info; | |
388 size_t full_screen_texture_size_in_bytes = | |
389 display_info.GetDisplayHeight() * | |
390 display_info.GetDisplayWidth() * | |
391 kBytesPerPixel; | |
392 if (!context->Initialize( | |
393 attrs, | |
394 false, | |
395 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE, | |
396 64 * 1024, // command buffer size | |
397 std::min(full_screen_texture_size_in_bytes, | |
398 kDefaultStartTransferBufferSize), | |
399 kDefaultMinTransferBufferSize, | |
400 std::min(3 * full_screen_texture_size_in_bytes, | |
401 kDefaultMaxTransferBufferSize))) { | |
402 LOG(ERROR) << "Failed to create 3D context for compositor."; | |
403 return scoped_ptr<cc::OutputSurface>(); | |
404 } | |
405 return scoped_ptr<cc::OutputSurface>( | |
406 new OutputSurfaceWithoutParent( | |
407 context.PassAs<WebKit::WebGraphicsContext3D>())); | |
408 } | 427 } |
| 428 |
| 429 DCHECK(window_); |
| 430 DCHECK(surface_id_); |
| 431 |
| 432 scoped_refptr<ContextProviderCommandBuffer> context_provider = |
| 433 ContextProviderCommandBuffer::Create( |
| 434 base::Bind(&CreateGpuProcessViewContext, |
| 435 attrs, |
| 436 surface_id_, |
| 437 weak_factory_.GetWeakPtr())); |
| 438 if (!context_provider.get()) { |
| 439 LOG(ERROR) << "Failed to create 3D context for compositor."; |
| 440 return scoped_ptr<cc::OutputSurface>(); |
| 441 } |
| 442 return scoped_ptr<cc::OutputSurface>( |
| 443 new OutputSurfaceWithoutParent(context_provider)); |
409 } | 444 } |
410 | 445 |
411 void CompositorImpl::OnLostResources() { | 446 void CompositorImpl::OnLostResources() { |
412 client_->DidLoseResources(); | 447 client_->DidLoseResources(); |
413 } | 448 } |
414 | 449 |
415 void CompositorImpl::DidCompleteSwapBuffers() { | 450 void CompositorImpl::DidCompleteSwapBuffers() { |
416 client_->OnSwapBuffersCompleted(); | 451 client_->OnSwapBuffersCompleted(); |
417 } | 452 } |
418 | 453 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 case ANDROID_BITMAP_FORMAT_RGBA_8888: | 532 case ANDROID_BITMAP_FORMAT_RGBA_8888: |
498 return GL_UNSIGNED_BYTE; | 533 return GL_UNSIGNED_BYTE; |
499 break; | 534 break; |
500 case ANDROID_BITMAP_FORMAT_RGB_565: | 535 case ANDROID_BITMAP_FORMAT_RGB_565: |
501 default: | 536 default: |
502 return GL_UNSIGNED_SHORT_5_6_5; | 537 return GL_UNSIGNED_SHORT_5_6_5; |
503 } | 538 } |
504 } | 539 } |
505 | 540 |
506 } // namespace content | 541 } // namespace content |
OLD | NEW |