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

Side by Side Diff: content/browser/renderer_host/compositor_impl_android.cc

Issue 287593002: android: content::UIResourceProvider and content::UIResourceClientAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update wrt to offline discussion Created 6 years, 6 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 "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 9
10 #include "base/android/jni_android.h" 10 #include "base/android/jni_android.h"
11 #include "base/android/scoped_java_ref.h" 11 #include "base/android/scoped_java_ref.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/containers/hash_tables.h" 14 #include "base/containers/hash_tables.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 "base/threading/thread_checker.h" 20 #include "base/threading/thread_checker.h"
21 #include "cc/base/switches.h" 21 #include "cc/base/switches.h"
22 #include "cc/input/input_handler.h" 22 #include "cc/input/input_handler.h"
23 #include "cc/layers/layer.h" 23 #include "cc/layers/layer.h"
24 #include "cc/output/compositor_frame.h" 24 #include "cc/output/compositor_frame.h"
25 #include "cc/output/context_provider.h" 25 #include "cc/output/context_provider.h"
26 #include "cc/output/output_surface.h" 26 #include "cc/output/output_surface.h"
27 #include "cc/resources/scoped_ui_resource.h"
28 #include "cc/resources/ui_resource_bitmap.h"
29 #include "cc/trees/layer_tree_host.h" 27 #include "cc/trees/layer_tree_host.h"
30 #include "content/browser/android/child_process_launcher_android.h" 28 #include "content/browser/android/child_process_launcher_android.h"
31 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" 29 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
32 #include "content/browser/gpu/gpu_surface_tracker.h" 30 #include "content/browser/gpu/gpu_surface_tracker.h"
33 #include "content/common/gpu/client/command_buffer_proxy_impl.h" 31 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
34 #include "content/common/gpu/client/context_provider_command_buffer.h" 32 #include "content/common/gpu/client/context_provider_command_buffer.h"
35 #include "content/common/gpu/client/gl_helper.h" 33 #include "content/common/gpu/client/gl_helper.h"
36 #include "content/common/gpu/client/gpu_channel_host.h" 34 #include "content/common/gpu/client/gpu_channel_host.h"
37 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" 35 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
38 #include "content/common/gpu/gpu_process_launch_causes.h" 36 #include "content/common/gpu/gpu_process_launch_causes.h"
39 #include "content/common/host_shared_bitmap_manager.h" 37 #include "content/common/host_shared_bitmap_manager.h"
40 #include "content/public/browser/android/compositor_client.h" 38 #include "content/public/browser/android/compositor_client.h"
41 #include "gpu/command_buffer/client/gles2_interface.h" 39 #include "gpu/command_buffer/client/gles2_interface.h"
42 #include "third_party/khronos/GLES2/gl2.h" 40 #include "third_party/khronos/GLES2/gl2.h"
43 #include "third_party/khronos/GLES2/gl2ext.h" 41 #include "third_party/khronos/GLES2/gl2ext.h"
44 #include "third_party/skia/include/core/SkMallocPixelRef.h" 42 #include "third_party/skia/include/core/SkMallocPixelRef.h"
45 #include "ui/base/android/window_android.h" 43 #include "ui/base/android/window_android.h"
46 #include "ui/gfx/android/device_display_info.h" 44 #include "ui/gfx/android/device_display_info.h"
47 #include "ui/gfx/android/java_bitmap.h"
48 #include "ui/gfx/frame_time.h" 45 #include "ui/gfx/frame_time.h"
49 #include "ui/gl/android/surface_texture.h" 46 #include "ui/gl/android/surface_texture.h"
50 #include "ui/gl/android/surface_texture_tracker.h" 47 #include "ui/gl/android/surface_texture_tracker.h"
51 #include "webkit/common/gpu/context_provider_in_process.h" 48 #include "webkit/common/gpu/context_provider_in_process.h"
52 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" 49 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h"
53 50
54 namespace gfx {
55 class JavaBitmap;
56 }
57
58 namespace { 51 namespace {
59 52
60 const unsigned int kMaxSwapBuffers = 2U; 53 const unsigned int kMaxSwapBuffers = 2U;
61 54
62 // Used to override capabilities_.adjust_deadline_for_parent to false 55 // Used to override capabilities_.adjust_deadline_for_parent to false
63 class OutputSurfaceWithoutParent : public cc::OutputSurface { 56 class OutputSurfaceWithoutParent : public cc::OutputSurface {
64 public: 57 public:
65 OutputSurfaceWithoutParent(const scoped_refptr< 58 OutputSurfaceWithoutParent(const scoped_refptr<
66 content::ContextProviderCommandBuffer>& context_provider) 59 content::ContextProviderCommandBuffer>& context_provider)
67 : cc::OutputSurface(context_provider) { 60 : cc::OutputSurface(context_provider) {
68 capabilities_.adjust_deadline_for_parent = false; 61 capabilities_.adjust_deadline_for_parent = false;
69 } 62 }
70 63
71 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE { 64 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE {
72 content::ContextProviderCommandBuffer* provider_command_buffer = 65 content::ContextProviderCommandBuffer* provider_command_buffer =
73 static_cast<content::ContextProviderCommandBuffer*>( 66 static_cast<content::ContextProviderCommandBuffer*>(
74 context_provider_.get()); 67 context_provider_.get());
75 content::CommandBufferProxyImpl* command_buffer_proxy = 68 content::CommandBufferProxyImpl* command_buffer_proxy =
76 provider_command_buffer->GetCommandBufferProxy(); 69 provider_command_buffer->GetCommandBufferProxy();
77 DCHECK(command_buffer_proxy); 70 DCHECK(command_buffer_proxy);
78 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); 71 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
79 72
80 OutputSurface::SwapBuffers(frame); 73 OutputSurface::SwapBuffers(frame);
81 } 74 }
82 }; 75 };
83 76
84 class TransientUIResource : public cc::ScopedUIResource {
85 public:
86 static scoped_ptr<TransientUIResource> Create(
87 cc::LayerTreeHost* host,
88 const cc::UIResourceBitmap& bitmap) {
89 return make_scoped_ptr(new TransientUIResource(host, bitmap));
90 }
91
92 virtual cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid,
93 bool resource_lost) OVERRIDE {
94 if (!retrieved_) {
95 cc::UIResourceBitmap old_bitmap(bitmap_);
96
97 // Return a place holder for all following calls to GetBitmap.
98 SkBitmap tiny_bitmap;
99 SkCanvas canvas(tiny_bitmap);
100 tiny_bitmap.setConfig(
101 SkBitmap::kARGB_8888_Config, 1, 1, 0, kOpaque_SkAlphaType);
102 tiny_bitmap.allocPixels();
103 canvas.drawColor(SK_ColorWHITE);
104 tiny_bitmap.setImmutable();
105
106 // Release our reference of the true bitmap.
107 bitmap_ = cc::UIResourceBitmap(tiny_bitmap);
108
109 retrieved_ = true;
110 return old_bitmap;
111 }
112 return bitmap_;
113 }
114
115 protected:
116 TransientUIResource(cc::LayerTreeHost* host,
117 const cc::UIResourceBitmap& bitmap)
118 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {}
119
120 private:
121 bool retrieved_;
122 };
123
124 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker { 77 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker {
125 public: 78 public:
126 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) { 79 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) {
127 thread_checker_.DetachFromThread(); 80 thread_checker_.DetachFromThread();
128 } 81 }
129 82
130 // Overridden from gfx::SurfaceTextureTracker: 83 // Overridden from gfx::SurfaceTextureTracker:
131 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture( 84 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture(
132 int primary_id, 85 int primary_id,
133 int secondary_id) OVERRIDE { 86 int secondary_id) OVERRIDE {
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 312
360 did_post_swapbuffers_ = false; 313 did_post_swapbuffers_ = false;
361 host_->Composite(frame_time); 314 host_->Composite(frame_time);
362 if (did_post_swapbuffers_) 315 if (did_post_swapbuffers_)
363 pending_swapbuffers_++; 316 pending_swapbuffers_++;
364 317
365 // Need to track vsync to avoid compositing more than once per frame. 318 // Need to track vsync to avoid compositing more than once per frame.
366 root_window_->RequestVSyncUpdate(); 319 root_window_->RequestVSyncUpdate();
367 } 320 }
368 321
322 UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
323 return ui_resource_provider_;
324 }
325
369 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) { 326 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
370 root_layer_->RemoveAllChildren(); 327 root_layer_->RemoveAllChildren();
371 if (root_layer) 328 if (root_layer)
372 root_layer_->AddChild(root_layer); 329 root_layer_->AddChild(root_layer);
373 } 330 }
374 331
375 void CompositorImpl::SetWindowSurface(ANativeWindow* window) { 332 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
376 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get(); 333 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
377 334
378 if (window_) { 335 if (window_) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 SetWindowSurface(window); 373 SetWindowSurface(window);
417 ANativeWindow_release(window); 374 ANativeWindow_release(window);
418 content::RegisterViewSurface(surface_id_, j_surface.obj()); 375 content::RegisterViewSurface(surface_id_, j_surface.obj());
419 } 376 }
420 } 377 }
421 378
422 void CompositorImpl::SetVisible(bool visible) { 379 void CompositorImpl::SetVisible(bool visible) {
423 if (!visible) { 380 if (!visible) {
424 if (WillComposite()) 381 if (WillComposite())
425 CancelComposite(); 382 CancelComposite();
426 ui_resource_map_.clear(); 383 ui_resource_provider_.SetLayerTreeHost(NULL);
427 host_.reset(); 384 host_.reset();
428 client_->UIResourcesAreInvalid();
429 } else if (!host_) { 385 } else if (!host_) {
430 DCHECK(!WillComposite()); 386 DCHECK(!WillComposite());
431 needs_composite_ = false; 387 needs_composite_ = false;
432 needs_animate_ = false; 388 needs_animate_ = false;
433 pending_swapbuffers_ = 0; 389 pending_swapbuffers_ = 0;
434 cc::LayerTreeSettings settings; 390 cc::LayerTreeSettings settings;
435 settings.refresh_rate = 60.0; 391 settings.refresh_rate = 60.0;
436 settings.impl_side_painting = false; 392 settings.impl_side_painting = false;
437 settings.allow_antialiasing = false; 393 settings.allow_antialiasing = false;
438 settings.calculate_top_controls_position = false; 394 settings.calculate_top_controls_position = false;
439 settings.top_controls_height = 0.f; 395 settings.top_controls_height = 0.f;
440 settings.highp_threshold_min = 2048; 396 settings.highp_threshold_min = 2048;
441 397
442 CommandLine* command_line = CommandLine::ForCurrentProcess(); 398 CommandLine* command_line = CommandLine::ForCurrentProcess();
443 settings.initial_debug_state.SetRecordRenderingStats( 399 settings.initial_debug_state.SetRecordRenderingStats(
444 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); 400 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
445 settings.initial_debug_state.show_fps_counter = 401 settings.initial_debug_state.show_fps_counter =
446 command_line->HasSwitch(cc::switches::kUIShowFPSCounter); 402 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
447 403
448 host_ = cc::LayerTreeHost::CreateSingleThreaded( 404 host_ = cc::LayerTreeHost::CreateSingleThreaded(
449 this, this, HostSharedBitmapManager::current(), settings); 405 this, this, HostSharedBitmapManager::current(), settings);
450 host_->SetRootLayer(root_layer_); 406 host_->SetRootLayer(root_layer_);
451 407
452 host_->SetVisible(true); 408 host_->SetVisible(true);
453 host_->SetLayerTreeHostClientReady(); 409 host_->SetLayerTreeHostClientReady();
454 host_->SetViewportSize(size_); 410 host_->SetViewportSize(size_);
455 host_->set_has_transparent_background(has_transparent_background_); 411 host_->set_has_transparent_background(has_transparent_background_);
456 host_->SetDeviceScaleFactor(device_scale_factor_); 412 host_->SetDeviceScaleFactor(device_scale_factor_);
457 // Need to recreate the UI resources because a new LayerTreeHost has been 413 ui_resource_provider_.SetLayerTreeHost(host_.get());
458 // created.
459 client_->DidLoseUIResources();
460 } 414 }
461 } 415 }
462 416
463 void CompositorImpl::setDeviceScaleFactor(float factor) { 417 void CompositorImpl::setDeviceScaleFactor(float factor) {
464 device_scale_factor_ = factor; 418 device_scale_factor_ = factor;
465 if (host_) 419 if (host_)
466 host_->SetDeviceScaleFactor(factor); 420 host_->SetDeviceScaleFactor(factor);
467 } 421 }
468 422
469 void CompositorImpl::SetWindowBounds(const gfx::Size& size) { 423 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
(...skipping 14 matching lines...) Expand all
484 438
485 void CompositorImpl::SetNeedsComposite() { 439 void CompositorImpl::SetNeedsComposite() {
486 if (!host_.get()) 440 if (!host_.get())
487 return; 441 return;
488 DCHECK(!needs_composite_ || WillComposite()); 442 DCHECK(!needs_composite_ || WillComposite());
489 443
490 needs_composite_ = true; 444 needs_composite_ = true;
491 PostComposite(COMPOSITE_EVENTUALLY); 445 PostComposite(COMPOSITE_EVENTUALLY);
492 } 446 }
493 447
494 cc::UIResourceId CompositorImpl::GenerateUIResourceFromUIResourceBitmap(
495 const cc::UIResourceBitmap& bitmap,
496 bool is_transient) {
497 if (!host_)
498 return 0;
499
500 cc::UIResourceId id = 0;
501 scoped_ptr<cc::UIResourceClient> resource;
502 if (is_transient) {
503 scoped_ptr<TransientUIResource> transient_resource =
504 TransientUIResource::Create(host_.get(), bitmap);
505 id = transient_resource->id();
506 resource = transient_resource.Pass();
507 } else {
508 scoped_ptr<cc::ScopedUIResource> scoped_resource =
509 cc::ScopedUIResource::Create(host_.get(), bitmap);
510 id = scoped_resource->id();
511 resource = scoped_resource.Pass();
512 }
513
514 ui_resource_map_.set(id, resource.Pass());
515 return id;
516 }
517
518 cc::UIResourceId CompositorImpl::GenerateUIResource(const SkBitmap& bitmap,
519 bool is_transient) {
520 return GenerateUIResourceFromUIResourceBitmap(cc::UIResourceBitmap(bitmap),
521 is_transient);
522 }
523
524 cc::UIResourceId CompositorImpl::GenerateCompressedUIResource(
525 const gfx::Size& size,
526 void* pixels,
527 bool is_transient) {
528 DCHECK_LT(0, size.width());
529 DCHECK_LT(0, size.height());
530 DCHECK_EQ(0, size.width() % 4);
531 DCHECK_EQ(0, size.height() % 4);
532
533 size_t data_size = size.width() * size.height() / 2;
534 SkImageInfo info = {size.width(), size.height() / 2, kAlpha_8_SkColorType,
535 kPremul_SkAlphaType};
536 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref =
537 skia::AdoptRef(SkMallocPixelRef::NewAllocate(info, 0, 0));
538 memcpy(etc1_pixel_ref->getAddr(), pixels, data_size);
539 etc1_pixel_ref->setImmutable();
540 return GenerateUIResourceFromUIResourceBitmap(
541 cc::UIResourceBitmap(etc1_pixel_ref, size), is_transient);
542 }
543
544 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) {
545 UIResourceMap::iterator it = ui_resource_map_.find(resource_id);
546 if (it != ui_resource_map_.end())
547 ui_resource_map_.erase(it);
548 }
549
550 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> 448 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
551 CreateGpuProcessViewContext( 449 CreateGpuProcessViewContext(
552 const blink::WebGraphicsContext3D::Attributes attributes, 450 const blink::WebGraphicsContext3D::Attributes attributes,
553 int surface_id) { 451 int surface_id) {
554 BrowserGpuChannelHostFactory* factory = 452 BrowserGpuChannelHostFactory* factory =
555 BrowserGpuChannelHostFactory::instance(); 453 BrowserGpuChannelHostFactory::instance();
556 CauseForGpuLaunch cause = 454 CauseForGpuLaunch cause =
557 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; 455 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
558 scoped_refptr<GpuChannelHost> gpu_channel_host( 456 scoped_refptr<GpuChannelHost> gpu_channel_host(
559 factory->EstablishGpuChannelSync(cause)); 457 factory->EstablishGpuChannelSync(cause));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 LOG(ERROR) << "Failed to create 3D context for compositor."; 506 LOG(ERROR) << "Failed to create 3D context for compositor.";
609 return scoped_ptr<cc::OutputSurface>(); 507 return scoped_ptr<cc::OutputSurface>();
610 } 508 }
611 509
612 return scoped_ptr<cc::OutputSurface>( 510 return scoped_ptr<cc::OutputSurface>(
613 new OutputSurfaceWithoutParent(context_provider)); 511 new OutputSurfaceWithoutParent(context_provider));
614 } 512 }
615 513
616 void CompositorImpl::OnLostResources() { 514 void CompositorImpl::OnLostResources() {
617 client_->DidLoseResources(); 515 client_->DidLoseResources();
516 ui_resource_provider_.UIResourcesAreInvalid();
618 } 517 }
619 518
620 void CompositorImpl::ScheduleComposite() { 519 void CompositorImpl::ScheduleComposite() {
621 DCHECK(!needs_composite_ || WillComposite()); 520 DCHECK(!needs_composite_ || WillComposite());
622 if (ignore_schedule_composite_) 521 if (ignore_schedule_composite_)
623 return; 522 return;
624 523
625 needs_composite_ = true; 524 needs_composite_ = true;
626 // We currently expect layer tree invalidations at most once per frame 525 // We currently expect layer tree invalidations at most once per frame
627 // during normal operation and therefore try to composite immediately 526 // during normal operation and therefore try to composite immediately
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 } 597 }
699 598
700 void CompositorImpl::SetNeedsAnimate() { 599 void CompositorImpl::SetNeedsAnimate() {
701 if (!host_) 600 if (!host_)
702 return; 601 return;
703 602
704 host_->SetNeedsAnimate(); 603 host_->SetNeedsAnimate();
705 } 604 }
706 605
707 } // namespace content 606 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698