Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / compositor / compositor.cc
blob996c9852f25e97fc5e3b3f917ab86731e3f674f9
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/profiler/scoped_tracker.h"
15 #include "base/strings/string_util.h"
16 #include "base/sys_info.h"
17 #include "base/trace_event/trace_event.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/output/latency_info_swap_promise.h"
24 #include "cc/scheduler/begin_frame_source.h"
25 #include "cc/surfaces/surface_id_allocator.h"
26 #include "cc/trees/layer_tree_host.h"
27 #include "third_party/skia/include/core/SkBitmap.h"
28 #include "ui/compositor/compositor_observer.h"
29 #include "ui/compositor/compositor_switches.h"
30 #include "ui/compositor/compositor_vsync_manager.h"
31 #include "ui/compositor/dip_util.h"
32 #include "ui/compositor/layer.h"
33 #include "ui/compositor/layer_animator_collection.h"
34 #include "ui/gfx/frame_time.h"
35 #include "ui/gl/gl_context.h"
36 #include "ui/gl/gl_switches.h"
38 namespace {
40 const double kDefaultRefreshRate = 60.0;
41 const double kTestRefreshRate = 200.0;
43 const int kCompositorLockTimeoutMs = 67;
45 } // namespace
47 namespace ui {
49 CompositorLock::CompositorLock(Compositor* compositor)
50 : compositor_(compositor) {
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));
59 CompositorLock::~CompositorLock() {
60 CancelLock();
63 void CompositorLock::CancelLock() {
64 if (!compositor_)
65 return;
66 compositor_->UnlockCompositor();
67 compositor_ = NULL;
70 Compositor::Compositor(gfx::AcceleratedWidget widget,
71 ui::ContextFactory* context_factory,
72 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
73 : context_factory_(context_factory),
74 root_layer_(NULL),
75 widget_(widget),
76 surface_id_allocator_(context_factory->CreateSurfaceIdAllocator()),
77 task_runner_(task_runner),
78 vsync_manager_(new CompositorVSyncManager()),
79 device_scale_factor_(0.0f),
80 last_started_frame_(0),
81 last_ended_frame_(0),
82 locks_will_time_out_(true),
83 compositor_lock_(NULL),
84 layer_animator_collection_(this),
85 weak_ptr_factory_(this) {
86 root_web_layer_ = cc::Layer::Create();
88 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
90 cc::LayerTreeSettings settings;
91 // When impl-side painting is enabled, this will ensure PictureLayers always
92 // can have LCD text, to match the previous behaviour with ContentLayers,
93 // where LCD-not-allowed notifications were ignored.
94 settings.layers_always_allowed_lcd_text = true;
95 settings.renderer_settings.refresh_rate =
96 context_factory_->DoesCreateTestContexts() ? kTestRefreshRate
97 : kDefaultRefreshRate;
98 settings.main_frame_before_activation_enabled = false;
99 settings.throttle_frame_production =
100 !command_line->HasSwitch(switches::kDisableGpuVsync);
101 settings.renderer_settings.partial_swap_enabled =
102 !command_line->HasSwitch(cc::switches::kUIDisablePartialSwap);
103 #if defined(OS_CHROMEOS)
104 settings.per_tile_painting_enabled = true;
105 #endif
106 #if defined(OS_WIN)
107 settings.renderer_settings.finish_rendering_on_resize = true;
108 #endif
110 // These flags should be mirrored by renderer versions in content/renderer/.
111 settings.initial_debug_state.show_debug_borders =
112 command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders);
113 settings.initial_debug_state.show_fps_counter =
114 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
115 settings.initial_debug_state.show_layer_animation_bounds_rects =
116 command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds);
117 settings.initial_debug_state.show_paint_rects =
118 command_line->HasSwitch(switches::kUIShowPaintRects);
119 settings.initial_debug_state.show_property_changed_rects =
120 command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects);
121 settings.initial_debug_state.show_surface_damage_rects =
122 command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects);
123 settings.initial_debug_state.show_screen_space_rects =
124 command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects);
125 settings.initial_debug_state.show_replica_screen_space_rects =
126 command_line->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects);
128 settings.initial_debug_state.SetRecordRenderingStats(
129 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
131 settings.impl_side_painting = IsUIImplSidePaintingEnabled();
132 settings.use_zero_copy = IsUIZeroCopyEnabled();
133 settings.use_one_copy = IsUIOneCopyEnabled();
134 settings.use_image_texture_target = context_factory_->GetImageTextureTarget();
135 // Note: gathering of pixel refs is only needed when using multiple
136 // raster threads.
137 settings.gather_pixel_refs = false;
139 settings.use_compositor_animation_timelines =
140 command_line->HasSwitch(switches::kUIEnableCompositorAnimationTimelines);
142 base::TimeTicks before_create = base::TimeTicks::Now();
143 host_ = cc::LayerTreeHost::CreateSingleThreaded(
144 this, this, context_factory_->GetSharedBitmapManager(),
145 context_factory_->GetGpuMemoryBufferManager(),
146 context_factory_->GetTaskGraphRunner(), settings, task_runner_, nullptr);
147 UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
148 base::TimeTicks::Now() - before_create);
149 host_->SetRootLayer(root_web_layer_);
150 host_->set_surface_id_namespace(surface_id_allocator_->id_namespace());
151 host_->SetLayerTreeHostClientReady();
154 Compositor::~Compositor() {
155 TRACE_EVENT0("shutdown", "Compositor::destructor");
157 CancelCompositorLock();
158 DCHECK(!compositor_lock_);
160 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
161 OnCompositingShuttingDown(this));
163 DCHECK(begin_frame_observer_list_.empty());
165 if (root_layer_)
166 root_layer_->SetCompositor(NULL);
168 // Stop all outstanding draws before telling the ContextFactory to tear
169 // down any contexts that the |host_| may rely upon.
170 host_.reset();
172 context_factory_->RemoveCompositor(this);
175 void Compositor::SetOutputSurface(
176 scoped_ptr<cc::OutputSurface> output_surface) {
177 host_->SetOutputSurface(output_surface.Pass());
180 void Compositor::ScheduleDraw() {
181 host_->SetNeedsCommit();
184 void Compositor::SetRootLayer(Layer* root_layer) {
185 if (root_layer_ == root_layer)
186 return;
187 if (root_layer_)
188 root_layer_->SetCompositor(NULL);
189 root_layer_ = root_layer;
190 if (root_layer_ && !root_layer_->GetCompositor())
191 root_layer_->SetCompositor(this);
192 root_web_layer_->RemoveAllChildren();
193 if (root_layer_)
194 root_web_layer_->AddChild(root_layer_->cc_layer());
197 void Compositor::SetHostHasTransparentBackground(
198 bool host_has_transparent_background) {
199 host_->set_has_transparent_background(host_has_transparent_background);
202 void Compositor::ScheduleFullRedraw() {
203 // TODO(enne): Some callers (mac) call this function expecting that it
204 // will also commit. This should probably just redraw the screen
205 // from damage and not commit. ScheduleDraw/ScheduleRedraw need
206 // better names.
207 host_->SetNeedsRedraw();
208 host_->SetNeedsCommit();
211 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) {
212 // TODO(enne): Make this not commit. See ScheduleFullRedraw.
213 host_->SetNeedsRedrawRect(damage_rect);
214 host_->SetNeedsCommit();
217 void Compositor::FinishAllRendering() {
218 host_->FinishAllRendering();
221 void Compositor::DisableSwapUntilResize() {
222 host_->FinishAllRendering();
223 context_factory_->ResizeDisplay(this, gfx::Size());
226 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) {
227 scoped_ptr<cc::SwapPromise> swap_promise(
228 new cc::LatencyInfoSwapPromise(latency_info));
229 host_->QueueSwapPromise(swap_promise.Pass());
232 void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) {
233 DCHECK_GT(scale, 0);
234 if (!size_in_pixel.IsEmpty()) {
235 size_ = size_in_pixel;
236 host_->SetViewportSize(size_in_pixel);
237 root_web_layer_->SetBounds(size_in_pixel);
238 context_factory_->ResizeDisplay(this, size_in_pixel);
240 if (device_scale_factor_ != scale) {
241 device_scale_factor_ = scale;
242 host_->SetDeviceScaleFactor(scale);
243 if (root_layer_)
244 root_layer_->OnDeviceScaleFactorChanged(scale);
248 void Compositor::SetBackgroundColor(SkColor color) {
249 host_->set_background_color(color);
250 ScheduleDraw();
253 void Compositor::SetVisible(bool visible) {
254 host_->SetVisible(visible);
257 bool Compositor::IsVisible() {
258 return host_->visible();
261 scoped_refptr<CompositorVSyncManager> Compositor::vsync_manager() const {
262 return vsync_manager_;
265 void Compositor::AddObserver(CompositorObserver* observer) {
266 observer_list_.AddObserver(observer);
269 void Compositor::RemoveObserver(CompositorObserver* observer) {
270 observer_list_.RemoveObserver(observer);
273 bool Compositor::HasObserver(const CompositorObserver* observer) const {
274 return observer_list_.HasObserver(observer);
277 void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) {
278 animation_observer_list_.AddObserver(observer);
279 host_->SetNeedsAnimate();
282 void Compositor::RemoveAnimationObserver(
283 CompositorAnimationObserver* observer) {
284 animation_observer_list_.RemoveObserver(observer);
287 bool Compositor::HasAnimationObserver(
288 const CompositorAnimationObserver* observer) const {
289 return animation_observer_list_.HasObserver(observer);
292 void Compositor::AddBeginFrameObserver(CompositorBeginFrameObserver* observer) {
293 DCHECK(std::find(begin_frame_observer_list_.begin(),
294 begin_frame_observer_list_.end(), observer) ==
295 begin_frame_observer_list_.end());
297 if (begin_frame_observer_list_.empty())
298 host_->SetChildrenNeedBeginFrames(true);
300 if (missed_begin_frame_args_.IsValid())
301 observer->OnSendBeginFrame(missed_begin_frame_args_);
303 begin_frame_observer_list_.push_back(observer);
306 void Compositor::RemoveBeginFrameObserver(
307 CompositorBeginFrameObserver* observer) {
308 auto it = std::find(begin_frame_observer_list_.begin(),
309 begin_frame_observer_list_.end(), observer);
310 DCHECK(it != begin_frame_observer_list_.end());
311 begin_frame_observer_list_.erase(it);
313 if (begin_frame_observer_list_.empty()) {
314 host_->SetChildrenNeedBeginFrames(false);
315 missed_begin_frame_args_ = cc::BeginFrameArgs();
319 void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) {
320 FOR_EACH_OBSERVER(CompositorAnimationObserver,
321 animation_observer_list_,
322 OnAnimationStep(args.frame_time));
323 if (animation_observer_list_.might_have_observers())
324 host_->SetNeedsAnimate();
327 void Compositor::BeginMainFrameNotExpectedSoon() {
330 void Compositor::Layout() {
331 if (root_layer_)
332 root_layer_->SendDamagedRects();
335 void Compositor::RequestNewOutputSurface() {
336 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/466870
337 // is fixed.
338 tracked_objects::ScopedTracker tracking_profile(
339 FROM_HERE_WITH_EXPLICIT_FUNCTION(
340 "466870 Compositor::RequestNewOutputSurface"));
342 context_factory_->CreateOutputSurface(weak_ptr_factory_.GetWeakPtr());
345 void Compositor::DidInitializeOutputSurface() {
348 void Compositor::DidFailToInitializeOutputSurface() {
349 // The OutputSurface should already be bound/initialized before being given to
350 // the Compositor.
351 NOTREACHED();
354 void Compositor::DidCommit() {
355 DCHECK(!IsLocked());
356 FOR_EACH_OBSERVER(CompositorObserver,
357 observer_list_,
358 OnCompositingDidCommit(this));
361 void Compositor::DidCommitAndDrawFrame() {
364 void Compositor::DidCompleteSwapBuffers() {
365 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
366 OnCompositingEnded(this));
369 void Compositor::DidPostSwapBuffers() {
370 base::TimeTicks start_time = gfx::FrameTime::Now();
371 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
372 OnCompositingStarted(this, start_time));
375 void Compositor::DidAbortSwapBuffers() {
376 FOR_EACH_OBSERVER(CompositorObserver,
377 observer_list_,
378 OnCompositingAborted(this));
381 void Compositor::SendBeginFramesToChildren(const cc::BeginFrameArgs& args) {
382 for (auto observer : begin_frame_observer_list_)
383 observer->OnSendBeginFrame(args);
385 missed_begin_frame_args_ = args;
386 missed_begin_frame_args_.type = cc::BeginFrameArgs::MISSED;
389 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const {
390 return host_->debug_state();
393 void Compositor::SetLayerTreeDebugState(
394 const cc::LayerTreeDebugState& debug_state) {
395 host_->SetDebugState(debug_state);
398 const cc::RendererSettings& Compositor::GetRendererSettings() const {
399 return host_->settings().renderer_settings;
402 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() {
403 if (!compositor_lock_) {
404 compositor_lock_ = new CompositorLock(this);
405 host_->SetDeferCommits(true);
406 FOR_EACH_OBSERVER(CompositorObserver,
407 observer_list_,
408 OnCompositingLockStateChanged(this));
410 return compositor_lock_;
413 void Compositor::UnlockCompositor() {
414 DCHECK(compositor_lock_);
415 compositor_lock_ = NULL;
416 host_->SetDeferCommits(false);
417 FOR_EACH_OBSERVER(CompositorObserver,
418 observer_list_,
419 OnCompositingLockStateChanged(this));
422 void Compositor::CancelCompositorLock() {
423 if (compositor_lock_)
424 compositor_lock_->CancelLock();
427 } // namespace ui