1/* 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#include "modules/desktop_capture/mac/desktop_configuration.h" 12 13#include <math.h> 14#include <algorithm> 15#include <Cocoa/Cocoa.h> 16 17#if !defined(MAC_OS_X_VERSION_10_7) || \ 18 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7 19 20@interface NSScreen (LionAPI) 21- (CGFloat)backingScaleFactor; 22- (NSRect)convertRectToBacking:(NSRect)aRect; 23@end 24 25#endif // MAC_OS_X_VERSION_10_7 26 27namespace webrtc { 28 29namespace { 30 31DesktopRect NSRectToDesktopRect(const NSRect& ns_rect) { 32 return DesktopRect::MakeLTRB( 33 static_cast<int>(floor(ns_rect.origin.x)), 34 static_cast<int>(floor(ns_rect.origin.y)), 35 static_cast<int>(ceil(ns_rect.origin.x + ns_rect.size.width)), 36 static_cast<int>(ceil(ns_rect.origin.y + ns_rect.size.height))); 37} 38 39// Inverts the position of |rect| from bottom-up coordinates to top-down, 40// relative to |bounds|. 41void InvertRectYOrigin(const DesktopRect& bounds, 42 DesktopRect* rect) { 43 assert(bounds.top() == 0); 44 *rect = DesktopRect::MakeXYWH( 45 rect->left(), bounds.bottom() - rect->bottom(), 46 rect->width(), rect->height()); 47} 48 49MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) { 50 MacDisplayConfiguration display_config; 51 52 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID. 53 NSDictionary* device_description = [screen deviceDescription]; 54 display_config.id = static_cast<CGDirectDisplayID>( 55 [[device_description objectForKey:@"NSScreenNumber"] intValue]); 56 57 // Determine the display's logical & physical dimensions. 58 NSRect ns_bounds = [screen frame]; 59 display_config.bounds = NSRectToDesktopRect(ns_bounds); 60 61 // If the host is running Mac OS X 10.7+ or later, query the scaling factor 62 // between logical and physical (aka "backing") pixels, otherwise assume 1:1. 63 if ([screen respondsToSelector:@selector(backingScaleFactor)] && 64 [screen respondsToSelector:@selector(convertRectToBacking:)]) { 65 display_config.dip_to_pixel_scale = [screen backingScaleFactor]; 66 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds]; 67 display_config.pixel_bounds = NSRectToDesktopRect(ns_pixel_bounds); 68 } else { 69 display_config.pixel_bounds = display_config.bounds; 70 } 71 72 // Determine if the display is built-in or external. 73 display_config.is_builtin = CGDisplayIsBuiltin(display_config.id); 74 75 return display_config; 76} 77 78} // namespace 79 80MacDisplayConfiguration::MacDisplayConfiguration() = default; 81MacDisplayConfiguration::MacDisplayConfiguration( 82 const MacDisplayConfiguration& other) = default; 83MacDisplayConfiguration::MacDisplayConfiguration( 84 MacDisplayConfiguration&& other) = default; 85MacDisplayConfiguration::~MacDisplayConfiguration() = default; 86 87MacDisplayConfiguration& MacDisplayConfiguration::operator=( 88 const MacDisplayConfiguration& other) = default; 89MacDisplayConfiguration& MacDisplayConfiguration::operator=( 90 MacDisplayConfiguration&& other) = default; 91 92MacDesktopConfiguration::MacDesktopConfiguration() = default; 93MacDesktopConfiguration::MacDesktopConfiguration( 94 const MacDesktopConfiguration& other) = default; 95MacDesktopConfiguration::MacDesktopConfiguration( 96 MacDesktopConfiguration&& other) = default; 97MacDesktopConfiguration::~MacDesktopConfiguration() = default; 98 99MacDesktopConfiguration& MacDesktopConfiguration::operator=( 100 const MacDesktopConfiguration& other) = default; 101MacDesktopConfiguration& MacDesktopConfiguration::operator=( 102 MacDesktopConfiguration&& other) = default; 103 104// static 105MacDesktopConfiguration MacDesktopConfiguration::GetCurrent(Origin origin) { 106 MacDesktopConfiguration desktop_config; 107 108 NSArray* screens = [NSScreen screens]; 109 assert(screens); 110 111 // Iterator over the monitors, adding the primary monitor and monitors whose 112 // DPI match that of the primary monitor. 113 for (NSUInteger i = 0; i < [screens count]; ++i) { 114 MacDisplayConfiguration display_config = 115 GetConfigurationForScreen([screens objectAtIndex: i]); 116 117 if (i == 0) 118 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale; 119 120 // Cocoa uses bottom-up coordinates, so if the caller wants top-down then 121 // we need to invert the positions of secondary monitors relative to the 122 // primary one (the primary monitor's position is (0,0) in both systems). 123 if (i > 0 && origin == TopLeftOrigin) { 124 InvertRectYOrigin(desktop_config.displays[0].bounds, 125 &display_config.bounds); 126 // |display_bounds| is density dependent, so we need to convert the 127 // primay monitor's position into the secondary monitor's density context. 128 float scaling_factor = display_config.dip_to_pixel_scale / 129 desktop_config.displays[0].dip_to_pixel_scale; 130 DesktopRect primary_bounds = DesktopRect::MakeLTRB( 131 desktop_config.displays[0].pixel_bounds.left() * scaling_factor, 132 desktop_config.displays[0].pixel_bounds.top() * scaling_factor, 133 desktop_config.displays[0].pixel_bounds.right() * scaling_factor, 134 desktop_config.displays[0].pixel_bounds.bottom() * scaling_factor); 135 InvertRectYOrigin(primary_bounds, &display_config.pixel_bounds); 136 } 137 138 // Add the display to the configuration. 139 desktop_config.displays.push_back(display_config); 140 141 // Update the desktop bounds to account for this display, unless the current 142 // display uses different DPI settings. 143 if (display_config.dip_to_pixel_scale == 144 desktop_config.dip_to_pixel_scale) { 145 desktop_config.bounds.UnionWith(display_config.bounds); 146 desktop_config.pixel_bounds.UnionWith(display_config.pixel_bounds); 147 } 148 } 149 150 return desktop_config; 151} 152 153// For convenience of comparing MacDisplayConfigurations in 154// MacDesktopConfiguration::Equals. 155bool operator==(const MacDisplayConfiguration& left, 156 const MacDisplayConfiguration& right) { 157 return left.id == right.id && 158 left.bounds.equals(right.bounds) && 159 left.pixel_bounds.equals(right.pixel_bounds) && 160 left.dip_to_pixel_scale == right.dip_to_pixel_scale; 161} 162 163bool MacDesktopConfiguration::Equals(const MacDesktopConfiguration& other) { 164 return bounds.equals(other.bounds) && 165 pixel_bounds.equals(other.pixel_bounds) && 166 dip_to_pixel_scale == other.dip_to_pixel_scale && 167 displays == other.displays; 168} 169 170const MacDisplayConfiguration* 171MacDesktopConfiguration::FindDisplayConfigurationById( 172 CGDirectDisplayID id) { 173 bool is_builtin = CGDisplayIsBuiltin(id); 174 for (MacDisplayConfigurations::const_iterator it = displays.begin(); 175 it != displays.end(); ++it) { 176 // The MBP having both discrete and integrated graphic cards will do 177 // automate graphics switching by default. When it switches from discrete to 178 // integrated one, the current display ID of the built-in display will 179 // change and this will cause screen capture stops. 180 // So make screen capture of built-in display continuing even if its display 181 // ID is changed. 182 if ((is_builtin && it->is_builtin) || (!is_builtin && it->id == id)) return &(*it); 183 } 184 return NULL; 185} 186 187} // namespace webrtc 188