1 // Copyright 2011 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 "cc/scheduler/frame_rate_controller.h"
7 #include "base/debug/trace_event.h"
8 #include "base/logging.h"
9 #include "cc/base/thread.h"
10 #include "cc/scheduler/delay_based_time_source.h"
11 #include "cc/scheduler/time_source.h"
15 class FrameRateControllerTimeSourceAdapter
: public TimeSourceClient
{
17 static scoped_ptr
<FrameRateControllerTimeSourceAdapter
> Create(
18 FrameRateController
* frame_rate_controller
) {
19 return make_scoped_ptr(
20 new FrameRateControllerTimeSourceAdapter(frame_rate_controller
));
22 virtual ~FrameRateControllerTimeSourceAdapter() {}
24 virtual void OnTimerTick() OVERRIDE
{ frame_rate_controller_
->OnTimerTick(); }
27 explicit FrameRateControllerTimeSourceAdapter(
28 FrameRateController
* frame_rate_controller
)
29 : frame_rate_controller_(frame_rate_controller
) {}
31 FrameRateController
* frame_rate_controller_
;
34 FrameRateController::FrameRateController(scoped_refptr
<TimeSource
> timer
)
36 num_frames_pending_(0),
37 max_frames_pending_(0),
40 swap_buffers_complete_supported_(true),
41 is_time_source_throttling_(true),
44 time_source_client_adapter_
=
45 FrameRateControllerTimeSourceAdapter::Create(this);
46 time_source_
->SetClient(time_source_client_adapter_
.get());
49 FrameRateController::FrameRateController(Thread
* thread
)
51 num_frames_pending_(0),
52 max_frames_pending_(0),
54 swap_buffers_complete_supported_(true),
55 is_time_source_throttling_(false),
59 FrameRateController::~FrameRateController() {
60 if (is_time_source_throttling_
)
61 time_source_
->SetActive(false);
64 void FrameRateController::SetActive(bool active
) {
65 if (active_
== active
)
67 TRACE_EVENT1("cc", "FrameRateController::SetActive", "active", active
);
70 if (is_time_source_throttling_
) {
71 time_source_
->SetActive(active
);
76 weak_factory_
.InvalidateWeakPtrs();
80 void FrameRateController::SetMaxFramesPending(int max_frames_pending
) {
81 DCHECK_GE(max_frames_pending
, 0);
82 max_frames_pending_
= max_frames_pending
;
85 void FrameRateController::SetTimebaseAndInterval(base::TimeTicks timebase
,
86 base::TimeDelta interval
) {
87 if (is_time_source_throttling_
)
88 time_source_
->SetTimebaseAndInterval(timebase
, interval
);
91 void FrameRateController::SetSwapBuffersCompleteSupported(bool supported
) {
92 swap_buffers_complete_supported_
= supported
;
95 void FrameRateController::OnTimerTick() {
98 // Check if we have too many frames in flight.
100 max_frames_pending_
&& num_frames_pending_
>= max_frames_pending_
;
101 TRACE_COUNTER_ID1("cc", "ThrottledCompositor", thread_
, throttled
);
104 client_
->BeginFrame(throttled
);
106 if (swap_buffers_complete_supported_
&& !is_time_source_throttling_
&&
111 void FrameRateController::PostManualTick() {
113 thread_
->PostTask(base::Bind(&FrameRateController::ManualTick
,
114 weak_factory_
.GetWeakPtr()));
118 void FrameRateController::ManualTick() { OnTimerTick(); }
120 void FrameRateController::DidSwapBuffers() {
121 if (swap_buffers_complete_supported_
)
122 num_frames_pending_
++;
123 else if (!is_time_source_throttling_
)
127 void FrameRateController::DidSwapBuffersComplete() {
128 DCHECK(swap_buffers_complete_supported_
);
130 DCHECK_GT(num_frames_pending_
, 0);
131 num_frames_pending_
--;
132 if (!is_time_source_throttling_
)
136 void FrameRateController::DidAbortAllPendingFrames() {
137 num_frames_pending_
= 0;
140 base::TimeTicks
FrameRateController::NextTickTime() {
141 if (is_time_source_throttling_
)
142 return time_source_
->NextTickTime();
144 return base::TimeTicks();
147 base::TimeTicks
FrameRateController::LastTickTime() {
148 if (is_time_source_throttling_
)
149 return time_source_
->LastTickTime();
151 return base::TimeTicks::Now();