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

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

Issue 1371523003: Android: Don't destroy LayerTreeHost when Surface goes away (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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"
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 new cc::SurfaceIdAllocator(++g_surface_id_namespace)); 223 new cc::SurfaceIdAllocator(++g_surface_id_namespace));
224 cc::SurfaceManager* manager = GetSurfaceManager(); 224 cc::SurfaceManager* manager = GetSurfaceManager();
225 DCHECK(manager); 225 DCHECK(manager);
226 allocator->RegisterSurfaceIdNamespace(manager); 226 allocator->RegisterSurfaceIdNamespace(manager);
227 return allocator.Pass(); 227 return allocator.Pass();
228 } 228 }
229 229
230 CompositorImpl::CompositorImpl(CompositorClient* client, 230 CompositorImpl::CompositorImpl(CompositorClient* client,
231 gfx::NativeWindow root_window) 231 gfx::NativeWindow root_window)
232 : root_layer_(cc::Layer::Create(Compositor::LayerSettings())), 232 : root_layer_(cc::Layer::Create(Compositor::LayerSettings())),
233 resource_manager_(&ui_resource_provider_), 233 resource_manager_,
234 surface_id_allocator_(GetSurfaceManager() ? CreateSurfaceIdAllocator() 234 surface_id_allocator_(GetSurfaceManager() ? CreateSurfaceIdAllocator()
235 : nullptr), 235 : nullptr),
236 has_transparent_background_(false), 236 has_transparent_background_(false),
237 device_scale_factor_(1), 237 device_scale_factor_(1),
238 window_(NULL), 238 window_(NULL),
239 surface_id_(0), 239 surface_id_(0),
240 client_(client), 240 client_(client),
241 root_window_(root_window), 241 root_window_(root_window),
242 did_post_swapbuffers_(false), 242 did_post_swapbuffers_(false),
243 ignore_schedule_composite_(false), 243 ignore_schedule_composite_(false),
244 needs_composite_(false), 244 needs_composite_(false),
245 needs_animate_(false), 245 needs_animate_(false),
246 will_composite_immediately_(false), 246 will_composite_immediately_(false),
247 composite_on_vsync_trigger_(DO_NOT_COMPOSITE), 247 composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
248 pending_swapbuffers_(0U), 248 pending_swapbuffers_(0U),
249 num_successive_context_creation_failures_(0), 249 num_successive_context_creation_failures_(0),
250 output_surface_request_pending_(false), 250 output_surface_request_pending_(false),
251 weak_factory_(this) { 251 weak_factory_(this) {
252 DCHECK(client); 252 DCHECK(client);
253 DCHECK(root_window); 253 DCHECK(root_window);
254 root_window->AttachCompositor(this); 254 root_window->AttachCompositor(this);
255 CreateLayerTreeHost();
256 resource_manager_.Init(host_.get());
255 } 257 }
256 258
257 CompositorImpl::~CompositorImpl() { 259 CompositorImpl::~CompositorImpl() {
258 root_window_->DetachCompositor(); 260 root_window_->DetachCompositor();
259 // Clean-up any surface references. 261 // Clean-up any surface references.
260 SetSurface(NULL); 262 SetSurface(NULL);
261 } 263 }
262 264
263 void CompositorImpl::PostComposite(CompositingTrigger trigger) { 265 void CompositorImpl::PostComposite(CompositingTrigger trigger) {
264 DCHECK(needs_composite_); 266 DCHECK(needs_composite_);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 current_composite_task_.reset(new base::CancelableClosure( 319 current_composite_task_.reset(new base::CancelableClosure(
318 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger))); 320 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
319 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 321 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
320 FROM_HERE, current_composite_task_->callback(), delay); 322 FROM_HERE, current_composite_task_->callback(), delay);
321 } 323 }
322 324
323 void CompositorImpl::Composite(CompositingTrigger trigger) { 325 void CompositorImpl::Composite(CompositingTrigger trigger) {
324 if (trigger == COMPOSITE_IMMEDIATELY) 326 if (trigger == COMPOSITE_IMMEDIATELY)
325 will_composite_immediately_ = false; 327 will_composite_immediately_ = false;
326 328
327 DCHECK(host_); 329 DCHECK(host_->visible());
328 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY); 330 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
329 DCHECK(needs_composite_); 331 DCHECK(needs_composite_);
330 DCHECK(!DidCompositeThisFrame()); 332 DCHECK(!DidCompositeThisFrame());
331 333
332 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers); 334 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers);
333 // Swap Ack accounting is unreliable if the OutputSurface was lost. 335 // Swap Ack accounting is unreliable if the OutputSurface was lost.
334 // In that case still attempt to composite, which will cause creation of a 336 // In that case still attempt to composite, which will cause creation of a
335 // new OutputSurface and reset pending_swapbuffers_. 337 // new OutputSurface and reset pending_swapbuffers_.
336 if (pending_swapbuffers_ == kMaxSwapBuffers && 338 if (pending_swapbuffers_ == kMaxSwapBuffers &&
337 !host_->output_surface_lost()) { 339 !host_->output_surface_lost()) {
(...skipping 24 matching lines...) Expand all
362 did_post_swapbuffers_ = false; 364 did_post_swapbuffers_ = false;
363 host_->Composite(frame_time); 365 host_->Composite(frame_time);
364 if (did_post_swapbuffers_) 366 if (did_post_swapbuffers_)
365 pending_swapbuffers_++; 367 pending_swapbuffers_++;
366 368
367 // Need to track vsync to avoid compositing more than once per frame. 369 // Need to track vsync to avoid compositing more than once per frame.
368 root_window_->RequestVSyncUpdate(); 370 root_window_->RequestVSyncUpdate();
369 } 371 }
370 372
371 ui::UIResourceProvider& CompositorImpl::GetUIResourceProvider() { 373 ui::UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
372 return ui_resource_provider_; 374 return *this;
373 } 375 }
374 376
375 ui::ResourceManager& CompositorImpl::GetResourceManager() { 377 ui::ResourceManager& CompositorImpl::GetResourceManager() {
376 return resource_manager_; 378 return resource_manager_;
377 } 379 }
378 380
379 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) { 381 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
380 if (subroot_layer_.get()) { 382 if (subroot_layer_.get()) {
381 subroot_layer_->RemoveFromParent(); 383 subroot_layer_->RemoveFromParent();
382 subroot_layer_ = NULL; 384 subroot_layer_ = NULL;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 if (window) { 435 if (window) {
434 SetWindowSurface(window); 436 SetWindowSurface(window);
435 ANativeWindow_release(window); 437 ANativeWindow_release(window);
436 RegisterViewSurface(surface_id_, j_surface.obj()); 438 RegisterViewSurface(surface_id_, j_surface.obj());
437 } 439 }
438 } 440 }
439 441
440 void CompositorImpl::CreateLayerTreeHost() { 442 void CompositorImpl::CreateLayerTreeHost() {
441 DCHECK(!host_); 443 DCHECK(!host_);
442 DCHECK(!WillCompositeThisFrame()); 444 DCHECK(!WillCompositeThisFrame());
443 needs_composite_ = false;
444 pending_swapbuffers_ = 0;
445 cc::LayerTreeSettings settings; 445 cc::LayerTreeSettings settings;
446 settings.renderer_settings.refresh_rate = 60.0; 446 settings.renderer_settings.refresh_rate = 60.0;
447 settings.renderer_settings.allow_antialiasing = false; 447 settings.renderer_settings.allow_antialiasing = false;
448 settings.renderer_settings.highp_threshold_min = 2048; 448 settings.renderer_settings.highp_threshold_min = 2048;
449 settings.use_zero_copy = true; 449 settings.use_zero_copy = true;
450 450
451 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 451 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
452 settings.initial_debug_state.SetRecordRenderingStats( 452 settings.initial_debug_state.SetRecordRenderingStats(
453 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); 453 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
454 settings.initial_debug_state.show_fps_counter = 454 settings.initial_debug_state.show_fps_counter =
455 command_line->HasSwitch(cc::switches::kUIShowFPSCounter); 455 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
456 // TODO(enne): Update this this compositor to use the scheduler. 456 // TODO(enne): Update this this compositor to use the scheduler.
457 settings.single_thread_proxy_scheduler = false; 457 settings.single_thread_proxy_scheduler = false;
458 458
459 if (command_line->HasSwitch( 459 if (command_line->HasSwitch(
460 switches::kEnableAndroidCompositorAnimationTimelines)) 460 switches::kEnableAndroidCompositorAnimationTimelines))
461 settings.use_compositor_animation_timelines = true; 461 settings.use_compositor_animation_timelines = true;
462 462
463 cc::LayerTreeHost::InitParams params; 463 cc::LayerTreeHost::InitParams params;
464 params.client = this; 464 params.client = this;
465 params.shared_bitmap_manager = HostSharedBitmapManager::current(); 465 params.shared_bitmap_manager = HostSharedBitmapManager::current();
466 params.gpu_memory_buffer_manager = BrowserGpuMemoryBufferManager::current(); 466 params.gpu_memory_buffer_manager = BrowserGpuMemoryBufferManager::current();
467 params.task_graph_runner = g_task_graph_runner.Pointer(); 467 params.task_graph_runner = g_task_graph_runner.Pointer();
468 params.main_task_runner = base::ThreadTaskRunnerHandle::Get(); 468 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
469 params.settings = &settings; 469 params.settings = &settings;
470 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params); 470 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
471 host_->SetRootLayer(root_layer_); 471 host_->SetRootLayer(root_layer_);
472 472 host_->SetVisible(false);
473 host_->SetVisible(true);
474 host_->SetLayerTreeHostClientReady(); 473 host_->SetLayerTreeHostClientReady();
475 host_->SetViewportSize(size_); 474 host_->SetViewportSize(size_);
476 host_->set_has_transparent_background(has_transparent_background_); 475 host_->set_has_transparent_background(has_transparent_background_);
477 host_->SetDeviceScaleFactor(device_scale_factor_); 476 host_->SetDeviceScaleFactor(device_scale_factor_);
478 477
479 if (needs_animate_) 478 if (needs_animate_)
480 host_->SetNeedsAnimate(); 479 host_->SetNeedsAnimate();
481 } 480 }
482 481
483 void CompositorImpl::SetVisible(bool visible) { 482 void CompositorImpl::SetVisible(bool visible) {
484 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible); 483 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
485 if (!visible) { 484 if (!visible) {
486 DCHECK(host_); 485 DCHECK(host_->visible());
487 // Look for any layers that were attached to the root for readback 486 // Look for any layers that were attached to the root for readback
488 // and are waiting for Composite() to happen. 487 // and are waiting for Composite() to happen.
489 bool readback_pending = false; 488 bool readback_pending = false;
490 for (size_t i = 0; i < root_layer_->children().size(); ++i) { 489 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
491 if (root_layer_->children()[i]->HasCopyRequest()) { 490 if (root_layer_->children()[i]->HasCopyRequest()) {
492 readback_pending = true; 491 readback_pending = true;
493 break; 492 break;
494 } 493 }
495 } 494 }
496 if (readback_pending) { 495 if (readback_pending) {
497 ignore_schedule_composite_ = true; 496 ignore_schedule_composite_ = true;
498 host_->Composite(base::TimeTicks::Now()); 497 host_->Composite(base::TimeTicks::Now());
499 ignore_schedule_composite_ = false; 498 ignore_schedule_composite_ = false;
500 } 499 }
501 if (WillComposite()) 500 if (WillComposite())
502 CancelComposite(); 501 CancelComposite();
503 ui_resource_provider_.SetLayerTreeHost(NULL); 502 host_->SetVisible(false);
504 host_.reset(); 503 host_->ReleaseOutputSurface();
504 pending_swapbuffers_ = 0;
505 needs_composite_ = false;
505 establish_gpu_channel_timeout_.Stop(); 506 establish_gpu_channel_timeout_.Stop();
506 output_surface_request_pending_ = false; 507 output_surface_request_pending_ = false;
507 display_client_.reset(); 508 display_client_.reset();
508 if (current_composite_task_) { 509 if (current_composite_task_) {
509 current_composite_task_->Cancel(); 510 current_composite_task_->Cancel();
510 current_composite_task_.reset(); 511 current_composite_task_.reset();
511 } 512 }
512 } else if (!host_) { 513 } else {
513 CreateLayerTreeHost(); 514 host_->SetVisible(true);
514 ui_resource_provider_.SetLayerTreeHost(host_.get()); 515 SetNeedsComposite();
515 } 516 }
516 } 517 }
517 518
518 void CompositorImpl::setDeviceScaleFactor(float factor) { 519 void CompositorImpl::setDeviceScaleFactor(float factor) {
519 device_scale_factor_ = factor; 520 device_scale_factor_ = factor;
520 if (host_) 521 if (host_)
521 host_->SetDeviceScaleFactor(factor); 522 host_->SetDeviceScaleFactor(factor);
522 } 523 }
523 524
524 void CompositorImpl::SetWindowBounds(const gfx::Size& size) { 525 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
525 if (size_ == size) 526 if (size_ == size)
526 return; 527 return;
527 528
528 size_ = size; 529 size_ = size;
529 if (host_) 530 if (host_)
530 host_->SetViewportSize(size); 531 host_->SetViewportSize(size);
531 if (display_client_) 532 if (display_client_)
532 display_client_->display()->Resize(size); 533 display_client_->display()->Resize(size);
533 root_layer_->SetBounds(size); 534 root_layer_->SetBounds(size);
534 } 535 }
535 536
536 void CompositorImpl::SetHasTransparentBackground(bool flag) { 537 void CompositorImpl::SetHasTransparentBackground(bool flag) {
537 has_transparent_background_ = flag; 538 has_transparent_background_ = flag;
538 if (host_) 539 if (host_)
539 host_->set_has_transparent_background(flag); 540 host_->set_has_transparent_background(flag);
540 } 541 }
541 542
542 void CompositorImpl::SetNeedsComposite() { 543 void CompositorImpl::SetNeedsComposite() {
543 if (!host_.get()) 544 if (!host_->visible())
544 return; 545 return;
545 DCHECK(!needs_composite_ || WillComposite()); 546 DCHECK(!needs_composite_ || WillComposite());
546 547
547 needs_composite_ = true; 548 needs_composite_ = true;
548 PostComposite(COMPOSITE_IMMEDIATELY); 549 PostComposite(COMPOSITE_IMMEDIATELY);
549 } 550 }
550 551
551 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> 552 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
552 CreateGpuProcessViewContext( 553 CreateGpuProcessViewContext(
553 const scoped_refptr<GpuChannelHost>& gpu_channel_host, 554 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 surface_output_surface->set_display_client(display_client_.get()); 680 surface_output_surface->set_display_client(display_client_.get());
680 display_client_->display()->Resize(size_); 681 display_client_->display()->Resize(size_);
681 host_->SetOutputSurface(surface_output_surface.Pass()); 682 host_->SetOutputSurface(surface_output_surface.Pass());
682 } else { 683 } else {
683 host_->SetOutputSurface(real_output_surface.Pass()); 684 host_->SetOutputSurface(real_output_surface.Pass());
684 } 685 }
685 } 686 }
686 687
687 void CompositorImpl::PopulateGpuCapabilities( 688 void CompositorImpl::PopulateGpuCapabilities(
688 gpu::Capabilities gpu_capabilities) { 689 gpu::Capabilities gpu_capabilities) {
689 ui_resource_provider_.SetSupportsETC1NonPowerOfTwo( 690 gpu_capabilities_ = gpu_capabilities;
690 gpu_capabilities.texture_format_etc1_npot);
691 } 691 }
692 692
693 void CompositorImpl::AddObserver(VSyncObserver* observer) { 693 void CompositorImpl::AddObserver(VSyncObserver* observer) {
694 observer_list_.AddObserver(observer); 694 observer_list_.AddObserver(observer);
695 } 695 }
696 696
697 void CompositorImpl::RemoveObserver(VSyncObserver* observer) { 697 void CompositorImpl::RemoveObserver(VSyncObserver* observer) {
698 observer_list_.RemoveObserver(observer); 698 observer_list_.RemoveObserver(observer);
699 } 699 }
700 700
701 cc::UIResourceId CompositorImpl::CreateUIResource(
702 cc::UIResourceClient* client) {
703 return host_->CreateUIResource(client);
704 }
705
706 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) {
707 host_->DeleteUIResource(resource_id);
708 }
709
710 bool CompositorImpl::SupportsETC1NonPowerOfTwo() const {
711 return gpu_capabilities_.texture_format_etc1_npot;
712 }
713
701 void CompositorImpl::ScheduleComposite() { 714 void CompositorImpl::ScheduleComposite() {
702 DCHECK(!needs_composite_ || WillComposite()); 715 if (ignore_schedule_composite_ || !host_->visible())
703 if (ignore_schedule_composite_)
704 return; 716 return;
705 717
718 DCHECK_IMPLIES(needs_composite_, WillComposite());
706 needs_composite_ = true; 719 needs_composite_ = true;
707 // We currently expect layer tree invalidations at most once per frame 720 // We currently expect layer tree invalidations at most once per frame
708 // during normal operation and therefore try to composite immediately 721 // during normal operation and therefore try to composite immediately
709 // to minimize latency. 722 // to minimize latency.
710 PostComposite(COMPOSITE_IMMEDIATELY); 723 PostComposite(COMPOSITE_IMMEDIATELY);
711 } 724 }
712 725
713 void CompositorImpl::ScheduleAnimation() { 726 void CompositorImpl::ScheduleAnimation() {
714 DCHECK(!needs_composite_ || WillComposite()); 727 DCHECK_IMPLIES(needs_composite_, WillComposite());
715 needs_animate_ = true; 728 needs_animate_ = true;
716 729
717 if (needs_composite_) 730 if (needs_composite_)
718 return; 731 return;
719 732
720 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation"); 733 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
721 needs_composite_ = true; 734 needs_composite_ = true;
722 PostComposite(COMPOSITE_EVENTUALLY); 735 PostComposite(COMPOSITE_EVENTUALLY);
723 } 736 }
724 737
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE; 791 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
779 PostComposite(trigger); 792 PostComposite(trigger);
780 } 793 }
781 794
782 FOR_EACH_OBSERVER(VSyncObserver, observer_list_, 795 FOR_EACH_OBSERVER(VSyncObserver, observer_list_,
783 OnUpdateVSyncParameters(frame_time, vsync_period)); 796 OnUpdateVSyncParameters(frame_time, vsync_period));
784 } 797 }
785 798
786 void CompositorImpl::SetNeedsAnimate() { 799 void CompositorImpl::SetNeedsAnimate() {
787 needs_animate_ = true; 800 needs_animate_ = true;
788 if (!host_) 801 if (!host_->visible())
789 return; 802 return;
790 803
791 host_->SetNeedsAnimate(); 804 host_->SetNeedsAnimate();
792 } 805 }
793 806
794 } // namespace content 807 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698