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