Bug 1855360 - Fix the skip-if syntax. a=bustage-fix
[gecko.git] / widget / Screen.cpp
blob71a16246248099286b21201f6094dc86c629071a
1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "Screen.h"
9 #include "mozilla/dom/DOMTypes.h"
10 #include "mozilla/dom/ScreenBinding.h"
11 #include "mozilla/Hal.h"
12 #include "mozilla/LookAndFeel.h"
13 #include "mozilla/StaticPrefs_layout.h"
15 namespace mozilla::widget {
17 NS_IMPL_ISUPPORTS(Screen, nsIScreen)
19 static hal::ScreenOrientation EffectiveOrientation(
20 hal::ScreenOrientation aOrientation, const LayoutDeviceIntRect& aRect) {
21 if (aOrientation == hal::ScreenOrientation::None) {
22 return aRect.Width() >= aRect.Height()
23 ? hal::ScreenOrientation::LandscapePrimary
24 : hal::ScreenOrientation::PortraitPrimary;
26 return aOrientation;
29 Screen::Screen(LayoutDeviceIntRect aRect, LayoutDeviceIntRect aAvailRect,
30 uint32_t aPixelDepth, uint32_t aColorDepth,
31 uint32_t aRefreshRate, DesktopToLayoutDeviceScale aContentsScale,
32 CSSToLayoutDeviceScale aDefaultCssScale, float aDPI,
33 IsPseudoDisplay aIsPseudoDisplay,
34 hal::ScreenOrientation aOrientation,
35 OrientationAngle aOrientationAngle)
36 : mRect(aRect),
37 mAvailRect(aAvailRect),
38 mRectDisplayPix(RoundedToInt(aRect / aContentsScale)),
39 mAvailRectDisplayPix(RoundedToInt(aAvailRect / aContentsScale)),
40 mPixelDepth(aPixelDepth),
41 mColorDepth(aColorDepth),
42 mRefreshRate(aRefreshRate),
43 mContentsScale(aContentsScale),
44 mDefaultCssScale(aDefaultCssScale),
45 mDPI(aDPI),
46 mScreenOrientation(EffectiveOrientation(aOrientation, aRect)),
47 mOrientationAngle(aOrientationAngle),
48 mIsPseudoDisplay(aIsPseudoDisplay == IsPseudoDisplay::Yes) {}
50 Screen::Screen(const dom::ScreenDetails& aScreen)
51 : mRect(aScreen.rect()),
52 mAvailRect(aScreen.availRect()),
53 mRectDisplayPix(aScreen.rectDisplayPix()),
54 mAvailRectDisplayPix(aScreen.availRectDisplayPix()),
55 mPixelDepth(aScreen.pixelDepth()),
56 mColorDepth(aScreen.colorDepth()),
57 mRefreshRate(aScreen.refreshRate()),
58 mContentsScale(aScreen.contentsScaleFactor()),
59 mDefaultCssScale(aScreen.defaultCSSScaleFactor()),
60 mDPI(aScreen.dpi()),
61 mScreenOrientation(aScreen.orientation()),
62 mOrientationAngle(aScreen.orientationAngle()),
63 mIsPseudoDisplay(aScreen.isPseudoDisplay()) {}
65 Screen::Screen(const Screen& aOther)
66 : mRect(aOther.mRect),
67 mAvailRect(aOther.mAvailRect),
68 mRectDisplayPix(aOther.mRectDisplayPix),
69 mAvailRectDisplayPix(aOther.mAvailRectDisplayPix),
70 mPixelDepth(aOther.mPixelDepth),
71 mColorDepth(aOther.mColorDepth),
72 mRefreshRate(aOther.mRefreshRate),
73 mContentsScale(aOther.mContentsScale),
74 mDefaultCssScale(aOther.mDefaultCssScale),
75 mDPI(aOther.mDPI),
76 mScreenOrientation(aOther.mScreenOrientation),
77 mOrientationAngle(aOther.mOrientationAngle),
78 mIsPseudoDisplay(aOther.mIsPseudoDisplay) {}
80 dom::ScreenDetails Screen::ToScreenDetails() const {
81 return dom::ScreenDetails(
82 mRect, mRectDisplayPix, mAvailRect, mAvailRectDisplayPix, mPixelDepth,
83 mColorDepth, mRefreshRate, mContentsScale, mDefaultCssScale, mDPI,
84 mScreenOrientation, mOrientationAngle, mIsPseudoDisplay);
87 NS_IMETHODIMP
88 Screen::GetRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
89 int32_t* aOutHeight) {
90 mRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
91 return NS_OK;
94 NS_IMETHODIMP
95 Screen::GetRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
96 int32_t* aOutWidth, int32_t* aOutHeight) {
97 mRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
98 return NS_OK;
101 NS_IMETHODIMP
102 Screen::GetAvailRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
103 int32_t* aOutHeight) {
104 mAvailRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
105 return NS_OK;
108 NS_IMETHODIMP
109 Screen::GetAvailRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
110 int32_t* aOutWidth, int32_t* aOutHeight) {
111 mAvailRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
112 return NS_OK;
115 NS_IMETHODIMP
116 Screen::GetColorGamut(dom::ScreenColorGamut* aScreenColorGamut) {
117 // TODO(zrhoffman, bug 1771373): Return a wider color gamut when one is
118 // available
119 *aScreenColorGamut = dom::ScreenColorGamut::Srgb;
120 return NS_OK;
123 NS_IMETHODIMP
124 Screen::GetPixelDepth(int32_t* aPixelDepth) {
125 *aPixelDepth = mPixelDepth;
126 return NS_OK;
129 NS_IMETHODIMP
130 Screen::GetColorDepth(int32_t* aColorDepth) {
131 *aColorDepth = mColorDepth;
132 return NS_OK;
135 NS_IMETHODIMP
136 Screen::GetContentsScaleFactor(double* aOutScale) {
137 *aOutScale = mContentsScale.scale;
138 return NS_OK;
141 CSSToLayoutDeviceScale Screen::GetCSSToLayoutDeviceScale(
142 IncludeOSZoom aIncludeOSZoom) const {
143 auto scale = CSSToLayoutDeviceScale(StaticPrefs::layout_css_devPixelsPerPx());
144 if (scale.scale <= 0.0) {
145 scale = mDefaultCssScale;
147 if (bool(aIncludeOSZoom)) {
148 scale.scale *= LookAndFeel::SystemZoomSettings().mFullZoom;
150 return scale;
153 NS_IMETHODIMP
154 Screen::GetDefaultCSSScaleFactor(double* aOutScale) {
155 *aOutScale = GetCSSToLayoutDeviceScale(IncludeOSZoom::Yes).scale;
156 return NS_OK;
159 NS_IMETHODIMP
160 Screen::GetDpi(float* aDPI) {
161 *aDPI = mDPI;
162 return NS_OK;
165 NS_IMETHODIMP
166 Screen::GetRefreshRate(int32_t* aRefreshRate) {
167 *aRefreshRate = mRefreshRate;
168 return NS_OK;
171 NS_IMETHODIMP
172 Screen::GetIsPseudoDisplay(bool* aIsPseudoDisplay) {
173 *aIsPseudoDisplay = mIsPseudoDisplay;
174 return NS_OK;
177 hal::ScreenOrientation Screen::GetDefaultOrientationType() const {
178 if (mRect.Width() >= mRect.Height()) {
179 if (mOrientationAngle == 0 || mOrientationAngle == 180) {
180 return hal::ScreenOrientation::LandscapePrimary;
182 return hal::ScreenOrientation::PortraitPrimary;
185 if (mOrientationAngle == 0 || mOrientationAngle == 180) {
186 return hal::ScreenOrientation::PortraitPrimary;
188 return hal::ScreenOrientation::LandscapePrimary;
191 } // namespace mozilla::widget