Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / cc / scheduler / frame_rate_controller.cc
blob4fcea235a5504da09ba9baaf28bf0055426189e5
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"
13 namespace cc {
15 class FrameRateControllerTimeSourceAdapter : public TimeSourceClient {
16 public:
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(); }
26 private:
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)
35 : client_(NULL),
36 num_frames_pending_(0),
37 max_frames_pending_(0),
38 time_source_(timer),
39 active_(false),
40 swap_buffers_complete_supported_(true),
41 is_time_source_throttling_(true),
42 weak_factory_(this),
43 thread_(NULL) {
44 time_source_client_adapter_ =
45 FrameRateControllerTimeSourceAdapter::Create(this);
46 time_source_->SetClient(time_source_client_adapter_.get());
49 FrameRateController::FrameRateController(Thread* thread)
50 : client_(NULL),
51 num_frames_pending_(0),
52 max_frames_pending_(0),
53 active_(false),
54 swap_buffers_complete_supported_(true),
55 is_time_source_throttling_(false),
56 weak_factory_(this),
57 thread_(thread) {}
59 FrameRateController::~FrameRateController() {
60 if (is_time_source_throttling_)
61 time_source_->SetActive(false);
64 void FrameRateController::SetActive(bool active) {
65 if (active_ == active)
66 return;
67 TRACE_EVENT1("cc", "FrameRateController::SetActive", "active", active);
68 active_ = active;
70 if (is_time_source_throttling_) {
71 time_source_->SetActive(active);
72 } else {
73 if (active)
74 PostManualTick();
75 else
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() {
96 DCHECK(active_);
98 // Check if we have too many frames in flight.
99 bool throttled =
100 max_frames_pending_ && num_frames_pending_ >= max_frames_pending_;
101 TRACE_COUNTER_ID1("cc", "ThrottledCompositor", thread_, throttled);
103 if (client_)
104 client_->BeginFrame(throttled);
106 if (swap_buffers_complete_supported_ && !is_time_source_throttling_ &&
107 !throttled)
108 PostManualTick();
111 void FrameRateController::PostManualTick() {
112 if (active_) {
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_)
124 PostManualTick();
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_)
133 PostManualTick();
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();
154 } // namespace cc