2 * Copyright (C) 2006-2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "PlatformScreen.h"
33 #import "HostWindow.h"
34 #import "ScreenProperties.h"
35 #import <ColorSync/ColorSync.h>
36 #import <pal/spi/cg/CoreGraphicsSPI.h>
37 #import <wtf/ProcessPrivilege.h>
40 bool CGDisplayUsesInvertedPolarity(void);
41 bool CGDisplayUsesForceToGray(void);
46 // These functions scale between screen and page coordinates because JavaScript/DOM operations
47 // assume that the screen and the page share the same coordinate system.
49 PlatformDisplayID displayID(NSScreen *screen)
51 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
52 return [[[screen deviceDescription] objectForKey:@"NSScreenNumber"] intValue];
55 static PlatformDisplayID displayID(Widget* widget)
60 auto* view = widget->root();
64 auto* hostWindow = view->hostWindow();
68 return hostWindow->displayID();
71 // Screen containing the menubar.
72 static NSScreen *firstScreen()
74 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
75 NSArray *screens = [NSScreen screens];
78 return [screens objectAtIndex:0];
81 static NSWindow *window(Widget* widget)
83 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
86 return widget->platformWidget().window;
89 static NSScreen *screen(Widget* widget)
91 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
92 // If the widget is in a window, use that, otherwise use the display ID from the host window.
93 // First case is for when the NSWindow is in the same process, second case for when it's not.
94 if (auto screenFromWindow = window(widget).screen)
95 return screenFromWindow;
96 return screen(displayID(widget));
99 static ScreenProperties& screenProperties()
101 static NeverDestroyed<ScreenProperties> screenProperties;
102 return screenProperties;
105 PlatformDisplayID primaryScreenDisplayID()
107 return screenProperties().primaryDisplayID;
110 ScreenProperties collectScreenProperties()
112 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
114 ScreenProperties screenProperties;
116 for (NSScreen *screen in [NSScreen screens]) {
117 auto displayID = WebCore::displayID(screen);
118 FloatRect screenAvailableRect = screen.visibleFrame;
119 screenAvailableRect.setY(NSMaxY(screen.frame) - (screenAvailableRect.y() + screenAvailableRect.height())); // flip
120 FloatRect screenRect = screen.frame;
122 RetainPtr<CGColorSpaceRef> colorSpace = screen.colorSpace.CGColorSpace;
124 int screenDepth = NSBitsPerPixelFromDepth(screen.depth);
125 int screenDepthPerComponent = NSBitsPerSampleFromDepth(screen.depth);
126 bool screenSupportsExtendedColor = [screen canRepresentDisplayGamut:NSDisplayGamutP3];
127 bool screenHasInvertedColors = CGDisplayUsesInvertedPolarity();
128 bool screenIsMonochrome = CGDisplayUsesForceToGray();
129 uint32_t displayMask = CGDisplayIDToOpenGLDisplayMask(displayID);
130 IORegistryGPUID gpuID = 0;
132 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
134 gpuID = gpuIDForDisplayMask(displayMask);
137 screenProperties.screenDataMap.set(displayID, ScreenData { screenAvailableRect, screenRect, colorSpace, screenDepth, screenDepthPerComponent, screenSupportsExtendedColor, screenHasInvertedColors, screenIsMonochrome, displayMask, gpuID });
139 if (!screenProperties.primaryDisplayID)
140 screenProperties.primaryDisplayID = displayID;
143 return screenProperties;
146 void setScreenProperties(const ScreenProperties& properties)
148 screenProperties() = properties;
151 static ScreenData screenData(PlatformDisplayID screendisplayID)
153 RELEASE_ASSERT(!screenProperties().screenDataMap.isEmpty());
155 // Return property of the first screen if the screen is not found in the map.
156 auto displayID = screendisplayID ? screendisplayID : primaryScreenDisplayID();
158 auto screenPropertiesForDisplay = screenProperties().screenDataMap.find(displayID);
159 if (screenPropertiesForDisplay != screenProperties().screenDataMap.end())
160 return screenPropertiesForDisplay->value;
163 // Last resort: use the first item in the screen list.
164 return screenProperties().screenDataMap.begin()->value;
167 uint32_t primaryOpenGLDisplayMask()
169 if (!screenProperties().screenDataMap.isEmpty())
170 return screenData(primaryScreenDisplayID()).displayMask;
175 uint32_t displayMaskForDisplay(PlatformDisplayID displayID)
177 if (!screenProperties().screenDataMap.isEmpty())
178 return screenData(displayID).displayMask;
180 ASSERT_NOT_REACHED();
184 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
185 IORegistryGPUID primaryGPUID()
187 return gpuIDForDisplay(screenProperties().primaryDisplayID);
190 IORegistryGPUID gpuIDForDisplay(PlatformDisplayID displayID)
192 #if ENABLE(WEBPROCESS_WINDOWSERVER_BLOCKING)
193 if (!screenProperties().screenDataMap.isEmpty())
194 return screenData(displayID).gpuID;
196 return gpuIDForDisplayMask(CGDisplayIDToOpenGLDisplayMask(displayID));
201 IORegistryGPUID gpuIDForDisplayMask(GLuint displayMask)
203 GLint numRenderers = 0;
204 CGLRendererInfoObj rendererInfo = nullptr;
205 CGLError error = CGLQueryRendererInfo(displayMask, &rendererInfo, &numRenderers);
206 if (!numRenderers || !rendererInfo || error != kCGLNoError)
209 // The 0th renderer should not be the software renderer.
211 error = CGLDescribeRenderer(rendererInfo, 0, kCGLRPAccelerated, &isAccelerated);
212 if (!isAccelerated || error != kCGLNoError) {
213 CGLDestroyRendererInfo(rendererInfo);
220 error = CGLDescribeRenderer(rendererInfo, 0, kCGLRPRegistryIDLow, &gpuIDLow);
221 if (error != kCGLNoError) {
222 CGLDestroyRendererInfo(rendererInfo);
226 error = CGLDescribeRenderer(rendererInfo, 0, kCGLRPRegistryIDHigh, &gpuIDHigh);
227 if (error != kCGLNoError) {
228 CGLDestroyRendererInfo(rendererInfo);
232 CGLDestroyRendererInfo(rendererInfo);
233 return (IORegistryGPUID) gpuIDHigh << 32 | gpuIDLow;
235 #endif // !__MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
237 static ScreenData getScreenProperties(Widget* widget)
239 return screenData(displayID(widget));
242 bool screenIsMonochrome(Widget* widget)
244 if (!screenProperties().screenDataMap.isEmpty())
245 return getScreenProperties(widget).screenIsMonochrome;
247 // This is a system-wide accessibility setting, same on all screens.
248 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
249 return CGDisplayUsesForceToGray();
252 bool screenHasInvertedColors()
254 if (!screenProperties().screenDataMap.isEmpty())
255 return screenData(primaryScreenDisplayID()).screenHasInvertedColors;
257 // This is a system-wide accessibility setting, same on all screens.
258 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
259 return CGDisplayUsesInvertedPolarity();
262 int screenDepth(Widget* widget)
264 if (!screenProperties().screenDataMap.isEmpty()) {
265 auto screenDepth = getScreenProperties(widget).screenDepth;
270 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
271 return NSBitsPerPixelFromDepth(screen(widget).depth);
274 int screenDepthPerComponent(Widget* widget)
276 if (!screenProperties().screenDataMap.isEmpty()) {
277 auto depthPerComponent = getScreenProperties(widget).screenDepthPerComponent;
278 ASSERT(depthPerComponent);
279 return depthPerComponent;
282 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
283 return NSBitsPerSampleFromDepth(screen(widget).depth);
286 FloatRect screenRectForDisplay(PlatformDisplayID displayID)
288 if (!screenProperties().screenDataMap.isEmpty()) {
289 auto screenRect = screenData(displayID).screenRect;
290 ASSERT(!screenRect.isEmpty());
294 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
295 return screen(displayID).frame;
298 FloatRect screenRectForPrimaryScreen()
300 return screenRectForDisplay(primaryScreenDisplayID());
303 FloatRect screenRect(Widget* widget)
305 if (!screenProperties().screenDataMap.isEmpty())
306 return getScreenProperties(widget).screenRect;
308 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
309 return toUserSpace([screen(widget) frame], window(widget));
312 FloatRect screenAvailableRect(Widget* widget)
314 if (!screenProperties().screenDataMap.isEmpty())
315 return getScreenProperties(widget).screenAvailableRect;
317 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
318 return toUserSpace([screen(widget) visibleFrame], window(widget));
321 NSScreen *screen(NSWindow *window)
323 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
324 return [window screen] ?: firstScreen();
327 NSScreen *screen(PlatformDisplayID displayID)
329 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
330 for (NSScreen *screen in [NSScreen screens]) {
331 if (WebCore::displayID(screen) == displayID)
334 return firstScreen();
337 CGColorSpaceRef screenColorSpace(Widget* widget)
339 if (!screenProperties().screenDataMap.isEmpty())
340 return getScreenProperties(widget).colorSpace.get();
342 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
343 return screen(widget).colorSpace.CGColorSpace;
346 bool screenSupportsExtendedColor(Widget* widget)
348 if (!screenProperties().screenDataMap.isEmpty())
349 return getScreenProperties(widget).screenSupportsExtendedColor;
351 ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
352 return [screen(widget) canRepresentDisplayGamut:NSDisplayGamutP3];
355 FloatRect toUserSpace(const NSRect& rect, NSWindow *destination)
357 FloatRect userRect = rect;
358 userRect.setY(NSMaxY([screen(destination) frame]) - (userRect.y() + userRect.height())); // flip
362 FloatRect toUserSpaceForPrimaryScreen(const NSRect& rect)
364 FloatRect userRect = rect;
365 userRect.setY(NSMaxY(screenRectForDisplay(primaryScreenDisplayID())) - (userRect.y() + userRect.height())); // flip
369 NSRect toDeviceSpace(const FloatRect& rect, NSWindow *source)
371 FloatRect deviceRect = rect;
372 deviceRect.setY(NSMaxY([screen(source) frame]) - (deviceRect.y() + deviceRect.height())); // flip
376 NSPoint flipScreenPoint(const NSPoint& screenPoint, NSScreen *screen)
378 NSPoint flippedPoint = screenPoint;
379 flippedPoint.y = NSMaxY([screen frame]) - flippedPoint.y;
383 } // namespace WebCore
385 #endif // PLATFORM(MAC)