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 compositor_
->task_runner_
->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 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
75 : context_factory_(context_factory
),
78 compositor_thread_loop_(context_factory
->GetCompositorMessageLoop()),
79 task_runner_(task_runner
),
80 vsync_manager_(new CompositorVSyncManager()),
81 device_scale_factor_(0.0f
),
82 last_started_frame_(0),
84 disable_schedule_composite_(false),
85 compositor_lock_(NULL
),
86 defer_draw_scheduling_(false),
87 waiting_on_compositing_end_(false),
88 draw_on_compositing_end_(false),
89 swap_state_(SWAP_NONE
),
90 layer_animator_collection_(this),
91 schedule_draw_factory_(this) {
92 root_web_layer_
= cc::Layer::Create();
94 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
96 cc::LayerTreeSettings settings
;
97 settings
.refresh_rate
=
98 context_factory_
->DoesCreateTestContexts()
100 : kDefaultRefreshRate
;
101 settings
.main_frame_before_draw_enabled
= false;
102 settings
.main_frame_before_activation_enabled
= false;
103 settings
.throttle_frame_production
=
104 !command_line
->HasSwitch(switches::kDisableGpuVsync
);
105 #if !defined(OS_MACOSX)
106 settings
.partial_swap_enabled
=
107 !command_line
->HasSwitch(cc::switches::kUIDisablePartialSwap
);
109 #if defined(OS_CHROMEOS)
110 settings
.per_tile_painting_enabled
= true;
113 // These flags should be mirrored by renderer versions in content/renderer/.
114 settings
.initial_debug_state
.show_debug_borders
=
115 command_line
->HasSwitch(cc::switches::kUIShowCompositedLayerBorders
);
116 settings
.initial_debug_state
.show_fps_counter
=
117 command_line
->HasSwitch(cc::switches::kUIShowFPSCounter
);
118 settings
.initial_debug_state
.show_layer_animation_bounds_rects
=
119 command_line
->HasSwitch(cc::switches::kUIShowLayerAnimationBounds
);
120 settings
.initial_debug_state
.show_paint_rects
=
121 command_line
->HasSwitch(switches::kUIShowPaintRects
);
122 settings
.initial_debug_state
.show_property_changed_rects
=
123 command_line
->HasSwitch(cc::switches::kUIShowPropertyChangedRects
);
124 settings
.initial_debug_state
.show_surface_damage_rects
=
125 command_line
->HasSwitch(cc::switches::kUIShowSurfaceDamageRects
);
126 settings
.initial_debug_state
.show_screen_space_rects
=
127 command_line
->HasSwitch(cc::switches::kUIShowScreenSpaceRects
);
128 settings
.initial_debug_state
.show_replica_screen_space_rects
=
129 command_line
->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects
);
130 settings
.initial_debug_state
.show_occluding_rects
=
131 command_line
->HasSwitch(cc::switches::kUIShowOccludingRects
);
132 settings
.initial_debug_state
.show_non_occluding_rects
=
133 command_line
->HasSwitch(cc::switches::kUIShowNonOccludingRects
);
135 settings
.initial_debug_state
.SetRecordRenderingStats(
136 command_line
->HasSwitch(cc::switches::kEnableGpuBenchmarking
));
138 settings
.impl_side_painting
= IsUIImplSidePaintingEnabled();
139 settings
.use_zero_copy
= IsUIZeroCopyEnabled();
141 base::TimeTicks before_create
= base::TimeTicks::Now();
142 if (compositor_thread_loop_
) {
143 host_
= cc::LayerTreeHost::CreateThreaded(
145 context_factory_
->GetSharedBitmapManager(),
148 compositor_thread_loop_
);
150 host_
= cc::LayerTreeHost::CreateSingleThreaded(
153 context_factory_
->GetSharedBitmapManager(),
157 UMA_HISTOGRAM_TIMES("GPU.CreateBrowserCompositor",
158 base::TimeTicks::Now() - before_create
);
159 host_
->SetRootLayer(root_web_layer_
);
160 host_
->SetLayerTreeHostClientReady();
163 Compositor::~Compositor() {
164 TRACE_EVENT0("shutdown", "Compositor::destructor");
166 CancelCompositorLock();
167 DCHECK(!compositor_lock_
);
170 root_layer_
->SetCompositor(NULL
);
172 // Stop all outstanding draws before telling the ContextFactory to tear
173 // down any contexts that the |host_| may rely upon.
176 context_factory_
->RemoveCompositor(this);
179 void Compositor::ScheduleDraw() {
180 if (compositor_thread_loop_
) {
181 host_
->SetNeedsCommit();
182 } else if (!defer_draw_scheduling_
) {
183 defer_draw_scheduling_
= true;
184 task_runner_
->PostTask(
186 base::Bind(&Compositor::Draw
, schedule_draw_factory_
.GetWeakPtr()));
190 void Compositor::SetRootLayer(Layer
* root_layer
) {
191 if (root_layer_
== root_layer
)
194 root_layer_
->SetCompositor(NULL
);
195 root_layer_
= root_layer
;
196 if (root_layer_
&& !root_layer_
->GetCompositor())
197 root_layer_
->SetCompositor(this);
198 root_web_layer_
->RemoveAllChildren();
200 root_web_layer_
->AddChild(root_layer_
->cc_layer());
203 void Compositor::SetHostHasTransparentBackground(
204 bool host_has_transparent_background
) {
205 host_
->set_has_transparent_background(host_has_transparent_background
);
208 void Compositor::Draw() {
209 DCHECK(!compositor_thread_loop_
);
211 defer_draw_scheduling_
= false;
212 if (waiting_on_compositing_end_
) {
213 draw_on_compositing_end_
= true;
219 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_
+ 1);
221 DCHECK_NE(swap_state_
, SWAP_POSTED
);
222 swap_state_
= SWAP_NONE
;
224 waiting_on_compositing_end_
= true;
225 last_started_frame_
++;
227 // TODO(nduca): Temporary while compositor calls
228 // compositeImmediately() directly.
229 base::TimeTicks now
= gfx::FrameTime::Now();
232 host_
->Composite(now
);
234 if (swap_state_
== SWAP_NONE
)
238 void Compositor::ScheduleFullRedraw() {
239 host_
->SetNeedsRedraw();
242 void Compositor::ScheduleRedrawRect(const gfx::Rect
& damage_rect
) {
243 host_
->SetNeedsRedrawRect(damage_rect
);
246 void Compositor::FinishAllRendering() {
247 host_
->FinishAllRendering();
250 void Compositor::SetLatencyInfo(const ui::LatencyInfo
& latency_info
) {
251 scoped_ptr
<cc::SwapPromise
> swap_promise(
252 new cc::LatencyInfoSwapPromise(latency_info
));
253 host_
->QueueSwapPromise(swap_promise
.Pass());
256 void Compositor::SetScaleAndSize(float scale
, const gfx::Size
& size_in_pixel
) {
258 if (!size_in_pixel
.IsEmpty()) {
259 size_
= size_in_pixel
;
260 host_
->SetViewportSize(size_in_pixel
);
261 root_web_layer_
->SetBounds(size_in_pixel
);
263 if (device_scale_factor_
!= scale
) {
264 device_scale_factor_
= scale
;
265 host_
->SetDeviceScaleFactor(scale
);
267 root_layer_
->OnDeviceScaleFactorChanged(scale
);
271 void Compositor::SetBackgroundColor(SkColor color
) {
272 host_
->set_background_color(color
);
276 scoped_refptr
<CompositorVSyncManager
> Compositor::vsync_manager() const {
277 return vsync_manager_
;
280 void Compositor::AddObserver(CompositorObserver
* observer
) {
281 observer_list_
.AddObserver(observer
);
284 void Compositor::RemoveObserver(CompositorObserver
* observer
) {
285 observer_list_
.RemoveObserver(observer
);
288 bool Compositor::HasObserver(CompositorObserver
* observer
) {
289 return observer_list_
.HasObserver(observer
);
292 void Compositor::Animate(base::TimeTicks frame_begin_time
) {
293 layer_animator_collection_
.Progress(frame_begin_time
);
294 if (layer_animator_collection_
.HasActiveAnimators())
295 host_
->SetNeedsAnimate();
298 void Compositor::Layout() {
299 // We're sending damage that will be addressed during this composite
300 // cycle, so we don't need to schedule another composite to address it.
301 disable_schedule_composite_
= true;
303 root_layer_
->SendDamagedRects();
304 disable_schedule_composite_
= false;
307 scoped_ptr
<cc::OutputSurface
> Compositor::CreateOutputSurface(bool fallback
) {
308 return context_factory_
->CreateOutputSurface(this, fallback
);
311 void Compositor::DidCommit() {
313 FOR_EACH_OBSERVER(CompositorObserver
,
315 OnCompositingDidCommit(this));
318 void Compositor::DidCommitAndDrawFrame() {
319 base::TimeTicks start_time
= gfx::FrameTime::Now();
320 FOR_EACH_OBSERVER(CompositorObserver
,
322 OnCompositingStarted(this, start_time
));
325 void Compositor::DidCompleteSwapBuffers() {
326 if (compositor_thread_loop_
) {
329 DCHECK_EQ(swap_state_
, SWAP_POSTED
);
331 swap_state_
= SWAP_COMPLETED
;
335 void Compositor::ScheduleComposite() {
336 if (!disable_schedule_composite_
)
340 void Compositor::ScheduleAnimation() {
344 void Compositor::DidPostSwapBuffers() {
345 DCHECK(!compositor_thread_loop_
);
346 DCHECK_EQ(swap_state_
, SWAP_NONE
);
347 swap_state_
= SWAP_POSTED
;
350 void Compositor::DidAbortSwapBuffers() {
351 if (!compositor_thread_loop_
) {
352 if (swap_state_
== SWAP_POSTED
) {
354 swap_state_
= SWAP_COMPLETED
;
358 FOR_EACH_OBSERVER(CompositorObserver
,
360 OnCompositingAborted(this));
363 void Compositor::ScheduleAnimationForLayerCollection() {
364 host_
->SetNeedsAnimate();
367 const cc::LayerTreeDebugState
& Compositor::GetLayerTreeDebugState() const {
368 return host_
->debug_state();
371 void Compositor::SetLayerTreeDebugState(
372 const cc::LayerTreeDebugState
& debug_state
) {
373 host_
->SetDebugState(debug_state
);
376 scoped_refptr
<CompositorLock
> Compositor::GetCompositorLock() {
377 if (!compositor_lock_
) {
378 compositor_lock_
= new CompositorLock(this);
379 if (compositor_thread_loop_
)
380 host_
->SetDeferCommits(true);
381 FOR_EACH_OBSERVER(CompositorObserver
,
383 OnCompositingLockStateChanged(this));
385 return compositor_lock_
;
388 void Compositor::UnlockCompositor() {
389 DCHECK(compositor_lock_
);
390 compositor_lock_
= NULL
;
391 if (compositor_thread_loop_
)
392 host_
->SetDeferCommits(false);
393 FOR_EACH_OBSERVER(CompositorObserver
,
395 OnCompositingLockStateChanged(this));
398 void Compositor::CancelCompositorLock() {
399 if (compositor_lock_
)
400 compositor_lock_
->CancelLock();
403 void Compositor::NotifyEnd() {
405 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_
);
406 waiting_on_compositing_end_
= false;
407 if (draw_on_compositing_end_
) {
408 draw_on_compositing_end_
= false;
410 // Call ScheduleDraw() instead of Draw() in order to allow other
411 // CompositorObservers to be notified before starting another
415 FOR_EACH_OBSERVER(CompositorObserver
,
417 OnCompositingEnded(this));