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

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: addressed sievers comments 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
« no previous file with comments | « content/browser/renderer_host/compositor_impl_android.h ('k') | content/content_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 pending_swapbuffers_++; 329 pending_swapbuffers_++;
377 330
378 // Need to track vsync to avoid compositing more than once per frame. 331 // Need to track vsync to avoid compositing more than once per frame.
379 root_window_->RequestVSyncUpdate(); 332 root_window_->RequestVSyncUpdate();
380 } 333 }
381 334
382 void CompositorImpl::OnGpuChannelEstablished() { 335 void CompositorImpl::OnGpuChannelEstablished() {
383 ScheduleComposite(); 336 ScheduleComposite();
384 } 337 }
385 338
339 UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
340 return ui_resource_provider_;
341 }
342
386 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) { 343 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
387 root_layer_->RemoveAllChildren(); 344 root_layer_->RemoveAllChildren();
388 if (root_layer) 345 if (root_layer)
389 root_layer_->AddChild(root_layer); 346 root_layer_->AddChild(root_layer);
390 } 347 }
391 348
392 void CompositorImpl::SetWindowSurface(ANativeWindow* window) { 349 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
393 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get(); 350 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
394 351
395 if (window_) { 352 if (window_) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 SetWindowSurface(window); 390 SetWindowSurface(window);
434 ANativeWindow_release(window); 391 ANativeWindow_release(window);
435 content::RegisterViewSurface(surface_id_, j_surface.obj()); 392 content::RegisterViewSurface(surface_id_, j_surface.obj());
436 } 393 }
437 } 394 }
438 395
439 void CompositorImpl::SetVisible(bool visible) { 396 void CompositorImpl::SetVisible(bool visible) {
440 if (!visible) { 397 if (!visible) {
441 if (WillComposite()) 398 if (WillComposite())
442 CancelComposite(); 399 CancelComposite();
443 ui_resource_map_.clear(); 400 ui_resource_provider_.SetLayerTreeHost(NULL);
444 host_.reset(); 401 host_.reset();
445 client_->UIResourcesAreInvalid();
446 } else if (!host_) { 402 } else if (!host_) {
447 DCHECK(!WillComposite()); 403 DCHECK(!WillComposite());
448 needs_composite_ = false; 404 needs_composite_ = false;
449 needs_animate_ = false; 405 needs_animate_ = false;
450 pending_swapbuffers_ = 0; 406 pending_swapbuffers_ = 0;
451 cc::LayerTreeSettings settings; 407 cc::LayerTreeSettings settings;
452 settings.refresh_rate = 60.0; 408 settings.refresh_rate = 60.0;
453 settings.impl_side_painting = false; 409 settings.impl_side_painting = false;
454 settings.allow_antialiasing = false; 410 settings.allow_antialiasing = false;
455 settings.calculate_top_controls_position = false; 411 settings.calculate_top_controls_position = false;
456 settings.top_controls_height = 0.f; 412 settings.top_controls_height = 0.f;
457 settings.highp_threshold_min = 2048; 413 settings.highp_threshold_min = 2048;
458 414
459 CommandLine* command_line = CommandLine::ForCurrentProcess(); 415 CommandLine* command_line = CommandLine::ForCurrentProcess();
460 settings.initial_debug_state.SetRecordRenderingStats( 416 settings.initial_debug_state.SetRecordRenderingStats(
461 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); 417 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
462 settings.initial_debug_state.show_fps_counter = 418 settings.initial_debug_state.show_fps_counter =
463 command_line->HasSwitch(cc::switches::kUIShowFPSCounter); 419 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
464 420
465 host_ = cc::LayerTreeHost::CreateSingleThreaded( 421 host_ = cc::LayerTreeHost::CreateSingleThreaded(
466 this, this, HostSharedBitmapManager::current(), settings); 422 this, this, HostSharedBitmapManager::current(), settings);
467 host_->SetRootLayer(root_layer_); 423 host_->SetRootLayer(root_layer_);
468 424
469 host_->SetVisible(true); 425 host_->SetVisible(true);
470 host_->SetLayerTreeHostClientReady(); 426 host_->SetLayerTreeHostClientReady();
471 host_->SetViewportSize(size_); 427 host_->SetViewportSize(size_);
472 host_->set_has_transparent_background(has_transparent_background_); 428 host_->set_has_transparent_background(has_transparent_background_);
473 host_->SetDeviceScaleFactor(device_scale_factor_); 429 host_->SetDeviceScaleFactor(device_scale_factor_);
474 // Need to recreate the UI resources because a new LayerTreeHost has been 430 ui_resource_provider_.SetLayerTreeHost(host_.get());
475 // created.
476 client_->DidLoseUIResources();
477 } 431 }
478 } 432 }
479 433
480 void CompositorImpl::setDeviceScaleFactor(float factor) { 434 void CompositorImpl::setDeviceScaleFactor(float factor) {
481 device_scale_factor_ = factor; 435 device_scale_factor_ = factor;
482 if (host_) 436 if (host_)
483 host_->SetDeviceScaleFactor(factor); 437 host_->SetDeviceScaleFactor(factor);
484 } 438 }
485 439
486 void CompositorImpl::SetWindowBounds(const gfx::Size& size) { 440 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
(...skipping 14 matching lines...) Expand all
501 455
502 void CompositorImpl::SetNeedsComposite() { 456 void CompositorImpl::SetNeedsComposite() {
503 if (!host_.get()) 457 if (!host_.get())
504 return; 458 return;
505 DCHECK(!needs_composite_ || WillComposite()); 459 DCHECK(!needs_composite_ || WillComposite());
506 460
507 needs_composite_ = true; 461 needs_composite_ = true;
508 PostComposite(COMPOSITE_IMMEDIATELY); 462 PostComposite(COMPOSITE_IMMEDIATELY);
509 } 463 }
510 464
511 cc::UIResourceId CompositorImpl::GenerateUIResourceFromUIResourceBitmap(
512 const cc::UIResourceBitmap& bitmap,
513 bool is_transient) {
514 if (!host_)
515 return 0;
516
517 cc::UIResourceId id = 0;
518 scoped_ptr<cc::UIResourceClient> resource;
519 if (is_transient) {
520 scoped_ptr<TransientUIResource> transient_resource =
521 TransientUIResource::Create(host_.get(), bitmap);
522 id = transient_resource->id();
523 resource = transient_resource.Pass();
524 } else {
525 scoped_ptr<cc::ScopedUIResource> scoped_resource =
526 cc::ScopedUIResource::Create(host_.get(), bitmap);
527 id = scoped_resource->id();
528 resource = scoped_resource.Pass();
529 }
530
531 ui_resource_map_.set(id, resource.Pass());
532 return id;
533 }
534
535 cc::UIResourceId CompositorImpl::GenerateUIResource(const SkBitmap& bitmap,
536 bool is_transient) {
537 return GenerateUIResourceFromUIResourceBitmap(cc::UIResourceBitmap(bitmap),
538 is_transient);
539 }
540
541 cc::UIResourceId CompositorImpl::GenerateCompressedUIResource(
542 const gfx::Size& size,
543 void* pixels,
544 bool is_transient) {
545 DCHECK_LT(0, size.width());
546 DCHECK_LT(0, size.height());
547 DCHECK_EQ(0, size.width() % 4);
548 DCHECK_EQ(0, size.height() % 4);
549
550 size_t data_size = size.width() * size.height() / 2;
551 SkImageInfo info = {size.width(), size.height() / 2, kAlpha_8_SkColorType,
552 kPremul_SkAlphaType};
553 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref =
554 skia::AdoptRef(SkMallocPixelRef::NewAllocate(info, 0, 0));
555 memcpy(etc1_pixel_ref->getAddr(), pixels, data_size);
556 etc1_pixel_ref->setImmutable();
557 return GenerateUIResourceFromUIResourceBitmap(
558 cc::UIResourceBitmap(etc1_pixel_ref, size), is_transient);
559 }
560
561 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) {
562 UIResourceMap::iterator it = ui_resource_map_.find(resource_id);
563 if (it != ui_resource_map_.end())
564 ui_resource_map_.erase(it);
565 }
566
567 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> 465 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
568 CreateGpuProcessViewContext( 466 CreateGpuProcessViewContext(
569 const scoped_refptr<GpuChannelHost>& gpu_channel_host, 467 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
570 const blink::WebGraphicsContext3D::Attributes attributes, 468 const blink::WebGraphicsContext3D::Attributes attributes,
571 int surface_id) { 469 int surface_id) {
572 DCHECK(gpu_channel_host); 470 DCHECK(gpu_channel_host);
573 471
574 GURL url("chrome://gpu/Compositor::createContext3D"); 472 GURL url("chrome://gpu/Compositor::createContext3D");
575 static const size_t kBytesPerPixel = 4; 473 static const size_t kBytesPerPixel = 4;
576 gfx::DeviceDisplayInfo display_info; 474 gfx::DeviceDisplayInfo display_info;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 LOG(ERROR) << "Failed to create 3D context for compositor."; 524 LOG(ERROR) << "Failed to create 3D context for compositor.";
627 return scoped_ptr<cc::OutputSurface>(); 525 return scoped_ptr<cc::OutputSurface>();
628 } 526 }
629 527
630 return scoped_ptr<cc::OutputSurface>( 528 return scoped_ptr<cc::OutputSurface>(
631 new OutputSurfaceWithoutParent(context_provider)); 529 new OutputSurfaceWithoutParent(context_provider));
632 } 530 }
633 531
634 void CompositorImpl::OnLostResources() { 532 void CompositorImpl::OnLostResources() {
635 client_->DidLoseResources(); 533 client_->DidLoseResources();
534 ui_resource_provider_.UIResourcesAreInvalid();
636 } 535 }
637 536
638 void CompositorImpl::ScheduleComposite() { 537 void CompositorImpl::ScheduleComposite() {
639 DCHECK(!needs_composite_ || WillComposite()); 538 DCHECK(!needs_composite_ || WillComposite());
640 if (ignore_schedule_composite_) 539 if (ignore_schedule_composite_)
641 return; 540 return;
642 541
643 needs_composite_ = true; 542 needs_composite_ = true;
644 // We currently expect layer tree invalidations at most once per frame 543 // We currently expect layer tree invalidations at most once per frame
645 // during normal operation and therefore try to composite immediately 544 // during normal operation and therefore try to composite immediately
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 } 617 }
719 618
720 void CompositorImpl::SetNeedsAnimate() { 619 void CompositorImpl::SetNeedsAnimate() {
721 if (!host_) 620 if (!host_)
722 return; 621 return;
723 622
724 host_->SetNeedsAnimate(); 623 host_->SetNeedsAnimate();
725 } 624 }
726 625
727 } // namespace content 626 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/compositor_impl_android.h ('k') | content/content_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698