Skip to content

Commit

Permalink
Bug 1812938 - Part 2. FindScreen returns Screen instead of nsIScreen.…
Browse files Browse the repository at this point in the history
… r=emilio

`nsDeviceContext` isn't scriptable, it is unnecessary to return `nsIScreen`
for `FindScreen`.

Differential Revision: https://phabricator.services.mozilla.com/D168030
  • Loading branch information
makotokato committed Feb 6, 2023
1 parent f93698c commit 234e570
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 58 deletions.
78 changes: 29 additions & 49 deletions gfx/src/nsDeviceContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
#include "mozilla/Preferences.h" // for Preferences
#include "mozilla/Services.h" // for GetObserverService
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/mozalloc.h" // for operator new
#include "nsCRT.h" // for nsCRT
#include "nsDebug.h" // for NS_ASSERTION, etc
#include "nsFont.h" // for nsFont
#include "nsFontCache.h" // for nsFontCache
#include "nsFontMetrics.h" // for nsFontMetrics
#include "nsAtom.h" // for nsAtom, NS_Atomize
#include "mozilla/mozalloc.h" // for operator new
#include "mozilla/widget/Screen.h" // for Screen
#include "nsCRT.h" // for nsCRT
#include "nsDebug.h" // for NS_ASSERTION, etc
#include "nsFont.h" // for nsFont
#include "nsFontCache.h" // for nsFontCache
#include "nsFontMetrics.h" // for nsFontMetrics
#include "nsAtom.h" // for nsAtom, NS_Atomize
#include "nsID.h"
#include "nsIDeviceContextSpec.h" // for nsIDeviceContextSpec
#include "nsLanguageAtomService.h" // for nsLanguageAtomService
#include "nsIObserver.h" // for nsIObserver, etc
#include "nsIObserverService.h" // for nsIObserverService
#include "nsIScreen.h" // for nsIScreen
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
#include "nsISupportsUtils.h" // for NS_ADDREF, NS_RELEASE
#include "nsIWidget.h" // for nsIWidget, NS_NATIVE_WINDOW
Expand All @@ -53,12 +53,7 @@ nsDeviceContext::nsDeviceContext()
mFullZoom(1.0f),
mPrintingScale(1.0f),
mPrintingTranslate(gfxPoint(0, 0)),
mIsCurrentlyPrintingDoc(false)
#ifdef DEBUG
,
mIsInitialized(false)
#endif
{
mIsCurrentlyPrintingDoc(false) {
MOZ_ASSERT(NS_IsMainThread(), "nsDeviceContext created off main thread");
}

Expand Down Expand Up @@ -106,25 +101,18 @@ void nsDeviceContext::SetDPI() {
UpdateAppUnitsForFullZoom();
}

nsresult nsDeviceContext::Init(nsIWidget* aWidget) {
#ifdef DEBUG
void nsDeviceContext::Init(nsIWidget* aWidget) {
if (mIsInitialized && mWidget == aWidget) {
return;
}

// We can't assert |!mIsInitialized| here since EndSwapDocShellsForDocument
// re-initializes nsDeviceContext objects. We can only assert in
// InitForPrinting (below).
mIsInitialized = true;
#endif

nsresult rv = NS_OK;
if (mScreenManager && mWidget == aWidget) return rv;

mWidget = aWidget;
SetDPI();

if (mScreenManager) return rv;

mScreenManager = do_GetService("@mozilla.org/gfx/screenmanager;1", &rv);

return rv;
}

// XXX This is only for printing. We should make that obvious in the name.
Expand Down Expand Up @@ -177,11 +165,10 @@ already_AddRefed<gfxContext> nsDeviceContext::CreateRenderingContextCommon(
}

uint32_t nsDeviceContext::GetDepth() {
nsCOMPtr<nsIScreen> screen;
FindScreen(getter_AddRefs(screen));
RefPtr<widget::Screen> screen = FindScreen();
if (!screen) {
ScreenManager& screenManager = ScreenManager::GetSingleton();
screenManager.GetPrimaryScreen(getter_AddRefs(screen));
screen = screenManager.GetPrimaryScreen();
MOZ_ASSERT(screen);
}
int32_t depth = 0;
Expand All @@ -190,11 +177,10 @@ uint32_t nsDeviceContext::GetDepth() {
}

dom::ScreenColorGamut nsDeviceContext::GetColorGamut() {
nsCOMPtr<nsIScreen> screen;
FindScreen(getter_AddRefs(screen));
RefPtr<widget::Screen> screen = FindScreen();
if (!screen) {
auto& screenManager = ScreenManager::GetSingleton();
screenManager.GetPrimaryScreen(getter_AddRefs(screen));
screen = screenManager.GetPrimaryScreen();
MOZ_ASSERT(screen);
}
dom::ScreenColorGamut colorGamut;
Expand Down Expand Up @@ -353,9 +339,7 @@ void nsDeviceContext::ComputeClientRectUsingScreen(nsRect* outRect) {
// monitor case, we only need to do the computation if we haven't done it
// once already, and remember that we have because we're assured it won't
// change.
nsCOMPtr<nsIScreen> screen;
FindScreen(getter_AddRefs(screen));
if (screen) {
if (RefPtr<widget::Screen> screen = FindScreen()) {
*outRect = LayoutDeviceIntRect::ToAppUnits(screen->GetAvailRect(),
AppUnitsPerDevPixel());
}
Expand All @@ -367,9 +351,7 @@ void nsDeviceContext::ComputeFullAreaUsingScreen(nsRect* outRect) {
// monitor case, we only need to do the computation if we haven't done it
// once already, and remember that we have because we're assured it won't
// change.
nsCOMPtr<nsIScreen> screen;
FindScreen(getter_AddRefs(screen));
if (screen) {
if (RefPtr<widget::Screen> screen = FindScreen()) {
*outRect = LayoutDeviceIntRect::ToAppUnits(screen->GetRect(),
AppUnitsPerDevPixel());
mWidth = outRect->Width();
Expand All @@ -382,19 +364,19 @@ void nsDeviceContext::ComputeFullAreaUsingScreen(nsRect* outRect) {
//
// Determines which screen intersects the largest area of the given surface.
//
void nsDeviceContext::FindScreen(nsIScreen** outScreen) {
if (!mWidget || !mScreenManager) {
return;
already_AddRefed<widget::Screen> nsDeviceContext::FindScreen() {
if (!mWidget) {
return nullptr;
}

CheckDPIChange();

nsCOMPtr<nsIScreen> screen = mWidget->GetWidgetScreen();
screen.forget(outScreen);

if (!(*outScreen)) {
mScreenManager->GetPrimaryScreen(outScreen);
if (RefPtr<widget::Screen> screen = mWidget->GetWidgetScreen()) {
return screen.forget();
}

ScreenManager& screenManager = ScreenManager::GetSingleton();
return screenManager.GetPrimaryScreen();
}

bool nsDeviceContext::CalcPrintingSize() {
Expand Down Expand Up @@ -451,9 +433,7 @@ void nsDeviceContext::UpdateAppUnitsForFullZoom() {
}

DesktopToLayoutDeviceScale nsDeviceContext::GetDesktopToDeviceScale() {
nsCOMPtr<nsIScreen> screen;
FindScreen(getter_AddRefs(screen));
if (screen) {
if (RefPtr<widget::Screen> screen = FindScreen()) {
return screen->GetDesktopToLayoutDeviceScale();
}
return DesktopToLayoutDeviceScale(1.0);
Expand Down
19 changes: 10 additions & 9 deletions gfx/src/nsDeviceContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ class nsIScreenManager;
class nsIWidget;
struct nsRect;

namespace mozilla::dom {
namespace mozilla {
namespace dom {
enum class ScreenColorGamut : uint8_t;
} // namespace mozilla::dom
} // namespace dom
namespace widget {
class Screen;
} // namespace widget
} // namespace mozilla

class nsDeviceContext final {
public:
Expand All @@ -49,9 +54,8 @@ class nsDeviceContext final {
/**
* Initialize the device context from a widget
* @param aWidget a widget to initialize the device context from
* @return error status
*/
nsresult Init(nsIWidget* aWidget);
void Init(nsIWidget* aWidget);

/**
* Initialize the device context from a device context spec
Expand Down Expand Up @@ -257,7 +261,7 @@ class nsDeviceContext final {
void SetDPI();
void ComputeClientRectUsingScreen(nsRect* outRect);
void ComputeFullAreaUsingScreen(nsRect* outRect);
void FindScreen(nsIScreen** outScreen);
already_AddRefed<mozilla::widget::Screen> FindScreen();

// Return false if the surface is not right
bool CalcPrintingSize();
Expand All @@ -273,13 +277,10 @@ class nsDeviceContext final {
gfxPoint mPrintingTranslate;

nsCOMPtr<nsIWidget> mWidget;
nsCOMPtr<nsIScreenManager> mScreenManager;
nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec;
RefPtr<PrintTarget> mPrintTarget;
bool mIsCurrentlyPrintingDoc;
#ifdef DEBUG
bool mIsInitialized;
#endif
bool mIsInitialized = false;
};

#endif /* _NS_DEVICECONTEXT_H_ */

0 comments on commit 234e570

Please sign in to comment.