OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "media/video/capture/screen/mac/desktop_configuration.h" | 5 #include "media/video/capture/screen/mac/desktop_configuration.h" |
6 | 6 |
| 7 #include <math.h> |
| 8 #include <algorithm> |
7 #include <Cocoa/Cocoa.h> | 9 #include <Cocoa/Cocoa.h> |
8 | 10 |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "skia/ext/skia_utils_mac.h" | |
11 | 12 |
12 #if !defined(MAC_OS_X_VERSION_10_7) || \ | 13 #if !defined(MAC_OS_X_VERSION_10_7) || \ |
13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | 14 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
14 | 15 |
15 @interface NSScreen (LionAPI) | 16 @interface NSScreen (LionAPI) |
16 - (CGFloat)backingScaleFactor; | 17 - (CGFloat)backingScaleFactor; |
17 - (NSRect)convertRectToBacking:(NSRect)aRect; | 18 - (NSRect)convertRectToBacking:(NSRect)aRect; |
18 @end | 19 @end |
19 | 20 |
20 #endif // 10.7 | 21 #endif // 10.7 |
21 | 22 |
22 namespace media { | 23 namespace media { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 SkIRect NSRectToSkIRect(const NSRect& ns_rect) { | 27 webrtc::DesktopRect NSRectToDesktopRect(const NSRect& ns_rect) { |
27 SkIRect result; | 28 return webrtc::DesktopRect::MakeLTRB( |
28 gfx::CGRectToSkRect(NSRectToCGRect(ns_rect)).roundOut(&result); | 29 static_cast<int>(floor(ns_rect.origin.x)), |
29 return result; | 30 static_cast<int>(floor(ns_rect.origin.y)), |
| 31 static_cast<int>(ceil(ns_rect.origin.x + ns_rect.size.width)), |
| 32 static_cast<int>(ceil(ns_rect.origin.y + ns_rect.size.height))); |
| 33 } |
| 34 |
| 35 webrtc::DesktopRect JoinRects(const webrtc::DesktopRect& a, |
| 36 const webrtc::DesktopRect& b) { |
| 37 return webrtc::DesktopRect::MakeLTRB( |
| 38 std::min(a.left(), b.left()), |
| 39 std::min(a.top(), b.top()), |
| 40 std::max(a.right(), b.right()), |
| 41 std::max(a.bottom(), b.bottom())); |
30 } | 42 } |
31 | 43 |
32 // Inverts the position of |rect| from bottom-up coordinates to top-down, | 44 // Inverts the position of |rect| from bottom-up coordinates to top-down, |
33 // relative to |bounds|. | 45 // relative to |bounds|. |
34 void InvertRectYOrigin(const SkIRect& bounds, SkIRect* rect) { | 46 void InvertRectYOrigin(const webrtc::DesktopRect& bounds, |
| 47 webrtc::DesktopRect* rect) { |
35 DCHECK_EQ(0, bounds.top()); | 48 DCHECK_EQ(0, bounds.top()); |
36 rect->setXYWH(rect->x(), bounds.bottom() - rect->bottom(), | 49 *rect = webrtc::DesktopRect::MakeXYWH( |
37 rect->width(), rect->height()); | 50 rect->left(), bounds.bottom() - rect->bottom(), |
| 51 rect->width(), rect->height()); |
38 } | 52 } |
39 | 53 |
40 MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) { | 54 MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) { |
41 MacDisplayConfiguration display_config; | 55 MacDisplayConfiguration display_config; |
42 | 56 |
43 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID. | 57 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID. |
44 NSDictionary* device_description = [screen deviceDescription]; | 58 NSDictionary* device_description = [screen deviceDescription]; |
45 display_config.id = static_cast<CGDirectDisplayID>( | 59 display_config.id = static_cast<CGDirectDisplayID>( |
46 [[device_description objectForKey:@"NSScreenNumber"] intValue]); | 60 [[device_description objectForKey:@"NSScreenNumber"] intValue]); |
47 | 61 |
48 // Determine the display's logical & physical dimensions. | 62 // Determine the display's logical & physical dimensions. |
49 NSRect ns_bounds = [screen frame]; | 63 NSRect ns_bounds = [screen frame]; |
50 display_config.bounds = NSRectToSkIRect(ns_bounds); | 64 display_config.bounds = NSRectToDesktopRect(ns_bounds); |
51 | 65 |
52 // If the host is running Mac OS X 10.7+ or later, query the scaling factor | 66 // If the host is running Mac OS X 10.7+ or later, query the scaling factor |
53 // between logical and physical (aka "backing") pixels, otherwise assume 1:1. | 67 // between logical and physical (aka "backing") pixels, otherwise assume 1:1. |
54 if ([screen respondsToSelector:@selector(backingScaleFactor)] && | 68 if ([screen respondsToSelector:@selector(backingScaleFactor)] && |
55 [screen respondsToSelector:@selector(convertRectToBacking:)]) { | 69 [screen respondsToSelector:@selector(convertRectToBacking:)]) { |
56 display_config.dip_to_pixel_scale = [screen backingScaleFactor]; | 70 display_config.dip_to_pixel_scale = [screen backingScaleFactor]; |
57 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds]; | 71 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds]; |
58 display_config.pixel_bounds = NSRectToSkIRect(ns_pixel_bounds); | 72 display_config.pixel_bounds = NSRectToDesktopRect(ns_pixel_bounds); |
59 } else { | 73 } else { |
60 display_config.pixel_bounds = display_config.bounds; | 74 display_config.pixel_bounds = display_config.bounds; |
61 } | 75 } |
62 | 76 |
63 return display_config; | 77 return display_config; |
64 } | 78 } |
65 | 79 |
66 } // namespace | 80 } // namespace |
67 | 81 |
68 MacDisplayConfiguration::MacDisplayConfiguration() | 82 MacDisplayConfiguration::MacDisplayConfiguration() |
69 : id(0), | 83 : id(0), |
70 bounds(SkIRect::MakeEmpty()), | |
71 pixel_bounds(SkIRect::MakeEmpty()), | |
72 dip_to_pixel_scale(1.0f) { | 84 dip_to_pixel_scale(1.0f) { |
73 } | 85 } |
74 | 86 |
75 MacDesktopConfiguration::MacDesktopConfiguration() | 87 MacDesktopConfiguration::MacDesktopConfiguration() |
76 : bounds(SkIRect::MakeEmpty()), | 88 : dip_to_pixel_scale(1.0f) { |
77 pixel_bounds(SkIRect::MakeEmpty()), | |
78 dip_to_pixel_scale(1.0f) { | |
79 } | 89 } |
80 | 90 |
81 MacDesktopConfiguration::~MacDesktopConfiguration() { | 91 MacDesktopConfiguration::~MacDesktopConfiguration() { |
82 } | 92 } |
83 | 93 |
84 // static | 94 // static |
85 MacDesktopConfiguration MacDesktopConfiguration::GetCurrent(Origin origin) { | 95 MacDesktopConfiguration MacDesktopConfiguration::GetCurrent(Origin origin) { |
86 MacDesktopConfiguration desktop_config; | 96 MacDesktopConfiguration desktop_config; |
87 | 97 |
88 NSArray* screens = [NSScreen screens]; | 98 NSArray* screens = [NSScreen screens]; |
89 CHECK(screens != NULL); | 99 CHECK(screens != NULL); |
90 | 100 |
91 // Iterator over the monitors, adding the primary monitor and monitors whose | 101 // Iterator over the monitors, adding the primary monitor and monitors whose |
92 // DPI match that of the primary monitor. | 102 // DPI match that of the primary monitor. |
93 for (NSUInteger i = 0; i < [screens count]; ++i) { | 103 for (NSUInteger i = 0; i < [screens count]; ++i) { |
94 MacDisplayConfiguration display_config | 104 MacDisplayConfiguration display_config = |
95 = GetConfigurationForScreen([screens objectAtIndex: i]); | 105 GetConfigurationForScreen([screens objectAtIndex: i]); |
96 | 106 |
97 // Handling mixed-DPI is hard, so we only return displays that match the | 107 // Handling mixed-DPI is hard, so we only return displays that match the |
98 // "primary" display's DPI. The primary display is always the first in the | 108 // "primary" display's DPI. The primary display is always the first in the |
99 // list returned by [NSScreen screens]. | 109 // list returned by [NSScreen screens]. |
100 if (i == 0) { | 110 if (i == 0) { |
101 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale; | 111 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale; |
102 } else if (desktop_config.dip_to_pixel_scale != | 112 } else if (desktop_config.dip_to_pixel_scale != |
103 display_config.dip_to_pixel_scale) { | 113 display_config.dip_to_pixel_scale) { |
104 continue; | 114 continue; |
105 } | 115 } |
106 | 116 |
107 // Cocoa uses bottom-up coordinates, so if the caller wants top-down then | 117 // Cocoa uses bottom-up coordinates, so if the caller wants top-down then |
108 // we need to invert the positions of secondary monitors relative to the | 118 // we need to invert the positions of secondary monitors relative to the |
109 // primary one (the primary monitor's position is (0,0) in both systems). | 119 // primary one (the primary monitor's position is (0,0) in both systems). |
110 if (i > 0 && origin == TopLeftOrigin) { | 120 if (i > 0 && origin == TopLeftOrigin) { |
111 InvertRectYOrigin(desktop_config.displays[0].bounds, | 121 InvertRectYOrigin(desktop_config.displays[0].bounds, |
112 &display_config.bounds); | 122 &display_config.bounds); |
113 InvertRectYOrigin(desktop_config.displays[0].pixel_bounds, | 123 InvertRectYOrigin(desktop_config.displays[0].pixel_bounds, |
114 &display_config.pixel_bounds); | 124 &display_config.pixel_bounds); |
115 } | 125 } |
116 | 126 |
117 // Add the display to the configuration. | 127 // Add the display to the configuration. |
118 desktop_config.displays.push_back(display_config); | 128 desktop_config.displays.push_back(display_config); |
119 | 129 |
120 // Update the desktop bounds to account for this display. | 130 // Update the desktop bounds to account for this display. |
121 desktop_config.bounds.join(display_config.bounds); | 131 desktop_config.bounds = |
122 desktop_config.pixel_bounds.join(display_config.pixel_bounds); | 132 JoinRects(desktop_config.bounds, display_config.bounds); |
| 133 desktop_config.pixel_bounds = |
| 134 JoinRects(desktop_config.pixel_bounds, display_config.pixel_bounds); |
123 } | 135 } |
124 | 136 |
125 return desktop_config; | 137 return desktop_config; |
126 } | 138 } |
127 | 139 |
128 } // namespace media | 140 } // namespace media |
OLD | NEW |