[email protected] | 192093059 | 2012-01-11 14:54:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 5 | #include "ui/compositor/compositor.h" |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 6 | |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 8 | #include <deque> |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 9 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 12 | #include "base/debug/trace_event.h" |
[email protected] | 4e2d03e2 | 2013-07-18 04:19:54 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
[email protected] | 2f2fd9e | 2013-12-05 03:36:30 | [diff] [blame] | 14 | #include "base/metrics/histogram.h" |
[email protected] | f3652ff9 | 2013-06-11 13:54:31 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 16 | #include "base/sys_info.h" |
[email protected] | d359203a | 2013-11-29 06:16:55 | [diff] [blame] | 17 | #include "cc/base/latency_info_swap_promise.h" |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 18 | #include "cc/base/switches.h" |
[email protected] | 3052b10f | 2013-03-18 07:41:21 | [diff] [blame] | 19 | #include "cc/input/input_handler.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 20 | #include "cc/layers/layer.h" |
[email protected] | 04c5900d | 2014-08-18 13:38:36 | [diff] [blame] | 21 | #include "cc/output/begin_frame_args.h" |
[email protected] | 7f0d825f | 2013-03-18 07:24:30 | [diff] [blame] | 22 | #include "cc/output/context_provider.h" |
jbauman | fdc3baa | 2014-10-10 00:22:09 | [diff] [blame] | 23 | #include "cc/surfaces/surface_id_allocator.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 24 | #include "cc/trees/layer_tree_host.h" |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 25 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 26 | #include "ui/compositor/compositor_observer.h" |
| 27 | #include "ui/compositor/compositor_switches.h" |
[email protected] | 2bd1fcf0 | 2014-02-12 22:35:53 | [diff] [blame] | 28 | #include "ui/compositor/compositor_vsync_manager.h" |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 29 | #include "ui/compositor/dip_util.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 30 | #include "ui/compositor/layer.h" |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 31 | #include "ui/compositor/layer_animator_collection.h" |
[email protected] | de2cf8c | 2013-10-25 19:46:46 | [diff] [blame] | 32 | #include "ui/gfx/frame_time.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 33 | #include "ui/gl/gl_context.h" |
[email protected] | cc2ae01 | 2012-09-21 19:35:25 | [diff] [blame] | 34 | #include "ui/gl/gl_switches.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
| 38 | const double kDefaultRefreshRate = 60.0; |
[email protected] | 7ddeaab | 2013-04-06 00:47:05 | [diff] [blame] | 39 | const double kTestRefreshRate = 200.0; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 40 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 41 | const int kCompositorLockTimeoutMs = 67; |
| 42 | |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 43 | } // namespace |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 44 | |
| 45 | namespace ui { |
| 46 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 47 | CompositorLock::CompositorLock(Compositor* compositor) |
| 48 | : compositor_(compositor) { |
[email protected] | bd30a23e | 2014-07-25 21:54:15 | [diff] [blame] | 49 | compositor_->task_runner_->PostDelayedTask( |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 50 | FROM_HERE, |
| 51 | base::Bind(&CompositorLock::CancelLock, AsWeakPtr()), |
| 52 | base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs)); |
| 53 | } |
| 54 | |
| 55 | CompositorLock::~CompositorLock() { |
| 56 | CancelLock(); |
| 57 | } |
| 58 | |
| 59 | void CompositorLock::CancelLock() { |
| 60 | if (!compositor_) |
| 61 | return; |
| 62 | compositor_->UnlockCompositor(); |
| 63 | compositor_ = NULL; |
| 64 | } |
| 65 | |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 66 | Compositor::Compositor(gfx::AcceleratedWidget widget, |
[email protected] | bd30a23e | 2014-07-25 21:54:15 | [diff] [blame] | 67 | ui::ContextFactory* context_factory, |
| 68 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 69 | : context_factory_(context_factory), |
| 70 | root_layer_(NULL), |
| 71 | widget_(widget), |
jbauman | fdc3baa | 2014-10-10 00:22:09 | [diff] [blame] | 72 | surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()), |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 73 | compositor_thread_loop_(context_factory->GetCompositorMessageLoop()), |
[email protected] | bd30a23e | 2014-07-25 21:54:15 | [diff] [blame] | 74 | task_runner_(task_runner), |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 75 | vsync_manager_(new CompositorVSyncManager()), |
| 76 | device_scale_factor_(0.0f), |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 77 | last_started_frame_(0), |
| 78 | last_ended_frame_(0), |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 79 | disable_schedule_composite_(false), |
| 80 | compositor_lock_(NULL), |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 81 | defer_draw_scheduling_(false), |
| 82 | waiting_on_compositing_end_(false), |
| 83 | draw_on_compositing_end_(false), |
| 84 | swap_state_(SWAP_NONE), |
| 85 | layer_animator_collection_(this), |
jbauman | 1a7a512 | 2014-10-28 00:22:52 | [diff] [blame] | 86 | weak_ptr_factory_(this) { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 87 | root_web_layer_ = cc::Layer::Create(); |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 88 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 89 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 90 | |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 91 | cc::LayerTreeSettings settings; |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 92 | settings.refresh_rate = |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 93 | context_factory_->DoesCreateTestContexts() |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 94 | ? kTestRefreshRate |
| 95 | : kDefaultRefreshRate; |
[email protected] | 0c7a561 | 2014-03-12 21:58:22 | [diff] [blame] | 96 | settings.main_frame_before_activation_enabled = false; |
[email protected] | 541f86ff | 2014-03-31 22:09:50 | [diff] [blame] | 97 | settings.throttle_frame_production = |
| 98 | !command_line->HasSwitch(switches::kDisableGpuVsync); |
[email protected] | 977cff1 | 2014-06-06 17:43:23 | [diff] [blame] | 99 | #if !defined(OS_MACOSX) |
[email protected] | 50e15727 | 2013-04-13 05:07:19 | [diff] [blame] | 100 | settings.partial_swap_enabled = |
| 101 | !command_line->HasSwitch(cc::switches::kUIDisablePartialSwap); |
[email protected] | 977cff1 | 2014-06-06 17:43:23 | [diff] [blame] | 102 | #endif |
[email protected] | c6d0ba7 | 2014-02-19 20:47:28 | [diff] [blame] | 103 | #if defined(OS_CHROMEOS) |
| 104 | settings.per_tile_painting_enabled = true; |
| 105 | #endif |
sunnyps | 34093979 | 2014-10-16 21:59:58 | [diff] [blame] | 106 | #if defined(OS_WIN) |
| 107 | settings.disable_hi_res_timer_tasks_on_battery = true; |
| 108 | #endif |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 109 | |
| 110 | // These flags should be mirrored by renderer versions in content/renderer/. |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 111 | settings.initial_debug_state.show_debug_borders = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 112 | command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 113 | settings.initial_debug_state.show_fps_counter = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 114 | command_line->HasSwitch(cc::switches::kUIShowFPSCounter); |
[email protected] | bf9ed2c | 2013-12-10 22:18:39 | [diff] [blame] | 115 | settings.initial_debug_state.show_layer_animation_bounds_rects = |
| 116 | command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 117 | settings.initial_debug_state.show_paint_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 118 | command_line->HasSwitch(switches::kUIShowPaintRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 119 | settings.initial_debug_state.show_property_changed_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 120 | command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 121 | settings.initial_debug_state.show_surface_damage_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 122 | command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 123 | settings.initial_debug_state.show_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 124 | command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 125 | settings.initial_debug_state.show_replica_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 126 | command_line->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 127 | settings.initial_debug_state.show_occluding_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 128 | command_line->HasSwitch(cc::switches::kUIShowOccludingRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 129 | settings.initial_debug_state.show_non_occluding_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 130 | command_line->HasSwitch(cc::switches::kUIShowNonOccludingRects); |
[email protected] | 302fe42 | 2012-06-11 14:49:11 | [diff] [blame] | 131 | |
[email protected] | fe3beef | 2014-02-06 09:20:53 | [diff] [blame] | 132 | settings.initial_debug_state.SetRecordRenderingStats( |
| 133 | command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); |
| 134 | |
[email protected] | dafdf505 | 2014-03-13 17:02:57 | [diff] [blame] | 135 | settings.impl_side_painting = IsUIImplSidePaintingEnabled(); |
[email protected] | 2cccfef | 2014-05-01 06:05:16 | [diff] [blame] | 136 | settings.use_zero_copy = IsUIZeroCopyEnabled(); |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 137 | settings.single_thread_proxy_scheduler = false; |
[email protected] | dafdf505 | 2014-03-13 17:02:57 | [diff] [blame] | 138 | |
[email protected] | 2f2fd9e | 2013-12-05 03:36:30 | [diff] [blame] | 139 | base::TimeTicks before_create = base::TimeTicks::Now(); |
dcheng | 0efb94f | 2014-08-25 21:49:41 | [diff] [blame] | 140 | if (compositor_thread_loop_.get()) { |
[email protected] | 943528e | 2013-11-07 05:01:32 | [diff] [blame] | 141 | host_ = cc::LayerTreeHost::CreateThreaded( |
[email protected] | 142b19f | 2014-03-14 21:50:36 | [diff] [blame] | 142 | this, |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 143 | context_factory_->GetSharedBitmapManager(), |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 144 | context_factory_->GetGpuMemoryBufferManager(), |
[email protected] | 142b19f | 2014-03-14 21:50:36 | [diff] [blame] | 145 | settings, |
[email protected] | bd30a23e | 2014-07-25 21:54:15 | [diff] [blame] | 146 | task_runner_, |
[email protected] | 7898d19 | 2014-05-10 19:00:51 | [diff] [blame] | 147 | compositor_thread_loop_); |
[email protected] | 943528e | 2013-11-07 05:01:32 | [diff] [blame] | 148 | } else { |
[email protected] | 142b19f | 2014-03-14 21:50:36 | [diff] [blame] | 149 | host_ = cc::LayerTreeHost::CreateSingleThreaded( |
[email protected] | 27e6a21 | 2014-07-18 15:51:27 | [diff] [blame] | 150 | this, |
| 151 | this, |
| 152 | context_factory_->GetSharedBitmapManager(), |
reveman | 22dd929 | 2014-10-13 20:52:05 | [diff] [blame] | 153 | context_factory_->GetGpuMemoryBufferManager(), |
[email protected] | 27e6a21 | 2014-07-18 15:51:27 | [diff] [blame] | 154 | settings, |
[email protected] | bd30a23e | 2014-07-25 21:54:15 | [diff] [blame] | 155 | task_runner_); |
[email protected] | 943528e | 2013-11-07 05:01:32 | [diff] [blame] | 156 | } |
[email protected] | 2f2fd9e | 2013-12-05 03:36:30 | [diff] [blame] | 157 | UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor", |
| 158 | base::TimeTicks::Now() - before_create); |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 159 | host_->SetRootLayer(root_web_layer_); |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame^] | 160 | host_->set_surface_id_namespace(surface_id_allocator_->id_namespace()); |
[email protected] | 14bd554 | 2013-05-08 21:51:30 | [diff] [blame] | 161 | host_->SetLayerTreeHostClientReady(); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | Compositor::~Compositor() { |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 165 | TRACE_EVENT0("shutdown", "Compositor::destructor"); |
| 166 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 167 | CancelCompositorLock(); |
| 168 | DCHECK(!compositor_lock_); |
| 169 | |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 170 | if (root_layer_) |
| 171 | root_layer_->SetCompositor(NULL); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 172 | |
| 173 | // Stop all outstanding draws before telling the ContextFactory to tear |
| 174 | // down any contexts that the |host_| may rely upon. |
| 175 | host_.reset(); |
| 176 | |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 177 | context_factory_->RemoveCompositor(this); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 178 | } |
| 179 | |
jbauman | 1a7a512 | 2014-10-28 00:22:52 | [diff] [blame] | 180 | void Compositor::SetOutputSurface( |
| 181 | scoped_ptr<cc::OutputSurface> output_surface) { |
| 182 | host_->SetOutputSurface(output_surface.Pass()); |
| 183 | } |
| 184 | |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 185 | void Compositor::ScheduleDraw() { |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 186 | if (compositor_thread_loop_.get()) { |
| 187 | host_->SetNeedsCommit(); |
| 188 | } else if (!defer_draw_scheduling_) { |
| 189 | defer_draw_scheduling_ = true; |
| 190 | task_runner_->PostTask( |
| 191 | FROM_HERE, |
jbauman | 1a7a512 | 2014-10-28 00:22:52 | [diff] [blame] | 192 | base::Bind(&Compositor::Draw, weak_ptr_factory_.GetWeakPtr())); |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 193 | } |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 196 | void Compositor::SetRootLayer(Layer* root_layer) { |
[email protected] | 12233c36 | 2011-11-21 16:09:25 | [diff] [blame] | 197 | if (root_layer_ == root_layer) |
| 198 | return; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 199 | if (root_layer_) |
| 200 | root_layer_->SetCompositor(NULL); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 201 | root_layer_ = root_layer; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 202 | if (root_layer_ && !root_layer_->GetCompositor()) |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 203 | root_layer_->SetCompositor(this); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 204 | root_web_layer_->RemoveAllChildren(); |
[email protected] | 66efabe | 2012-08-18 03:06:06 | [diff] [blame] | 205 | if (root_layer_) |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 206 | root_web_layer_->AddChild(root_layer_->cc_layer()); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 209 | void Compositor::SetHostHasTransparentBackground( |
| 210 | bool host_has_transparent_background) { |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 211 | host_->set_has_transparent_background(host_has_transparent_background); |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 212 | } |
| 213 | |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 214 | void Compositor::Draw() { |
| 215 | DCHECK(!compositor_thread_loop_.get()); |
| 216 | |
| 217 | defer_draw_scheduling_ = false; |
| 218 | if (waiting_on_compositing_end_) { |
| 219 | draw_on_compositing_end_ = true; |
| 220 | return; |
| 221 | } |
| 222 | if (!root_layer_) |
| 223 | return; |
| 224 | |
| 225 | TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_ + 1); |
| 226 | |
| 227 | DCHECK_NE(swap_state_, SWAP_POSTED); |
| 228 | swap_state_ = SWAP_NONE; |
| 229 | |
| 230 | waiting_on_compositing_end_ = true; |
| 231 | last_started_frame_++; |
| 232 | if (!IsLocked()) { |
| 233 | // TODO(nduca): Temporary while compositor calls |
| 234 | // compositeImmediately() directly. |
| 235 | cc::BeginFrameArgs args = |
| 236 | cc::BeginFrameArgs::Create(gfx::FrameTime::Now(), |
| 237 | base::TimeTicks(), |
| 238 | cc::BeginFrameArgs::DefaultInterval()); |
| 239 | BeginMainFrame(args); |
| 240 | host_->Composite(args.frame_time); |
| 241 | } |
| 242 | if (swap_state_ == SWAP_NONE) |
| 243 | NotifyEnd(); |
| 244 | } |
| 245 | |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 246 | void Compositor::ScheduleFullRedraw() { |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 247 | host_->SetNeedsRedraw(); |
[email protected] | 7df588fbd | 2012-02-10 14:15:56 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 250 | void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 251 | host_->SetNeedsRedrawRect(damage_rect); |
| 252 | } |
| 253 | |
[email protected] | b5e2a73 | 2014-05-13 21:27:50 | [diff] [blame] | 254 | void Compositor::FinishAllRendering() { |
| 255 | host_->FinishAllRendering(); |
| 256 | } |
| 257 | |
[email protected] | 66239a2 | 2013-06-05 03:38:26 | [diff] [blame] | 258 | void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { |
[email protected] | d359203a | 2013-11-29 06:16:55 | [diff] [blame] | 259 | scoped_ptr<cc::SwapPromise> swap_promise( |
| 260 | new cc::LatencyInfoSwapPromise(latency_info)); |
| 261 | host_->QueueSwapPromise(swap_promise.Pass()); |
[email protected] | 66239a2 | 2013-06-05 03:38:26 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 264 | void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) { |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 265 | DCHECK_GT(scale, 0); |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 266 | if (!size_in_pixel.IsEmpty()) { |
| 267 | size_ = size_in_pixel; |
[email protected] | 18ce5970 | 2013-04-09 04:58:40 | [diff] [blame] | 268 | host_->SetViewportSize(size_in_pixel); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 269 | root_web_layer_->SetBounds(size_in_pixel); |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 270 | } |
[email protected] | 2e2216e4 | 2012-05-17 15:17:00 | [diff] [blame] | 271 | if (device_scale_factor_ != scale) { |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 272 | device_scale_factor_ = scale; |
[email protected] | caa2166 | 2014-05-14 10:02:32 | [diff] [blame] | 273 | host_->SetDeviceScaleFactor(scale); |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 274 | if (root_layer_) |
| 275 | root_layer_->OnDeviceScaleFactorChanged(scale); |
| 276 | } |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 277 | } |
| 278 | |
[email protected] | 8760192 | 2013-04-02 03:56:42 | [diff] [blame] | 279 | void Compositor::SetBackgroundColor(SkColor color) { |
| 280 | host_->set_background_color(color); |
| 281 | ScheduleDraw(); |
| 282 | } |
| 283 | |
ccameron | 18bbc2a | 2014-08-28 22:36:16 | [diff] [blame] | 284 | void Compositor::SetVisible(bool visible) { |
| 285 | host_->SetVisible(visible); |
| 286 | } |
| 287 | |
[email protected] | 2bd1fcf0 | 2014-02-12 22:35:53 | [diff] [blame] | 288 | scoped_refptr<CompositorVSyncManager> Compositor::vsync_manager() const { |
| 289 | return vsync_manager_; |
| 290 | } |
| 291 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 292 | void Compositor::AddObserver(CompositorObserver* observer) { |
[email protected] | 80be3388 | 2014-08-13 09:34:19 | [diff] [blame] | 293 | #if defined(OS_MACOSX) |
| 294 | // Debugging instrumentation for crbug.com/401630. |
| 295 | // TODO(ccameron): remove this. |
| 296 | CHECK(observer); |
[email protected] | 6b4504f | 2014-08-09 10:29:47 | [diff] [blame] | 297 | if (!observer_list_.HasObserver(observer)) |
| 298 | observer->observing_count_ += 1; |
| 299 | #endif |
| 300 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 301 | observer_list_.AddObserver(observer); |
| 302 | } |
| 303 | |
| 304 | void Compositor::RemoveObserver(CompositorObserver* observer) { |
[email protected] | 80be3388 | 2014-08-13 09:34:19 | [diff] [blame] | 305 | #if defined(OS_MACOSX) |
| 306 | // Debugging instrumentation for crbug.com/401630. |
| 307 | // TODO(ccameron): remove this. |
[email protected] | 6b4504f | 2014-08-09 10:29:47 | [diff] [blame] | 308 | if (observer_list_.HasObserver(observer)) |
| 309 | observer->observing_count_ -= 1; |
| 310 | #endif |
| 311 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 312 | observer_list_.RemoveObserver(observer); |
| 313 | } |
| 314 | |
[email protected] | 3ce2feb | 2011-09-19 18:44:23 | [diff] [blame] | 315 | bool Compositor::HasObserver(CompositorObserver* observer) { |
| 316 | return observer_list_.HasObserver(observer); |
| 317 | } |
| 318 | |
[email protected] | 5ffe6d14 | 2014-08-01 16:04:38 | [diff] [blame] | 319 | void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) { |
| 320 | animation_observer_list_.AddObserver(observer); |
| 321 | host_->SetNeedsAnimate(); |
| 322 | } |
| 323 | |
| 324 | void Compositor::RemoveAnimationObserver( |
| 325 | CompositorAnimationObserver* observer) { |
| 326 | animation_observer_list_.RemoveObserver(observer); |
| 327 | } |
| 328 | |
| 329 | bool Compositor::HasAnimationObserver(CompositorAnimationObserver* observer) { |
| 330 | return animation_observer_list_.HasObserver(observer); |
| 331 | } |
| 332 | |
[email protected] | 04c5900d | 2014-08-18 13:38:36 | [diff] [blame] | 333 | void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) { |
[email protected] | 5ffe6d14 | 2014-08-01 16:04:38 | [diff] [blame] | 334 | FOR_EACH_OBSERVER(CompositorAnimationObserver, |
| 335 | animation_observer_list_, |
[email protected] | 04c5900d | 2014-08-18 13:38:36 | [diff] [blame] | 336 | OnAnimationStep(args.frame_time)); |
[email protected] | 5ffe6d14 | 2014-08-01 16:04:38 | [diff] [blame] | 337 | if (animation_observer_list_.might_have_observers()) |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 338 | host_->SetNeedsAnimate(); |
| 339 | } |
| 340 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 341 | void Compositor::Layout() { |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 342 | // We're sending damage that will be addressed during this composite |
| 343 | // cycle, so we don't need to schedule another composite to address it. |
| 344 | disable_schedule_composite_ = true; |
[email protected] | f78649ea | 2012-02-23 18:39:04 | [diff] [blame] | 345 | if (root_layer_) |
[email protected] | cedc395 | 2012-03-06 06:15:55 | [diff] [blame] | 346 | root_layer_->SendDamagedRects(); |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 347 | disable_schedule_composite_ = false; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 348 | } |
| 349 | |
enne | 2097cab | 2014-09-25 20:16:31 | [diff] [blame] | 350 | void Compositor::RequestNewOutputSurface(bool fallback) { |
jbauman | 1a7a512 | 2014-10-28 00:22:52 | [diff] [blame] | 351 | context_factory_->CreateOutputSurface(weak_ptr_factory_.GetWeakPtr(), |
| 352 | fallback); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 353 | } |
| 354 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 355 | void Compositor::DidCommit() { |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 356 | DCHECK(!IsLocked()); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 357 | FOR_EACH_OBSERVER(CompositorObserver, |
| 358 | observer_list_, |
| 359 | OnCompositingDidCommit(this)); |
| 360 | } |
| 361 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 362 | void Compositor::DidCommitAndDrawFrame() { |
[email protected] | de2cf8c | 2013-10-25 19:46:46 | [diff] [blame] | 363 | base::TimeTicks start_time = gfx::FrameTime::Now(); |
[email protected] | a8f677c | 2012-03-23 01:36:06 | [diff] [blame] | 364 | FOR_EACH_OBSERVER(CompositorObserver, |
| 365 | observer_list_, |
[email protected] | 3b608551 | 2013-02-21 01:26:20 | [diff] [blame] | 366 | OnCompositingStarted(this, start_time)); |
[email protected] | ba7aeb8 | 2012-02-24 23:36:13 | [diff] [blame] | 367 | } |
| 368 | |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 369 | void Compositor::DidCompleteSwapBuffers() { |
| 370 | if (compositor_thread_loop_.get()) { |
| 371 | NotifyEnd(); |
| 372 | } else { |
| 373 | DCHECK_EQ(swap_state_, SWAP_POSTED); |
| 374 | NotifyEnd(); |
| 375 | swap_state_ = SWAP_COMPLETED; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void Compositor::ScheduleComposite() { |
| 380 | if (!disable_schedule_composite_) |
| 381 | ScheduleDraw(); |
| 382 | } |
| 383 | |
| 384 | void Compositor::ScheduleAnimation() { |
| 385 | ScheduleComposite(); |
| 386 | } |
| 387 | |
| 388 | void Compositor::DidPostSwapBuffers() { |
| 389 | DCHECK(!compositor_thread_loop_.get()); |
| 390 | DCHECK_EQ(swap_state_, SWAP_NONE); |
| 391 | swap_state_ = SWAP_POSTED; |
| 392 | } |
| 393 | |
[email protected] | 4d7e46a | 2013-11-08 05:33:40 | [diff] [blame] | 394 | void Compositor::DidAbortSwapBuffers() { |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 395 | if (!compositor_thread_loop_.get()) { |
| 396 | if (swap_state_ == SWAP_POSTED) { |
| 397 | NotifyEnd(); |
| 398 | swap_state_ = SWAP_COMPLETED; |
| 399 | } |
| 400 | } |
| 401 | |
[email protected] | 4d7e46a | 2013-11-08 05:33:40 | [diff] [blame] | 402 | FOR_EACH_OBSERVER(CompositorObserver, |
| 403 | observer_list_, |
| 404 | OnCompositingAborted(this)); |
| 405 | } |
| 406 | |
[email protected] | 2e77cdbb | 2013-04-29 13:59:14 | [diff] [blame] | 407 | const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 408 | return host_->debug_state(); |
[email protected] | 918f8db4 | 2013-04-27 01:53:40 | [diff] [blame] | 409 | } |
| 410 | |
[email protected] | 2e77cdbb | 2013-04-29 13:59:14 | [diff] [blame] | 411 | void Compositor::SetLayerTreeDebugState( |
| 412 | const cc::LayerTreeDebugState& debug_state) { |
[email protected] | 918f8db4 | 2013-04-27 01:53:40 | [diff] [blame] | 413 | host_->SetDebugState(debug_state); |
| 414 | } |
| 415 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 416 | scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 417 | if (!compositor_lock_) { |
| 418 | compositor_lock_ = new CompositorLock(this); |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 419 | if (compositor_thread_loop_.get()) |
| 420 | host_->SetDeferCommits(true); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 421 | FOR_EACH_OBSERVER(CompositorObserver, |
| 422 | observer_list_, |
| 423 | OnCompositingLockStateChanged(this)); |
| 424 | } |
| 425 | return compositor_lock_; |
| 426 | } |
| 427 | |
| 428 | void Compositor::UnlockCompositor() { |
| 429 | DCHECK(compositor_lock_); |
| 430 | compositor_lock_ = NULL; |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 431 | if (compositor_thread_loop_.get()) |
| 432 | host_->SetDeferCommits(false); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 433 | FOR_EACH_OBSERVER(CompositorObserver, |
| 434 | observer_list_, |
| 435 | OnCompositingLockStateChanged(this)); |
| 436 | } |
| 437 | |
| 438 | void Compositor::CancelCompositorLock() { |
| 439 | if (compositor_lock_) |
| 440 | compositor_lock_->CancelLock(); |
| 441 | } |
| 442 | |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 443 | void Compositor::NotifyEnd() { |
| 444 | last_ended_frame_++; |
| 445 | TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); |
| 446 | waiting_on_compositing_end_ = false; |
| 447 | if (draw_on_compositing_end_) { |
| 448 | draw_on_compositing_end_ = false; |
| 449 | |
| 450 | // Call ScheduleDraw() instead of Draw() in order to allow other |
| 451 | // CompositorObservers to be notified before starting another |
| 452 | // draw cycle. |
| 453 | ScheduleDraw(); |
| 454 | } |
| 455 | FOR_EACH_OBSERVER( |
| 456 | CompositorObserver, observer_list_, OnCompositingEnded(this)); |
| 457 | } |
| 458 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 459 | } // namespace ui |