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/begin_frame_args.h"
22 #include "cc/output/context_provider.h"
23 #include "cc/trees/layer_tree_host.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
25 #include "ui/compositor/compositor_observer.h"
26 #include "ui/compositor/compositor_switches.h"
27 #include "ui/compositor/compositor_vsync_manager.h"
28 #include "ui/compositor/dip_util.h"
29 #include "ui/compositor/layer.h"
30 #include "ui/compositor/layer_animator_collection.h"
31 #include "ui/gfx/frame_time.h"
32 #include "ui/gl/gl_context.h"
33 #include "ui/gl/gl_switches.h"
37 const double kDefaultRefreshRate
= 60.0;
38 const double kTestRefreshRate
= 200.0;
40 const int kCompositorLockTimeoutMs
= 67;
46 CompositorLock::CompositorLock(Compositor
* compositor
)
47 : compositor_(compositor
) {
48 compositor_
->task_runner_
->PostDelayedTask(
50 base::Bind(&CompositorLock::CancelLock
, AsWeakPtr()),
51 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs
));
54 CompositorLock::~CompositorLock() {
58 void CompositorLock::CancelLock() {
61 compositor_
->UnlockCompositor();
67 namespace {} // namespace
71 Compositor::Compositor(gfx::AcceleratedWidget widget
,
72 ui::ContextFactory
* context_factory
,
73 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
74 : context_factory_(context_factory
),
77 compositor_thread_loop_(context_factory
->GetCompositorMessageLoop()),
78 task_runner_(task_runner
),
79 vsync_manager_(new CompositorVSyncManager()),
80 device_scale_factor_(0.0f
),
81 last_started_frame_(0),
83 disable_schedule_composite_(false),
84 compositor_lock_(NULL
),
85 defer_draw_scheduling_(false),
86 waiting_on_compositing_end_(false),
87 draw_on_compositing_end_(false),
88 swap_state_(SWAP_NONE
),
89 layer_animator_collection_(this),
90 schedule_draw_factory_(this) {
91 root_web_layer_
= cc::Layer::Create();
93 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
95 cc::LayerTreeSettings settings
;
96 settings
.refresh_rate
=
97 context_factory_
->DoesCreateTestContexts()
99 : kDefaultRefreshRate
;
100 settings
.main_frame_before_draw_enabled
= false;
101 settings
.main_frame_before_activation_enabled
= false;
102 settings
.throttle_frame_production
=
103 !command_line
->HasSwitch(switches::kDisableGpuVsync
);
104 #if !defined(OS_MACOSX)
105 settings
.partial_swap_enabled
=
106 !command_line
->HasSwitch(cc::switches::kUIDisablePartialSwap
);
108 #if defined(OS_CHROMEOS)
109 settings
.per_tile_painting_enabled
= true;
112 // These flags should be mirrored by renderer versions in content/renderer/.
113 settings
.initial_debug_state
.show_debug_borders
=
114 command_line
->HasSwitch(cc::switches::kUIShowCompositedLayerBorders
);
115 settings
.initial_debug_state
.show_fps_counter
=
116 command_line
->HasSwitch(cc::switches::kUIShowFPSCounter
);
117 settings
.initial_debug_state
.show_layer_animation_bounds_rects
=
118 command_line
->HasSwitch(cc::switches::kUIShowLayerAnimationBounds
);
119 settings
.initial_debug_state
.show_paint_rects
=
120 command_line
->HasSwitch(switches::kUIShowPaintRects
);
121 settings
.initial_debug_state
.show_property_changed_rects
=
122 command_line
->HasSwitch(cc::switches::kUIShowPropertyChangedRects
);
123 settings
.initial_debug_state
.show_surface_damage_rects
=
124 command_line
->HasSwitch(cc::switches::kUIShowSurfaceDamageRects
);
125 settings
.initial_debug_state
.show_screen_space_rects
=
126 command_line
->HasSwitch(cc::switches::kUIShowScreenSpaceRects
);
127 settings
.initial_debug_state
.show_replica_screen_space_rects
=
128 command_line
->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects
);
129 settings
.initial_debug_state
.show_occluding_rects
=
130 command_line
->HasSwitch(cc::switches::kUIShowOccludingRects
);
131 settings
.initial_debug_state
.show_non_occluding_rects
=
132 command_line
->HasSwitch(cc::switches::kUIShowNonOccludingRects
);
134 settings
.initial_debug_state
.SetRecordRenderingStats(
135 command_line
->HasSwitch(cc::switches::kEnableGpuBenchmarking
));
137 settings
.impl_side_painting
= IsUIImplSidePaintingEnabled();
138 settings
.use_zero_copy
= IsUIZeroCopyEnabled();
140 base::TimeTicks before_create
= base::TimeTicks::Now();
141 if (compositor_thread_loop_
) {
142 host_
= cc::LayerTreeHost::CreateThreaded(
144 context_factory_
->GetSharedBitmapManager(),
147 compositor_thread_loop_
);
149 host_
= cc::LayerTreeHost::CreateSingleThreaded(
152 context_factory_
->GetSharedBitmapManager(),
156 UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
157 base::TimeTicks::Now() - before_create
);
158 host_
->SetRootLayer(root_web_layer_
);
159 host_
->SetLayerTreeHostClientReady();
162 Compositor::~Compositor() {
163 TRACE_EVENT0("shutdown", "Compositor::destructor");
165 CancelCompositorLock();
166 DCHECK(!compositor_lock_
);
169 root_layer_
->SetCompositor(NULL
);
171 // Stop all outstanding draws before telling the ContextFactory to tear
172 // down any contexts that the |host_| may rely upon.
175 context_factory_
->RemoveCompositor(this);
178 void Compositor::ScheduleDraw() {
179 if (compositor_thread_loop_
) {
180 host_
->SetNeedsCommit();
181 } else if (!defer_draw_scheduling_
) {
182 defer_draw_scheduling_
= true;
183 task_runner_
->PostTask(
185 base::Bind(&Compositor::Draw
, schedule_draw_factory_
.GetWeakPtr()));
189 void Compositor::SetRootLayer(Layer
* root_layer
) {
190 if (root_layer_
== root_layer
)
193 root_layer_
->SetCompositor(NULL
);
194 root_layer_
= root_layer
;
195 if (root_layer_
&& !root_layer_
->GetCompositor())
196 root_layer_
->SetCompositor(this);
197 root_web_layer_
->RemoveAllChildren();
199 root_web_layer_
->AddChild(root_layer_
->cc_layer());
202 void Compositor::SetHostHasTransparentBackground(
203 bool host_has_transparent_background
) {
204 host_
->set_has_transparent_background(host_has_transparent_background
);
207 void Compositor::Draw() {
208 DCHECK(!compositor_thread_loop_
);
210 defer_draw_scheduling_
= false;
211 if (waiting_on_compositing_end_
) {
212 draw_on_compositing_end_
= true;
218 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_
+ 1);
220 DCHECK_NE(swap_state_
, SWAP_POSTED
);
221 swap_state_
= SWAP_NONE
;
223 waiting_on_compositing_end_
= true;
224 last_started_frame_
++;
226 // TODO(nduca): Temporary while compositor calls
227 // compositeImmediately() directly.
228 cc::BeginFrameArgs args
=
229 cc::BeginFrameArgs::Create(gfx::FrameTime::Now(),
231 cc::BeginFrameArgs::DefaultInterval());
232 BeginMainFrame(args
);
234 host_
->Composite(args
.frame_time
);
236 if (swap_state_
== SWAP_NONE
)
240 void Compositor::ScheduleFullRedraw() {
241 host_
->SetNeedsRedraw();
244 void Compositor::ScheduleRedrawRect(const gfx::Rect
& damage_rect
) {
245 host_
->SetNeedsRedrawRect(damage_rect
);
248 void Compositor::FinishAllRendering() {
249 host_
->FinishAllRendering();
252 void Compositor::SetLatencyInfo(const ui::LatencyInfo
& latency_info
) {
253 scoped_ptr
<cc::SwapPromise
> swap_promise(
254 new cc::LatencyInfoSwapPromise(latency_info
));
255 host_
->QueueSwapPromise(swap_promise
.Pass());
258 void Compositor::SetScaleAndSize(float scale
, const gfx::Size
& size_in_pixel
) {
260 if (!size_in_pixel
.IsEmpty()) {
261 size_
= size_in_pixel
;
262 host_
->SetViewportSize(size_in_pixel
);
263 root_web_layer_
->SetBounds(size_in_pixel
);
265 if (device_scale_factor_
!= scale
) {
266 device_scale_factor_
= scale
;
267 host_
->SetDeviceScaleFactor(scale
);
269 root_layer_
->OnDeviceScaleFactorChanged(scale
);
273 void Compositor::SetBackgroundColor(SkColor color
) {
274 host_
->set_background_color(color
);
278 scoped_refptr
<CompositorVSyncManager
> Compositor::vsync_manager() const {
279 return vsync_manager_
;
282 void Compositor::AddObserver(CompositorObserver
* observer
) {
283 #if defined(OS_MACOSX)
284 // Debugging instrumentation for crbug.com/401630.
285 // TODO(ccameron): remove this.
287 if (!observer_list_
.HasObserver(observer
))
288 observer
->observing_count_
+= 1;
291 observer_list_
.AddObserver(observer
);
294 void Compositor::RemoveObserver(CompositorObserver
* observer
) {
295 #if defined(OS_MACOSX)
296 // Debugging instrumentation for crbug.com/401630.
297 // TODO(ccameron): remove this.
298 if (observer_list_
.HasObserver(observer
))
299 observer
->observing_count_
-= 1;
302 observer_list_
.RemoveObserver(observer
);
305 bool Compositor::HasObserver(CompositorObserver
* observer
) {
306 return observer_list_
.HasObserver(observer
);
309 void Compositor::AddAnimationObserver(CompositorAnimationObserver
* observer
) {
310 animation_observer_list_
.AddObserver(observer
);
311 host_
->SetNeedsAnimate();
314 void Compositor::RemoveAnimationObserver(
315 CompositorAnimationObserver
* observer
) {
316 animation_observer_list_
.RemoveObserver(observer
);
319 bool Compositor::HasAnimationObserver(CompositorAnimationObserver
* observer
) {
320 return animation_observer_list_
.HasObserver(observer
);
323 void Compositor::BeginMainFrame(const cc::BeginFrameArgs
& args
) {
324 FOR_EACH_OBSERVER(CompositorAnimationObserver
,
325 animation_observer_list_
,
326 OnAnimationStep(args
.frame_time
));
327 if (animation_observer_list_
.might_have_observers())
328 host_
->SetNeedsAnimate();
331 void Compositor::Layout() {
332 // We're sending damage that will be addressed during this composite
333 // cycle, so we don't need to schedule another composite to address it.
334 disable_schedule_composite_
= true;
336 root_layer_
->SendDamagedRects();
337 disable_schedule_composite_
= false;
340 scoped_ptr
<cc::OutputSurface
> Compositor::CreateOutputSurface(bool fallback
) {
341 return context_factory_
->CreateOutputSurface(this, fallback
);
344 void Compositor::DidCommit() {
346 FOR_EACH_OBSERVER(CompositorObserver
,
348 OnCompositingDidCommit(this));
351 void Compositor::DidCommitAndDrawFrame() {
352 base::TimeTicks start_time
= gfx::FrameTime::Now();
353 FOR_EACH_OBSERVER(CompositorObserver
,
355 OnCompositingStarted(this, start_time
));
358 void Compositor::DidCompleteSwapBuffers() {
359 if (compositor_thread_loop_
) {
362 DCHECK_EQ(swap_state_
, SWAP_POSTED
);
364 swap_state_
= SWAP_COMPLETED
;
368 void Compositor::ScheduleComposite() {
369 if (!disable_schedule_composite_
)
373 void Compositor::ScheduleAnimation() {
377 void Compositor::DidPostSwapBuffers() {
378 DCHECK(!compositor_thread_loop_
);
379 DCHECK_EQ(swap_state_
, SWAP_NONE
);
380 swap_state_
= SWAP_POSTED
;
383 void Compositor::DidAbortSwapBuffers() {
384 if (!compositor_thread_loop_
) {
385 if (swap_state_
== SWAP_POSTED
) {
387 swap_state_
= SWAP_COMPLETED
;
391 FOR_EACH_OBSERVER(CompositorObserver
,
393 OnCompositingAborted(this));
396 const cc::LayerTreeDebugState
& Compositor::GetLayerTreeDebugState() const {
397 return host_
->debug_state();
400 void Compositor::SetLayerTreeDebugState(
401 const cc::LayerTreeDebugState
& debug_state
) {
402 host_
->SetDebugState(debug_state
);
405 scoped_refptr
<CompositorLock
> Compositor::GetCompositorLock() {
406 if (!compositor_lock_
) {
407 compositor_lock_
= new CompositorLock(this);
408 if (compositor_thread_loop_
)
409 host_
->SetDeferCommits(true);
410 FOR_EACH_OBSERVER(CompositorObserver
,
412 OnCompositingLockStateChanged(this));
414 return compositor_lock_
;
417 void Compositor::UnlockCompositor() {
418 DCHECK(compositor_lock_
);
419 compositor_lock_
= NULL
;
420 if (compositor_thread_loop_
)
421 host_
->SetDeferCommits(false);
422 FOR_EACH_OBSERVER(CompositorObserver
,
424 OnCompositingLockStateChanged(this));
427 void Compositor::CancelCompositorLock() {
428 if (compositor_lock_
)
429 compositor_lock_
->CancelLock();
432 void Compositor::NotifyEnd() {
434 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_
);
435 waiting_on_compositing_end_
= false;
436 if (draw_on_compositing_end_
) {
437 draw_on_compositing_end_
= false;
439 // Call ScheduleDraw() instead of Draw() in order to allow other
440 // CompositorObservers to be notified before starting another
445 CompositorObserver
, observer_list_
, OnCompositingEnded(this));