Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / accelerated_widget_mac / display_link_mac.cc
blob1945d2bcabae374891df90e0339080160485e170
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.
5 #include "ui/accelerated_widget_mac/display_link_mac.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/trace_event/trace_event.h"
11 namespace base {
13 template<>
14 struct ScopedTypeRefTraits<CVDisplayLinkRef> {
15 static void Retain(CVDisplayLinkRef object) {
16 CVDisplayLinkRetain(object);
18 static void Release(CVDisplayLinkRef object) {
19 CVDisplayLinkRelease(object);
23 } // namespace base
25 namespace ui {
27 // static
28 scoped_refptr<DisplayLinkMac> DisplayLinkMac::GetForDisplay(
29 CGDirectDisplayID display_id) {
30 if (!display_id)
31 return nullptr;
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;
39 CVReturn ret = kCVReturnSuccess;
41 base::ScopedTypeRef<CVDisplayLinkRef> display_link;
42 ret = CVDisplayLinkCreateWithCGDisplay(
43 display_id,
44 display_link.InitializeInto());
45 if (ret != kCVReturnSuccess) {
46 LOG(ERROR) << "CVDisplayLinkCreateWithActiveCGDisplays failed: " << ret;
47 return NULL;
50 scoped_refptr<DisplayLinkMac> display_link_mac;
51 display_link_mac = new DisplayLinkMac(display_id, display_link);
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;
61 return display_link_mac;
64 DisplayLinkMac::DisplayLinkMac(
65 CGDirectDisplayID display_id,
66 base::ScopedTypeRef<CVDisplayLinkRef> display_link)
67 : main_thread_task_runner_(
68 base::MessageLoop::current()->task_runner()),
69 display_id_(display_id),
70 display_link_(display_link),
71 timebase_and_interval_valid_(false) {
72 DCHECK(display_map_.Get().find(display_id) == display_map_.Get().end());
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;
80 display_map_.Get().insert(std::make_pair(display_id_, this));
83 DisplayLinkMac::~DisplayLinkMac() {
84 StopDisplayLink();
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);
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;
99 bool DisplayLinkMac::GetVSyncParameters(
100 base::TimeTicks* timebase, base::TimeDelta* interval) {
101 if (!timebase_and_interval_valid_) {
102 StartOrContinueDisplayLink();
103 return false;
106 *timebase = timebase_;
107 *interval = interval_;
108 return true;
111 void DisplayLinkMac::NotifyCurrentTime(const base::TimeTicks& now) {
112 if (now >= recalculate_time_)
113 StartOrContinueDisplayLink();
116 void DisplayLinkMac::Tick(const CVTimeStamp& cv_time) {
117 TRACE_EVENT0("ui", "DisplayLinkMac::Tick");
119 // Verify that videoRefreshPeriod is 32 bits.
120 DCHECK((cv_time.videoRefreshPeriod & ~0xffffFFFFull) == 0ull);
122 // Verify that the numerator and denominator make some sense.
123 uint32 numerator = static_cast<uint32>(cv_time.videoRefreshPeriod);
124 uint32 denominator = cv_time.videoTimeScale;
125 if (numerator <= 0 || denominator <= 0) {
126 LOG(WARNING) << "Unexpected numerator or denominator, bailing.";
127 return;
130 timebase_ = base::TimeTicks::FromInternalValue(
131 cv_time.hostTime / 1000);
132 interval_ = base::TimeDelta::FromMicroseconds(
133 1000000 * static_cast<int64>(numerator) / denominator);
134 timebase_and_interval_valid_ = true;
136 // Don't restart the display link for 10 seconds.
137 recalculate_time_ = base::TimeTicks::Now() +
138 base::TimeDelta::FromSeconds(10);
139 StopDisplayLink();
142 void DisplayLinkMac::StartOrContinueDisplayLink() {
143 if (CVDisplayLinkIsRunning(display_link_))
144 return;
146 CVReturn ret = CVDisplayLinkStart(display_link_);
147 if (ret != kCVReturnSuccess) {
148 LOG(ERROR) << "CVDisplayLinkStart failed: " << ret;
152 void DisplayLinkMac::StopDisplayLink() {
153 if (!CVDisplayLinkIsRunning(display_link_))
154 return;
156 CVReturn ret = CVDisplayLinkStop(display_link_);
157 if (ret != kCVReturnSuccess) {
158 LOG(ERROR) << "CVDisplayLinkStop failed: " << ret;
162 // static
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) {
170 TRACE_EVENT0("ui", "DisplayLinkMac::DisplayLinkCallback");
171 DisplayLinkMac* display_link_mac = static_cast<DisplayLinkMac*>(context);
172 display_link_mac->main_thread_task_runner_->PostTask(
173 FROM_HERE,
174 base::Bind(&DisplayLinkMac::Tick, display_link_mac, *output_time));
175 return kCVReturnSuccess;
178 // static
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;
190 // static
191 base::LazyInstance<DisplayLinkMac::DisplayMap>
192 DisplayLinkMac::display_map_ = LAZY_INSTANCE_INITIALIZER;
194 } // ui