Skip to content

Commit

Permalink
Bug 1747677 - Add utility method to convert to ScreenConfiguration. r…
Browse files Browse the repository at this point in the history
…=gsvelto,geckoview-reviewers,calu

Gecko uses the redundant code from nsIScreen to SCreenConfiguration. So we
should add a convert method for newer platform implementations.

Differential Revision: https://phabricator.services.mozilla.com/D134698
  • Loading branch information
makotokato committed Dec 30, 2021
1 parent 17388f5 commit f8c3f43
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 80 deletions.
34 changes: 6 additions & 28 deletions hal/android/AndroidHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
#include "mozilla/dom/network/Constants.h"
#include "mozilla/java/GeckoAppShellWrappers.h"
#include "mozilla/java/GeckoRuntimeWrappers.h"
#include "nsIScreenManager.h"
#include "mozilla/widget/ScreenManager.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"

using namespace mozilla::dom;
using namespace mozilla::hal;
Expand Down Expand Up @@ -95,33 +94,12 @@ void GetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration) {
return;
}

nsresult rv;
nsCOMPtr<nsIScreenManager> screenMgr =
do_GetService("@mozilla.org/gfx/screenmanager;1", &rv);
if (NS_FAILED(rv)) {
NS_ERROR("Can't find nsIScreenManager!");
return;
}

int32_t colorDepth, pixelDepth;
int16_t angle;
hal::ScreenOrientation orientation;
nsCOMPtr<nsIScreen> screen;

int32_t rectX, rectY, rectWidth, rectHeight;

screenMgr->GetPrimaryScreen(getter_AddRefs(screen));

screen->GetRect(&rectX, &rectY, &rectWidth, &rectHeight);
screen->GetColorDepth(&colorDepth);
screen->GetPixelDepth(&pixelDepth);
orientation =
RefPtr<widget::Screen> screen =
widget::ScreenManager::GetSingleton().GetPrimaryScreen();
*aScreenConfiguration = screen->ToScreenConfiguration();
aScreenConfiguration->orientation() =
static_cast<hal::ScreenOrientation>(bridge->GetScreenOrientation());
angle = bridge->GetScreenAngle();

*aScreenConfiguration =
hal::ScreenConfiguration(nsIntRect(rectX, rectY, rectWidth, rectHeight),
orientation, angle, colorDepth, pixelDepth);
aScreenConfiguration->angle() = bridge->GetScreenAngle();
}

RefPtr<MozPromise<bool, bool, false>> LockScreenOrientation(
Expand Down
33 changes: 10 additions & 23 deletions hal/fallback/FallbackScreenConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,22 @@
#define mozilla_fallback_FallbackScreenConfiguration_h

#include "Hal.h"
#include "nsIScreenManager.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/widget/ScreenManager.h"

namespace mozilla {
namespace fallback {

inline void GetCurrentScreenConfiguration(
hal::ScreenConfiguration* aScreenConfiguration) {
nsresult rv;
nsCOMPtr<nsIScreenManager> screenMgr =
do_GetService("@mozilla.org/gfx/screenmanager;1", &rv);
if (NS_FAILED(rv)) {
NS_ERROR("Can't find nsIScreenManager!");
return;
}

int32_t colorDepth, pixelDepth, x, y, w, h;
hal::ScreenOrientation orientation;
nsCOMPtr<nsIScreen> screen;

screenMgr->GetPrimaryScreen(getter_AddRefs(screen));
screen->GetRect(&x, &y, &w, &h);
screen->GetColorDepth(&colorDepth);
screen->GetPixelDepth(&pixelDepth);
orientation = w >= h ? hal::eScreenOrientation_LandscapePrimary
: hal::eScreenOrientation_PortraitPrimary;

*aScreenConfiguration = hal::ScreenConfiguration(
nsIntRect(x, y, w, h), orientation, 0, colorDepth, pixelDepth);
RefPtr<widget::Screen> screen =
widget::ScreenManager::GetSingleton().GetPrimaryScreen();

*aScreenConfiguration = screen->ToScreenConfiguration();
aScreenConfiguration->orientation() =
aScreenConfiguration->rect().Width() >=
aScreenConfiguration->rect().Height()
? hal::eScreenOrientation_LandscapePrimary
: hal::eScreenOrientation_PortraitPrimary;
}

} // namespace fallback
Expand Down
7 changes: 7 additions & 0 deletions widget/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Screen.h"

#include "mozilla/dom/DOMTypes.h"
#include "mozilla/Hal.h"
#include "mozilla/StaticPrefs_layout.h"

namespace mozilla {
Expand Down Expand Up @@ -56,6 +57,12 @@ mozilla::dom::ScreenDetails Screen::ToScreenDetails() {
mColorDepth, mContentsScale, mDefaultCssScale, mDPI);
}

mozilla::hal::ScreenConfiguration Screen::ToScreenConfiguration() {
return mozilla::hal::ScreenConfiguration(
nsIntRect(mRect.x, mRect.y, mRect.width, mRect.height),
hal::eScreenOrientation_None, 0, mColorDepth, mPixelDepth);
}

NS_IMETHODIMP
Screen::GetRect(int32_t* aOutLeft, int32_t* aOutTop, int32_t* aOutWidth,
int32_t* aOutHeight) {
Expand Down
8 changes: 8 additions & 0 deletions widget/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace mozilla {
namespace dom {
class ScreenDetails;
} // namespace dom
namespace hal {
class ScreenConfiguration;
}
} // namespace mozilla

namespace mozilla {
Expand All @@ -34,6 +37,11 @@ class Screen final : public nsIScreen {

mozilla::dom::ScreenDetails ToScreenDetails();

// Convert to hal's ScreenConfiguration.
// NOTE: Since Screen doesn't have orientation and angle information,
// these can't be set.
mozilla::hal::ScreenConfiguration ToScreenConfiguration();

private:
virtual ~Screen() = default;

Expand Down
14 changes: 9 additions & 5 deletions widget/ScreenManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,24 @@ ScreenManager::ScreenForRect(int32_t aX, int32_t aY, int32_t aWidth,
// The screen with the menubar/taskbar. This shouldn't be needed very
// often.
//
NS_IMETHODIMP
ScreenManager::GetPrimaryScreen(nsIScreen** aPrimaryScreen) {
already_AddRefed<Screen> ScreenManager::GetPrimaryScreen() {
if (mScreenList.IsEmpty()) {
MOZ_LOG(sScreenLog, LogLevel::Warning,
("No screen available. This can happen in xpcshell."));
RefPtr<Screen> ret = new Screen(
LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0,
DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */);
ret.forget(aPrimaryScreen);
return NS_OK;
return ret.forget();
}

RefPtr<Screen> ret = mScreenList[0];
ret.forget(aPrimaryScreen);
return ret.forget();
}

NS_IMETHODIMP
ScreenManager::GetPrimaryScreen(nsIScreen** aPrimaryScreen) {
nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
screen.forget(aPrimaryScreen);
return NS_OK;
}

Expand Down
1 change: 1 addition & 0 deletions widget/ScreenManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ScreenManager final : public nsIScreenManager {
static void Refresh(nsTArray<RefPtr<Screen>>&& aScreens);
void Refresh(nsTArray<mozilla::dom::ScreenDetails>&& aScreens);
void CopyScreensToRemote(mozilla::dom::ContentParent* aContentParent);
already_AddRefed<Screen> GetPrimaryScreen();

private:
ScreenManager();
Expand Down
34 changes: 10 additions & 24 deletions widget/android/GeckoScreenOrientation.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include "nsAppShell.h"
#include "nsCOMPtr.h"
#include "nsIScreenManager.h"

#include "mozilla/Hal.h"
#include "mozilla/java/GeckoScreenOrientationNatives.h"
#include "mozilla/widget/ScreenManager.h"

namespace mozilla {

Expand All @@ -21,29 +21,15 @@ class GeckoScreenOrientation final

public:
static void OnOrientationChange(int16_t aOrientation, int16_t aAngle) {
nsCOMPtr<nsIScreenManager> screenMgr =
do_GetService("@mozilla.org/gfx/screenmanager;1");
nsCOMPtr<nsIScreen> screen;

if (!screenMgr ||
NS_FAILED(screenMgr->GetPrimaryScreen(getter_AddRefs(screen))) ||
!screen) {
return;
}

nsIntRect rect;
int32_t colorDepth, pixelDepth;

if (NS_FAILED(
screen->GetRect(&rect.x, &rect.y, &rect.width, &rect.height)) ||
NS_FAILED(screen->GetColorDepth(&colorDepth)) ||
NS_FAILED(screen->GetPixelDepth(&pixelDepth))) {
return;
}

hal::NotifyScreenConfigurationChange(hal::ScreenConfiguration(
rect, static_cast<hal::ScreenOrientation>(aOrientation), aAngle,
colorDepth, pixelDepth));
RefPtr<widget::Screen> screen =
widget::ScreenManager::GetSingleton().GetPrimaryScreen();
hal::ScreenConfiguration screenConfiguration =
screen->ToScreenConfiguration();
screenConfiguration.orientation() =
static_cast<hal::ScreenOrientation>(aOrientation);
screenConfiguration.angle() = aAngle;

hal::NotifyScreenConfigurationChange(screenConfiguration);
}
};

Expand Down

0 comments on commit f8c3f43

Please sign in to comment.