15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/compositor.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <deque>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/debug/trace_event.h"
13ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/message_loop/message_loop.h"
14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/metrics/histogram.h"
155e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string_util.h"
164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/sys_info.h"
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "cc/base/latency_info_swap_promise.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/base/switches.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/input/input_handler.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/layer.h"
2103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "cc/output/begin_frame_args.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/output/context_provider.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/trees/layer_tree_host.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkBitmap.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/compositor_observer.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/compositor_switches.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/compositor/compositor_vsync_manager.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/dip_util.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer.h"
3046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "ui/compositor/layer_animator_collection.h"
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/gfx/frame_time.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gl/gl_context.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gl/gl_switches.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const double kDefaultRefreshRate = 60.0;
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const double kTestRefreshRate = 200.0;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kCompositorLockTimeoutMs = 67;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CompositorLock::CompositorLock(Compositor* compositor)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : compositor_(compositor) {
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  compositor_->task_runner_->PostDelayedTask(
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FROM_HERE,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&CompositorLock::CancelLock, AsWeakPtr()),
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs));
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CompositorLock::~CompositorLock() {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CancelLock();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CompositorLock::CancelLock() {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!compositor_)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  compositor_->UnlockCompositor();
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  compositor_ = NULL;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ui
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace {}  // namespace
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)Compositor::Compositor(gfx::AcceleratedWidget widget,
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                       ui::ContextFactory* context_factory,
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                       scoped_refptr<base::SingleThreadTaskRunner> task_runner)
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : context_factory_(context_factory),
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      root_layer_(NULL),
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      widget_(widget),
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      compositor_thread_loop_(context_factory->GetCompositorMessageLoop()),
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      task_runner_(task_runner),
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      vsync_manager_(new CompositorVSyncManager()),
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      device_scale_factor_(0.0f),
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      last_started_frame_(0),
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      last_ended_frame_(0),
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      disable_schedule_composite_(false),
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      compositor_lock_(NULL),
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      defer_draw_scheduling_(false),
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      waiting_on_compositing_end_(false),
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      draw_on_compositing_end_(false),
88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      swap_state_(SWAP_NONE),
8946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      layer_animator_collection_(this),
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      schedule_draw_factory_(this) {
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  root_web_layer_ = cc::Layer::Create();
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  cc::LayerTreeSettings settings;
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.refresh_rate =
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      context_factory_->DoesCreateTestContexts()
98c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      ? kTestRefreshRate
99c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      : kDefaultRefreshRate;
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  settings.main_frame_before_draw_enabled = false;
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  settings.main_frame_before_activation_enabled = false;
102e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  settings.throttle_frame_production =
103e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      !command_line->HasSwitch(switches::kDisableGpuVsync);
10446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if !defined(OS_MACOSX)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.partial_swap_enabled =
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      !command_line->HasSwitch(cc::switches::kUIDisablePartialSwap);
10746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_CHROMEOS)
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  settings.per_tile_painting_enabled = true;
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // These flags should be mirrored by renderer versions in content/renderer/.
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_debug_borders =
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders);
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_fps_counter =
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  settings.initial_debug_state.show_layer_animation_bounds_rects =
118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds);
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_paint_rects =
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(switches::kUIShowPaintRects);
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_property_changed_rects =
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects);
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_surface_damage_rects =
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects);
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_screen_space_rects =
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects);
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_replica_screen_space_rects =
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects);
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_occluding_rects =
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowOccludingRects);
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  settings.initial_debug_state.show_non_occluding_rects =
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      command_line->HasSwitch(cc::switches::kUIShowNonOccludingRects);
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  settings.initial_debug_state.SetRecordRenderingStats(
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  settings.impl_side_painting = IsUIImplSidePaintingEnabled();
1385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  settings.use_zero_copy = IsUIZeroCopyEnabled();
13903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  settings.single_thread_proxy_scheduler = false;
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
141a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::TimeTicks before_create = base::TimeTicks::Now();
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (compositor_thread_loop_.get()) {
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    host_ = cc::LayerTreeHost::CreateThreaded(
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        this,
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        context_factory_->GetSharedBitmapManager(),
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        settings,
1475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        task_runner_,
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        compositor_thread_loop_);
149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    host_ = cc::LayerTreeHost::CreateSingleThreaded(
1515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        this,
1525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        this,
1535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        context_factory_->GetSharedBitmapManager(),
1545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        settings,
1555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        task_runner_);
156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
157a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
158a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                      base::TimeTicks::Now() - before_create);
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  host_->SetRootLayer(root_web_layer_);
160b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  host_->SetLayerTreeHostClientReady();
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Compositor::~Compositor() {
16458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  TRACE_EVENT0("shutdown", "Compositor::destructor");
16558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CancelCompositorLock();
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!compositor_lock_);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (root_layer_)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    root_layer_->SetCompositor(NULL);
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stop all outstanding draws before telling the ContextFactory to tear
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // down any contexts that the |host_| may rely upon.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_.reset();
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  context_factory_->RemoveCompositor(this);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::ScheduleDraw() {
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (compositor_thread_loop_.get()) {
181cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    host_->SetNeedsCommit();
1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else if (!defer_draw_scheduling_) {
1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    defer_draw_scheduling_ = true;
1845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    task_runner_->PostTask(
1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        FROM_HERE,
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        base::Bind(&Compositor::Draw, schedule_draw_factory_.GetWeakPtr()));
1878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::SetRootLayer(Layer* root_layer) {
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (root_layer_ == root_layer)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (root_layer_)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    root_layer_->SetCompositor(NULL);
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  root_layer_ = root_layer;
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (root_layer_ && !root_layer_->GetCompositor())
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    root_layer_->SetCompositor(this);
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  root_web_layer_->RemoveAllChildren();
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (root_layer_)
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    root_web_layer_->AddChild(root_layer_->cc_layer());
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::SetHostHasTransparentBackground(
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool host_has_transparent_background) {
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  host_->set_has_transparent_background(host_has_transparent_background);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void Compositor::Draw() {
2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(!compositor_thread_loop_.get());
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  defer_draw_scheduling_ = false;
2128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (waiting_on_compositing_end_) {
2138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    draw_on_compositing_end_ = true;
2148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
2158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!root_layer_)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_ + 1);
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
221e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  DCHECK_NE(swap_state_, SWAP_POSTED);
222e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  swap_state_ = SWAP_NONE;
223e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  waiting_on_compositing_end_ = true;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_started_frame_++;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!IsLocked()) {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(nduca): Temporary while compositor calls
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // compositeImmediately() directly.
22903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    cc::BeginFrameArgs args =
23003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        cc::BeginFrameArgs::Create(gfx::FrameTime::Now(),
23103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                   base::TimeTicks(),
23203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                                   cc::BeginFrameArgs::DefaultInterval());
23303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    BeginMainFrame(args);
23403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    host_->Composite(args.frame_time);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
236e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  if (swap_state_ == SWAP_NONE)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NotifyEnd();
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void Compositor::ScheduleFullRedraw() {
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  host_->SetNeedsRedraw();
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) {
245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  host_->SetNeedsRedrawRect(damage_rect);
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
248cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void Compositor::FinishAllRendering() {
249cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  host_->FinishAllRendering();
250cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
251cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) {
253a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  scoped_ptr<cc::SwapPromise> swap_promise(
254a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      new cc::LatencyInfoSwapPromise(latency_info));
255a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  host_->QueueSwapPromise(swap_promise.Pass());
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) {
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_GT(scale, 0);
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!size_in_pixel.IsEmpty()) {
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    size_ = size_in_pixel;
262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    host_->SetViewportSize(size_in_pixel);
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    root_web_layer_->SetBounds(size_in_pixel);
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (device_scale_factor_ != scale) {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    device_scale_factor_ = scale;
267cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    host_->SetDeviceScaleFactor(scale);
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (root_layer_)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      root_layer_->OnDeviceScaleFactorChanged(scale);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void Compositor::SetBackgroundColor(SkColor color) {
274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  host_->set_background_color(color);
275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ScheduleDraw();
276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid Compositor::SetVisible(bool visible) {
2791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  host_->SetVisible(visible);
2801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
2811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)scoped_refptr<CompositorVSyncManager> Compositor::vsync_manager() const {
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return vsync_manager_;
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::AddObserver(CompositorObserver* observer) {
2876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(OS_MACOSX)
2886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Debugging instrumentation for crbug.com/401630.
2896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // TODO(ccameron): remove this.
2906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  CHECK(observer);
2916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!observer_list_.HasObserver(observer))
2926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    observer->observing_count_ += 1;
2936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
2946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observer_list_.AddObserver(observer);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::RemoveObserver(CompositorObserver* observer) {
2996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(OS_MACOSX)
3006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Debugging instrumentation for crbug.com/401630.
3016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // TODO(ccameron): remove this.
3026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (observer_list_.HasObserver(observer))
3036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    observer->observing_count_ -= 1;
3046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
3056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observer_list_.RemoveObserver(observer);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool Compositor::HasObserver(CompositorObserver* observer) {
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return observer_list_.HasObserver(observer);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) {
3145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  animation_observer_list_.AddObserver(observer);
3155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  host_->SetNeedsAnimate();
3165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
3175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void Compositor::RemoveAnimationObserver(
3195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    CompositorAnimationObserver* observer) {
3205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  animation_observer_list_.RemoveObserver(observer);
3215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
3225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool Compositor::HasAnimationObserver(CompositorAnimationObserver* observer) {
3245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return animation_observer_list_.HasObserver(observer);
3255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
3265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
32703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) {
3285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  FOR_EACH_OBSERVER(CompositorAnimationObserver,
3295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                    animation_observer_list_,
33003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                    OnAnimationStep(args.frame_time));
3315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (animation_observer_list_.might_have_observers())
33246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    host_->SetNeedsAnimate();
33346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
33446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Compositor::Layout() {
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We're sending damage that will be addressed during this composite
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // cycle, so we don't need to schedule another composite to address it.
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  disable_schedule_composite_ = true;
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (root_layer_)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    root_layer_->SendDamagedRects();
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  disable_schedule_composite_ = false;
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid Compositor::RequestNewOutputSurface(bool fallback) {
3451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  host_->SetOutputSurface(
3461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      context_factory_->CreateOutputSurface(this, fallback));
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Compositor::DidCommit() {
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!IsLocked());
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(CompositorObserver,
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    observer_list_,
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    OnCompositingDidCommit(this));
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Compositor::DidCommitAndDrawFrame() {
3571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::TimeTicks start_time = gfx::FrameTime::Now();
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(CompositorObserver,
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    observer_list_,
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    OnCompositingStarted(this, start_time));
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Compositor::DidCompleteSwapBuffers() {
3641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (compositor_thread_loop_.get()) {
365f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    NotifyEnd();
366f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
367e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    DCHECK_EQ(swap_state_, SWAP_POSTED);
368e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    NotifyEnd();
369e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    swap_state_ = SWAP_COMPLETED;
370f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
371f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
372f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Compositor::ScheduleComposite() {
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!disable_schedule_composite_)
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ScheduleDraw();
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
378f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void Compositor::ScheduleAnimation() {
379f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ScheduleComposite();
380f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
381f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
382f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void Compositor::DidPostSwapBuffers() {
3831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(!compositor_thread_loop_.get());
384e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  DCHECK_EQ(swap_state_, SWAP_NONE);
385e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  swap_state_ = SWAP_POSTED;
386f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
387f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
388f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void Compositor::DidAbortSwapBuffers() {
3891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!compositor_thread_loop_.get()) {
390e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    if (swap_state_ == SWAP_POSTED) {
391e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      NotifyEnd();
392e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      swap_state_ = SWAP_COMPLETED;
393f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
394f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
395f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
396f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FOR_EACH_OBSERVER(CompositorObserver,
397f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    observer_list_,
398f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                    OnCompositingAborted(this));
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const {
402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return host_->debug_state();
403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void Compositor::SetLayerTreeDebugState(
406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const cc::LayerTreeDebugState& debug_state) {
407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  host_->SetDebugState(debug_state);
408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<CompositorLock> Compositor::GetCompositorLock() {
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!compositor_lock_) {
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    compositor_lock_ = new CompositorLock(this);
4131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (compositor_thread_loop_.get())
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      host_->SetDeferCommits(true);
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FOR_EACH_OBSERVER(CompositorObserver,
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      observer_list_,
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      OnCompositingLockStateChanged(this));
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return compositor_lock_;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::UnlockCompositor() {
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(compositor_lock_);
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  compositor_lock_ = NULL;
4251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (compositor_thread_loop_.get())
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    host_->SetDeferCommits(false);
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(CompositorObserver,
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    observer_list_,
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    OnCompositingLockStateChanged(this));
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::CancelCompositorLock() {
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (compositor_lock_)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    compositor_lock_->CancelLock();
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Compositor::NotifyEnd() {
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_ended_frame_++;
4398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_);
4408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  waiting_on_compositing_end_ = false;
4418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (draw_on_compositing_end_) {
4428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    draw_on_compositing_end_ = false;
4438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Call ScheduleDraw() instead of Draw() in order to allow other
4458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // CompositorObservers to be notified before starting another
4468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // draw cycle.
4478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ScheduleDraw();
4488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  FOR_EACH_OBSERVER(
4506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      CompositorObserver, observer_list_, OnCompositingEnded(this));
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ui
454