Skip to content

Commit

Permalink
Bug 1698946 p1: Add and maintain ClearType parameters gfxVars. r=jfkt…
Browse files Browse the repository at this point in the history
  • Loading branch information
bobowen committed Aug 5, 2021
1 parent ef0308e commit 58f7260
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 30 deletions.
6 changes: 6 additions & 0 deletions gfx/config/gfxVars.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class gfxVarReceiver;
_(UseOMTP, bool, false) \
_(AllowD3D11KeyedMutex, bool, false) \
_(SystemTextQuality, int32_t, 5 /* CLEARTYPE_QUALITY */) \
_(SystemTextClearTypeLevel, float, 1.0f) \
_(SystemTextEnhancedContrast, float, 1.0f) \
_(SystemTextGamma, float, 2.2f) \
_(SystemTextPixelGeometry, int32_t, 1 /* pixel geometry RGB */) \
_(SystemTextRenderingMode, int32_t, 0) \
_(SystemGDIGamma, float, 1.4f) \
_(LayersWindowRecordingPath, nsCString, nsCString()) \
_(RemoteCanvasEnabled, bool, false) \
_(UseDoubleBufferingWithCompositor, bool, false) \
Expand Down
1 change: 1 addition & 0 deletions gfx/ipc/GraphicsMessages.ipdlh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ union GfxVarValue
nsCString;
nsString;
int32_t;
float;
};

struct GfxVarUpdate
Expand Down
16 changes: 16 additions & 0 deletions gfx/thebes/gfxDWriteCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
#include <windows.h>
#include <dwrite.h>

#define GFX_CLEARTYPE_PARAMS "gfx.font_rendering.cleartype_params."
#define GFX_CLEARTYPE_PARAMS_GAMMA "gfx.font_rendering.cleartype_params.gamma"
#define GFX_CLEARTYPE_PARAMS_CONTRAST \
"gfx.font_rendering.cleartype_params.enhanced_contrast"
#define GFX_CLEARTYPE_PARAMS_LEVEL \
"gfx.font_rendering.cleartype_params.cleartype_level"
#define GFX_CLEARTYPE_PARAMS_STRUCTURE \
"gfx.font_rendering.cleartype_params.pixel_structure"
#define GFX_CLEARTYPE_PARAMS_MODE \
"gfx.font_rendering.cleartype_params.rendering_mode"

#define DISPLAY1_REGISTRY_KEY \
HKEY_CURRENT_USER, L"Software\\Microsoft\\Avalon.Graphics\\DISPLAY1"

#define ENHANCED_CONTRAST_VALUE_NAME L"EnhancedContrastLevel"

static inline DWRITE_FONT_STRETCH DWriteFontStretchFromStretch(
mozilla::FontStretch aStretch) {
if (aStretch == mozilla::FontStretch::UltraCondensed()) {
Expand Down
140 changes: 130 additions & 10 deletions gfx/thebes/gfxDWriteFonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#include "gfxDWriteFontList.h"
#include "gfxContext.h"
#include "gfxTextRun.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/Preferences.h"

#include "harfbuzz/hb.h"
#include "mozilla/FontPropertyTypes.h"
Expand Down Expand Up @@ -61,16 +63,12 @@ static BYTE GetSystemTextQuality() {
// "Retrieves a contrast value that is used in ClearType smoothing. Valid
// contrast values are from 1000 to 2200. The default value is 1400."
static FLOAT GetSystemGDIGamma() {
static FLOAT sGDIGamma = 0.0f;
if (!sGDIGamma) {
UINT value = 0;
if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &value, 0) ||
value < 1000 || value > 2200) {
value = 1400;
}
sGDIGamma = value / 1000.0f;
UINT value = 0;
if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &value, 0) ||
value < 1000 || value > 2200) {
value = 1400;
}
return sGDIGamma;
return value / 1000.0f;
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -96,11 +94,34 @@ gfxDWriteFont::gfxDWriteFont(const RefPtr<UnscaledFontDWrite>& aUnscaledFont,

gfxDWriteFont::~gfxDWriteFont() { delete mMetrics; }

void gfxDWriteFont::UpdateSystemTextQuality() {
/* static */
bool gfxDWriteFont::InitDWriteSupport() {
if (!Factory::EnsureDWriteFactory()) {
return false;
}

if (XRE_IsParentProcess()) {
UpdateSystemTextVars();
}

return true;
}

/* static */
void gfxDWriteFont::UpdateSystemTextVars() {
MOZ_ASSERT(XRE_IsParentProcess());

BYTE newQuality = GetSystemTextQuality();
if (gfxVars::SystemTextQuality() != newQuality) {
gfxVars::SetSystemTextQuality(newQuality);
}

FLOAT newGDIGamma = GetSystemGDIGamma();
if (gfxVars::SystemGDIGamma() != newGDIGamma) {
gfxVars::SetSystemGDIGamma(newGDIGamma);
}

UpdateClearTypeVars();
}

void gfxDWriteFont::SystemTextQualityChanged() {
Expand All @@ -112,6 +133,105 @@ void gfxDWriteFont::SystemTextQualityChanged() {
gfxPlatform::ForceGlobalReflow();
}

/* static */
void gfxDWriteFont::UpdateClearTypeVars() {
MOZ_ASSERT(XRE_IsParentProcess());

if (!Factory::GetDWriteFactory()) {
return;
}

// First set sensible hard coded defaults.
float clearTypeLevel = 1.0f;
float enhancedContrast = 1.0f;
float gamma = 2.2f;
int pixelGeometry = DWRITE_PIXEL_GEOMETRY_RGB;
int renderingMode = DWRITE_RENDERING_MODE_DEFAULT;

// Override these from DWrite function if available.
RefPtr<IDWriteRenderingParams> defaultRenderingParams;
HRESULT hr = Factory::GetDWriteFactory()->CreateRenderingParams(
getter_AddRefs(defaultRenderingParams));
if (SUCCEEDED(hr) && defaultRenderingParams) {
clearTypeLevel = defaultRenderingParams->GetClearTypeLevel();

// For enhanced contrast, we only use the default if the user has set it
// in the registry (by using the ClearType Tuner).
// XXXbobowen it seems slightly odd that we do this and only for enhanced
// contrast, but this reproduces previous functionality from
// gfxWindowsPlatform::SetupClearTypeParams.
HKEY hKey;
LONG res = RegOpenKeyExW(DISPLAY1_REGISTRY_KEY, 0, KEY_READ, &hKey);
if (res == ERROR_SUCCESS) {
res = RegQueryValueExW(hKey, ENHANCED_CONTRAST_VALUE_NAME, nullptr,
nullptr, nullptr, nullptr);
if (res == ERROR_SUCCESS) {
enhancedContrast = defaultRenderingParams->GetEnhancedContrast();
}
RegCloseKey(hKey);
}

gamma = defaultRenderingParams->GetGamma();
pixelGeometry = defaultRenderingParams->GetPixelGeometry();
renderingMode = defaultRenderingParams->GetRenderingMode();
} else {
gfxWarning() << "Failed to create default rendering params";
}

// Finally override from prefs if valid values are set. If ClearType is
// turned off we just use the default params, this reproduces the previous
// functionality that was spread across gfxDWriteFont::GetScaledFont and
// gfxWindowsPlatform::SetupClearTypeParams, but it seems odd because the
// default params will still be the ClearType ones although we won't use the
// anti-alias for ClearType because of GetSystemDefaultAAMode.
if (gfxVars::SystemTextQuality() == CLEARTYPE_QUALITY) {
int32_t prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_LEVEL, -1);
if (prefInt >= 0 && prefInt <= 100) {
clearTypeLevel = float(prefInt / 100.0);
}

prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_CONTRAST, -1);
if (prefInt >= 0 && prefInt <= 1000) {
enhancedContrast = float(prefInt / 100.0);
}

prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_GAMMA, -1);
if (prefInt >= 1000 && prefInt <= 2200) {
gamma = float(prefInt / 1000.0);
}

prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_STRUCTURE, -1);
if (prefInt >= 0 && prefInt <= 2) {
pixelGeometry = prefInt;
}

prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_MODE, -1);
if (prefInt >= 0 && prefInt <= 5) {
renderingMode = prefInt;
}
}

if (gfxVars::SystemTextClearTypeLevel() != clearTypeLevel) {
gfxVars::SetSystemTextClearTypeLevel(clearTypeLevel);
}

if (gfxVars::SystemTextEnhancedContrast() != enhancedContrast) {
gfxVars::SetSystemTextEnhancedContrast(enhancedContrast);
}

if (gfxVars::SystemTextGamma() != gamma) {
gfxVars::SetSystemTextGamma(gamma);
}

if (gfxVars::SystemTextPixelGeometry() != pixelGeometry) {
gfxVars::SetSystemTextPixelGeometry(pixelGeometry);
}

if (gfxVars::SystemTextRenderingMode() != renderingMode) {
gfxVars::SetSystemTextRenderingMode(renderingMode);
}
}

UniquePtr<gfxFont> gfxDWriteFont::CopyWithAntialiasOption(
AntialiasOption anAAOption) {
auto entry = static_cast<gfxDWriteFontEntry*>(mFontEntry.get());
Expand Down
8 changes: 7 additions & 1 deletion gfx/thebes/gfxDWriteFonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ class gfxDWriteFont final : public gfxFont {
AntialiasOption = kAntialiasDefault);
~gfxDWriteFont();

static void UpdateSystemTextQuality();
static bool InitDWriteSupport();

// These Update functions update gfxVars with font settings, they must only be
// called in the parent process.
static void UpdateSystemTextVars();
static void UpdateClearTypeVars();

static void SystemTextQualityChanged();

mozilla::UniquePtr<gfxFont> CopyWithAntialiasOption(
Expand Down
21 changes: 4 additions & 17 deletions gfx/thebes/gfxWindowsPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,6 @@ class GfxD2DVramReporter final : public nsIMemoryReporter {

NS_IMPL_ISUPPORTS(GfxD2DVramReporter, nsIMemoryReporter)

#define GFX_CLEARTYPE_PARAMS "gfx.font_rendering.cleartype_params."
#define GFX_CLEARTYPE_PARAMS_GAMMA "gfx.font_rendering.cleartype_params.gamma"
#define GFX_CLEARTYPE_PARAMS_CONTRAST \
"gfx.font_rendering.cleartype_params.enhanced_contrast"
#define GFX_CLEARTYPE_PARAMS_LEVEL \
"gfx.font_rendering.cleartype_params.cleartype_level"
#define GFX_CLEARTYPE_PARAMS_STRUCTURE \
"gfx.font_rendering.cleartype_params.pixel_structure"
#define GFX_CLEARTYPE_PARAMS_MODE \
"gfx.font_rendering.cleartype_params.rendering_mode"

class GPUAdapterReporter final : public nsIMemoryReporter {
// Callers must Release the DXGIAdapter after use or risk mem-leak
static bool GetDXGIAdapter(IDXGIAdapter** aDXGIAdapter) {
Expand Down Expand Up @@ -408,7 +397,7 @@ bool gfxWindowsPlatform::CanUseHardwareVideoDecoding() {

bool gfxWindowsPlatform::InitDWriteSupport() {
mozilla::ScopedGfxFeatureReporter reporter("DWrite");
if (!Factory::EnsureDWriteFactory()) {
if (!gfxDWriteFont::InitDWriteSupport()) {
return false;
}

Expand Down Expand Up @@ -1074,6 +1063,9 @@ void gfxWindowsPlatform::FontsPrefsChanged(const char* aPref) {
if (aPref &&
!strncmp(GFX_CLEARTYPE_PARAMS, aPref, strlen(GFX_CLEARTYPE_PARAMS))) {
SetupClearTypeParams();
if (XRE_IsParentProcess()) {
gfxDWriteFont::UpdateClearTypeVars();
}
} else {
clearTextFontCaches = false;
}
Expand All @@ -1086,11 +1078,6 @@ void gfxWindowsPlatform::FontsPrefsChanged(const char* aPref) {
}
}

#define DISPLAY1_REGISTRY_KEY \
HKEY_CURRENT_USER, L"Software\\Microsoft\\Avalon.Graphics\\DISPLAY1"

#define ENHANCED_CONTRAST_VALUE_NAME L"EnhancedContrastLevel"

void gfxWindowsPlatform::SetupClearTypeParams() {
if (DWriteEnabled()) {
// any missing prefs will default to invalid (-1) and be ignored;
Expand Down
4 changes: 2 additions & 2 deletions widget/windows/nsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5249,7 +5249,7 @@ bool nsWindow::ProcessMessage(UINT msg, WPARAM& wParam, LPARAM& lParam,
}
if (wParam == SPI_SETFONTSMOOTHING ||
wParam == SPI_SETFONTSMOOTHINGTYPE) {
gfxDWriteFont::UpdateSystemTextQuality();
gfxDWriteFont::UpdateSystemTextVars();
break;
}
if (lParam) {
Expand Down Expand Up @@ -5443,7 +5443,7 @@ bool nsWindow::ProcessMessage(UINT msg, WPARAM& wParam, LPARAM& lParam,
* ClearType changes often don't send a WM_SETTINGCHANGE message. But they
* do seem to always send a WM_NCPAINT message, so let's update on that.
*/
gfxDWriteFont::UpdateSystemTextQuality();
gfxDWriteFont::UpdateSystemTextVars();

/*
* Reset the non-client paint region so that it excludes the
Expand Down

0 comments on commit 58f7260

Please sign in to comment.