[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> |
dcheng | 79c1449 | 2015-12-18 05:07:26 | [diff] [blame] | 9 | #include <utility> |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 10 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 12 | #include "base/command_line.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" |
ssid | 334fb87a | 2015-01-27 20:12:07 | [diff] [blame] | 17 | #include "base/trace_event/trace_event.h" |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 18 | #include "cc/animation/animation_host.h" |
| 19 | #include "cc/animation/animation_id_provider.h" |
| 20 | #include "cc/animation/animation_timeline.h" |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 21 | #include "cc/base/switches.h" |
[email protected] | 3052b10f | 2013-03-18 07:41:21 | [diff] [blame] | 22 | #include "cc/input/input_handler.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 23 | #include "cc/layers/layer.h" |
[email protected] | 04c5900d | 2014-08-18 13:38:36 | [diff] [blame] | 24 | #include "cc/output/begin_frame_args.h" |
[email protected] | 7f0d825f | 2013-03-18 07:24:30 | [diff] [blame] | 25 | #include "cc/output/context_provider.h" |
jamesr | f313a21 | 2015-03-16 21:27:37 | [diff] [blame] | 26 | #include "cc/output/latency_info_swap_promise.h" |
simonhong | a7e3ac4 | 2014-11-11 20:50:22 | [diff] [blame] | 27 | #include "cc/scheduler/begin_frame_source.h" |
jbauman | fdc3baa | 2014-10-10 00:22:09 | [diff] [blame] | 28 | #include "cc/surfaces/surface_id_allocator.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 29 | #include "cc/trees/layer_tree_host.h" |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 30 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 31 | #include "ui/compositor/compositor_observer.h" |
| 32 | #include "ui/compositor/compositor_switches.h" |
dbeam | e627c52 | 2015-05-05 03:25:52 | [diff] [blame] | 33 | #include "ui/compositor/compositor_vsync_manager.h" |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 34 | #include "ui/compositor/dip_util.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 35 | #include "ui/compositor/layer.h" |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 36 | #include "ui/compositor/layer_animator_collection.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 37 | #include "ui/gl/gl_context.h" |
[email protected] | cc2ae01 | 2012-09-21 19:35:25 | [diff] [blame] | 38 | #include "ui/gl/gl_switches.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 39 | |
| 40 | namespace { |
| 41 | |
| 42 | const double kDefaultRefreshRate = 60.0; |
[email protected] | 7ddeaab | 2013-04-06 00:47:05 | [diff] [blame] | 43 | const double kTestRefreshRate = 200.0; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 44 | |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 45 | } // namespace |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 46 | |
| 47 | namespace ui { |
| 48 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 49 | CompositorLock::CompositorLock(Compositor* compositor) |
| 50 | : compositor_(compositor) { |
ccameron | 00e438cd | 2015-03-12 06:18:14 | [diff] [blame] | 51 | if (compositor_->locks_will_time_out_) { |
| 52 | compositor_->task_runner_->PostDelayedTask( |
| 53 | FROM_HERE, |
| 54 | base::Bind(&CompositorLock::CancelLock, AsWeakPtr()), |
| 55 | base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs)); |
| 56 | } |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | CompositorLock::~CompositorLock() { |
| 60 | CancelLock(); |
| 61 | } |
| 62 | |
| 63 | void CompositorLock::CancelLock() { |
| 64 | if (!compositor_) |
| 65 | return; |
| 66 | compositor_->UnlockCompositor(); |
| 67 | compositor_ = NULL; |
| 68 | } |
| 69 | |
sievers | b2a31d337 | 2015-08-25 19:27:13 | [diff] [blame] | 70 | Compositor::Compositor(ui::ContextFactory* context_factory, |
[email protected] | bd30a23e | 2014-07-25 21:54:15 | [diff] [blame] | 71 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 72 | : context_factory_(context_factory), |
| 73 | root_layer_(NULL), |
sievers | b2a31d337 | 2015-08-25 19:27:13 | [diff] [blame] | 74 | widget_(gfx::kNullAcceleratedWidget), |
sievers | ca8baca | 2015-10-05 23:38:02 | [diff] [blame] | 75 | widget_valid_(false), |
| 76 | output_surface_requested_(false), |
jbauman | fdc3baa | 2014-10-10 00:22:09 | [diff] [blame] | 77 | surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()), |
[email protected] | bd30a23e | 2014-07-25 21:54:15 | [diff] [blame] | 78 | task_runner_(task_runner), |
dbeam | e627c52 | 2015-05-05 03:25:52 | [diff] [blame] | 79 | vsync_manager_(new CompositorVSyncManager()), |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 80 | device_scale_factor_(0.0f), |
isherman | 4f0404e1 | 2014-12-06 01:01:33 | [diff] [blame] | 81 | last_started_frame_(0), |
| 82 | last_ended_frame_(0), |
ccameron | 00e438cd | 2015-03-12 06:18:14 | [diff] [blame] | 83 | locks_will_time_out_(true), |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 84 | compositor_lock_(NULL), |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 85 | layer_animator_collection_(this), |
jbauman | 1a7a512 | 2014-10-28 00:22:52 | [diff] [blame] | 86 | weak_ptr_factory_(this) { |
loyso | a6edaaff | 2015-05-25 03:26:44 | [diff] [blame] | 87 | root_web_layer_ = cc::Layer::Create(Layer::UILayerSettings()); |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 88 | |
avi | 6b10fd0 | 2014-12-23 05:51:23 | [diff] [blame] | 89 | base::CommandLine* command_line = base::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; |
loyso | a6edaaff | 2015-05-25 03:26:44 | [diff] [blame] | 92 | |
danakj | 0006e95b | 2015-08-08 00:36:54 | [diff] [blame] | 93 | // This will ensure PictureLayers always can have LCD text, to match the |
| 94 | // previous behaviour with ContentLayers, where LCD-not-allowed notifications |
| 95 | // were ignored. |
danakj | 3f76ace | 2014-11-18 16:56:00 | [diff] [blame] | 96 | settings.layers_always_allowed_lcd_text = true; |
danakj | 0006e95b | 2015-08-08 00:36:54 | [diff] [blame] | 97 | // Use occlusion to allow more overlapping windows to take less memory. |
| 98 | settings.use_occlusion_for_tile_prioritization = true; |
jbauman | c5be44c | 2014-11-20 22:17:12 | [diff] [blame] | 99 | settings.renderer_settings.refresh_rate = |
| 100 | context_factory_->DoesCreateTestContexts() ? kTestRefreshRate |
| 101 | : kDefaultRefreshRate; |
[email protected] | 0c7a561 | 2014-03-12 21:58:22 | [diff] [blame] | 102 | settings.main_frame_before_activation_enabled = false; |
jincheol.jo | 5899bd55 | 2015-07-15 02:06:46 | [diff] [blame] | 103 | if (command_line->HasSwitch(switches::kDisableGpuVsync)) { |
| 104 | std::string display_vsync_string = |
| 105 | command_line->GetSwitchValueASCII(switches::kDisableGpuVsync); |
| 106 | if (display_vsync_string == "gpu") { |
| 107 | settings.renderer_settings.disable_display_vsync = true; |
| 108 | } else if (display_vsync_string == "beginframe") { |
| 109 | settings.wait_for_beginframe_interval = false; |
| 110 | } else { |
| 111 | settings.renderer_settings.disable_display_vsync = true; |
| 112 | settings.wait_for_beginframe_interval = false; |
| 113 | } |
| 114 | } |
jbauman | c5be44c | 2014-11-20 22:17:12 | [diff] [blame] | 115 | settings.renderer_settings.partial_swap_enabled = |
danakj | ee38c1c | 2015-10-15 19:22:50 | [diff] [blame] | 116 | !command_line->HasSwitch(switches::kUIDisablePartialSwap); |
sunnyps | 34093979 | 2014-10-16 21:59:58 | [diff] [blame] | 117 | #if defined(OS_WIN) |
jbauman | 616238a | 2014-12-03 03:17:53 | [diff] [blame] | 118 | settings.renderer_settings.finish_rendering_on_resize = true; |
ccameron | 07e6ae2 | 2015-11-20 04:46:29 | [diff] [blame] | 119 | #elif defined(OS_MACOSX) |
| 120 | settings.renderer_settings.delay_releasing_overlay_resources = true; |
sunnyps | 34093979 | 2014-10-16 21:59:58 | [diff] [blame] | 121 | #endif |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 122 | |
| 123 | // These flags should be mirrored by renderer versions in content/renderer/. |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 124 | settings.initial_debug_state.show_debug_borders = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 125 | command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders); |
[email protected] | bf9ed2c | 2013-12-10 22:18:39 | [diff] [blame] | 126 | settings.initial_debug_state.show_layer_animation_bounds_rects = |
| 127 | command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 128 | settings.initial_debug_state.show_paint_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 129 | command_line->HasSwitch(switches::kUIShowPaintRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 130 | settings.initial_debug_state.show_property_changed_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 131 | command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 132 | settings.initial_debug_state.show_surface_damage_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 133 | command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 134 | settings.initial_debug_state.show_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 135 | command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 136 | settings.initial_debug_state.show_replica_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 137 | command_line->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects); |
[email protected] | 302fe42 | 2012-06-11 14:49:11 | [diff] [blame] | 138 | |
[email protected] | fe3beef | 2014-02-06 09:20:53 | [diff] [blame] | 139 | settings.initial_debug_state.SetRecordRenderingStats( |
| 140 | command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); |
| 141 | |
ajuma | b6adea9 | 2015-12-06 17:19:15 | [diff] [blame] | 142 | if (command_line->HasSwitch(cc::switches::kDisableCompositorPropertyTrees)) |
| 143 | settings.use_property_trees = false; |
[email protected] | 2cccfef | 2014-05-01 06:05:16 | [diff] [blame] | 144 | settings.use_zero_copy = IsUIZeroCopyEnabled(); |
danakj | add7682 | 2015-05-27 02:09:43 | [diff] [blame] | 145 | |
rbyers | 77d1de5 | 2015-12-18 15:56:22 | [diff] [blame] | 146 | settings.renderer_settings.use_rgba_4444_textures = |
| 147 | command_line->HasSwitch(switches::kUIEnableRGBA4444Textures); |
reveman | cfd85ee | 2015-08-20 21:14:01 | [diff] [blame] | 148 | |
ericrk | 1d17f75 | 2015-10-20 03:03:07 | [diff] [blame] | 149 | // UI compositor always uses partial raster if not using zero-copy. Zero copy |
| 150 | // doesn't currently support partial raster. |
| 151 | settings.use_partial_raster = !settings.use_zero_copy; |
| 152 | |
reveman | eb0fda2 | 2015-10-26 20:27:47 | [diff] [blame] | 153 | // Use CPU_READ_WRITE_PERSISTENT memory buffers to support partial tile |
| 154 | // raster if needed. |
| 155 | gfx::BufferUsage usage = |
| 156 | settings.use_partial_raster |
| 157 | ? gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT |
| 158 | : gfx::BufferUsage::GPU_READ_CPU_READ_WRITE; |
danakj | add7682 | 2015-05-27 02:09:43 | [diff] [blame] | 159 | |
jie.a.chen | 0ffadce3 | 2015-08-07 12:59:23 | [diff] [blame] | 160 | for (size_t format = 0; |
| 161 | format < static_cast<size_t>(gfx::BufferFormat::LAST) + 1; format++) { |
| 162 | DCHECK_GT(settings.use_image_texture_targets.size(), format); |
| 163 | settings.use_image_texture_targets[format] = |
| 164 | context_factory_->GetImageTextureTarget( |
| 165 | static_cast<gfx::BufferFormat>(format), usage); |
| 166 | } |
danakj | add7682 | 2015-05-27 02:09:43 | [diff] [blame] | 167 | |
vmpstr | dfd2286 | 2015-09-25 17:42:41 | [diff] [blame] | 168 | // Note: Only enable image decode tasks if we have more than one worker |
| 169 | // thread. |
| 170 | settings.image_decode_tasks_enabled = false; |
[email protected] | dafdf505 | 2014-03-13 17:02:57 | [diff] [blame] | 171 | |
loyso | 4cc0ad4e | 2015-12-21 23:34:03 | [diff] [blame^] | 172 | settings.use_compositor_animation_timelines = !command_line->HasSwitch( |
| 173 | switches::kUIDisableCompositorAnimationTimelines); |
loyso | 85a000ae | 2015-06-02 01:31:27 | [diff] [blame] | 174 | |
sohan.jyoti | db8e7268 | 2015-11-04 17:46:32 | [diff] [blame] | 175 | #if !defined(OS_ANDROID) |
| 176 | // TODO(sohanjg): Revisit this memory usage in tile manager. |
| 177 | cc::ManagedMemoryPolicy policy( |
| 178 | 512 * 1024 * 1024, gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE, |
| 179 | settings.memory_policy_.num_resources_limit); |
| 180 | settings.memory_policy_ = policy; |
| 181 | #endif |
| 182 | |
[email protected] | 2f2fd9e | 2013-12-05 03:36:30 | [diff] [blame] | 183 | base::TimeTicks before_create = base::TimeTicks::Now(); |
sadrul | 6780f3da | 2015-05-11 17:01:52 | [diff] [blame] | 184 | |
| 185 | cc::LayerTreeHost::InitParams params; |
| 186 | params.client = this; |
| 187 | params.shared_bitmap_manager = context_factory_->GetSharedBitmapManager(); |
| 188 | params.gpu_memory_buffer_manager = |
| 189 | context_factory_->GetGpuMemoryBufferManager(); |
| 190 | params.task_graph_runner = context_factory_->GetTaskGraphRunner(); |
| 191 | params.settings = &settings; |
| 192 | params.main_task_runner = task_runner_; |
| 193 | host_ = cc::LayerTreeHost::CreateSingleThreaded(this, ¶ms); |
[email protected] | 2f2fd9e | 2013-12-05 03:36:30 | [diff] [blame] | 194 | UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor", |
| 195 | base::TimeTicks::Now() - before_create); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 196 | |
| 197 | if (settings.use_compositor_animation_timelines) { |
| 198 | animation_timeline_ = cc::AnimationTimeline::Create( |
| 199 | cc::AnimationIdProvider::NextTimelineId()); |
| 200 | host_->animation_host()->AddAnimationTimeline(animation_timeline_.get()); |
| 201 | } |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 202 | host_->SetRootLayer(root_web_layer_); |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 203 | host_->set_surface_id_namespace(surface_id_allocator_->id_namespace()); |
sievers | 71c62dd5 | 2015-10-07 01:44:39 | [diff] [blame] | 204 | host_->SetVisible(true); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | Compositor::~Compositor() { |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 208 | TRACE_EVENT0("shutdown", "Compositor::destructor"); |
| 209 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 210 | CancelCompositorLock(); |
| 211 | DCHECK(!compositor_lock_); |
| 212 | |
ccameron | 92bcf31 | 2015-01-23 21:04:09 | [diff] [blame] | 213 | FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 214 | OnCompositingShuttingDown(this)); |
| 215 | |
lof84 | adf2ce86 | 2015-06-02 22:23:32 | [diff] [blame] | 216 | FOR_EACH_OBSERVER(CompositorAnimationObserver, animation_observer_list_, |
| 217 | OnCompositingShuttingDown(this)); |
| 218 | |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 219 | if (root_layer_) |
loyso | ac00846 | 2015-05-27 01:05:50 | [diff] [blame] | 220 | root_layer_->ResetCompositor(); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 221 | |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 222 | if (animation_timeline_) |
| 223 | host_->animation_host()->RemoveAnimationTimeline(animation_timeline_.get()); |
| 224 | |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 225 | // Stop all outstanding draws before telling the ContextFactory to tear |
| 226 | // down any contexts that the |host_| may rely upon. |
| 227 | host_.reset(); |
| 228 | |
[email protected] | 4183bf09 | 2014-05-17 17:33:56 | [diff] [blame] | 229 | context_factory_->RemoveCompositor(this); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 230 | } |
| 231 | |
jbauman | 1a7a512 | 2014-10-28 00:22:52 | [diff] [blame] | 232 | void Compositor::SetOutputSurface( |
| 233 | scoped_ptr<cc::OutputSurface> output_surface) { |
sievers | ca8baca | 2015-10-05 23:38:02 | [diff] [blame] | 234 | output_surface_requested_ = false; |
dcheng | 79c1449 | 2015-12-18 05:07:26 | [diff] [blame] | 235 | host_->SetOutputSurface(std::move(output_surface)); |
jbauman | 1a7a512 | 2014-10-28 00:22:52 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 238 | void Compositor::ScheduleDraw() { |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 239 | host_->SetNeedsCommit(); |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 242 | void Compositor::SetRootLayer(Layer* root_layer) { |
[email protected] | 12233c36 | 2011-11-21 16:09:25 | [diff] [blame] | 243 | if (root_layer_ == root_layer) |
| 244 | return; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 245 | if (root_layer_) |
loyso | ac00846 | 2015-05-27 01:05:50 | [diff] [blame] | 246 | root_layer_->ResetCompositor(); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 247 | root_layer_ = root_layer; |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 248 | root_web_layer_->RemoveAllChildren(); |
[email protected] | 66efabe | 2012-08-18 03:06:06 | [diff] [blame] | 249 | if (root_layer_) |
loyso | ac00846 | 2015-05-27 01:05:50 | [diff] [blame] | 250 | root_layer_->SetCompositor(this, root_web_layer_); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 251 | } |
| 252 | |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 253 | cc::AnimationTimeline* Compositor::GetAnimationTimeline() const { |
| 254 | return animation_timeline_.get(); |
| 255 | } |
| 256 | |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 257 | void Compositor::SetHostHasTransparentBackground( |
| 258 | bool host_has_transparent_background) { |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 259 | host_->set_has_transparent_background(host_has_transparent_background); |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 260 | } |
| 261 | |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 262 | void Compositor::ScheduleFullRedraw() { |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 263 | // TODO(enne): Some callers (mac) call this function expecting that it |
| 264 | // will also commit. This should probably just redraw the screen |
| 265 | // from damage and not commit. ScheduleDraw/ScheduleRedraw need |
| 266 | // better names. |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 267 | host_->SetNeedsRedraw(); |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 268 | host_->SetNeedsCommit(); |
[email protected] | 7df588fbd | 2012-02-10 14:15:56 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 271 | void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 272 | // TODO(enne): Make this not commit. See ScheduleFullRedraw. |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 273 | host_->SetNeedsRedrawRect(damage_rect); |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 274 | host_->SetNeedsCommit(); |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 275 | } |
| 276 | |
chirantan | 40e4cd8 | 2015-02-19 01:08:19 | [diff] [blame] | 277 | void Compositor::FinishAllRendering() { |
| 278 | host_->FinishAllRendering(); |
| 279 | } |
| 280 | |
jbauman | 616238a | 2014-12-03 03:17:53 | [diff] [blame] | 281 | void Compositor::DisableSwapUntilResize() { |
[email protected] | b5e2a73 | 2014-05-13 21:27:50 | [diff] [blame] | 282 | host_->FinishAllRendering(); |
jbauman | 616238a | 2014-12-03 03:17:53 | [diff] [blame] | 283 | context_factory_->ResizeDisplay(this, gfx::Size()); |
[email protected] | b5e2a73 | 2014-05-13 21:27:50 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | 66239a2 | 2013-06-05 03:38:26 | [diff] [blame] | 286 | void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { |
[email protected] | d359203a | 2013-11-29 06:16:55 | [diff] [blame] | 287 | scoped_ptr<cc::SwapPromise> swap_promise( |
| 288 | new cc::LatencyInfoSwapPromise(latency_info)); |
dcheng | 79c1449 | 2015-12-18 05:07:26 | [diff] [blame] | 289 | host_->QueueSwapPromise(std::move(swap_promise)); |
[email protected] | 66239a2 | 2013-06-05 03:38:26 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 292 | void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) { |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 293 | DCHECK_GT(scale, 0); |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 294 | if (!size_in_pixel.IsEmpty()) { |
| 295 | size_ = size_in_pixel; |
[email protected] | 18ce5970 | 2013-04-09 04:58:40 | [diff] [blame] | 296 | host_->SetViewportSize(size_in_pixel); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 297 | root_web_layer_->SetBounds(size_in_pixel); |
jbauman | 616238a | 2014-12-03 03:17:53 | [diff] [blame] | 298 | context_factory_->ResizeDisplay(this, size_in_pixel); |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 299 | } |
[email protected] | 2e2216e4 | 2012-05-17 15:17:00 | [diff] [blame] | 300 | if (device_scale_factor_ != scale) { |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 301 | device_scale_factor_ = scale; |
[email protected] | caa2166 | 2014-05-14 10:02:32 | [diff] [blame] | 302 | host_->SetDeviceScaleFactor(scale); |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 303 | if (root_layer_) |
| 304 | root_layer_->OnDeviceScaleFactorChanged(scale); |
| 305 | } |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 306 | } |
| 307 | |
[email protected] | 8760192 | 2013-04-02 03:56:42 | [diff] [blame] | 308 | void Compositor::SetBackgroundColor(SkColor color) { |
| 309 | host_->set_background_color(color); |
| 310 | ScheduleDraw(); |
| 311 | } |
| 312 | |
ccameron | 18bbc2a | 2014-08-28 22:36:16 | [diff] [blame] | 313 | void Compositor::SetVisible(bool visible) { |
| 314 | host_->SetVisible(visible); |
| 315 | } |
| 316 | |
chirantan | 40e4cd8 | 2015-02-19 01:08:19 | [diff] [blame] | 317 | bool Compositor::IsVisible() { |
| 318 | return host_->visible(); |
| 319 | } |
| 320 | |
simonhong | 047d61b | 2015-05-22 02:37:58 | [diff] [blame] | 321 | void Compositor::SetAuthoritativeVSyncInterval( |
| 322 | const base::TimeDelta& interval) { |
| 323 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 324 | cc::switches::kEnableBeginFrameScheduling)) { |
| 325 | host_->SetAuthoritativeVSyncInterval(interval); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | vsync_manager_->SetAuthoritativeVSyncInterval(interval); |
| 330 | } |
| 331 | |
sievers | ca8baca | 2015-10-05 23:38:02 | [diff] [blame] | 332 | void Compositor::SetAcceleratedWidget(gfx::AcceleratedWidget widget) { |
sievers | b2a31d337 | 2015-08-25 19:27:13 | [diff] [blame] | 333 | // This function should only get called once. |
sievers | ca8baca | 2015-10-05 23:38:02 | [diff] [blame] | 334 | DCHECK(!widget_valid_); |
sievers | b2a31d337 | 2015-08-25 19:27:13 | [diff] [blame] | 335 | widget_ = widget; |
sievers | ca8baca | 2015-10-05 23:38:02 | [diff] [blame] | 336 | widget_valid_ = true; |
| 337 | if (output_surface_requested_) |
| 338 | context_factory_->CreateOutputSurface(weak_ptr_factory_.GetWeakPtr()); |
| 339 | } |
| 340 | |
sievers | 13e7828 | 2015-10-06 01:54:40 | [diff] [blame] | 341 | gfx::AcceleratedWidget Compositor::ReleaseAcceleratedWidget() { |
| 342 | DCHECK(!IsVisible()); |
| 343 | if (!host_->output_surface_lost()) |
| 344 | host_->ReleaseOutputSurface(); |
| 345 | context_factory_->RemoveCompositor(this); |
| 346 | widget_valid_ = false; |
| 347 | gfx::AcceleratedWidget widget = widget_; |
| 348 | widget_ = gfx::kNullAcceleratedWidget; |
| 349 | return widget; |
| 350 | } |
| 351 | |
sievers | ca8baca | 2015-10-05 23:38:02 | [diff] [blame] | 352 | gfx::AcceleratedWidget Compositor::widget() const { |
| 353 | DCHECK(widget_valid_); |
| 354 | return widget_; |
sievers | b2a31d337 | 2015-08-25 19:27:13 | [diff] [blame] | 355 | } |
| 356 | |
dbeam | e627c52 | 2015-05-05 03:25:52 | [diff] [blame] | 357 | scoped_refptr<CompositorVSyncManager> Compositor::vsync_manager() const { |
| 358 | return vsync_manager_; |
[email protected] | 2bd1fcf0 | 2014-02-12 22:35:53 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 361 | void Compositor::AddObserver(CompositorObserver* observer) { |
| 362 | observer_list_.AddObserver(observer); |
| 363 | } |
| 364 | |
| 365 | void Compositor::RemoveObserver(CompositorObserver* observer) { |
| 366 | observer_list_.RemoveObserver(observer); |
| 367 | } |
| 368 | |
mgiuca | 64ccf236 | 2014-11-10 06:44:23 | [diff] [blame] | 369 | bool Compositor::HasObserver(const CompositorObserver* observer) const { |
[email protected] | 3ce2feb | 2011-09-19 18:44:23 | [diff] [blame] | 370 | return observer_list_.HasObserver(observer); |
| 371 | } |
| 372 | |
[email protected] | 5ffe6d14 | 2014-08-01 16:04:38 | [diff] [blame] | 373 | void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) { |
| 374 | animation_observer_list_.AddObserver(observer); |
| 375 | host_->SetNeedsAnimate(); |
| 376 | } |
| 377 | |
| 378 | void Compositor::RemoveAnimationObserver( |
| 379 | CompositorAnimationObserver* observer) { |
| 380 | animation_observer_list_.RemoveObserver(observer); |
| 381 | } |
| 382 | |
mgiuca | 64ccf236 | 2014-11-10 06:44:23 | [diff] [blame] | 383 | bool Compositor::HasAnimationObserver( |
| 384 | const CompositorAnimationObserver* observer) const { |
[email protected] | 5ffe6d14 | 2014-08-01 16:04:38 | [diff] [blame] | 385 | return animation_observer_list_.HasObserver(observer); |
| 386 | } |
| 387 | |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 388 | void Compositor::AddBeginFrameObserver(CompositorBeginFrameObserver* observer) { |
jdduke | 7620c3c8 | 2015-10-22 21:46:47 | [diff] [blame] | 389 | if (!begin_frame_observer_list_.might_have_observers()) |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 390 | host_->SetChildrenNeedBeginFrames(true); |
| 391 | |
jdduke | 7620c3c8 | 2015-10-22 21:46:47 | [diff] [blame] | 392 | begin_frame_observer_list_.AddObserver(observer); |
| 393 | |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 394 | if (missed_begin_frame_args_.IsValid()) |
| 395 | observer->OnSendBeginFrame(missed_begin_frame_args_); |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void Compositor::RemoveBeginFrameObserver( |
| 399 | CompositorBeginFrameObserver* observer) { |
jdduke | 7620c3c8 | 2015-10-22 21:46:47 | [diff] [blame] | 400 | begin_frame_observer_list_.RemoveObserver(observer); |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 401 | |
jdduke | 7620c3c8 | 2015-10-22 21:46:47 | [diff] [blame] | 402 | // As this call may take place while iterating over observers, unsubscription |
| 403 | // from |host_| is performed after iteration in |SendBeginFramesToChildren()|. |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 404 | } |
| 405 | |
[email protected] | 04c5900d | 2014-08-18 13:38:36 | [diff] [blame] | 406 | void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) { |
[email protected] | 5ffe6d14 | 2014-08-01 16:04:38 | [diff] [blame] | 407 | FOR_EACH_OBSERVER(CompositorAnimationObserver, |
| 408 | animation_observer_list_, |
[email protected] | 04c5900d | 2014-08-18 13:38:36 | [diff] [blame] | 409 | OnAnimationStep(args.frame_time)); |
[email protected] | 5ffe6d14 | 2014-08-01 16:04:38 | [diff] [blame] | 410 | if (animation_observer_list_.might_have_observers()) |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 411 | host_->SetNeedsAnimate(); |
| 412 | } |
| 413 | |
rmcilroy | 0a19362a | 2015-02-18 12:34:25 | [diff] [blame] | 414 | void Compositor::BeginMainFrameNotExpectedSoon() { |
| 415 | } |
| 416 | |
danakj | 0b5eae6c | 2015-04-24 22:35:11 | [diff] [blame] | 417 | static void SendDamagedRectsRecursive(ui::Layer* layer) { |
| 418 | layer->SendDamagedRects(); |
| 419 | for (auto* child : layer->children()) |
| 420 | SendDamagedRectsRecursive(child); |
| 421 | } |
| 422 | |
wkorman | 7265db01 | 2015-11-03 04:08:25 | [diff] [blame] | 423 | void Compositor::UpdateLayerTreeHost() { |
danakj | 0b5eae6c | 2015-04-24 22:35:11 | [diff] [blame] | 424 | if (!root_layer()) |
| 425 | return; |
| 426 | SendDamagedRectsRecursive(root_layer()); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 427 | } |
| 428 | |
enne | 7f8fdde | 2014-12-10 21:32:09 | [diff] [blame] | 429 | void Compositor::RequestNewOutputSurface() { |
sievers | ca8baca | 2015-10-05 23:38:02 | [diff] [blame] | 430 | DCHECK(!output_surface_requested_); |
| 431 | output_surface_requested_ = true; |
| 432 | if (widget_valid_) |
| 433 | context_factory_->CreateOutputSurface(weak_ptr_factory_.GetWeakPtr()); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 434 | } |
| 435 | |
enne | 7f8fdde | 2014-12-10 21:32:09 | [diff] [blame] | 436 | void Compositor::DidInitializeOutputSurface() { |
enne | 7f8fdde | 2014-12-10 21:32:09 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void Compositor::DidFailToInitializeOutputSurface() { |
danakj | 13d9708 | 2015-02-25 23:04:47 | [diff] [blame] | 440 | // The OutputSurface should already be bound/initialized before being given to |
| 441 | // the Compositor. |
| 442 | NOTREACHED(); |
enne | 7f8fdde | 2014-12-10 21:32:09 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 445 | void Compositor::DidCommit() { |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 446 | DCHECK(!IsLocked()); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 447 | FOR_EACH_OBSERVER(CompositorObserver, |
| 448 | observer_list_, |
| 449 | OnCompositingDidCommit(this)); |
| 450 | } |
| 451 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 452 | void Compositor::DidCommitAndDrawFrame() { |
weiliangc | 1f27b28 | 2014-10-09 17:10:33 | [diff] [blame] | 453 | } |
| 454 | |
isherman | 4f0404e1 | 2014-12-06 01:01:33 | [diff] [blame] | 455 | void Compositor::DidCompleteSwapBuffers() { |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 456 | FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 457 | OnCompositingEnded(this)); |
isherman | 4f0404e1 | 2014-12-06 01:01:33 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void Compositor::DidPostSwapBuffers() { |
abhishek.ka | 7215854d | 2015-05-26 06:13:17 | [diff] [blame] | 461 | base::TimeTicks start_time = base::TimeTicks::Now(); |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 462 | FOR_EACH_OBSERVER(CompositorObserver, observer_list_, |
| 463 | OnCompositingStarted(this, start_time)); |
isherman | 4f0404e1 | 2014-12-06 01:01:33 | [diff] [blame] | 464 | } |
| 465 | |
[email protected] | 4d7e46a | 2013-11-08 05:33:40 | [diff] [blame] | 466 | void Compositor::DidAbortSwapBuffers() { |
[email protected] | 4d7e46a | 2013-11-08 05:33:40 | [diff] [blame] | 467 | FOR_EACH_OBSERVER(CompositorObserver, |
| 468 | observer_list_, |
| 469 | OnCompositingAborted(this)); |
| 470 | } |
| 471 | |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 472 | void Compositor::SendBeginFramesToChildren(const cc::BeginFrameArgs& args) { |
jdduke | 7620c3c8 | 2015-10-22 21:46:47 | [diff] [blame] | 473 | FOR_EACH_OBSERVER(CompositorBeginFrameObserver, begin_frame_observer_list_, |
| 474 | OnSendBeginFrame(args)); |
| 475 | |
| 476 | // Unsubscription is performed here, after iteration, to handle the case where |
| 477 | // the last BeginFrame observer is removed while iterating over the observers. |
| 478 | if (!begin_frame_observer_list_.might_have_observers()) { |
| 479 | host_->SetChildrenNeedBeginFrames(false); |
| 480 | // Unsubscription should reset |missed_begin_frame_args_|, avoiding stale |
| 481 | // BeginFrame dispatch when the next BeginFrame observer is added. |
| 482 | missed_begin_frame_args_ = cc::BeginFrameArgs(); |
| 483 | return; |
| 484 | } |
simonhong | 8af4c83 | 2015-03-21 07:40:51 | [diff] [blame] | 485 | |
| 486 | missed_begin_frame_args_ = args; |
| 487 | missed_begin_frame_args_.type = cc::BeginFrameArgs::MISSED; |
| 488 | } |
| 489 | |
[email protected] | 2e77cdbb | 2013-04-29 13:59:14 | [diff] [blame] | 490 | const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 491 | return host_->debug_state(); |
[email protected] | 918f8db4 | 2013-04-27 01:53:40 | [diff] [blame] | 492 | } |
| 493 | |
[email protected] | 2e77cdbb | 2013-04-29 13:59:14 | [diff] [blame] | 494 | void Compositor::SetLayerTreeDebugState( |
| 495 | const cc::LayerTreeDebugState& debug_state) { |
[email protected] | 918f8db4 | 2013-04-27 01:53:40 | [diff] [blame] | 496 | host_->SetDebugState(debug_state); |
| 497 | } |
| 498 | |
jbauman | c5be44c | 2014-11-20 22:17:12 | [diff] [blame] | 499 | const cc::RendererSettings& Compositor::GetRendererSettings() const { |
| 500 | return host_->settings().renderer_settings; |
| 501 | } |
| 502 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 503 | scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 504 | if (!compositor_lock_) { |
| 505 | compositor_lock_ = new CompositorLock(this); |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 506 | host_->SetDeferCommits(true); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 507 | FOR_EACH_OBSERVER(CompositorObserver, |
| 508 | observer_list_, |
| 509 | OnCompositingLockStateChanged(this)); |
| 510 | } |
| 511 | return compositor_lock_; |
| 512 | } |
| 513 | |
| 514 | void Compositor::UnlockCompositor() { |
| 515 | DCHECK(compositor_lock_); |
| 516 | compositor_lock_ = NULL; |
weiliangc | 5efa0a1 | 2015-01-29 19:56:46 | [diff] [blame] | 517 | host_->SetDeferCommits(false); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 518 | FOR_EACH_OBSERVER(CompositorObserver, |
| 519 | observer_list_, |
| 520 | OnCompositingLockStateChanged(this)); |
| 521 | } |
| 522 | |
| 523 | void Compositor::CancelCompositorLock() { |
| 524 | if (compositor_lock_) |
| 525 | compositor_lock_->CancelLock(); |
| 526 | } |
| 527 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 528 | } // namespace ui |