From 0be02f6de017fcfa3764960d1731a1a883b239dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 17 Jan 2017 17:21:00 +0700 Subject: [PATCH] Improve smallwindow detection, code cleanup. --- Core/Core.cpp | 28 ++++++++++++++++++++++------ Core/Core.h | 2 +- Windows/MainWindow.cpp | 11 +---------- ext/native/gfx_es2/draw_text.cpp | 32 +++++++++++++++++++------------- ext/native/gfx_es2/draw_text.h | 2 ++ 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/Core/Core.cpp b/Core/Core.cpp index aa0104d58e..5c2ae47f8e 100644 --- a/Core/Core.cpp +++ b/Core/Core.cpp @@ -130,22 +130,38 @@ bool Core_GetPowerSaving() { return powerSaving; } +static int ScreenDPI() { + HDC screenDC = GetDC(nullptr); + int dotsPerInch = GetDeviceCaps(screenDC, LOGPIXELSY); + ReleaseDC(nullptr, screenDC); + return dotsPerInch; +} + +static bool IsWindowSmall(int pixelWidth, int pixelHeight) { + // Can't take this from config as it will not be set if windows is maximized. + int w = (int)(pixelWidth * g_dpi_scale); + int h = (int)(pixelHeight * g_dpi_scale); + return g_Config.IsPortrait() ? (h < 480 + 80) : (w < 480 + 80); +} + // TODO: Feels like this belongs elsewhere. -bool UpdateScreenScale(int width, int height, bool smallWindow) { +bool UpdateScreenScale(int width, int height) { + bool smallWindow; #ifdef _WIN32 // Use legacy DPI handling, because we still compile as XP compatible we don't get the new SDK, unless // we do unholy tricks. - HDC screenDC = GetDC(nullptr); - double hPixelsPerInch = GetDeviceCaps(screenDC, LOGPIXELSY); - ReleaseDC(nullptr, screenDC); - g_dpi = hPixelsPerInch; + g_dpi = ScreenDPI(); g_dpi_scale = 96.0f / g_dpi; + smallWindow = IsWindowSmall(width, height); #else g_dpi = 96; g_dpi_scale = 1.0f; #endif - + if (smallWindow) { + g_dpi /= 2; + g_dpi_scale *= 2.0f; + } pixel_in_dps = 1.0f / g_dpi_scale; int new_dp_xres = width * g_dpi_scale; diff --git a/Core/Core.h b/Core/Core.h index d21ec4847a..889e05ccbe 100644 --- a/Core/Core.h +++ b/Core/Core.h @@ -47,7 +47,7 @@ bool Core_IsInactive(); void Core_WaitInactive(); void Core_WaitInactive(int milliseconds); -bool UpdateScreenScale(int width, int height, bool smallWindow); +bool UpdateScreenScale(int width, int height); // Don't run the core when minimized etc. void Core_NotifyWindowHidden(bool hidden); diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index 010f57ed79..19c78593e6 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -204,15 +204,6 @@ namespace MainWindow rcOuter.top = g_Config.iWindowY; } - static bool IsWindowSmall() { - // Can't take this from config as it will not be set if windows is maximized. - RECT rc; - GetWindowRect(hwndMain, &rc); - int width = (int)((rc.right - rc.left) * g_dpi_scale); - int height = (int)((rc.bottom - rc.top) * g_dpi_scale); - return g_Config.IsPortrait() ? (height < 480 + 80) : (width < 480 + 80); - } - void SetWindowSize(int zoom) { AssertCurrentThreadName("Main"); RECT rc, rcOuter; @@ -281,7 +272,7 @@ namespace MainWindow PSP_CoreParameter().pixelHeight = height; } - if (UpdateScreenScale(width, height, IsWindowSmall())) { + if (UpdateScreenScale(width, height)) { NativeMessageReceived("gpu resized", ""); } diff --git a/ext/native/gfx_es2/draw_text.cpp b/ext/native/gfx_es2/draw_text.cpp index bd85729c49..52810185fc 100644 --- a/ext/native/gfx_es2/draw_text.cpp +++ b/ext/native/gfx_es2/draw_text.cpp @@ -55,6 +55,7 @@ TextDrawer::TextDrawer(Draw::DrawContext *thin3d) : thin3d_(thin3d), ctx_(nullpt // These probably shouldn't be state. fontScaleX_ = 1.0f; fontScaleY_ = 1.0f; + last_dpi_scale_ = g_dpi_scale; ctx_ = new TextDrawerContext(); ctx_->hDC = CreateCompatibleDC(NULL); @@ -75,12 +76,7 @@ TextDrawer::TextDrawer(Draw::DrawContext *thin3d) : thin3d_(thin3d), ctx_(nullpt } TextDrawer::~TextDrawer() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); + ClearCache(); for (auto iter = fontMap_.begin(); iter != fontMap_.end(); ++iter) { DeleteObject(iter->second->hFont); @@ -304,12 +300,7 @@ TextDrawer::TextDrawer(Draw::DrawContext *thin3d) : thin3d_(thin3d), ctx_(NULL) } TextDrawer::~TextDrawer() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); + ClearCache(); } uint32_t TextDrawer::SetFont(const char *fontName, int size, int flags) { @@ -441,6 +432,15 @@ void TextDrawer::DrawString(DrawBuffer &target, const char *str, float x, float #endif +void TextDrawer::ClearCache() { + for (auto &iter : cache_) { + if (iter.second->texture) + iter.second->texture->Release(); + } + cache_.clear(); + sizeCache_.clear(); +} + void TextDrawer::WrapString(std::string &out, const char *str, float maxW) { TextDrawerWordWrapper wrapper(this, str, maxW); out = wrapper.Wrapped(); @@ -476,7 +476,13 @@ void TextDrawer::DrawStringRect(DrawBuffer &target, const char *str, const Bound void TextDrawer::OncePerFrame() { frameCount_++; - // Use a prime number to reduce clashing with other rhythms + // If DPI changed (small-mode, future proper monitor DPI support), drop everything. + if (g_dpi_scale != last_dpi_scale_) { + last_dpi_scale_ = g_dpi_scale; + ClearCache(); + } + + // Drop old strings. Use a prime number to reduce clashing with other rhythms if (frameCount_ % 23 == 0) { for (auto iter = cache_.begin(); iter != cache_.end();) { if (frameCount_ - iter->second->lastUsedFrame > 100) { diff --git a/ext/native/gfx_es2/draw_text.h b/ext/native/gfx_es2/draw_text.h index f210d62fc7..dd910bf8fb 100644 --- a/ext/native/gfx_es2/draw_text.h +++ b/ext/native/gfx_es2/draw_text.h @@ -68,11 +68,13 @@ public: private: Draw::DrawContext *thin3d_; + void ClearCache(); void WrapString(std::string &out, const char *str, float maxWidth); int frameCount_; float fontScaleX_; float fontScaleY_; + float last_dpi_scale_; TextDrawerContext *ctx_; #ifdef USING_QT_UI