OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/scheduler/frame_rate_controller.h" | 5 #include "cc/scheduler/frame_rate_controller.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "cc/base/thread.h" | 9 #include "cc/base/thread.h" |
10 #include "cc/scheduler/delay_based_time_source.h" | 10 #include "cc/scheduler/delay_based_time_source.h" |
11 #include "cc/scheduler/time_source.h" | 11 #include "cc/scheduler/time_source.h" |
12 | 12 |
13 namespace cc { | 13 namespace cc { |
14 | 14 |
15 class FrameRateControllerTimeSourceAdapter : public TimeSourceClient { | 15 class FrameRateControllerTimeSourceAdapter : public TimeSourceClient { |
16 public: | 16 public: |
17 static scoped_ptr<FrameRateControllerTimeSourceAdapter> Create( | 17 static scoped_ptr<FrameRateControllerTimeSourceAdapter> Create( |
18 FrameRateController* frame_rate_controller) { | 18 FrameRateController* frame_rate_controller) { |
19 return make_scoped_ptr( | 19 return make_scoped_ptr( |
20 new FrameRateControllerTimeSourceAdapter(frame_rate_controller)); | 20 new FrameRateControllerTimeSourceAdapter(frame_rate_controller)); |
21 } | 21 } |
22 virtual ~FrameRateControllerTimeSourceAdapter() {} | 22 virtual ~FrameRateControllerTimeSourceAdapter() {} |
23 | 23 |
24 virtual void OnTimerTick() OVERRIDE { frame_rate_controller_->OnTimerTick(); } | 24 virtual void OnTimerTick() OVERRIDE { |
| 25 frame_rate_controller_->OnTimerTick(); |
| 26 } |
25 | 27 |
26 private: | 28 private: |
27 explicit FrameRateControllerTimeSourceAdapter( | 29 explicit FrameRateControllerTimeSourceAdapter( |
28 FrameRateController* frame_rate_controller) | 30 FrameRateController* frame_rate_controller) |
29 : frame_rate_controller_(frame_rate_controller) {} | 31 : frame_rate_controller_(frame_rate_controller) {} |
30 | 32 |
31 FrameRateController* frame_rate_controller_; | 33 FrameRateController* frame_rate_controller_; |
32 }; | 34 }; |
33 | 35 |
34 FrameRateController::FrameRateController(scoped_refptr<TimeSource> timer) | 36 FrameRateController::FrameRateController(scoped_refptr<TimeSource> timer) |
35 : client_(NULL), | 37 : client_(NULL), |
36 num_frames_pending_(0), | 38 num_frames_pending_(0), |
37 max_frames_pending_(0), | 39 max_swaps_pending_(0), |
38 time_source_(timer), | 40 time_source_(timer), |
39 active_(false), | 41 active_(false), |
40 is_time_source_throttling_(true), | 42 is_time_source_throttling_(true), |
41 weak_factory_(this), | 43 weak_factory_(this), |
42 thread_(NULL) { | 44 thread_(NULL) { |
43 time_source_client_adapter_ = | 45 time_source_client_adapter_ = |
44 FrameRateControllerTimeSourceAdapter::Create(this); | 46 FrameRateControllerTimeSourceAdapter::Create(this); |
45 time_source_->SetClient(time_source_client_adapter_.get()); | 47 time_source_->SetClient(time_source_client_adapter_.get()); |
46 } | 48 } |
47 | 49 |
48 FrameRateController::FrameRateController(Thread* thread) | 50 FrameRateController::FrameRateController(Thread* thread) |
49 : client_(NULL), | 51 : client_(NULL), |
50 num_frames_pending_(0), | 52 num_frames_pending_(0), |
51 max_frames_pending_(0), | 53 max_swaps_pending_(0), |
52 active_(false), | 54 active_(false), |
53 is_time_source_throttling_(false), | 55 is_time_source_throttling_(false), |
54 weak_factory_(this), | 56 weak_factory_(this), |
55 thread_(thread) {} | 57 thread_(thread) {} |
56 | 58 |
57 FrameRateController::~FrameRateController() { | 59 FrameRateController::~FrameRateController() { |
58 if (is_time_source_throttling_) | 60 if (is_time_source_throttling_) |
59 time_source_->SetActive(false); | 61 time_source_->SetActive(false); |
60 } | 62 } |
61 | 63 |
62 void FrameRateController::SetActive(bool active) { | 64 void FrameRateController::SetActive(bool active) { |
63 if (active_ == active) | 65 if (active_ == active) |
64 return; | 66 return; |
65 TRACE_EVENT1("cc", "FrameRateController::SetActive", "active", active); | 67 TRACE_EVENT1("cc", "FrameRateController::SetActive", "active", active); |
66 active_ = active; | 68 active_ = active; |
67 | 69 |
68 if (is_time_source_throttling_) { | 70 if (is_time_source_throttling_) { |
69 time_source_->SetActive(active); | 71 time_source_->SetActive(active); |
70 } else { | 72 } else { |
71 if (active) | 73 if (active) |
72 PostManualTick(); | 74 PostManualTick(); |
73 else | 75 else |
74 weak_factory_.InvalidateWeakPtrs(); | 76 weak_factory_.InvalidateWeakPtrs(); |
75 } | 77 } |
76 } | 78 } |
77 | 79 |
78 void FrameRateController::SetMaxFramesPending(int max_frames_pending) { | 80 void FrameRateController::SetMaxSwapsPending(int max_swaps_pending) { |
79 DCHECK_GE(max_frames_pending, 0); | 81 DCHECK_GE(max_swaps_pending, 0); |
80 max_frames_pending_ = max_frames_pending; | 82 max_swaps_pending_ = max_swaps_pending; |
81 } | 83 } |
82 | 84 |
83 void FrameRateController::SetTimebaseAndInterval(base::TimeTicks timebase, | 85 void FrameRateController::SetTimebaseAndInterval(base::TimeTicks timebase, |
84 base::TimeDelta interval) { | 86 base::TimeDelta interval) { |
85 if (is_time_source_throttling_) | 87 if (is_time_source_throttling_) |
86 time_source_->SetTimebaseAndInterval(timebase, interval); | 88 time_source_->SetTimebaseAndInterval(timebase, interval); |
87 } | 89 } |
88 | 90 |
89 void FrameRateController::OnTimerTick() { | 91 void FrameRateController::OnTimerTick() { |
| 92 TRACE_EVENT0("cc", "FrameRateController::OnTimerTick"); |
90 DCHECK(active_); | 93 DCHECK(active_); |
91 | 94 |
92 // Check if we have too many frames in flight. | 95 // Check if we have too many frames in flight. |
93 bool throttled = | 96 bool throttled = |
94 max_frames_pending_ && num_frames_pending_ >= max_frames_pending_; | 97 max_swaps_pending_ && num_frames_pending_ >= max_swaps_pending_; |
95 TRACE_COUNTER_ID1("cc", "ThrottledCompositor", thread_, throttled); | 98 TRACE_COUNTER_ID1("cc", "ThrottledCompositor", thread_, throttled); |
96 | 99 |
97 if (client_) | 100 if (client_) { |
98 client_->BeginFrame(throttled); | 101 client_->FrameRateControllerTick(throttled); |
| 102 } |
99 | 103 |
100 if (!is_time_source_throttling_ && !throttled) | 104 if (!is_time_source_throttling_ && !throttled) |
101 PostManualTick(); | 105 PostManualTick(); |
102 } | 106 } |
103 | 107 |
104 void FrameRateController::PostManualTick() { | 108 void FrameRateController::PostManualTick() { |
105 if (active_) { | 109 if (active_) { |
106 thread_->PostTask(base::Bind(&FrameRateController::ManualTick, | 110 thread_->PostTask(base::Bind(&FrameRateController::ManualTick, |
107 weak_factory_.GetWeakPtr())); | 111 weak_factory_.GetWeakPtr())); |
108 } | 112 } |
109 } | 113 } |
110 | 114 |
111 void FrameRateController::ManualTick() { OnTimerTick(); } | 115 void FrameRateController::ManualTick() { |
| 116 OnTimerTick(); |
| 117 } |
112 | 118 |
113 void FrameRateController::DidSwapBuffers() { | 119 void FrameRateController::DidSwapBuffers() { |
114 num_frames_pending_++; | 120 num_frames_pending_++; |
115 } | 121 } |
116 | 122 |
117 void FrameRateController::DidSwapBuffersComplete() { | 123 void FrameRateController::DidSwapBuffersComplete() { |
118 DCHECK_GT(num_frames_pending_, 0); | 124 DCHECK_GT(num_frames_pending_, 0); |
119 num_frames_pending_--; | 125 num_frames_pending_--; |
120 if (!is_time_source_throttling_) | 126 if (!is_time_source_throttling_) |
121 PostManualTick(); | 127 PostManualTick(); |
(...skipping 11 matching lines...) Expand all Loading... |
133 } | 139 } |
134 | 140 |
135 base::TimeTicks FrameRateController::LastTickTime() { | 141 base::TimeTicks FrameRateController::LastTickTime() { |
136 if (is_time_source_throttling_) | 142 if (is_time_source_throttling_) |
137 return time_source_->LastTickTime(); | 143 return time_source_->LastTickTime(); |
138 | 144 |
139 return base::TimeTicks::Now(); | 145 return base::TimeTicks::Now(); |
140 } | 146 } |
141 | 147 |
142 } // namespace cc | 148 } // namespace cc |
OLD | NEW |