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