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> |
alexeypa (please no reviews)
2013/04/26 21:33:58
nit #1: should <algorithm> go first?
nit #2: shoul
Sergey Ulanov
2013/05/07 22:25:50
C headers go before C++: http://google-styleguide.
| |
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>(ns_rect.origin.x), |
alexeypa (please no reviews)
2013/04/26 21:33:58
nit: Should this function check/sanitize |ns_rect|
Sergey Ulanov
2013/05/07 22:25:50
ns_rect comes from the OS, so sanitizing it here i
| |
29 return result; | 30 static_cast<int>(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, | |
alexeypa (please no reviews)
2013/04/26 21:33:58
nit: Consider moving this to DesktopRect.
Sergey Ulanov
2013/05/07 22:25:50
This is the only case where we need it, so don't t
| |
36 const webrtc::DesktopRect& b) { | |
37 return webrtc::DesktopRect::MakeLTRB( | |
38 std::min(a.left(), b.left()), std::min(a.top(), b.top()), | |
alexeypa (please no reviews)
2013/04/26 21:33:58
nit: I think it will be more readable if each para
Sergey Ulanov
2013/05/07 22:25:50
Done.
| |
39 std::max(a.right(), b.right()), std::max(a.bottom(), b.bottom())); | |
30 } | 40 } |
31 | 41 |
32 // Inverts the position of |rect| from bottom-up coordinates to top-down, | 42 // Inverts the position of |rect| from bottom-up coordinates to top-down, |
33 // relative to |bounds|. | 43 // relative to |bounds|. |
34 void InvertRectYOrigin(const SkIRect& bounds, SkIRect* rect) { | 44 void InvertRectYOrigin(const webrtc::DesktopRect& bounds, |
45 webrtc::DesktopRect* rect) { | |
35 DCHECK_EQ(0, bounds.top()); | 46 DCHECK_EQ(0, bounds.top()); |
36 rect->setXYWH(rect->x(), bounds.bottom() - rect->bottom(), | 47 *rect = webrtc::DesktopRect::MakeXYWH( |
37 rect->width(), rect->height()); | 48 rect->left(), bounds.bottom() - rect->bottom(), |
49 rect->width(), rect->height()); | |
38 } | 50 } |
39 | 51 |
40 MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) { | 52 MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) { |
41 MacDisplayConfiguration display_config; | 53 MacDisplayConfiguration display_config; |
42 | 54 |
43 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID. | 55 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID. |
44 NSDictionary* device_description = [screen deviceDescription]; | 56 NSDictionary* device_description = [screen deviceDescription]; |
45 display_config.id = static_cast<CGDirectDisplayID>( | 57 display_config.id = static_cast<CGDirectDisplayID>( |
46 [[device_description objectForKey:@"NSScreenNumber"] intValue]); | 58 [[device_description objectForKey:@"NSScreenNumber"] intValue]); |
47 | 59 |
48 // Determine the display's logical & physical dimensions. | 60 // Determine the display's logical & physical dimensions. |
49 NSRect ns_bounds = [screen frame]; | 61 NSRect ns_bounds = [screen frame]; |
50 display_config.bounds = NSRectToSkIRect(ns_bounds); | 62 display_config.bounds = NSRectToDesktopRect(ns_bounds); |
51 | 63 |
52 // If the host is running Mac OS X 10.7+ or later, query the scaling factor | 64 // 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. | 65 // between logical and physical (aka "backing") pixels, otherwise assume 1:1. |
54 if ([screen respondsToSelector:@selector(backingScaleFactor)] && | 66 if ([screen respondsToSelector:@selector(backingScaleFactor)] && |
55 [screen respondsToSelector:@selector(convertRectToBacking:)]) { | 67 [screen respondsToSelector:@selector(convertRectToBacking:)]) { |
56 display_config.dip_to_pixel_scale = [screen backingScaleFactor]; | 68 display_config.dip_to_pixel_scale = [screen backingScaleFactor]; |
57 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds]; | 69 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds]; |
58 display_config.pixel_bounds = NSRectToSkIRect(ns_pixel_bounds); | 70 display_config.pixel_bounds = NSRectToDesktopRect(ns_pixel_bounds); |
59 } else { | 71 } else { |
60 display_config.pixel_bounds = display_config.bounds; | 72 display_config.pixel_bounds = display_config.bounds; |
61 } | 73 } |
62 | 74 |
63 return display_config; | 75 return display_config; |
64 } | 76 } |
65 | 77 |
66 } // namespace | 78 } // namespace |
67 | 79 |
68 MacDisplayConfiguration::MacDisplayConfiguration() | 80 MacDisplayConfiguration::MacDisplayConfiguration() |
69 : id(0), | 81 : id(0), |
70 bounds(SkIRect::MakeEmpty()), | |
71 pixel_bounds(SkIRect::MakeEmpty()), | |
72 dip_to_pixel_scale(1.0f) { | 82 dip_to_pixel_scale(1.0f) { |
73 } | 83 } |
74 | 84 |
75 MacDesktopConfiguration::MacDesktopConfiguration() | 85 MacDesktopConfiguration::MacDesktopConfiguration() |
76 : bounds(SkIRect::MakeEmpty()), | 86 : dip_to_pixel_scale(1.0f) { |
77 pixel_bounds(SkIRect::MakeEmpty()), | |
78 dip_to_pixel_scale(1.0f) { | |
79 } | 87 } |
80 | 88 |
81 MacDesktopConfiguration::~MacDesktopConfiguration() { | 89 MacDesktopConfiguration::~MacDesktopConfiguration() { |
82 } | 90 } |
83 | 91 |
84 // static | 92 // static |
85 MacDesktopConfiguration MacDesktopConfiguration::GetCurrent(Origin origin) { | 93 MacDesktopConfiguration MacDesktopConfiguration::GetCurrent(Origin origin) { |
86 MacDesktopConfiguration desktop_config; | 94 MacDesktopConfiguration desktop_config; |
87 | 95 |
88 NSArray* screens = [NSScreen screens]; | 96 NSArray* screens = [NSScreen screens]; |
89 CHECK(screens != NULL); | 97 CHECK(screens != NULL); |
90 | 98 |
91 // Iterator over the monitors, adding the primary monitor and monitors whose | 99 // Iterator over the monitors, adding the primary monitor and monitors whose |
92 // DPI match that of the primary monitor. | 100 // DPI match that of the primary monitor. |
93 for (NSUInteger i = 0; i < [screens count]; ++i) { | 101 for (NSUInteger i = 0; i < [screens count]; ++i) { |
94 MacDisplayConfiguration display_config | 102 MacDisplayConfiguration display_config = |
95 = GetConfigurationForScreen([screens objectAtIndex: i]); | 103 GetConfigurationForScreen([screens objectAtIndex: i]); |
96 | 104 |
97 // Handling mixed-DPI is hard, so we only return displays that match the | 105 // 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 | 106 // "primary" display's DPI. The primary display is always the first in the |
99 // list returned by [NSScreen screens]. | 107 // list returned by [NSScreen screens]. |
100 if (i == 0) { | 108 if (i == 0) { |
101 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale; | 109 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale; |
102 } else if (desktop_config.dip_to_pixel_scale != | 110 } else if (desktop_config.dip_to_pixel_scale != |
103 display_config.dip_to_pixel_scale) { | 111 display_config.dip_to_pixel_scale) { |
104 continue; | 112 continue; |
105 } | 113 } |
106 | 114 |
107 // Cocoa uses bottom-up coordinates, so if the caller wants top-down then | 115 // 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 | 116 // 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). | 117 // primary one (the primary monitor's position is (0,0) in both systems). |
110 if (i > 0 && origin == TopLeftOrigin) { | 118 if (i > 0 && origin == TopLeftOrigin) { |
111 InvertRectYOrigin(desktop_config.displays[0].bounds, | 119 InvertRectYOrigin(desktop_config.displays[0].bounds, |
112 &display_config.bounds); | 120 &display_config.bounds); |
113 InvertRectYOrigin(desktop_config.displays[0].pixel_bounds, | 121 InvertRectYOrigin(desktop_config.displays[0].pixel_bounds, |
114 &display_config.pixel_bounds); | 122 &display_config.pixel_bounds); |
115 } | 123 } |
116 | 124 |
117 // Add the display to the configuration. | 125 // Add the display to the configuration. |
118 desktop_config.displays.push_back(display_config); | 126 desktop_config.displays.push_back(display_config); |
119 | 127 |
120 // Update the desktop bounds to account for this display. | 128 // Update the desktop bounds to account for this display. |
121 desktop_config.bounds.join(display_config.bounds); | 129 desktop_config.bounds = |
122 desktop_config.pixel_bounds.join(display_config.pixel_bounds); | 130 JoinRects(desktop_config.bounds, display_config.bounds); |
Wez
2013/04/26 18:48:14
nit: Can we have DesktopRect provide a join() or u
Sergey Ulanov
2013/05/07 22:25:50
This is the only place where we need it at the mom
| |
131 desktop_config.pixel_bounds = | |
132 JoinRects(desktop_config.pixel_bounds, display_config.pixel_bounds); | |
123 } | 133 } |
124 | 134 |
125 return desktop_config; | 135 return desktop_config; |
126 } | 136 } |
127 | 137 |
128 } // namespace media | 138 } // namespace media |
OLD | NEW |