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"
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"
39 const double kDefaultRefreshRate
= 60.0;
40 const double kTestRefreshRate
= 200.0;
42 const int kCompositorLockTimeoutMs
= 67;
48 CompositorLock::CompositorLock(Compositor
* compositor
)
49 : compositor_(compositor
) {
50 compositor_
->task_runner_
->PostDelayedTask(
52 base::Bind(&CompositorLock::CancelLock
, AsWeakPtr()),
53 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs
));
56 CompositorLock::~CompositorLock() {
60 void CompositorLock::CancelLock() {
63 compositor_
->UnlockCompositor();
67 Compositor::Compositor(gfx::AcceleratedWidget widget
,
68 ui::ContextFactory
* context_factory
,
69 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
70 : context_factory_(context_factory
),
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),
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
);
102 #if defined(OS_CHROMEOS)
103 settings
.per_tile_painting_enabled
= true;
106 settings
.disable_hi_res_timer_tasks_on_battery
=
107 !context_factory_
->DoesCreateTestContexts();
108 settings
.renderer_settings
.finish_rendering_on_resize
= true;
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();
134 settings
.use_one_copy
= IsUIOneCopyEnabled();
135 settings
.use_image_texture_target
= context_factory_
->GetImageTextureTarget();
137 base::TimeTicks before_create
= base::TimeTicks::Now();
138 host_
= cc::LayerTreeHost::CreateSingleThreaded(
139 this, this, context_factory_
->GetSharedBitmapManager(),
140 context_factory_
->GetGpuMemoryBufferManager(), settings
, task_runner_
,
142 UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
143 base::TimeTicks::Now() - before_create
);
144 host_
->SetRootLayer(root_web_layer_
);
145 host_
->set_surface_id_namespace(surface_id_allocator_
->id_namespace());
146 host_
->SetLayerTreeHostClientReady();
149 Compositor::~Compositor() {
150 TRACE_EVENT0("shutdown", "Compositor::destructor");
152 CancelCompositorLock();
153 DCHECK(!compositor_lock_
);
155 FOR_EACH_OBSERVER(CompositorObserver
, observer_list_
,
156 OnCompositingShuttingDown(this));
159 root_layer_
->SetCompositor(NULL
);
161 // Stop all outstanding draws before telling the ContextFactory to tear
162 // down any contexts that the |host_| may rely upon.
165 context_factory_
->RemoveCompositor(this);
168 void Compositor::SetOutputSurface(
169 scoped_ptr
<cc::OutputSurface
> output_surface
) {
170 host_
->SetOutputSurface(output_surface
.Pass());
173 void Compositor::ScheduleDraw() {
174 host_
->SetNeedsCommit();
177 void Compositor::SetRootLayer(Layer
* root_layer
) {
178 if (root_layer_
== root_layer
)
181 root_layer_
->SetCompositor(NULL
);
182 root_layer_
= root_layer
;
183 if (root_layer_
&& !root_layer_
->GetCompositor())
184 root_layer_
->SetCompositor(this);
185 root_web_layer_
->RemoveAllChildren();
187 root_web_layer_
->AddChild(root_layer_
->cc_layer());
190 void Compositor::SetHostHasTransparentBackground(
191 bool host_has_transparent_background
) {
192 host_
->set_has_transparent_background(host_has_transparent_background
);
195 void Compositor::ScheduleFullRedraw() {
196 // TODO(enne): Some callers (mac) call this function expecting that it
197 // will also commit. This should probably just redraw the screen
198 // from damage and not commit. ScheduleDraw/ScheduleRedraw need
200 host_
->SetNeedsRedraw();
201 host_
->SetNeedsCommit();
204 void Compositor::ScheduleRedrawRect(const gfx::Rect
& damage_rect
) {
205 // TODO(enne): Make this not commit. See ScheduleFullRedraw.
206 host_
->SetNeedsRedrawRect(damage_rect
);
207 host_
->SetNeedsCommit();
210 void Compositor::FinishAllRendering() {
211 host_
->FinishAllRendering();
214 void Compositor::DisableSwapUntilResize() {
215 host_
->FinishAllRendering();
216 context_factory_
->ResizeDisplay(this, gfx::Size());
219 void Compositor::SetLatencyInfo(const ui::LatencyInfo
& latency_info
) {
220 scoped_ptr
<cc::SwapPromise
> swap_promise(
221 new cc::LatencyInfoSwapPromise(latency_info
));
222 host_
->QueueSwapPromise(swap_promise
.Pass());
225 void Compositor::SetScaleAndSize(float scale
, const gfx::Size
& size_in_pixel
) {
227 if (!size_in_pixel
.IsEmpty()) {
228 size_
= size_in_pixel
;
229 host_
->SetViewportSize(size_in_pixel
);
230 root_web_layer_
->SetBounds(size_in_pixel
);
231 context_factory_
->ResizeDisplay(this, size_in_pixel
);
233 if (device_scale_factor_
!= scale
) {
234 device_scale_factor_
= scale
;
235 host_
->SetDeviceScaleFactor(scale
);
237 root_layer_
->OnDeviceScaleFactorChanged(scale
);
241 void Compositor::SetBackgroundColor(SkColor color
) {
242 host_
->set_background_color(color
);
246 void Compositor::SetVisible(bool visible
) {
247 host_
->SetVisible(visible
);
250 bool Compositor::IsVisible() {
251 return host_
->visible();
254 scoped_refptr
<CompositorVSyncManager
> Compositor::vsync_manager() const {
255 return vsync_manager_
;
258 void Compositor::AddObserver(CompositorObserver
* observer
) {
259 observer_list_
.AddObserver(observer
);
262 void Compositor::RemoveObserver(CompositorObserver
* observer
) {
263 observer_list_
.RemoveObserver(observer
);
266 bool Compositor::HasObserver(const CompositorObserver
* observer
) const {
267 return observer_list_
.HasObserver(observer
);
270 void Compositor::AddAnimationObserver(CompositorAnimationObserver
* observer
) {
271 animation_observer_list_
.AddObserver(observer
);
272 host_
->SetNeedsAnimate();
275 void Compositor::RemoveAnimationObserver(
276 CompositorAnimationObserver
* observer
) {
277 animation_observer_list_
.RemoveObserver(observer
);
280 bool Compositor::HasAnimationObserver(
281 const CompositorAnimationObserver
* observer
) const {
282 return animation_observer_list_
.HasObserver(observer
);
285 void Compositor::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
286 FOR_EACH_OBSERVER(CompositorAnimationObserver
,
287 animation_observer_list_
,
288 OnAnimationStep(args
.frame_time
));
289 if (animation_observer_list_
.might_have_observers())
290 host_
->SetNeedsAnimate();
293 void Compositor::BeginMainFrameNotExpectedSoon() {
296 void Compositor::Layout() {
297 // We're sending damage that will be addressed during this composite
298 // cycle, so we don't need to schedule another composite to address it.
299 disable_schedule_composite_
= true;
301 root_layer_
->SendDamagedRects();
302 disable_schedule_composite_
= false;
305 void Compositor::RequestNewOutputSurface() {
306 context_factory_
->CreateOutputSurface(weak_ptr_factory_
.GetWeakPtr());
309 void Compositor::DidInitializeOutputSurface() {
312 void Compositor::DidFailToInitializeOutputSurface() {
313 // The OutputSurface should already be bound/initialized before being given to
318 void Compositor::DidCommit() {
320 FOR_EACH_OBSERVER(CompositorObserver
,
322 OnCompositingDidCommit(this));
325 void Compositor::DidCommitAndDrawFrame() {
328 void Compositor::DidCompleteSwapBuffers() {
329 FOR_EACH_OBSERVER(CompositorObserver
, observer_list_
,
330 OnCompositingEnded(this));
333 void Compositor::DidPostSwapBuffers() {
334 base::TimeTicks start_time
= gfx::FrameTime::Now();
335 FOR_EACH_OBSERVER(CompositorObserver
, observer_list_
,
336 OnCompositingStarted(this, start_time
));
339 void Compositor::DidAbortSwapBuffers() {
340 FOR_EACH_OBSERVER(CompositorObserver
,
342 OnCompositingAborted(this));
345 const cc::LayerTreeDebugState
& Compositor::GetLayerTreeDebugState() const {
346 return host_
->debug_state();
349 void Compositor::SetLayerTreeDebugState(
350 const cc::LayerTreeDebugState
& debug_state
) {
351 host_
->SetDebugState(debug_state
);
354 const cc::RendererSettings
& Compositor::GetRendererSettings() const {
355 return host_
->settings().renderer_settings
;
358 scoped_refptr
<CompositorLock
> Compositor::GetCompositorLock() {
359 if (!compositor_lock_
) {
360 compositor_lock_
= new CompositorLock(this);
361 host_
->SetDeferCommits(true);
362 FOR_EACH_OBSERVER(CompositorObserver
,
364 OnCompositingLockStateChanged(this));
366 return compositor_lock_
;
369 void Compositor::UnlockCompositor() {
370 DCHECK(compositor_lock_
);
371 compositor_lock_
= NULL
;
372 host_
->SetDeferCommits(false);
373 FOR_EACH_OBSERVER(CompositorObserver
,
375 OnCompositingLockStateChanged(this));
378 void Compositor::CancelCompositorLock() {
379 if (compositor_lock_
)
380 compositor_lock_
->CancelLock();