Enable right clicking on the applist doodle web contents and log the data.
[chromium-blink-merge.git] / ui / compositor / compositor.cc
blob287eaf19751ffc5e0d4f4fe09826482f812fef7c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/compositor/compositor.h"
7 #include <algorithm>
8 #include <deque>
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram.h"
14 #include "base/strings/string_util.h"
15 #include "base/sys_info.h"
16 #include "base/trace_event/trace_event.h"
17 #include "cc/base/latency_info_swap_promise.h"
18 #include "cc/base/switches.h"
19 #include "cc/input/input_handler.h"
20 #include "cc/layers/layer.h"
21 #include "cc/output/begin_frame_args.h"
22 #include "cc/output/context_provider.h"
23 #include "cc/scheduler/begin_frame_source.h"
24 #include "cc/surfaces/surface_id_allocator.h"
25 #include "cc/trees/layer_tree_host.h"
26 #include "third_party/skia/include/core/SkBitmap.h"
27 #include "ui/compositor/compositor_observer.h"
28 #include "ui/compositor/compositor_switches.h"
29 #include "ui/compositor/compositor_vsync_manager.h"
30 #include "ui/compositor/dip_util.h"
31 #include "ui/compositor/layer.h"
32 #include "ui/compositor/layer_animator_collection.h"
33 #include "ui/gfx/frame_time.h"
34 #include "ui/gl/gl_context.h"
35 #include "ui/gl/gl_switches.h"
37 namespace {
39 const double kDefaultRefreshRate = 60.0;
40 const double kTestRefreshRate = 200.0;
42 const int kCompositorLockTimeoutMs = 67;
44 } // namespace
46 namespace ui {
48 CompositorLock::CompositorLock(Compositor* compositor)
49 : compositor_(compositor) {
50 compositor_->task_runner_->PostDelayedTask(
51 FROM_HERE,
52 base::Bind(&CompositorLock::CancelLock, AsWeakPtr()),
53 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs));
56 CompositorLock::~CompositorLock() {
57 CancelLock();
60 void CompositorLock::CancelLock() {
61 if (!compositor_)
62 return;
63 compositor_->UnlockCompositor();
64 compositor_ = NULL;
67 Compositor::Compositor(gfx::AcceleratedWidget widget,
68 ui::ContextFactory* context_factory,
69 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
70 : context_factory_(context_factory),
71 root_layer_(NULL),
72 widget_(widget),
73 surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()),
74 task_runner_(task_runner),
75 vsync_manager_(new CompositorVSyncManager()),
76 device_scale_factor_(0.0f),
77 last_started_frame_(0),
78 last_ended_frame_(0),
79 disable_schedule_composite_(false),
80 compositor_lock_(NULL),
81 layer_animator_collection_(this),
82 weak_ptr_factory_(this) {
83 root_web_layer_ = cc::Layer::Create();
85 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
87 cc::LayerTreeSettings settings;
88 // When impl-side painting is enabled, this will ensure PictureLayers always
89 // can have LCD text, to match the previous behaviour with ContentLayers,
90 // where LCD-not-allowed notifications were ignored.
91 settings.layers_always_allowed_lcd_text = true;
92 settings.renderer_settings.refresh_rate =
93 context_factory_->DoesCreateTestContexts() ? kTestRefreshRate
94 : kDefaultRefreshRate;
95 settings.main_frame_before_activation_enabled = false;
96 settings.throttle_frame_production =
97 !command_line->HasSwitch(switches::kDisableGpuVsync);
98 #if !defined(OS_MACOSX)
99 settings.renderer_settings.partial_swap_enabled =
100 !command_line->HasSwitch(cc::switches::kUIDisablePartialSwap);
101 #endif
102 #if defined(OS_CHROMEOS)
103 settings.per_tile_painting_enabled = true;
104 #endif
105 #if defined(OS_WIN)
106 settings.disable_hi_res_timer_tasks_on_battery =
107 !context_factory_->DoesCreateTestContexts();
108 settings.renderer_settings.finish_rendering_on_resize = true;
109 #endif
111 // These flags should be mirrored by renderer versions in content/renderer/.
112 settings.initial_debug_state.show_debug_borders =
113 command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders);
114 settings.initial_debug_state.show_fps_counter =
115 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
116 settings.initial_debug_state.show_layer_animation_bounds_rects =
117 command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds);
118 settings.initial_debug_state.show_paint_rects =
119 command_line->HasSwitch(switches::kUIShowPaintRects);
120 settings.initial_debug_state.show_property_changed_rects =
121 command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects);
122 settings.initial_debug_state.show_surface_damage_rects =
123 command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects);
124 settings.initial_debug_state.show_screen_space_rects =
125 command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects);
126 settings.initial_debug_state.show_replica_screen_space_rects =
127 command_line->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects);
129 settings.initial_debug_state.SetRecordRenderingStats(
130 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
132 settings.impl_side_painting = IsUIImplSidePaintingEnabled();
133 settings.use_zero_copy = IsUIZeroCopyEnabled();
135 base::TimeTicks before_create = base::TimeTicks::Now();
136 host_ = cc::LayerTreeHost::CreateSingleThreaded(
137 this, this, context_factory_->GetSharedBitmapManager(),
138 context_factory_->GetGpuMemoryBufferManager(), settings, task_runner_,
139 nullptr);
140 UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
141 base::TimeTicks::Now() - before_create);
142 host_->SetRootLayer(root_web_layer_);
143 host_->set_surface_id_namespace(surface_id_allocator_->id_namespace());
144 host_->SetLayerTreeHostClientReady();
147 Compositor::~Compositor() {
148 TRACE_EVENT0("shutdown", "Compositor::destructor");
150 CancelCompositorLock();
151 DCHECK(!compositor_lock_);
153 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
154 OnCompositingShuttingDown(this));
156 if (root_layer_)
157 root_layer_->SetCompositor(NULL);
159 // Stop all outstanding draws before telling the ContextFactory to tear
160 // down any contexts that the |host_| may rely upon.
161 host_.reset();
163 context_factory_->RemoveCompositor(this);
166 void Compositor::SetOutputSurface(
167 scoped_ptr<cc::OutputSurface> output_surface) {
168 host_->SetOutputSurface(output_surface.Pass());
171 void Compositor::ScheduleDraw() {
172 host_->SetNeedsCommit();
175 void Compositor::SetRootLayer(Layer* root_layer) {
176 if (root_layer_ == root_layer)
177 return;
178 if (root_layer_)
179 root_layer_->SetCompositor(NULL);
180 root_layer_ = root_layer;
181 if (root_layer_ && !root_layer_->GetCompositor())
182 root_layer_->SetCompositor(this);
183 root_web_layer_->RemoveAllChildren();
184 if (root_layer_)
185 root_web_layer_->AddChild(root_layer_->cc_layer());
188 void Compositor::SetHostHasTransparentBackground(
189 bool host_has_transparent_background) {
190 host_->set_has_transparent_background(host_has_transparent_background);
193 void Compositor::ScheduleFullRedraw() {
194 // TODO(enne): Some callers (mac) call this function expecting that it
195 // will also commit. This should probably just redraw the screen
196 // from damage and not commit. ScheduleDraw/ScheduleRedraw need
197 // better names.
198 host_->SetNeedsRedraw();
199 host_->SetNeedsCommit();
202 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) {
203 // TODO(enne): Make this not commit. See ScheduleFullRedraw.
204 host_->SetNeedsRedrawRect(damage_rect);
205 host_->SetNeedsCommit();
208 void Compositor::FinishAllRendering() {
209 host_->FinishAllRendering();
212 void Compositor::DisableSwapUntilResize() {
213 host_->FinishAllRendering();
214 context_factory_->ResizeDisplay(this, gfx::Size());
217 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) {
218 scoped_ptr<cc::SwapPromise> swap_promise(
219 new cc::LatencyInfoSwapPromise(latency_info));
220 host_->QueueSwapPromise(swap_promise.Pass());
223 void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) {
224 DCHECK_GT(scale, 0);
225 if (!size_in_pixel.IsEmpty()) {
226 size_ = size_in_pixel;
227 host_->SetViewportSize(size_in_pixel);
228 root_web_layer_->SetBounds(size_in_pixel);
229 context_factory_->ResizeDisplay(this, size_in_pixel);
231 if (device_scale_factor_ != scale) {
232 device_scale_factor_ = scale;
233 host_->SetDeviceScaleFactor(scale);
234 if (root_layer_)
235 root_layer_->OnDeviceScaleFactorChanged(scale);
239 void Compositor::SetBackgroundColor(SkColor color) {
240 host_->set_background_color(color);
241 ScheduleDraw();
244 void Compositor::SetVisible(bool visible) {
245 host_->SetVisible(visible);
248 bool Compositor::IsVisible() {
249 return host_->visible();
252 scoped_refptr<CompositorVSyncManager> Compositor::vsync_manager() const {
253 return vsync_manager_;
256 void Compositor::AddObserver(CompositorObserver* observer) {
257 observer_list_.AddObserver(observer);
260 void Compositor::RemoveObserver(CompositorObserver* observer) {
261 observer_list_.RemoveObserver(observer);
264 bool Compositor::HasObserver(const CompositorObserver* observer) const {
265 return observer_list_.HasObserver(observer);
268 void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) {
269 animation_observer_list_.AddObserver(observer);
270 host_->SetNeedsAnimate();
273 void Compositor::RemoveAnimationObserver(
274 CompositorAnimationObserver* observer) {
275 animation_observer_list_.RemoveObserver(observer);
278 bool Compositor::HasAnimationObserver(
279 const CompositorAnimationObserver* observer) const {
280 return animation_observer_list_.HasObserver(observer);
283 void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) {
284 FOR_EACH_OBSERVER(CompositorAnimationObserver,
285 animation_observer_list_,
286 OnAnimationStep(args.frame_time));
287 if (animation_observer_list_.might_have_observers())
288 host_->SetNeedsAnimate();
291 void Compositor::BeginMainFrameNotExpectedSoon() {
294 void Compositor::Layout() {
295 // We're sending damage that will be addressed during this composite
296 // cycle, so we don't need to schedule another composite to address it.
297 disable_schedule_composite_ = true;
298 if (root_layer_)
299 root_layer_->SendDamagedRects();
300 disable_schedule_composite_ = false;
303 void Compositor::RequestNewOutputSurface() {
304 context_factory_->CreateOutputSurface(weak_ptr_factory_.GetWeakPtr());
307 void Compositor::DidInitializeOutputSurface() {
310 void Compositor::DidFailToInitializeOutputSurface() {
311 // The OutputSurface should already be bound/initialized before being given to
312 // the Compositor.
313 NOTREACHED();
316 void Compositor::DidCommit() {
317 DCHECK(!IsLocked());
318 FOR_EACH_OBSERVER(CompositorObserver,
319 observer_list_,
320 OnCompositingDidCommit(this));
323 void Compositor::DidCommitAndDrawFrame() {
326 void Compositor::DidCompleteSwapBuffers() {
327 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
328 OnCompositingEnded(this));
331 void Compositor::DidPostSwapBuffers() {
332 base::TimeTicks start_time = gfx::FrameTime::Now();
333 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
334 OnCompositingStarted(this, start_time));
337 void Compositor::DidAbortSwapBuffers() {
338 FOR_EACH_OBSERVER(CompositorObserver,
339 observer_list_,
340 OnCompositingAborted(this));
343 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const {
344 return host_->debug_state();
347 void Compositor::SetLayerTreeDebugState(
348 const cc::LayerTreeDebugState& debug_state) {
349 host_->SetDebugState(debug_state);
352 const cc::RendererSettings& Compositor::GetRendererSettings() const {
353 return host_->settings().renderer_settings;
356 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() {
357 if (!compositor_lock_) {
358 compositor_lock_ = new CompositorLock(this);
359 host_->SetDeferCommits(true);
360 FOR_EACH_OBSERVER(CompositorObserver,
361 observer_list_,
362 OnCompositingLockStateChanged(this));
364 return compositor_lock_;
367 void Compositor::UnlockCompositor() {
368 DCHECK(compositor_lock_);
369 compositor_lock_ = NULL;
370 host_->SetDeferCommits(false);
371 FOR_EACH_OBSERVER(CompositorObserver,
372 observer_list_,
373 OnCompositingLockStateChanged(this));
376 void Compositor::CancelCompositorLock() {
377 if (compositor_lock_)
378 compositor_lock_->CancelLock();
381 } // namespace ui