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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 template<> | 15 template<> |
16 struct ScopedTypeRefTraits<CVDisplayLinkRef> { | 16 struct ScopedTypeRefTraits<CVDisplayLinkRef> { |
17 static CVDisplayLinkRef InvalidValue() { return nullptr; } | 17 static CVDisplayLinkRef InvalidValue() { return nullptr; } |
18 static CVDisplayLinkRef Retain(CVDisplayLinkRef object) { | 18 static CVDisplayLinkRef Retain(CVDisplayLinkRef object) { |
19 return CVDisplayLinkRetain(object); | 19 return CVDisplayLinkRetain(object); |
20 } | 20 } |
21 static void Release(CVDisplayLinkRef object) { | 21 static void Release(CVDisplayLinkRef object) { |
22 CVDisplayLinkRelease(object); | 22 CVDisplayLinkRelease(object); |
23 } | 23 } |
24 }; | 24 }; |
25 | 25 |
26 } // namespace base | 26 } // namespace base |
27 | 27 |
| 28 namespace { |
| 29 |
| 30 // Empty callback set during tear down. |
| 31 CVReturn VoidDisplayLinkCallback(CVDisplayLinkRef display_link, |
| 32 const CVTimeStamp* now, |
| 33 const CVTimeStamp* output_time, |
| 34 CVOptionFlags flags_in, |
| 35 CVOptionFlags* flags_out, |
| 36 void* context) { |
| 37 return kCVReturnSuccess; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
28 namespace ui { | 42 namespace ui { |
29 | 43 |
30 // static | 44 // static |
31 scoped_refptr<DisplayLinkMac> DisplayLinkMac::GetForDisplay( | 45 scoped_refptr<DisplayLinkMac> DisplayLinkMac::GetForDisplay( |
32 CGDirectDisplayID display_id) { | 46 CGDirectDisplayID display_id) { |
33 if (!display_id) | 47 if (!display_id) |
34 return nullptr; | 48 return nullptr; |
35 | 49 |
36 // Return the existing display link for this display, if it exists. | 50 // Return the existing display link for this display, if it exists. |
37 DisplayMap::iterator found = display_map_.Get().find(display_id); | 51 DisplayMap::iterator found = display_map_.Get().find(display_id); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 DPLOG_IF(ERROR, register_error != kCGErrorSuccess) | 93 DPLOG_IF(ERROR, register_error != kCGErrorSuccess) |
80 << "CGDisplayRegisterReconfigurationCallback: " | 94 << "CGDisplayRegisterReconfigurationCallback: " |
81 << register_error; | 95 << register_error; |
82 } | 96 } |
83 display_map_.Get().insert(std::make_pair(display_id_, this)); | 97 display_map_.Get().insert(std::make_pair(display_id_, this)); |
84 } | 98 } |
85 | 99 |
86 DisplayLinkMac::~DisplayLinkMac() { | 100 DisplayLinkMac::~DisplayLinkMac() { |
87 StopDisplayLink(); | 101 StopDisplayLink(); |
88 | 102 |
| 103 // Usually |display_link_| holds the last reference to CVDisplayLinkRef, but |
| 104 // that's not guaranteed, so it might not free all resources after the |
| 105 // destructor completes. Ensure the callback is cleared out regardless to |
| 106 // avoid possible crashes (see http://crbug.com/564780). |
| 107 CVReturn ret = CVDisplayLinkSetOutputCallback( |
| 108 display_link_, VoidDisplayLinkCallback, nullptr); |
| 109 DCHECK_EQ(kCGErrorSuccess, ret); |
| 110 |
89 DisplayMap::iterator found = display_map_.Get().find(display_id_); | 111 DisplayMap::iterator found = display_map_.Get().find(display_id_); |
90 DCHECK(found != display_map_.Get().end()); | 112 DCHECK(found != display_map_.Get().end()); |
91 DCHECK(found->second == this); | 113 DCHECK(found->second == this); |
92 display_map_.Get().erase(found); | 114 display_map_.Get().erase(found); |
93 if (display_map_.Get().empty()) { | 115 if (display_map_.Get().empty()) { |
94 CGError remove_error = CGDisplayRemoveReconfigurationCallback( | 116 CGError remove_error = CGDisplayRemoveReconfigurationCallback( |
95 DisplayReconfigurationCallBack, nullptr); | 117 DisplayReconfigurationCallBack, nullptr); |
96 DPLOG_IF(ERROR, remove_error != kCGErrorSuccess) | 118 DPLOG_IF(ERROR, remove_error != kCGErrorSuccess) |
97 << "CGDisplayRemoveReconfigurationCallback: " | 119 << "CGDisplayRemoveReconfigurationCallback: " |
98 << remove_error; | 120 << remove_error; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 DisplayLinkMac* display_link_mac = found->second; | 211 DisplayLinkMac* display_link_mac = found->second; |
190 display_link_mac->timebase_and_interval_valid_ = false; | 212 display_link_mac->timebase_and_interval_valid_ = false; |
191 } | 213 } |
192 | 214 |
193 // static | 215 // static |
194 base::LazyInstance<DisplayLinkMac::DisplayMap> | 216 base::LazyInstance<DisplayLinkMac::DisplayMap> |
195 DisplayLinkMac::display_map_ = LAZY_INSTANCE_INITIALIZER; | 217 DisplayLinkMac::display_map_ = LAZY_INSTANCE_INITIALIZER; |
196 | 218 |
197 } // ui | 219 } // ui |
198 | 220 |
OLD | NEW |