| 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 swap_buffers_complete_supported_(true), | 42 swap_buffers_complete_supported_(true), |
| 41 is_time_source_throttling_(true), | 43 is_time_source_throttling_(true), |
| 42 weak_factory_(this), | 44 weak_factory_(this), |
| 43 thread_(NULL) { | 45 thread_(NULL) { |
| 44 time_source_client_adapter_ = | 46 time_source_client_adapter_ = |
| 45 FrameRateControllerTimeSourceAdapter::Create(this); | 47 FrameRateControllerTimeSourceAdapter::Create(this); |
| 46 time_source_->SetClient(time_source_client_adapter_.get()); | 48 time_source_->SetClient(time_source_client_adapter_.get()); |
| 47 } | 49 } |
| 48 | 50 |
| 49 FrameRateController::FrameRateController(Thread* thread) | 51 FrameRateController::FrameRateController(Thread* thread) |
| 50 : client_(NULL), | 52 : client_(NULL), |
| 51 num_frames_pending_(0), | 53 num_frames_pending_(0), |
| 52 max_frames_pending_(0), | 54 max_swaps_pending_(0), |
| 53 active_(false), | 55 active_(false), |
| 54 swap_buffers_complete_supported_(true), | 56 swap_buffers_complete_supported_(true), |
| 55 is_time_source_throttling_(false), | 57 is_time_source_throttling_(false), |
| 56 weak_factory_(this), | 58 weak_factory_(this), |
| 57 thread_(thread) {} | 59 thread_(thread) {} |
| 58 | 60 |
| 59 FrameRateController::~FrameRateController() { | 61 FrameRateController::~FrameRateController() { |
| 60 if (is_time_source_throttling_) | 62 if (is_time_source_throttling_) |
| 61 time_source_->SetActive(false); | 63 time_source_->SetActive(false); |
| 62 } | 64 } |
| 63 | 65 |
| 64 void FrameRateController::SetActive(bool active) { | 66 void FrameRateController::SetActive(bool active) { |
| 65 if (active_ == active) | 67 if (active_ == active) |
| 66 return; | 68 return; |
| 67 TRACE_EVENT1("cc", "FrameRateController::SetActive", "active", active); | 69 TRACE_EVENT1("cc", "FrameRateController::SetActive", "active", active); |
| 68 active_ = active; | 70 active_ = active; |
| 69 | 71 |
| 70 if (is_time_source_throttling_) { | 72 if (is_time_source_throttling_) { |
| 71 time_source_->SetActive(active); | 73 time_source_->SetActive(active); |
| 72 } else { | 74 } else { |
| 73 if (active) | 75 if (active) |
| 74 PostManualTick(); | 76 PostManualTick(); |
| 75 else | 77 else |
| 76 weak_factory_.InvalidateWeakPtrs(); | 78 weak_factory_.InvalidateWeakPtrs(); |
| 77 } | 79 } |
| 78 } | 80 } |
| 79 | 81 |
| 80 void FrameRateController::SetMaxFramesPending(int max_frames_pending) { | 82 void FrameRateController::SetMaxSwapsPending(int max_swaps_pending) { |
| 81 DCHECK_GE(max_frames_pending, 0); | 83 DCHECK_GE(max_swaps_pending, 0); |
| 82 max_frames_pending_ = max_frames_pending; | 84 max_swaps_pending_ = max_swaps_pending; |
| 83 } | 85 } |
| 84 | 86 |
| 85 void FrameRateController::SetTimebaseAndInterval(base::TimeTicks timebase, | 87 void FrameRateController::SetTimebaseAndInterval(base::TimeTicks timebase, |
| 86 base::TimeDelta interval) { | 88 base::TimeDelta interval) { |
| 87 if (is_time_source_throttling_) | 89 if (is_time_source_throttling_) |
| 88 time_source_->SetTimebaseAndInterval(timebase, interval); | 90 time_source_->SetTimebaseAndInterval(timebase, interval); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void FrameRateController::SetSwapBuffersCompleteSupported(bool supported) { | 93 void FrameRateController::SetSwapBuffersCompleteSupported(bool supported) { |
| 92 swap_buffers_complete_supported_ = supported; | 94 swap_buffers_complete_supported_ = supported; |
| 93 } | 95 } |
| 94 | 96 |
| 95 void FrameRateController::OnTimerTick() { | 97 void FrameRateController::OnTimerTick() { |
| 98 TRACE_EVENT0("cc", "FrameRateController::OnTimerTick"); |
| 96 DCHECK(active_); | 99 DCHECK(active_); |
| 97 | 100 |
| 98 // Check if we have too many frames in flight. | 101 // Check if we have too many frames in flight. |
| 99 bool throttled = | 102 bool throttled = |
| 100 max_frames_pending_ && num_frames_pending_ >= max_frames_pending_; | 103 max_swaps_pending_ && num_frames_pending_ >= max_swaps_pending_; |
| 101 TRACE_COUNTER_ID1("cc", "ThrottledCompositor", thread_, throttled); | 104 TRACE_COUNTER_ID1("cc", "ThrottledCompositor", thread_, throttled); |
| 102 | 105 |
| 103 if (client_) | 106 if (client_) { |
| 104 client_->BeginFrame(throttled); | 107 client_->FrameRateControllerTick(throttled); |
| 108 } |
| 105 | 109 |
| 106 if (swap_buffers_complete_supported_ && !is_time_source_throttling_ && | 110 if (swap_buffers_complete_supported_ && !is_time_source_throttling_ && |
| 107 !throttled) | 111 !throttled) |
| 108 PostManualTick(); | 112 PostManualTick(); |
| 109 } | 113 } |
| 110 | 114 |
| 111 void FrameRateController::PostManualTick() { | 115 void FrameRateController::PostManualTick() { |
| 112 if (active_) { | 116 if (active_) { |
| 113 thread_->PostTask(base::Bind(&FrameRateController::ManualTick, | 117 thread_->PostTask(base::Bind(&FrameRateController::ManualTick, |
| 114 weak_factory_.GetWeakPtr())); | 118 weak_factory_.GetWeakPtr())); |
| 115 } | 119 } |
| 116 } | 120 } |
| 117 | 121 |
| 118 void FrameRateController::ManualTick() { OnTimerTick(); } | 122 void FrameRateController::ManualTick() { |
| 123 OnTimerTick(); |
| 124 } |
| 119 | 125 |
| 120 void FrameRateController::DidSwapBuffers() { | 126 void FrameRateController::DidSwapBuffers() { |
| 121 if (swap_buffers_complete_supported_) | 127 if (swap_buffers_complete_supported_) |
| 122 num_frames_pending_++; | 128 num_frames_pending_++; |
| 123 else if (!is_time_source_throttling_) | 129 else if (!is_time_source_throttling_) |
| 124 PostManualTick(); | 130 PostManualTick(); |
| 125 } | 131 } |
| 126 | 132 |
| 127 void FrameRateController::DidSwapBuffersComplete() { | 133 void FrameRateController::DidSwapBuffersComplete() { |
| 128 DCHECK(swap_buffers_complete_supported_); | 134 DCHECK(swap_buffers_complete_supported_); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 145 } | 151 } |
| 146 | 152 |
| 147 base::TimeTicks FrameRateController::LastTickTime() { | 153 base::TimeTicks FrameRateController::LastTickTime() { |
| 148 if (is_time_source_throttling_) | 154 if (is_time_source_throttling_) |
| 149 return time_source_->LastTickTime(); | 155 return time_source_->LastTickTime(); |
| 150 | 156 |
| 151 return base::TimeTicks::Now(); | 157 return base::TimeTicks::Now(); |
| 152 } | 158 } |
| 153 | 159 |
| 154 } // namespace cc | 160 } // namespace cc |
| OLD | NEW |