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/debug/trace_event.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/strings/string_util.h"
16 #include "base/sys_info.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/context_provider.h"
22 #include "cc/trees/layer_tree_host.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
24 #include "ui/compositor/compositor_observer.h"
25 #include "ui/compositor/compositor_switches.h"
26 #include "ui/compositor/compositor_vsync_manager.h"
27 #include "ui/compositor/dip_util.h"
28 #include "ui/compositor/layer.h"
29 #include "ui/compositor/layer_animator_collection.h"
30 #include "ui/gfx/frame_time.h"
31 #include "ui/gl/gl_context.h"
32 #include "ui/gl/gl_switches.h"
36 const double kDefaultRefreshRate
= 60.0;
37 const double kTestRefreshRate
= 200.0;
39 const int kCompositorLockTimeoutMs
= 67;
45 CompositorLock::CompositorLock(Compositor
* compositor
)
46 : compositor_(compositor
) {
47 base::MessageLoop::current()->PostDelayedTask(
49 base::Bind(&CompositorLock::CancelLock
, AsWeakPtr()),
50 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs
));
53 CompositorLock::~CompositorLock() {
57 void CompositorLock::CancelLock() {
60 compositor_
->UnlockCompositor();
72 Compositor::Compositor(gfx::AcceleratedWidget widget
,
73 ui::ContextFactory
* context_factory
)
74 : context_factory_(context_factory
),
77 compositor_thread_loop_(context_factory
->GetCompositorMessageLoop()),
78 vsync_manager_(new CompositorVSyncManager()),
79 device_scale_factor_(0.0f
),
80 last_started_frame_(0),
82 disable_schedule_composite_(false),
83 compositor_lock_(NULL
),
84 defer_draw_scheduling_(false),
85 waiting_on_compositing_end_(false),
86 draw_on_compositing_end_(false),
87 swap_state_(SWAP_NONE
),
88 layer_animator_collection_(this),
89 schedule_draw_factory_(this) {
90 root_web_layer_
= cc::Layer::Create();
92 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
94 cc::LayerTreeSettings settings
;
95 settings
.refresh_rate
=
96 context_factory_
->DoesCreateTestContexts()
98 : kDefaultRefreshRate
;
99 settings
.main_frame_before_draw_enabled
= false;
100 settings
.main_frame_before_activation_enabled
= false;
101 settings
.throttle_frame_production
=
102 !command_line
->HasSwitch(switches::kDisableGpuVsync
);
103 #if !defined(OS_MACOSX)
104 settings
.partial_swap_enabled
=
105 !command_line
->HasSwitch(cc::switches::kUIDisablePartialSwap
);
107 #if defined(OS_CHROMEOS)
108 settings
.per_tile_painting_enabled
= 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
);
128 settings
.initial_debug_state
.show_occluding_rects
=
129 command_line
->HasSwitch(cc::switches::kUIShowOccludingRects
);
130 settings
.initial_debug_state
.show_non_occluding_rects
=
131 command_line
->HasSwitch(cc::switches::kUIShowNonOccludingRects
);
133 settings
.initial_debug_state
.SetRecordRenderingStats(
134 command_line
->HasSwitch(cc::switches::kEnableGpuBenchmarking
));
136 settings
.impl_side_painting
= IsUIImplSidePaintingEnabled();
137 settings
.use_zero_copy
= IsUIZeroCopyEnabled();
139 base::TimeTicks before_create
= base::TimeTicks::Now();
140 if (compositor_thread_loop_
) {
141 host_
= cc::LayerTreeHost::CreateThreaded(
143 context_factory_
->GetSharedBitmapManager(),
145 compositor_thread_loop_
);
147 host_
= cc::LayerTreeHost::CreateSingleThreaded(
148 this, this, context_factory_
->GetSharedBitmapManager(), settings
);
150 UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
151 base::TimeTicks::Now() - before_create
);
152 host_
->SetRootLayer(root_web_layer_
);
153 host_
->SetLayerTreeHostClientReady();
156 Compositor::~Compositor() {
157 TRACE_EVENT0("shutdown", "Compositor::destructor");
159 CancelCompositorLock();
160 DCHECK(!compositor_lock_
);
163 root_layer_
->SetCompositor(NULL
);
165 // Stop all outstanding draws before telling the ContextFactory to tear
166 // down any contexts that the |host_| may rely upon.
169 context_factory_
->RemoveCompositor(this);
172 void Compositor::ScheduleDraw() {
173 if (compositor_thread_loop_
) {
174 host_
->SetNeedsCommit();
175 } else if (!defer_draw_scheduling_
) {
176 defer_draw_scheduling_
= true;
177 base::MessageLoop::current()->PostTask(
179 base::Bind(&Compositor::Draw
, schedule_draw_factory_
.GetWeakPtr()));
183 void Compositor::SetRootLayer(Layer
* root_layer
) {
184 if (root_layer_
== root_layer
)
187 root_layer_
->SetCompositor(NULL
);
188 root_layer_
= root_layer
;
189 if (root_layer_
&& !root_layer_
->GetCompositor())
190 root_layer_
->SetCompositor(this);
191 root_web_layer_
->RemoveAllChildren();
193 root_web_layer_
->AddChild(root_layer_
->cc_layer());
196 void Compositor::SetHostHasTransparentBackground(
197 bool host_has_transparent_background
) {
198 host_
->set_has_transparent_background(host_has_transparent_background
);
201 void Compositor::Draw() {
202 DCHECK(!compositor_thread_loop_
);
204 defer_draw_scheduling_
= false;
205 if (waiting_on_compositing_end_
) {
206 draw_on_compositing_end_
= true;
212 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_
+ 1);
214 DCHECK_NE(swap_state_
, SWAP_POSTED
);
215 swap_state_
= SWAP_NONE
;
217 waiting_on_compositing_end_
= true;
218 last_started_frame_
++;
220 // TODO(nduca): Temporary while compositor calls
221 // compositeImmediately() directly.
222 base::TimeTicks now
= gfx::FrameTime::Now();
225 host_
->Composite(now
);
227 if (swap_state_
== SWAP_NONE
)
231 void Compositor::ScheduleFullRedraw() {
232 host_
->SetNeedsRedraw();
235 void Compositor::ScheduleRedrawRect(const gfx::Rect
& damage_rect
) {
236 host_
->SetNeedsRedrawRect(damage_rect
);
239 void Compositor::FinishAllRendering() {
240 host_
->FinishAllRendering();
243 void Compositor::SetLatencyInfo(const ui::LatencyInfo
& latency_info
) {
244 scoped_ptr
<cc::SwapPromise
> swap_promise(
245 new cc::LatencyInfoSwapPromise(latency_info
));
246 host_
->QueueSwapPromise(swap_promise
.Pass());
249 void Compositor::SetScaleAndSize(float scale
, const gfx::Size
& size_in_pixel
) {
251 if (!size_in_pixel
.IsEmpty()) {
252 size_
= size_in_pixel
;
253 host_
->SetViewportSize(size_in_pixel
);
254 root_web_layer_
->SetBounds(size_in_pixel
);
256 if (device_scale_factor_
!= scale
) {
257 device_scale_factor_
= scale
;
258 host_
->SetDeviceScaleFactor(scale
);
260 root_layer_
->OnDeviceScaleFactorChanged(scale
);
264 void Compositor::SetBackgroundColor(SkColor color
) {
265 host_
->set_background_color(color
);
269 scoped_refptr
<CompositorVSyncManager
> Compositor::vsync_manager() const {
270 return vsync_manager_
;
273 void Compositor::AddObserver(CompositorObserver
* observer
) {
274 observer_list_
.AddObserver(observer
);
277 void Compositor::RemoveObserver(CompositorObserver
* observer
) {
278 observer_list_
.RemoveObserver(observer
);
281 bool Compositor::HasObserver(CompositorObserver
* observer
) {
282 return observer_list_
.HasObserver(observer
);
285 void Compositor::Animate(base::TimeTicks frame_begin_time
) {
286 layer_animator_collection_
.Progress(frame_begin_time
);
287 if (layer_animator_collection_
.HasActiveAnimators())
288 host_
->SetNeedsAnimate();
291 void Compositor::Layout() {
292 // We're sending damage that will be addressed during this composite
293 // cycle, so we don't need to schedule another composite to address it.
294 disable_schedule_composite_
= true;
296 root_layer_
->SendDamagedRects();
297 disable_schedule_composite_
= false;
300 scoped_ptr
<cc::OutputSurface
> Compositor::CreateOutputSurface(bool fallback
) {
301 return context_factory_
->CreateOutputSurface(this, fallback
);
304 void Compositor::DidCommit() {
306 FOR_EACH_OBSERVER(CompositorObserver
,
308 OnCompositingDidCommit(this));
311 void Compositor::DidCommitAndDrawFrame() {
312 base::TimeTicks start_time
= gfx::FrameTime::Now();
313 FOR_EACH_OBSERVER(CompositorObserver
,
315 OnCompositingStarted(this, start_time
));
318 void Compositor::DidCompleteSwapBuffers() {
319 if (compositor_thread_loop_
) {
322 DCHECK_EQ(swap_state_
, SWAP_POSTED
);
324 swap_state_
= SWAP_COMPLETED
;
328 void Compositor::ScheduleComposite() {
329 if (!disable_schedule_composite_
)
333 void Compositor::ScheduleAnimation() {
337 void Compositor::DidPostSwapBuffers() {
338 DCHECK(!compositor_thread_loop_
);
339 DCHECK_EQ(swap_state_
, SWAP_NONE
);
340 swap_state_
= SWAP_POSTED
;
343 void Compositor::DidAbortSwapBuffers() {
344 if (!compositor_thread_loop_
) {
345 if (swap_state_
== SWAP_POSTED
) {
347 swap_state_
= SWAP_COMPLETED
;
351 FOR_EACH_OBSERVER(CompositorObserver
,
353 OnCompositingAborted(this));
356 void Compositor::ScheduleAnimationForLayerCollection() {
357 host_
->SetNeedsAnimate();
360 const cc::LayerTreeDebugState
& Compositor::GetLayerTreeDebugState() const {
361 return host_
->debug_state();
364 void Compositor::SetLayerTreeDebugState(
365 const cc::LayerTreeDebugState
& debug_state
) {
366 host_
->SetDebugState(debug_state
);
369 scoped_refptr
<CompositorLock
> Compositor::GetCompositorLock() {
370 if (!compositor_lock_
) {
371 compositor_lock_
= new CompositorLock(this);
372 if (compositor_thread_loop_
)
373 host_
->SetDeferCommits(true);
374 FOR_EACH_OBSERVER(CompositorObserver
,
376 OnCompositingLockStateChanged(this));
378 return compositor_lock_
;
381 void Compositor::UnlockCompositor() {
382 DCHECK(compositor_lock_
);
383 compositor_lock_
= NULL
;
384 if (compositor_thread_loop_
)
385 host_
->SetDeferCommits(false);
386 FOR_EACH_OBSERVER(CompositorObserver
,
388 OnCompositingLockStateChanged(this));
391 void Compositor::CancelCompositorLock() {
392 if (compositor_lock_
)
393 compositor_lock_
->CancelLock();
396 void Compositor::NotifyEnd() {
398 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_
);
399 waiting_on_compositing_end_
= false;
400 if (draw_on_compositing_end_
) {
401 draw_on_compositing_end_
= false;
403 // Call ScheduleDraw() instead of Draw() in order to allow other
404 // CompositorObservers to be notified before starting another
408 FOR_EACH_OBSERVER(CompositorObserver
,
410 OnCompositingEnded(this));