Bug 1874684 - Part 20: Tag stack classes with MOZ_STACK_CLASS. r=allstarschh
[gecko.git] / widget / Screen.cpp
blobccc78a9413f11707492ff70938175da0d70dcdff
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, IsHDR aIsHDR,
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),
49 mIsHDR(aIsHDR == IsHDR::Yes) {}
51 Screen::Screen(const dom::ScreenDetails& aScreen)
52 : mRect(aScreen.rect()),
53 mAvailRect(aScreen.availRect()),
54 mRectDisplayPix(aScreen.rectDisplayPix()),
55 mAvailRectDisplayPix(aScreen.availRectDisplayPix()),
56 mPixelDepth(aScreen.pixelDepth()),
57 mColorDepth(aScreen.colorDepth()),
58 mRefreshRate(aScreen.refreshRate()),
59 mContentsScale(aScreen.contentsScaleFactor()),
60 mDefaultCssScale(aScreen.defaultCSSScaleFactor()),
61 mDPI(aScreen.dpi()),
62 mScreenOrientation(aScreen.orientation()),
63 mOrientationAngle(aScreen.orientationAngle()),
64 mIsPseudoDisplay(aScreen.isPseudoDisplay()),
65 mIsHDR(aScreen.isHDR()) {}
67 Screen::Screen(const Screen& aOther)
68 : mRect(aOther.mRect),
69 mAvailRect(aOther.mAvailRect),
70 mRectDisplayPix(aOther.mRectDisplayPix),
71 mAvailRectDisplayPix(aOther.mAvailRectDisplayPix),
72 mPixelDepth(aOther.mPixelDepth),
73 mColorDepth(aOther.mColorDepth),
74 mRefreshRate(aOther.mRefreshRate),
75 mContentsScale(aOther.mContentsScale),
76 mDefaultCssScale(aOther.mDefaultCssScale),
77 mDPI(aOther.mDPI),
78 mScreenOrientation(aOther.mScreenOrientation),
79 mOrientationAngle(aOther.mOrientationAngle),
80 mIsPseudoDisplay(aOther.mIsPseudoDisplay),
81 mIsHDR(aOther.mIsHDR) {}
83 dom::ScreenDetails Screen::ToScreenDetails() const {
84 return dom::ScreenDetails(
85 mRect, mRectDisplayPix, mAvailRect, mAvailRectDisplayPix, mPixelDepth,
86 mColorDepth, mRefreshRate, mContentsScale, mDefaultCssScale, mDPI,
87 mScreenOrientation, mOrientationAngle, mIsPseudoDisplay, mIsHDR);
90 NS_IMETHODIMP
91 Screen::GetRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
92 int32_t* aOutHeight) {
93 mRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
94 return NS_OK;
97 NS_IMETHODIMP
98 Screen::GetRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
99 int32_t* aOutWidth, int32_t* aOutHeight) {
100 mRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
101 return NS_OK;
104 NS_IMETHODIMP
105 Screen::GetAvailRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
106 int32_t* aOutHeight) {
107 mAvailRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
108 return NS_OK;
111 NS_IMETHODIMP
112 Screen::GetAvailRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
113 int32_t* aOutWidth, int32_t* aOutHeight) {
114 mAvailRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
115 return NS_OK;
118 NS_IMETHODIMP
119 Screen::GetColorGamut(dom::ScreenColorGamut* aScreenColorGamut) {
120 // TODO(zrhoffman, bug 1771373): Return a wider color gamut when one is
121 // available
122 *aScreenColorGamut = dom::ScreenColorGamut::Srgb;
123 return NS_OK;
126 NS_IMETHODIMP
127 Screen::GetPixelDepth(int32_t* aPixelDepth) {
128 *aPixelDepth = mPixelDepth;
129 return NS_OK;
132 NS_IMETHODIMP
133 Screen::GetColorDepth(int32_t* aColorDepth) {
134 *aColorDepth = mColorDepth;
135 return NS_OK;
138 NS_IMETHODIMP
139 Screen::GetContentsScaleFactor(double* aOutScale) {
140 *aOutScale = mContentsScale.scale;
141 return NS_OK;
144 CSSToLayoutDeviceScale Screen::GetCSSToLayoutDeviceScale(
145 IncludeOSZoom aIncludeOSZoom) const {
146 auto scale = CSSToLayoutDeviceScale(StaticPrefs::layout_css_devPixelsPerPx());
147 if (scale.scale <= 0.0) {
148 scale = mDefaultCssScale;
150 if (bool(aIncludeOSZoom)) {
151 scale.scale *= LookAndFeel::SystemZoomSettings().mFullZoom;
153 return scale;
156 NS_IMETHODIMP
157 Screen::GetDefaultCSSScaleFactor(double* aOutScale) {
158 *aOutScale = GetCSSToLayoutDeviceScale(IncludeOSZoom::Yes).scale;
159 return NS_OK;
162 NS_IMETHODIMP
163 Screen::GetDpi(float* aDPI) {
164 *aDPI = mDPI;
165 return NS_OK;
168 NS_IMETHODIMP
169 Screen::GetRefreshRate(int32_t* aRefreshRate) {
170 *aRefreshRate = mRefreshRate;
171 return NS_OK;
174 NS_IMETHODIMP
175 Screen::GetIsPseudoDisplay(bool* aIsPseudoDisplay) {
176 *aIsPseudoDisplay = mIsPseudoDisplay;
177 return NS_OK;
180 hal::ScreenOrientation Screen::GetDefaultOrientationType() const {
181 if (mRect.Width() >= mRect.Height()) {
182 if (mOrientationAngle == 0 || mOrientationAngle == 180) {
183 return hal::ScreenOrientation::LandscapePrimary;
185 return hal::ScreenOrientation::PortraitPrimary;
188 if (mOrientationAngle == 0 || mOrientationAngle == 180) {
189 return hal::ScreenOrientation::PortraitPrimary;
191 return hal::ScreenOrientation::LandscapePrimary;
194 } // namespace mozilla::widget