Bug 1761003 [wpt PR 33324] - Add comment pointing to followup bug, and fix typos...
[gecko.git] / widget / Screen.cpp
blob07cb5a466e46c3289d094f15ce7fc1afb2c10c97
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/Hal.h"
11 #include "mozilla/StaticPrefs_layout.h"
13 namespace mozilla::widget {
15 NS_IMPL_ISUPPORTS(Screen, nsIScreen)
17 static hal::ScreenOrientation EffectiveOrientation(
18 hal::ScreenOrientation aOrientation, const LayoutDeviceIntRect& aRect) {
19 if (aOrientation == hal::ScreenOrientation::None) {
20 return aRect.Width() >= aRect.Height()
21 ? hal::ScreenOrientation::LandscapePrimary
22 : hal::ScreenOrientation::PortraitPrimary;
24 return aOrientation;
27 Screen::Screen(LayoutDeviceIntRect aRect, LayoutDeviceIntRect aAvailRect,
28 uint32_t aPixelDepth, uint32_t aColorDepth,
29 DesktopToLayoutDeviceScale aContentsScale,
30 CSSToLayoutDeviceScale aDefaultCssScale, float aDPI,
31 hal::ScreenOrientation aOrientation,
32 OrientationAngle aOrientationAngle)
33 : mRect(aRect),
34 mAvailRect(aAvailRect),
35 mRectDisplayPix(RoundedToInt(aRect / aContentsScale)),
36 mAvailRectDisplayPix(RoundedToInt(aAvailRect / aContentsScale)),
37 mPixelDepth(aPixelDepth),
38 mColorDepth(aColorDepth),
39 mContentsScale(aContentsScale),
40 mDefaultCssScale(aDefaultCssScale),
41 mDPI(aDPI),
42 mScreenOrientation(EffectiveOrientation(aOrientation, aRect)),
43 mOrientationAngle(aOrientationAngle) {}
45 Screen::Screen(const dom::ScreenDetails& aScreen)
46 : mRect(aScreen.rect()),
47 mAvailRect(aScreen.availRect()),
48 mRectDisplayPix(aScreen.rectDisplayPix()),
49 mAvailRectDisplayPix(aScreen.availRectDisplayPix()),
50 mPixelDepth(aScreen.pixelDepth()),
51 mColorDepth(aScreen.colorDepth()),
52 mContentsScale(aScreen.contentsScaleFactor()),
53 mDefaultCssScale(aScreen.defaultCSSScaleFactor()),
54 mDPI(aScreen.dpi()),
55 mScreenOrientation(aScreen.orientation()),
56 mOrientationAngle(aScreen.orientationAngle()) {}
58 Screen::Screen(const Screen& aOther)
59 : mRect(aOther.mRect),
60 mAvailRect(aOther.mAvailRect),
61 mRectDisplayPix(aOther.mRectDisplayPix),
62 mAvailRectDisplayPix(aOther.mAvailRectDisplayPix),
63 mPixelDepth(aOther.mPixelDepth),
64 mColorDepth(aOther.mColorDepth),
65 mContentsScale(aOther.mContentsScale),
66 mDefaultCssScale(aOther.mDefaultCssScale),
67 mDPI(aOther.mDPI),
68 mScreenOrientation(aOther.mScreenOrientation),
69 mOrientationAngle(aOther.mOrientationAngle) {}
71 dom::ScreenDetails Screen::ToScreenDetails() const {
72 return dom::ScreenDetails(mRect, mRectDisplayPix, mAvailRect,
73 mAvailRectDisplayPix, mPixelDepth, mColorDepth,
74 mContentsScale, mDefaultCssScale, mDPI,
75 mScreenOrientation, mOrientationAngle);
78 NS_IMETHODIMP
79 Screen::GetRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
80 int32_t* aOutHeight) {
81 mRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
82 return NS_OK;
85 NS_IMETHODIMP
86 Screen::GetRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
87 int32_t* aOutWidth, int32_t* aOutHeight) {
88 mRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
89 return NS_OK;
92 NS_IMETHODIMP
93 Screen::GetAvailRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
94 int32_t* aOutHeight) {
95 mAvailRect.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
96 return NS_OK;
99 NS_IMETHODIMP
100 Screen::GetAvailRectDisplayPix(int32_t* aOutLeft, int32_t* aOutTop,
101 int32_t* aOutWidth, int32_t* aOutHeight) {
102 mAvailRectDisplayPix.GetRect(aOutLeft, aOutTop, aOutWidth, aOutHeight);
103 return NS_OK;
106 NS_IMETHODIMP
107 Screen::GetPixelDepth(int32_t* aPixelDepth) {
108 *aPixelDepth = mPixelDepth;
109 return NS_OK;
112 NS_IMETHODIMP
113 Screen::GetColorDepth(int32_t* aColorDepth) {
114 *aColorDepth = mColorDepth;
115 return NS_OK;
118 NS_IMETHODIMP
119 Screen::GetContentsScaleFactor(double* aOutScale) {
120 *aOutScale = mContentsScale.scale;
121 return NS_OK;
124 NS_IMETHODIMP
125 Screen::GetDefaultCSSScaleFactor(double* aOutScale) {
126 double scale = StaticPrefs::layout_css_devPixelsPerPx();
127 if (scale > 0.0) {
128 *aOutScale = scale;
129 } else {
130 *aOutScale = mDefaultCssScale.scale;
132 return NS_OK;
135 NS_IMETHODIMP
136 Screen::GetDpi(float* aDPI) {
137 *aDPI = mDPI;
138 return NS_OK;
141 } // namespace mozilla::widget