OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/accelerated_widget_mac/display_link_mac.h" | 5 #include "ui/accelerated_widget_mac/display_link_mac.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 template<> | 13 template<> |
14 struct ScopedTypeRefTraits<CVDisplayLinkRef> { | 14 struct ScopedTypeRefTraits<CVDisplayLinkRef> { |
15 static void Retain(CVDisplayLinkRef object) { | 15 static void Retain(CVDisplayLinkRef object) { |
16 CVDisplayLinkRetain(object); | 16 CVDisplayLinkRetain(object); |
17 } | 17 } |
18 static void Release(CVDisplayLinkRef object) { | 18 static void Release(CVDisplayLinkRef object) { |
19 CVDisplayLinkRelease(object); | 19 CVDisplayLinkRelease(object); |
20 } | 20 } |
21 }; | 21 }; |
22 | 22 |
23 } // namespace base | 23 } // namespace base |
24 | 24 |
25 namespace ui { | 25 namespace ui { |
26 | 26 |
27 // static | 27 // static |
28 scoped_refptr<DisplayLinkMac> DisplayLinkMac::GetForDisplay( | 28 scoped_refptr<DisplayLinkMac> DisplayLinkMac::GetForDisplay( |
29 CGDirectDisplayID display_id) { | 29 CGDirectDisplayID display_id) { |
30 if (!display_id) | |
31 return nullptr; | |
32 | |
33 // Return the existing display link for this display, if it exists. | 30 // Return the existing display link for this display, if it exists. |
34 DisplayMap::iterator found = display_map_.Get().find(display_id); | 31 DisplayMap::iterator found = display_map_.Get().find(display_id); |
35 if (found != display_map_.Get().end()) { | 32 if (found != display_map_.Get().end()) { |
36 return found->second; | 33 return found->second; |
37 } | 34 } |
38 | 35 |
39 CVReturn ret = kCVReturnSuccess; | 36 CVReturn ret = kCVReturnSuccess; |
40 | 37 |
41 base::ScopedTypeRef<CVDisplayLinkRef> display_link; | 38 base::ScopedTypeRef<CVDisplayLinkRef> display_link; |
42 ret = CVDisplayLinkCreateWithCGDisplay( | 39 ret = CVDisplayLinkCreateWithCGDisplay( |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 if (!timebase_and_interval_valid_) { | 98 if (!timebase_and_interval_valid_) { |
102 StartOrContinueDisplayLink(); | 99 StartOrContinueDisplayLink(); |
103 return false; | 100 return false; |
104 } | 101 } |
105 | 102 |
106 *timebase = timebase_; | 103 *timebase = timebase_; |
107 *interval = interval_; | 104 *interval = interval_; |
108 return true; | 105 return true; |
109 } | 106 } |
110 | 107 |
111 base::TimeTicks DisplayLinkMac::GetNextVSyncTimeAfter( | |
112 const base::TimeTicks& from, double interval_fraction) { | |
113 if (!timebase_and_interval_valid_) { | |
114 StartOrContinueDisplayLink(); | |
115 return from; | |
116 } | |
117 | |
118 // Compute the previous vsync time. | |
119 base::TimeTicks previous_vsync = | |
120 interval_ * ((from - timebase_remainder_) / interval_) + | |
121 timebase_remainder_; | |
122 | |
123 // Return |interval_fraction| through the next vsync. | |
124 return previous_vsync + (1 + interval_fraction) * interval_; | |
125 } | |
126 | |
127 void DisplayLinkMac::Tick(const CVTimeStamp& cv_time) { | 108 void DisplayLinkMac::Tick(const CVTimeStamp& cv_time) { |
128 TRACE_EVENT0("ui", "DisplayLinkMac::Tick"); | 109 TRACE_EVENT0("ui", "DisplayLinkMac::Tick"); |
129 | 110 |
130 // Verify that videoRefreshPeriod is 32 bits. | 111 // Verify that videoRefreshPeriod is 32 bits. |
131 DCHECK((cv_time.videoRefreshPeriod & ~0xffffFFFFull) == 0ull); | 112 DCHECK((cv_time.videoRefreshPeriod & ~0xffffFFFFull) == 0ull); |
132 | 113 |
133 // Verify that the numerator and denominator make some sense. | 114 // Verify that the numerator and denominator make some sense. |
134 uint32 numerator = static_cast<uint32>(cv_time.videoRefreshPeriod); | 115 uint32 numerator = static_cast<uint32>(cv_time.videoRefreshPeriod); |
135 uint32 denominator = cv_time.videoTimeScale; | 116 uint32 denominator = cv_time.videoTimeScale; |
136 if (numerator <= 0 || denominator <= 0) { | 117 if (numerator <= 0 || denominator <= 0) { |
137 LOG(WARNING) << "Unexpected numerator or denominator, bailing."; | 118 LOG(WARNING) << "Unexpected numerator or denominator, bailing."; |
138 return; | 119 return; |
139 } | 120 } |
140 | 121 |
141 timebase_ = base::TimeTicks::FromInternalValue( | 122 timebase_ = base::TimeTicks::FromInternalValue( |
142 cv_time.hostTime / 1000); | 123 cv_time.hostTime / 1000); |
143 interval_ = base::TimeDelta::FromMicroseconds( | 124 interval_ = base::TimeDelta::FromMicroseconds( |
144 1000000 * static_cast<int64>(numerator) / denominator); | 125 1000000 * static_cast<int64>(numerator) / denominator); |
145 // Compute |timebase_remainder_| to be the first vsync after time zero. | |
146 timebase_remainder_ = | |
147 timebase_ - interval_ * ((timebase_ - base::TimeTicks()) / interval_); | |
148 timebase_and_interval_valid_ = true; | 126 timebase_and_interval_valid_ = true; |
149 | 127 |
150 StopDisplayLink(); | 128 StopDisplayLink(); |
151 } | 129 } |
152 | 130 |
153 void DisplayLinkMac::StartOrContinueDisplayLink() { | 131 void DisplayLinkMac::StartOrContinueDisplayLink() { |
154 if (CVDisplayLinkIsRunning(display_link_)) | 132 if (CVDisplayLinkIsRunning(display_link_)) |
155 return; | 133 return; |
156 | 134 |
157 CVReturn ret = CVDisplayLinkStart(display_link_); | 135 CVReturn ret = CVDisplayLinkStart(display_link_); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 DisplayLinkMac* display_link_mac = found->second; | 175 DisplayLinkMac* display_link_mac = found->second; |
198 display_link_mac->timebase_and_interval_valid_ = false; | 176 display_link_mac->timebase_and_interval_valid_ = false; |
199 } | 177 } |
200 | 178 |
201 // static | 179 // static |
202 base::LazyInstance<DisplayLinkMac::DisplayMap> | 180 base::LazyInstance<DisplayLinkMac::DisplayMap> |
203 DisplayLinkMac::display_map_ = LAZY_INSTANCE_INITIALIZER; | 181 DisplayLinkMac::display_map_ = LAZY_INSTANCE_INITIALIZER; |
204 | 182 |
205 } // ui | 183 } // ui |
206 | 184 |
OLD | NEW |