Fixed the incorrect PDF sizing on emulated devices in devtools.
[chromium-blink-merge.git] / ui / compositor / compositor.cc
blob14ceca4f9085784552b7062b3dc560ec8ccfcd8a
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_display_lists = IsUISlimmingPaintEnabled();
133 settings.use_zero_copy = IsUIZeroCopyEnabled();
134 settings.use_one_copy = IsUIOneCopyEnabled();
135 settings.use_image_texture_target = context_factory_->GetImageTextureTarget();
136 // Note: gathering of pixel refs is only needed when using multiple
137 // raster threads.
138 settings.gather_pixel_refs = false;
140 settings.use_compositor_animation_timelines =
141 command_line->HasSwitch(switches::kUIEnableCompositorAnimationTimelines);
143 base::TimeTicks before_create = base::TimeTicks::Now();
144 host_ = cc::LayerTreeHost::CreateSingleThreaded(
145 this, this, context_factory_->GetSharedBitmapManager(),
146 context_factory_->GetGpuMemoryBufferManager(),
147 context_factory_->GetTaskGraphRunner(), settings, task_runner_, nullptr);
148 UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
149 base::TimeTicks::Now() - before_create);
150 host_->SetRootLayer(root_web_layer_);
151 host_->set_surface_id_namespace(surface_id_allocator_->id_namespace());
152 host_->SetLayerTreeHostClientReady();
155 Compositor::~Compositor() {
156 TRACE_EVENT0("shutdown", "Compositor::destructor");
158 CancelCompositorLock();
159 DCHECK(!compositor_lock_);
161 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
162 OnCompositingShuttingDown(this));
164 DCHECK(begin_frame_observer_list_.empty());
166 if (root_layer_)
167 root_layer_->SetCompositor(NULL);
169 // Stop all outstanding draws before telling the ContextFactory to tear
170 // down any contexts that the |host_| may rely upon.
171 host_.reset();
173 context_factory_->RemoveCompositor(this);
176 void Compositor::SetOutputSurface(
177 scoped_ptr<cc::OutputSurface> output_surface) {
178 host_->SetOutputSurface(output_surface.Pass());
181 void Compositor::ScheduleDraw() {
182 host_->SetNeedsCommit();
185 void Compositor::SetRootLayer(Layer* root_layer) {
186 if (root_layer_ == root_layer)
187 return;
188 if (root_layer_)
189 root_layer_->SetCompositor(NULL);
190 root_layer_ = root_layer;
191 if (root_layer_ && !root_layer_->GetCompositor())
192 root_layer_->SetCompositor(this);
193 root_web_layer_->RemoveAllChildren();
194 if (root_layer_)
195 root_web_layer_->AddChild(root_layer_->cc_layer());
198 void Compositor::SetHostHasTransparentBackground(
199 bool host_has_transparent_background) {
200 host_->set_has_transparent_background(host_has_transparent_background);
203 void Compositor::ScheduleFullRedraw() {
204 // TODO(enne): Some callers (mac) call this function expecting that it
205 // will also commit. This should probably just redraw the screen
206 // from damage and not commit. ScheduleDraw/ScheduleRedraw need
207 // better names.
208 host_->SetNeedsRedraw();
209 host_->SetNeedsCommit();
212 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) {
213 // TODO(enne): Make this not commit. See ScheduleFullRedraw.
214 host_->SetNeedsRedrawRect(damage_rect);
215 host_->SetNeedsCommit();
218 void Compositor::FinishAllRendering() {
219 host_->FinishAllRendering();
222 void Compositor::DisableSwapUntilResize() {
223 host_->FinishAllRendering();
224 context_factory_->ResizeDisplay(this, gfx::Size());
227 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) {
228 scoped_ptr<cc::SwapPromise> swap_promise(
229 new cc::LatencyInfoSwapPromise(latency_info));
230 host_->QueueSwapPromise(swap_promise.Pass());
233 void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) {
234 DCHECK_GT(scale, 0);
235 if (!size_in_pixel.IsEmpty()) {
236 size_ = size_in_pixel;
237 host_->SetViewportSize(size_in_pixel);
238 root_web_layer_->SetBounds(size_in_pixel);
239 context_factory_->ResizeDisplay(this, size_in_pixel);
241 if (device_scale_factor_ != scale) {
242 device_scale_factor_ = scale;
243 host_->SetDeviceScaleFactor(scale);
244 if (root_layer_)
245 root_layer_->OnDeviceScaleFactorChanged(scale);
249 void Compositor::SetBackgroundColor(SkColor color) {
250 host_->set_background_color(color);
251 ScheduleDraw();
254 void Compositor::SetVisible(bool visible) {
255 host_->SetVisible(visible);
258 bool Compositor::IsVisible() {
259 return host_->visible();
262 scoped_refptr<CompositorVSyncManager> Compositor::vsync_manager() const {
263 return vsync_manager_;
266 void Compositor::AddObserver(CompositorObserver* observer) {
267 observer_list_.AddObserver(observer);
270 void Compositor::RemoveObserver(CompositorObserver* observer) {
271 observer_list_.RemoveObserver(observer);
274 bool Compositor::HasObserver(const CompositorObserver* observer) const {
275 return observer_list_.HasObserver(observer);
278 void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) {
279 animation_observer_list_.AddObserver(observer);
280 host_->SetNeedsAnimate();
283 void Compositor::RemoveAnimationObserver(
284 CompositorAnimationObserver* observer) {
285 animation_observer_list_.RemoveObserver(observer);
288 bool Compositor::HasAnimationObserver(
289 const CompositorAnimationObserver* observer) const {
290 return animation_observer_list_.HasObserver(observer);
293 void Compositor::AddBeginFrameObserver(CompositorBeginFrameObserver* observer) {
294 DCHECK(std::find(begin_frame_observer_list_.begin(),
295 begin_frame_observer_list_.end(), observer) ==
296 begin_frame_observer_list_.end());
298 if (begin_frame_observer_list_.empty())
299 host_->SetChildrenNeedBeginFrames(true);
301 if (missed_begin_frame_args_.IsValid())
302 observer->OnSendBeginFrame(missed_begin_frame_args_);
304 begin_frame_observer_list_.push_back(observer);
307 void Compositor::RemoveBeginFrameObserver(
308 CompositorBeginFrameObserver* observer) {
309 auto it = std::find(begin_frame_observer_list_.begin(),
310 begin_frame_observer_list_.end(), observer);
311 DCHECK(it != begin_frame_observer_list_.end());
312 begin_frame_observer_list_.erase(it);
314 if (begin_frame_observer_list_.empty()) {
315 host_->SetChildrenNeedBeginFrames(false);
316 missed_begin_frame_args_ = cc::BeginFrameArgs();
320 void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) {
321 FOR_EACH_OBSERVER(CompositorAnimationObserver,
322 animation_observer_list_,
323 OnAnimationStep(args.frame_time));
324 if (animation_observer_list_.might_have_observers())
325 host_->SetNeedsAnimate();
328 void Compositor::BeginMainFrameNotExpectedSoon() {
331 void Compositor::Layout() {
332 if (root_layer_)
333 root_layer_->SendDamagedRects();
336 void Compositor::RequestNewOutputSurface() {
337 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/466870
338 // is fixed.
339 tracked_objects::ScopedTracker tracking_profile(
340 FROM_HERE_WITH_EXPLICIT_FUNCTION(
341 "466870 Compositor::RequestNewOutputSurface"));
343 context_factory_->CreateOutputSurface(weak_ptr_factory_.GetWeakPtr());
346 void Compositor::DidInitializeOutputSurface() {
349 void Compositor::DidFailToInitializeOutputSurface() {
350 // The OutputSurface should already be bound/initialized before being given to
351 // the Compositor.
352 NOTREACHED();
355 void Compositor::DidCommit() {
356 DCHECK(!IsLocked());
357 FOR_EACH_OBSERVER(CompositorObserver,
358 observer_list_,
359 OnCompositingDidCommit(this));
362 void Compositor::DidCommitAndDrawFrame() {
365 void Compositor::DidCompleteSwapBuffers() {
366 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
367 OnCompositingEnded(this));
370 void Compositor::DidPostSwapBuffers() {
371 base::TimeTicks start_time = gfx::FrameTime::Now();
372 FOR_EACH_OBSERVER(CompositorObserver, observer_list_,
373 OnCompositingStarted(this, start_time));
376 void Compositor::DidAbortSwapBuffers() {
377 FOR_EACH_OBSERVER(CompositorObserver,
378 observer_list_,
379 OnCompositingAborted(this));
382 void Compositor::SendBeginFramesToChildren(const cc::BeginFrameArgs& args) {
383 for (auto observer : begin_frame_observer_list_)
384 observer->OnSendBeginFrame(args);
386 missed_begin_frame_args_ = args;
387 missed_begin_frame_args_.type = cc::BeginFrameArgs::MISSED;
390 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const {
391 return host_->debug_state();
394 void Compositor::SetLayerTreeDebugState(
395 const cc::LayerTreeDebugState& debug_state) {
396 host_->SetDebugState(debug_state);
399 const cc::RendererSettings& Compositor::GetRendererSettings() const {
400 return host_->settings().renderer_settings;
403 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() {
404 if (!compositor_lock_) {
405 compositor_lock_ = new CompositorLock(this);
406 host_->SetDeferCommits(true);
407 FOR_EACH_OBSERVER(CompositorObserver,
408 observer_list_,
409 OnCompositingLockStateChanged(this));
411 return compositor_lock_;
414 void Compositor::UnlockCompositor() {
415 DCHECK(compositor_lock_);
416 compositor_lock_ = NULL;
417 host_->SetDeferCommits(false);
418 FOR_EACH_OBSERVER(CompositorObserver,
419 observer_list_,
420 OnCompositingLockStateChanged(this));
423 void Compositor::CancelCompositorLock() {
424 if (compositor_lock_)
425 compositor_lock_->CancelLock();
428 } // namespace ui