Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: media/video/capture/screen/mac/desktop_configuration.mm

Issue 12221103: Fix ScreenCapturerMac handling of secondary configurations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review nits. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <Cocoa/Cocoa.h> 7 #include <Cocoa/Cocoa.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "skia/ext/skia_utils_mac.h" 10 #include "skia/ext/skia_utils_mac.h"
11 11
12 #if !defined(MAC_OS_X_VERSION_10_7) || \ 12 #if !defined(MAC_OS_X_VERSION_10_7) || \
13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
14 14
15 @interface NSScreen (LionAPI) 15 @interface NSScreen (LionAPI)
16 - (CGFloat)backingScaleFactor; 16 - (CGFloat)backingScaleFactor;
17 - (NSRect)convertRectToBacking:(NSRect)aRect; 17 - (NSRect)convertRectToBacking:(NSRect)aRect;
18 @end 18 @end
19 19
20 #endif // 10.7 20 #endif // 10.7
21 21
22 namespace media { 22 namespace media {
23 23
24 MacDisplayConfiguration::MacDisplayConfiguration() 24 namespace {
25 : id(0),
26 bounds(SkIRect::MakeEmpty()),
27 pixel_bounds(SkIRect::MakeEmpty()),
28 dip_to_pixel_scale(1.0f) {
29 }
30 25
31 static SkIRect NSRectToSkIRect(const NSRect& ns_rect) { 26 SkIRect NSRectToSkIRect(const NSRect& ns_rect) {
32 SkIRect result; 27 SkIRect result;
33 gfx::CGRectToSkRect(NSRectToCGRect(ns_rect)).roundOut(&result); 28 gfx::CGRectToSkRect(NSRectToCGRect(ns_rect)).roundOut(&result);
34 return result; 29 return result;
35 } 30 }
36 31
37 static MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) { 32 // Inverts the position of |rect| about the horizontal mid-point of |bounds|.
33 // If |bounds| refers to a Cartesian coordinate space then this effectively
34 // converts |rect| from Cartesian (bottom-left origin) to inverse-Cartesian
35 // (top-left origin) coordinates.
36 void InvertRectYOrigin(const SkIRect& bounds, SkIRect* rect) {
37 rect->setXYWH(rect->x(), bounds.top() + bounds.bottom() - rect->bottom(),
38 rect->width(), rect->height());
39 }
40
41 MacDisplayConfiguration GetConfigurationForScreen(NSScreen* screen) {
38 MacDisplayConfiguration display_config; 42 MacDisplayConfiguration display_config;
39 43
40 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID. 44 // Fetch the NSScreenNumber, which is also the CGDirectDisplayID.
41 NSDictionary* device_description = [screen deviceDescription]; 45 NSDictionary* device_description = [screen deviceDescription];
42 display_config.id = static_cast<CGDirectDisplayID>( 46 display_config.id = static_cast<CGDirectDisplayID>(
43 [[device_description objectForKey:@"NSScreenNumber"] intValue]); 47 [[device_description objectForKey:@"NSScreenNumber"] intValue]);
44 48
45 // Determine the display's logical & physical dimensions. 49 // Determine the display's logical & physical dimensions.
46 NSRect ns_bounds = [screen frame]; 50 NSRect ns_bounds = [screen frame];
47 display_config.bounds = NSRectToSkIRect(ns_bounds); 51 display_config.bounds = NSRectToSkIRect(ns_bounds);
48 52
49 // If the host is running Mac OS X 10.7+ or later, query the scaling factor 53 // 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. 54 // between logical and physical (aka "backing") pixels, otherwise assume 1:1.
51 if ([screen respondsToSelector:@selector(backingScaleFactor)] && 55 if ([screen respondsToSelector:@selector(backingScaleFactor)] &&
52 [screen respondsToSelector:@selector(convertRectToBacking:)]) { 56 [screen respondsToSelector:@selector(convertRectToBacking:)]) {
53 display_config.dip_to_pixel_scale = [screen backingScaleFactor]; 57 display_config.dip_to_pixel_scale = [screen backingScaleFactor];
54 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds]; 58 NSRect ns_pixel_bounds = [screen convertRectToBacking: ns_bounds];
55 display_config.pixel_bounds = NSRectToSkIRect(ns_pixel_bounds); 59 display_config.pixel_bounds = NSRectToSkIRect(ns_pixel_bounds);
56 } else { 60 } else {
57 display_config.pixel_bounds = display_config.bounds; 61 display_config.pixel_bounds = display_config.bounds;
58 } 62 }
59 63
60 return display_config; 64 return display_config;
61 } 65 }
62 66
67 } // namespace
68
69 MacDisplayConfiguration::MacDisplayConfiguration()
70 : id(0),
71 bounds(SkIRect::MakeEmpty()),
72 pixel_bounds(SkIRect::MakeEmpty()),
73 dip_to_pixel_scale(1.0f) {
74 }
75
63 MacDesktopConfiguration::MacDesktopConfiguration() 76 MacDesktopConfiguration::MacDesktopConfiguration()
64 : bounds(SkIRect::MakeEmpty()), 77 : bounds(SkIRect::MakeEmpty()),
65 pixel_bounds(SkIRect::MakeEmpty()), 78 pixel_bounds(SkIRect::MakeEmpty()),
66 dip_to_pixel_scale(1.0f) { 79 dip_to_pixel_scale(1.0f) {
67 } 80 }
68 81
69 MacDesktopConfiguration::~MacDesktopConfiguration() { 82 MacDesktopConfiguration::~MacDesktopConfiguration() {
70 } 83 }
71 84
72 // static 85 // static
73 MacDesktopConfiguration MacDesktopConfiguration::GetCurrent() { 86 MacDesktopConfiguration MacDesktopConfiguration::GetCurrent(Origin origin) {
74 MacDesktopConfiguration desktop_config; 87 MacDesktopConfiguration desktop_config;
75 88
76 NSArray* screens = [NSScreen screens]; 89 NSArray* screens = [NSScreen screens];
77 CHECK(screens != NULL); 90 CHECK(screens != NULL);
78 91
79 // Iterator over the monitors, adding the primary monitor and monitors whose 92 // Iterator over the monitors, adding the primary monitor and monitors whose
80 // DPI match that of the primary monitor. 93 // DPI match that of the primary monitor.
81 for (NSUInteger i = 0; i < [screens count]; ++i) { 94 for (NSUInteger i = 0; i < [screens count]; ++i) {
82 MacDisplayConfiguration display_config 95 MacDisplayConfiguration display_config
83 = GetConfigurationForScreen([screens objectAtIndex: i]); 96 = GetConfigurationForScreen([screens objectAtIndex: i]);
84 97
85 // Handling mixed-DPI is hard, so we only return displays that match the 98 // 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 99 // "primary" display's DPI. The primary display is always the first in the
87 // list returned by [NSScreen screens]. 100 // list returned by [NSScreen screens].
88 if (i == 0) { 101 if (i == 0) {
89 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale; 102 desktop_config.dip_to_pixel_scale = display_config.dip_to_pixel_scale;
90 } else if (desktop_config.dip_to_pixel_scale != 103 } else if (desktop_config.dip_to_pixel_scale !=
91 display_config.dip_to_pixel_scale) { 104 display_config.dip_to_pixel_scale) {
92 continue; 105 continue;
93 } 106 }
94 107
108 // Mac uses Cartesian (bottom-left origin) coordinates, so if the caller
109 // wants inverse Cartesian (top-left origin), secondary monitors need
110 // their coordinates inverting relative to the primary monitor's bounds.
111 if (i > 0 && origin == TopLeftOrigin) {
112 InvertRectYOrigin(desktop_config.displays[0].bounds,
113 &display_config.bounds);
114 InvertRectYOrigin(desktop_config.displays[0].pixel_bounds,
115 &display_config.pixel_bounds);
116 }
117
95 // Add the display to the configuration. 118 // Add the display to the configuration.
96 desktop_config.displays.push_back(display_config); 119 desktop_config.displays.push_back(display_config);
97 120
98 // Update the desktop bounds to account for this display. 121 // Update the desktop bounds to account for this display.
99 desktop_config.bounds.join(display_config.bounds); 122 desktop_config.bounds.join(display_config.bounds);
100 desktop_config.pixel_bounds.join(display_config.pixel_bounds); 123 desktop_config.pixel_bounds.join(display_config.pixel_bounds);
101 } 124 }
102 125
103 return desktop_config; 126 return desktop_config;
104 } 127 }
105 128
106 } // namespace media 129 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698