| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/capturer/mac/desktop_configuration.h" | |
| 6 | |
| 7 #include <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "skia/ext/skia_utils_mac.h" | |
| 11 | |
| 12 #if !defined(MAC_OS_X_VERSION_10_7) || \ | |
| 13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | |
| 14 | |
| 15 @interface NSScreen (LionAPI) | |
| 16 - (CGFloat)backingScaleFactor; | |
| 17 - (NSRect)convertRectToBacking:(NSRect)aRect; | |
| 18 @end | |
| 19 | |
| 20 #endif // 10.7 | |
| 21 | |
| 22 namespace remoting { | |
| 23 | |
| 24 MacDisplayConfiguration::MacDisplayConfiguration() | |
| 25 : id(0), | |
| 26 bounds(SkIRect::MakeEmpty()), | |
| 27 pixel_bounds(SkIRect::MakeEmpty()), | |
| 28 dip_to_pixel_scale(1.0f) { | |
| 29 } | |
| 30 | |
| 31 static SkIRect NSRectToSkIRect(const NSRect& ns_rect) { | |
| 32 SkIRect result; | |
| 33 gfx::CGRectToSkRect(NSRectToCGRect(ns_rect)).roundOut(&result); | |
| 34 return result; | |
| 35 } | |
| 36 | |
| 37 static MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) { | |
| 38 MacDisplayConfiguration display_config; | |
| 39 | |
| 40 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID. | |
| 41 NSDictionary* device_description = [screen deviceDescription]; | |
| 42 display_config.id = static_cast<CGDirectDisplayID>( | |
| 43 [[device_description objectForKey:@"NSScreenNumber"] intValue]); | |
| 44 | |
| 45 // Determine the display's logical & physical dimensions. | |
| 46 NSRect ns_bounds = [screen frame]; | |
| 47 display_config.bounds = NSRectToSkIRect(ns_bounds); | |
| 48 | |
| 49 // If the host is running Mac OS X 10.7+ or later, query the scaling factor | |
| 50 // between logical and physical (aka "backing") pixels, otherwise assume 1:1. | |
| 51 if ([screen respondsToSelector:@selector(backingScaleFactor)] && | |
| 52 [screen respondsToSelector:@selector(convertRectToBacking:)]) { | |
| 53 display_config.dip_to_pixel_scale = [screen backingScaleFactor]; | |
| 54 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds]; | |
| 55 display_config.pixel_bounds = NSRectToSkIRect(ns_pixel_bounds); | |
| 56 } else { | |
| 57 display_config.pixel_bounds = display_config.bounds; | |
| 58 } | |
| 59 | |
| 60 return display_config; | |
| 61 } | |
| 62 | |
| 63 MacDesktopConfiguration::MacDesktopConfiguration() | |
| 64 : bounds(SkIRect::MakeEmpty()), | |
| 65 pixel_bounds(SkIRect::MakeEmpty()), | |
| 66 dip_to_pixel_scale(1.0f) { | |
| 67 } | |
| 68 | |
| 69 MacDesktopConfiguration::~MacDesktopConfiguration() { | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 MacDesktopConfiguration MacDesktopConfiguration::GetCurrent() { | |
| 74 MacDesktopConfiguration desktop_config; | |
| 75 | |
| 76 NSArray* screens = [NSScreen screens]; | |
| 77 CHECK(screens != NULL); | |
| 78 | |
| 79 // Iterator over the monitors, adding the primary monitor and monitors whose | |
| 80 // DPI match that of the primary monitor. | |
| 81 for (NSUInteger i = 0; i < [screens count]; ++i) { | |
| 82 MacDisplayConfiguration display_config | |
| 83 = GetConfigurationForScreen([screens objectAtIndex: i]); | |
| 84 | |
| 85 // Handling mixed-DPI is hard, so we only return displays that match the | |
| 86 // "primary" display's DPI. The primary display is always the first in the | |
| 87 // list returned by [NSScreen screens]. | |
| 88 if (i == 0) { | |
| 89 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale; | |
| 90 } else if (desktop_config.dip_to_pixel_scale != | |
| 91 display_config.dip_to_pixel_scale) { | |
| 92 continue; | |
| 93 } | |
| 94 | |
| 95 // Add the display to the configuration. | |
| 96 desktop_config.displays.push_back(display_config); | |
| 97 | |
| 98 // Update the desktop bounds to account for this display. | |
| 99 desktop_config.bounds.join(display_config.bounds); | |
| 100 desktop_config.pixel_bounds.join(display_config.pixel_bounds); | |
| 101 } | |
| 102 | |
| 103 return desktop_config; | |
| 104 } | |
| 105 | |
| 106 } // namespace remoting | |
| OLD | NEW |