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/. */
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
;
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
)
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
),
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()),
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
),
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
);
88 Screen::GetRect(int32_t* aOutLeft
, int32_t* aOutTop
, int32_t* aOutWidth
,
89 int32_t* aOutHeight
) {
90 mRect
.GetRect(aOutLeft
, aOutTop
, aOutWidth
, aOutHeight
);
95 Screen::GetRectDisplayPix(int32_t* aOutLeft
, int32_t* aOutTop
,
96 int32_t* aOutWidth
, int32_t* aOutHeight
) {
97 mRectDisplayPix
.GetRect(aOutLeft
, aOutTop
, aOutWidth
, aOutHeight
);
102 Screen::GetAvailRect(int32_t* aOutLeft
, int32_t* aOutTop
, int32_t* aOutWidth
,
103 int32_t* aOutHeight
) {
104 mAvailRect
.GetRect(aOutLeft
, aOutTop
, aOutWidth
, aOutHeight
);
109 Screen::GetAvailRectDisplayPix(int32_t* aOutLeft
, int32_t* aOutTop
,
110 int32_t* aOutWidth
, int32_t* aOutHeight
) {
111 mAvailRectDisplayPix
.GetRect(aOutLeft
, aOutTop
, aOutWidth
, aOutHeight
);
116 Screen::GetColorGamut(dom::ScreenColorGamut
* aScreenColorGamut
) {
117 // TODO(zrhoffman, bug 1771373): Return a wider color gamut when one is
119 *aScreenColorGamut
= dom::ScreenColorGamut::Srgb
;
124 Screen::GetPixelDepth(int32_t* aPixelDepth
) {
125 *aPixelDepth
= mPixelDepth
;
130 Screen::GetColorDepth(int32_t* aColorDepth
) {
131 *aColorDepth
= mColorDepth
;
136 Screen::GetContentsScaleFactor(double* aOutScale
) {
137 *aOutScale
= mContentsScale
.scale
;
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
;
154 Screen::GetDefaultCSSScaleFactor(double* aOutScale
) {
155 *aOutScale
= GetCSSToLayoutDeviceScale(IncludeOSZoom::Yes
).scale
;
160 Screen::GetDpi(float* aDPI
) {
166 Screen::GetRefreshRate(int32_t* aRefreshRate
) {
167 *aRefreshRate
= mRefreshRate
;
172 Screen::GetIsPseudoDisplay(bool* aIsPseudoDisplay
) {
173 *aIsPseudoDisplay
= mIsPseudoDisplay
;
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