Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/vsync_time_source.h" | 5 #include "cc/vsync_time_source.h" |
| 6 | 6 |
| 7 namespace cc { | 7 namespace cc { |
| 8 | 8 |
| 9 scoped_refptr<VSyncTimeSource> VSyncTimeSource::create( | 9 scoped_refptr<VSyncTimeSource> VSyncTimeSource::create( |
| 10 VSyncProvider* vsync_provider) { | 10 VSyncProvider* vsync_provider) { |
| 11 return make_scoped_refptr(new VSyncTimeSource(vsync_provider)); | 11 return make_scoped_refptr(new VSyncTimeSource(vsync_provider)); |
| 12 } | 12 } |
| 13 | 13 |
| 14 VSyncTimeSource::VSyncTimeSource(VSyncProvider* vsync_provider) | 14 VSyncTimeSource::VSyncTimeSource(VSyncProvider* vsync_provider) |
| 15 : vsync_provider_(vsync_provider) | 15 : vsync_provider_(vsync_provider) |
| 16 , client_(0) | 16 , client_(0) |
| 17 , active_(false) | 17 , active_(false) |
| 18 , notification_requested_(false) {} | 18 , notification_requested_(false) |
| 19 , did_synthesize_last_vsync_(false) {} | |
| 19 | 20 |
| 20 VSyncTimeSource::~VSyncTimeSource() {} | 21 VSyncTimeSource::~VSyncTimeSource() {} |
| 21 | 22 |
| 22 void VSyncTimeSource::setClient(TimeSourceClient* client) { | 23 void VSyncTimeSource::setClient(TimeSourceClient* client) { |
| 23 client_ = client; | 24 client_ = client; |
| 24 } | 25 } |
| 25 | 26 |
| 26 void VSyncTimeSource::setActive(bool active) { | 27 void VSyncTimeSource::setActive(bool active) { |
| 27 if (active_ == active) | 28 if (active_ == active) |
| 28 return; | 29 return; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 47 base::TimeTicks VSyncTimeSource::nextTickTime() { | 48 base::TimeTicks VSyncTimeSource::nextTickTime() { |
| 48 return active() ? last_tick_time_ + interval_ : base::TimeTicks(); | 49 return active() ? last_tick_time_ + interval_ : base::TimeTicks(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void VSyncTimeSource::setTimebaseAndInterval(base::TimeTicks, | 52 void VSyncTimeSource::setTimebaseAndInterval(base::TimeTicks, |
| 52 base::TimeDelta interval) { | 53 base::TimeDelta interval) { |
| 53 interval_ = interval; | 54 interval_ = interval; |
| 54 } | 55 } |
| 55 | 56 |
| 56 void VSyncTimeSource::DidVSync(base::TimeTicks frame_time) { | 57 void VSyncTimeSource::DidVSync(base::TimeTicks frame_time) { |
| 58 // We can use input events to synthesize vsync events, but we must be careful | |
|
jamesr
2013/01/16 04:13:26
this logic seems really fishy to me. I think you
Sami
2013/01/16 10:49:15
I suppose you're right. Doing this in the browser
| |
| 59 // to avoid ticking multiple times during a single frame. To do that we keep | |
| 60 // track of when the last synthesized vsync happened and ignore any real vsync | |
| 61 // notifications during the first half of the same frame. | |
| 62 double intervals_since_last_tick = | |
| 63 (frame_time - last_tick_time_).InSecondsF() / interval_.InSecondsF(); | |
| 57 last_tick_time_ = frame_time; | 64 last_tick_time_ = frame_time; |
| 65 if (did_synthesize_last_vsync_) { | |
| 66 did_synthesize_last_vsync_ = false; | |
| 67 if (intervals_since_last_tick < .5) | |
| 68 return; | |
|
jamesr
2013/01/16 04:13:26
we should trace this early-out
| |
| 69 } | |
| 58 if (!active_) { | 70 if (!active_) { |
| 59 if (notification_requested_) { | 71 if (notification_requested_) { |
| 60 notification_requested_ = false; | 72 notification_requested_ = false; |
| 61 vsync_provider_->RequestVSyncNotification(NULL); | 73 vsync_provider_->RequestVSyncNotification(NULL); |
| 62 } | 74 } |
| 63 return; | 75 return; |
| 64 } | 76 } |
| 65 if (client_) | 77 if (client_) |
| 66 client_->onTimerTick(); | 78 client_->onTimerTick(); |
| 67 } | 79 } |
| 68 | 80 |
| 81 base::TimeTicks VSyncTimeSource::Now() const | |
| 82 { | |
| 83 return base::TimeTicks::Now(); | |
| 84 } | |
| 85 | |
| 86 void VSyncTimeSource::DidReceiveLastInputEventForVSync() { | |
| 87 if (!active_ || interval_ == base::TimeDelta()) | |
| 88 return; | |
| 89 | |
| 90 // Since input events currently aren't timestamped, we need to estimate the | |
| 91 // current frame time. | |
| 92 double intervals_since_last_tick = | |
| 93 (Now() - last_tick_time_).InSecondsF() / interval_.InSecondsF(); | |
| 94 last_tick_time_ += interval_ * std::max(1., floor(intervals_since_last_tick)); | |
| 95 did_synthesize_last_vsync_ = true; | |
| 96 | |
| 97 if (client_) | |
| 98 client_->onTimerTick(); | |
| 99 } | |
| 100 | |
| 69 } // namespace cc | 101 } // namespace cc |
| OLD | NEW |