[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
ccameron | 9913d646 | 2015-06-08 22:07:57 | [diff] [blame] | 5 | #include "ui/accelerated_widget_mac/display_link_mac.h" |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 6 | |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 7 | #include "base/logging.h" |
ccameron | efa5e1b | 2015-06-09 18:47:02 | [diff] [blame] | 8 | #include "base/message_loop/message_loop.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 9 | #include "base/trace_event/trace_event.h" |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 10 | |
[email protected] | 3d09c413 | 2014-02-11 14:24:40 | [diff] [blame] | 11 | namespace base { |
| 12 | |
| 13 | template<> |
| 14 | struct ScopedTypeRefTraits<CVDisplayLinkRef> { |
| 15 | static void Retain(CVDisplayLinkRef object) { |
| 16 | CVDisplayLinkRetain(object); |
| 17 | } |
| 18 | static void Release(CVDisplayLinkRef object) { |
| 19 | CVDisplayLinkRelease(object); |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | } // namespace base |
| 24 | |
ccameron | 9913d646 | 2015-06-08 22:07:57 | [diff] [blame] | 25 | namespace ui { |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 26 | |
| 27 | // static |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 28 | scoped_refptr<DisplayLinkMac> DisplayLinkMac::GetForDisplay( |
| 29 | CGDirectDisplayID display_id) { |
ccameron | 01ae4cbc | 2015-08-10 10:44:16 | [diff] [blame] | 30 | if (!display_id) |
| 31 | return nullptr; |
| 32 | |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 33 | // Return the existing display link for this display, if it exists. |
| 34 | DisplayMap::iterator found = display_map_.Get().find(display_id); |
| 35 | if (found != display_map_.Get().end()) { |
| 36 | return found->second; |
| 37 | } |
| 38 | |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 39 | CVReturn ret = kCVReturnSuccess; |
| 40 | |
[email protected] | 3d09c413 | 2014-02-11 14:24:40 | [diff] [blame] | 41 | base::ScopedTypeRef<CVDisplayLinkRef> display_link; |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 42 | ret = CVDisplayLinkCreateWithCGDisplay( |
| 43 | display_id, |
| 44 | display_link.InitializeInto()); |
[email protected] | 3d09c413 | 2014-02-11 14:24:40 | [diff] [blame] | 45 | if (ret != kCVReturnSuccess) { |
| 46 | LOG(ERROR) << "CVDisplayLinkCreateWithActiveCGDisplays failed: " << ret; |
| 47 | return NULL; |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 3d09c413 | 2014-02-11 14:24:40 | [diff] [blame] | 50 | scoped_refptr<DisplayLinkMac> display_link_mac; |
ccameron | efa5e1b | 2015-06-09 18:47:02 | [diff] [blame] | 51 | display_link_mac = new DisplayLinkMac(display_id, display_link); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 52 | ret = CVDisplayLinkSetOutputCallback( |
| 53 | display_link_mac->display_link_, |
| 54 | &DisplayLinkCallback, |
| 55 | display_link_mac.get()); |
| 56 | if (ret != kCVReturnSuccess) { |
| 57 | LOG(ERROR) << "CVDisplayLinkSetOutputCallback failed: " << ret; |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | return display_link_mac; |
| 62 | } |
| 63 | |
[email protected] | 3d09c413 | 2014-02-11 14:24:40 | [diff] [blame] | 64 | DisplayLinkMac::DisplayLinkMac( |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 65 | CGDirectDisplayID display_id, |
[email protected] | 3d09c413 | 2014-02-11 14:24:40 | [diff] [blame] | 66 | base::ScopedTypeRef<CVDisplayLinkRef> display_link) |
ccameron | efa5e1b | 2015-06-09 18:47:02 | [diff] [blame] | 67 | : main_thread_task_runner_( |
| 68 | base::MessageLoop::current()->task_runner()), |
ccameron | 9913d646 | 2015-06-08 22:07:57 | [diff] [blame] | 69 | display_id_(display_id), |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 70 | display_link_(display_link), |
[email protected] | 3d09c413 | 2014-02-11 14:24:40 | [diff] [blame] | 71 | timebase_and_interval_valid_(false) { |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 72 | DCHECK(display_map_.Get().find(display_id) == display_map_.Get().end()); |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 73 | if (display_map_.Get().empty()) { |
| 74 | CGError register_error = CGDisplayRegisterReconfigurationCallback( |
| 75 | DisplayReconfigurationCallBack, nullptr); |
| 76 | DPLOG_IF(ERROR, register_error != kCGErrorSuccess) |
| 77 | << "CGDisplayRegisterReconfigurationCallback: " |
| 78 | << register_error; |
| 79 | } |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 80 | display_map_.Get().insert(std::make_pair(display_id_, this)); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | DisplayLinkMac::~DisplayLinkMac() { |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 84 | StopDisplayLink(); |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 85 | |
| 86 | DisplayMap::iterator found = display_map_.Get().find(display_id_); |
| 87 | DCHECK(found != display_map_.Get().end()); |
| 88 | DCHECK(found->second == this); |
| 89 | display_map_.Get().erase(found); |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 90 | if (display_map_.Get().empty()) { |
| 91 | CGError remove_error = CGDisplayRemoveReconfigurationCallback( |
| 92 | DisplayReconfigurationCallBack, nullptr); |
| 93 | DPLOG_IF(ERROR, remove_error != kCGErrorSuccess) |
| 94 | << "CGDisplayRemoveReconfigurationCallback: " |
| 95 | << remove_error; |
| 96 | } |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool DisplayLinkMac::GetVSyncParameters( |
| 100 | base::TimeTicks* timebase, base::TimeDelta* interval) { |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 101 | if (!timebase_and_interval_valid_) { |
| 102 | StartOrContinueDisplayLink(); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 103 | return false; |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 104 | } |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 105 | |
| 106 | *timebase = timebase_; |
| 107 | *interval = interval_; |
| 108 | return true; |
| 109 | } |
| 110 | |
ccameron | c8429f2 | 2015-08-17 20:02:41 | [diff] [blame^] | 111 | void DisplayLinkMac::NotifyCurrentTime(const base::TimeTicks& now) { |
| 112 | if (now >= recalculate_time_) |
| 113 | StartOrContinueDisplayLink(); |
| 114 | } |
| 115 | |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 116 | void DisplayLinkMac::Tick(const CVTimeStamp& cv_time) { |
ccameron | 9913d646 | 2015-06-08 22:07:57 | [diff] [blame] | 117 | TRACE_EVENT0("ui", "DisplayLinkMac::Tick"); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 118 | |
| 119 | // Verify that videoRefreshPeriod is 32 bits. |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 120 | DCHECK((cv_time.videoRefreshPeriod & ~0xffffFFFFull) == 0ull); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 121 | |
| 122 | // Verify that the numerator and denominator make some sense. |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 123 | uint32 numerator = static_cast<uint32>(cv_time.videoRefreshPeriod); |
| 124 | uint32 denominator = cv_time.videoTimeScale; |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 125 | if (numerator <= 0 || denominator <= 0) { |
| 126 | LOG(WARNING) << "Unexpected numerator or denominator, bailing."; |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | timebase_ = base::TimeTicks::FromInternalValue( |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 131 | cv_time.hostTime / 1000); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 132 | interval_ = base::TimeDelta::FromMicroseconds( |
| 133 | 1000000 * static_cast<int64>(numerator) / denominator); |
| 134 | timebase_and_interval_valid_ = true; |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 135 | |
ccameron | c8429f2 | 2015-08-17 20:02:41 | [diff] [blame^] | 136 | // Don't restart the display link for 10 seconds. |
| 137 | recalculate_time_ = base::TimeTicks::Now() + |
| 138 | base::TimeDelta::FromSeconds(10); |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 139 | StopDisplayLink(); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void DisplayLinkMac::StartOrContinueDisplayLink() { |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 143 | if (CVDisplayLinkIsRunning(display_link_)) |
| 144 | return; |
| 145 | |
| 146 | CVReturn ret = CVDisplayLinkStart(display_link_); |
| 147 | if (ret != kCVReturnSuccess) { |
| 148 | LOG(ERROR) << "CVDisplayLinkStart failed: " << ret; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void DisplayLinkMac::StopDisplayLink() { |
| 153 | if (!CVDisplayLinkIsRunning(display_link_)) |
| 154 | return; |
| 155 | |
| 156 | CVReturn ret = CVDisplayLinkStop(display_link_); |
| 157 | if (ret != kCVReturnSuccess) { |
| 158 | LOG(ERROR) << "CVDisplayLinkStop failed: " << ret; |
| 159 | } |
| 160 | } |
| 161 | |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 162 | // static |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 163 | CVReturn DisplayLinkMac::DisplayLinkCallback( |
| 164 | CVDisplayLinkRef display_link, |
| 165 | const CVTimeStamp* now, |
| 166 | const CVTimeStamp* output_time, |
| 167 | CVOptionFlags flags_in, |
| 168 | CVOptionFlags* flags_out, |
| 169 | void* context) { |
ccameron | 9913d646 | 2015-06-08 22:07:57 | [diff] [blame] | 170 | TRACE_EVENT0("ui", "DisplayLinkMac::DisplayLinkCallback"); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 171 | DisplayLinkMac* display_link_mac = static_cast<DisplayLinkMac*>(context); |
ccameron | 9913d646 | 2015-06-08 22:07:57 | [diff] [blame] | 172 | display_link_mac->main_thread_task_runner_->PostTask( |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 173 | FROM_HERE, |
| 174 | base::Bind(&DisplayLinkMac::Tick, display_link_mac, *output_time)); |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 175 | return kCVReturnSuccess; |
| 176 | } |
| 177 | |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 178 | // static |
ccameron | 26d78d9 | 2015-04-01 17:33:52 | [diff] [blame] | 179 | void DisplayLinkMac::DisplayReconfigurationCallBack( |
| 180 | CGDirectDisplayID display, |
| 181 | CGDisplayChangeSummaryFlags flags, |
| 182 | void* user_info) { |
| 183 | DisplayMap::iterator found = display_map_.Get().find(display); |
| 184 | if (found == display_map_.Get().end()) |
| 185 | return; |
| 186 | DisplayLinkMac* display_link_mac = found->second; |
| 187 | display_link_mac->timebase_and_interval_valid_ = false; |
| 188 | } |
| 189 | |
| 190 | // static |
[email protected] | c6d2165 | 2014-03-04 08:23:19 | [diff] [blame] | 191 | base::LazyInstance<DisplayLinkMac::DisplayMap> |
| 192 | DisplayLinkMac::display_map_ = LAZY_INSTANCE_INITIALIZER; |
| 193 | |
ccameron | 9913d646 | 2015-06-08 22:07:57 | [diff] [blame] | 194 | } // ui |
[email protected] | f5fca21 | 2014-02-05 18:23:00 | [diff] [blame] | 195 | |