Bug 1746870: part 1) Minorly extend documentation in <jsactors.rst>. r=hsivonen
[gecko.git] / widget / ScreenManager.cpp
blob1bc29ab664072babeda6867225c4e1a5f9ee37c2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 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 "ScreenManager.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/dom/ContentParent.h"
11 #include "mozilla/dom/DOMTypes.h"
12 #include "mozilla/Logging.h"
13 #include "mozilla/StaticPtr.h"
14 #ifdef MOZ_WAYLAND
15 # include "mozilla/WidgetUtilsGtk.h"
16 #endif /* MOZ_WAYLAND */
18 static mozilla::LazyLogModule sScreenLog("WidgetScreen");
20 namespace mozilla::widget {
22 NS_IMPL_ISUPPORTS(ScreenManager, nsIScreenManager)
24 ScreenManager::ScreenManager() = default;
26 ScreenManager::~ScreenManager() = default;
28 static StaticRefPtr<ScreenManager> sSingleton;
30 ScreenManager& ScreenManager::GetSingleton() {
31 if (!sSingleton) {
32 sSingleton = new ScreenManager();
33 ClearOnShutdown(&sSingleton);
35 return *sSingleton;
38 already_AddRefed<ScreenManager> ScreenManager::GetAddRefedSingleton() {
39 RefPtr<ScreenManager> sm = &GetSingleton();
40 return sm.forget();
43 void ScreenManager::SetHelper(UniquePtr<Helper> aHelper) {
44 mHelper = std::move(aHelper);
47 // static
48 void ScreenManager::Refresh(nsTArray<RefPtr<Screen>>&& aScreens) {
49 if (PastShutdownPhase(ShutdownPhase::XPCOMShutdown)) {
50 // We don't refresh screen data if starting XPCOM shutdown path.
51 // GetSingleton returns invalid data since it is freed.
52 return;
54 GetSingleton().RefreshInternal(std::move(aScreens));
57 void ScreenManager::RefreshInternal(nsTArray<RefPtr<Screen>>&& aScreens) {
58 MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refresh screens"));
60 mScreenList = std::move(aScreens);
62 CopyScreensToAllRemotesIfIsParent();
65 void ScreenManager::Refresh(nsTArray<mozilla::dom::ScreenDetails>&& aScreens) {
66 MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refresh screens from IPC"));
68 mScreenList.Clear();
69 for (auto& screen : aScreens) {
70 mScreenList.AppendElement(new Screen(screen));
73 CopyScreensToAllRemotesIfIsParent();
76 template <class Range>
77 void ScreenManager::CopyScreensToRemoteRange(Range aRemoteRange) {
78 AutoTArray<dom::ScreenDetails, 4> screens;
79 for (auto& screen : mScreenList) {
80 screens.AppendElement(screen->ToScreenDetails());
82 for (auto cp : aRemoteRange) {
83 MOZ_LOG(sScreenLog, LogLevel::Debug,
84 ("Send screens to [Pid %d]", cp->Pid()));
85 if (!cp->SendRefreshScreens(screens)) {
86 MOZ_LOG(sScreenLog, LogLevel::Error,
87 ("SendRefreshScreens to [Pid %d] failed", cp->Pid()));
92 void ScreenManager::CopyScreensToRemote(dom::ContentParent* aContentParent) {
93 MOZ_ASSERT(aContentParent);
94 MOZ_ASSERT(XRE_IsParentProcess());
96 auto range = {aContentParent};
97 CopyScreensToRemoteRange(range);
100 void ScreenManager::CopyScreensToAllRemotesIfIsParent() {
101 if (XRE_IsContentProcess()) {
102 return;
105 MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refreshing all ContentParents"));
107 CopyScreensToRemoteRange(
108 dom::ContentParent::AllProcesses(dom::ContentParent::eLive));
111 // Returns the screen that contains the rectangle. If the rect overlaps
112 // multiple screens, it picks the screen with the greatest area of intersection.
114 // The coordinates are in desktop pixels.
116 NS_IMETHODIMP
117 ScreenManager::ScreenForRect(int32_t aX, int32_t aY, int32_t aWidth,
118 int32_t aHeight, nsIScreen** aOutScreen) {
119 #if defined(MOZ_WAYLAND) && defined(MOZ_LOGGING)
120 static bool inWayland = mozilla::widget::GdkIsWaylandDisplay();
121 if (inWayland) {
122 MOZ_LOG(sScreenLog, LogLevel::Warning,
123 ("Getting screen in wayland, primary display will be returned."));
125 #endif
127 if (mScreenList.IsEmpty()) {
128 MOZ_LOG(sScreenLog, LogLevel::Warning,
129 ("No screen available. This can happen in xpcshell."));
130 RefPtr<Screen> ret = new Screen(
131 LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0,
132 DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */);
133 ret.forget(aOutScreen);
134 return NS_OK;
137 // Optimize for the common case. If the number of screens is only
138 // one then just return the primary screen.
139 if (mScreenList.Length() == 1) {
140 return GetPrimaryScreen(aOutScreen);
143 // which screen should we return?
144 Screen* which = mScreenList[0].get();
146 // walk the list of screens and find the one that has the most
147 // surface area.
148 uint32_t area = 0;
149 DesktopIntRect windowRect(aX, aY, aWidth, aHeight);
150 for (auto& screen : mScreenList) {
151 int32_t x, y, width, height;
152 x = y = width = height = 0;
153 screen->GetRectDisplayPix(&x, &y, &width, &height);
154 // calculate the surface area
155 DesktopIntRect screenRect(x, y, width, height);
156 screenRect.IntersectRect(screenRect, windowRect);
157 uint32_t tempArea = screenRect.Area();
158 if (tempArea > area) {
159 which = screen.get();
160 area = tempArea;
164 // If the rect intersects one or more screen,
165 // return the screen that has the largest intersection.
166 if (area > 0) {
167 RefPtr<Screen> ret = which;
168 ret.forget(aOutScreen);
169 return NS_OK;
172 // If the rect does not intersect a screen, find
173 // a screen that is nearest to the rect.
174 uint32_t distance = UINT32_MAX;
175 for (auto& screen : mScreenList) {
176 int32_t x, y, width, height;
177 x = y = width = height = 0;
178 screen->GetRectDisplayPix(&x, &y, &width, &height);
180 uint32_t distanceX = 0;
181 if (aX > (x + width)) {
182 distanceX = aX - (x + width);
183 } else if ((aX + aWidth) < x) {
184 distanceX = x - (aX + aWidth);
187 uint32_t distanceY = 0;
188 if (aY > (y + height)) {
189 distanceY = aY - (y + height);
190 } else if ((aY + aHeight) < y) {
191 distanceY = y - (aY + aHeight);
194 uint32_t tempDistance = distanceX * distanceX + distanceY * distanceY;
195 if (tempDistance < distance) {
196 which = screen.get();
197 distance = tempDistance;
198 if (distance == 0) {
199 break;
204 RefPtr<Screen> ret = which;
205 ret.forget(aOutScreen);
206 return NS_OK;
209 // The screen with the menubar/taskbar. This shouldn't be needed very
210 // often.
212 NS_IMETHODIMP
213 ScreenManager::GetPrimaryScreen(nsIScreen** aPrimaryScreen) {
214 if (mScreenList.IsEmpty()) {
215 MOZ_LOG(sScreenLog, LogLevel::Warning,
216 ("No screen available. This can happen in xpcshell."));
217 RefPtr<Screen> ret = new Screen(
218 LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0,
219 DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */);
220 ret.forget(aPrimaryScreen);
221 return NS_OK;
224 RefPtr<Screen> ret = mScreenList[0];
225 ret.forget(aPrimaryScreen);
226 return NS_OK;
229 NS_IMETHODIMP
230 ScreenManager::GetTotalScreenPixels(int64_t* aTotalScreenPixels) {
231 MOZ_ASSERT(aTotalScreenPixels);
233 if (mScreenList.IsEmpty()) {
234 MOZ_LOG(sScreenLog, LogLevel::Warning,
235 ("No screen available. This can happen in xpcshell."));
236 *aTotalScreenPixels = 0;
237 return NS_OK;
240 int64_t pixels = 0;
241 for (auto& screen : mScreenList) {
242 int32_t x, y, width, height;
243 x = y = width = height = 0;
244 screen->GetRect(&x, &y, &width, &height);
245 pixels += width * height;
248 *aTotalScreenPixels = pixels;
249 return NS_OK;
252 } // namespace mozilla::widget