From 28cf69f67eaa88c699f2ebef3425929264506c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 1 Jun 2024 09:58:58 +0200 Subject: [PATCH] Remove duplicate code from the text rendering backends --- Common/Render/Text/draw_text.cpp | 43 ++++++++++++++++++++++++ Common/Render/Text/draw_text.h | 4 +-- Common/Render/Text/draw_text_android.cpp | 43 ------------------------ Common/Render/Text/draw_text_android.h | 3 -- Common/Render/Text/draw_text_cocoa.h | 3 -- Common/Render/Text/draw_text_cocoa.mm | 42 ----------------------- Common/Render/Text/draw_text_qt.cpp | 41 ---------------------- Common/Render/Text/draw_text_qt.h | 3 -- Common/Render/Text/draw_text_sdl.cpp | 42 ----------------------- Common/Render/Text/draw_text_sdl.h | 4 --- Common/Render/Text/draw_text_uwp.cpp | 41 ---------------------- Common/Render/Text/draw_text_uwp.h | 4 --- Common/Render/Text/draw_text_win.cpp | 41 ---------------------- Common/Render/Text/draw_text_win.h | 4 --- 14 files changed, 45 insertions(+), 273 deletions(-) diff --git a/Common/Render/Text/draw_text.cpp b/Common/Render/Text/draw_text.cpp index 657baca68a..8e5a1edc0a 100644 --- a/Common/Render/Text/draw_text.cpp +++ b/Common/Render/Text/draw_text.cpp @@ -164,6 +164,49 @@ bool TextDrawer::DrawStringBitmapRect(std::vector &bitmapData, TextStri return DrawStringBitmap(bitmapData, entry, texFormat, toDraw.c_str(), align, fullColor); } +void TextDrawer::ClearCache() { + for (auto &iter : cache_) { + if (iter.second->texture) + iter.second->texture->Release(); + } + cache_.clear(); + sizeCache_.clear(); + fontHash_ = 0; +} + +void TextDrawer::OncePerFrame() { + frameCount_++; + // If DPI changed (small-mode, future proper monitor DPI support), drop everything. + float newDpiScale = CalculateDPIScale(); + if (newDpiScale != dpiScale_) { + INFO_LOG(G3D, "DPI Scale changed (%f to %f) - wiping font cache (%d items)", dpiScale_, newDpiScale, (int)cache_.size()); + dpiScale_ = newDpiScale; + ClearCache(); + ClearFonts(); + } + + // 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) { + if (iter->second->texture) + iter->second->texture->Release(); + cache_.erase(iter++); + } else { + iter++; + } + } + + for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { + if (frameCount_ - iter->second->lastUsedFrame > 100) { + sizeCache_.erase(iter++); + } else { + iter++; + } + } + } +} + TextDrawer *TextDrawer::Create(Draw::DrawContext *draw) { TextDrawer *drawer = nullptr; #if defined(__LIBRETRO__) diff --git a/Common/Render/Text/draw_text.h b/Common/Render/Text/draw_text.h index 7db3f2ba0d..9f8f7c08e6 100644 --- a/Common/Render/Text/draw_text.h +++ b/Common/Render/Text/draw_text.h @@ -56,7 +56,7 @@ public: virtual bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) = 0; bool DrawStringBitmapRect(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, const Bounds &bounds, int align, bool fullColor); // Use for housekeeping like throwing out old strings. - virtual void OncePerFrame() = 0; + void OncePerFrame(); float CalculateDPIScale(); void SetForcedDPIScale(float dpi) { @@ -69,9 +69,9 @@ public: protected: TextDrawer(Draw::DrawContext *draw); + void ClearCache(); virtual bool SupportsColorEmoji() const = 0; - virtual void ClearCache() = 0; virtual void ClearFonts() = 0; void WrapString(std::string &out, std::string_view str, float maxWidth, int flags); diff --git a/Common/Render/Text/draw_text_android.cpp b/Common/Render/Text/draw_text_android.cpp index 59372bf98f..5f0a4bb264 100644 --- a/Common/Render/Text/draw_text_android.cpp +++ b/Common/Render/Text/draw_text_android.cpp @@ -189,47 +189,4 @@ void TextDrawerAndroid::ClearFonts() { fontMap_.clear(); // size is precomputed using dpiScale_. } -void TextDrawerAndroid::ClearCache() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); -} - -void TextDrawerAndroid::OncePerFrame() { - frameCount_++; - // If DPI changed (small-mode, future proper monitor DPI support), drop everything. - float newDpiScale = CalculateDPIScale(); - if (newDpiScale != dpiScale_) { - // TODO: Don't bother if it's a no-op (cache already empty) - INFO_LOG(G3D, "DPI Scale changed (%f to %f) - wiping font cache (%d items, %d fonts)", dpiScale_, newDpiScale, (int)cache_.size(), (int)fontMap_.size()); - dpiScale_ = newDpiScale; - ClearCache(); - ClearFonts(); - } - - // 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) { - if (iter->second->texture) - iter->second->texture->Release(); - cache_.erase(iter++); - } else { - iter++; - } - } - - for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { - if (frameCount_ - iter->second->lastUsedFrame > 100) { - sizeCache_.erase(iter++); - } else { - iter++; - } - } - } -} - #endif diff --git a/Common/Render/Text/draw_text_android.h b/Common/Render/Text/draw_text_android.h index 2cf25df0f1..17212aa331 100644 --- a/Common/Render/Text/draw_text_android.h +++ b/Common/Render/Text/draw_text_android.h @@ -23,13 +23,10 @@ public: void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. void MeasureString(std::string_view str, float *w, float *h) override; bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) override; - // Use for housekeeping like throwing out old strings. - void OncePerFrame() override; protected: bool SupportsColorEmoji() const override { return true; } - void ClearCache() override; void ClearFonts() override; private: diff --git a/Common/Render/Text/draw_text_cocoa.h b/Common/Render/Text/draw_text_cocoa.h index 6f99f5ceed..011d523a3f 100644 --- a/Common/Render/Text/draw_text_cocoa.h +++ b/Common/Render/Text/draw_text_cocoa.h @@ -20,13 +20,10 @@ public: void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. void MeasureString(std::string_view str, float *w, float *h) override; bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) override; - // Use for housekeeping like throwing out old strings. - void OncePerFrame() override; protected: bool SupportsColorEmoji() const override { return true; } - void ClearCache() override; void ClearFonts() override; TextDrawerContext *ctx_; diff --git a/Common/Render/Text/draw_text_cocoa.mm b/Common/Render/Text/draw_text_cocoa.mm index d7831e9dc4..80e436797e 100644 --- a/Common/Render/Text/draw_text_cocoa.mm +++ b/Common/Render/Text/draw_text_cocoa.mm @@ -290,46 +290,4 @@ bool TextDrawerCocoa::DrawStringBitmap(std::vector &bitmapData, TextStr return true; } -void TextDrawerCocoa::ClearCache() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); -} - -void TextDrawerCocoa::OncePerFrame() { - frameCount_++; - // If DPI changed (small-mode, future proper monitor DPI support), drop everything. - float newDpiScale = CalculateDPIScale(); - if (newDpiScale != dpiScale_) { - INFO_LOG(G3D, "TextDrawerCocoa: DPI scale: %0.1f", newDpiScale); - dpiScale_ = newDpiScale; - ClearCache(); - ClearFonts(); - } - - // 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) { - if (iter->second->texture) - iter->second->texture->Release(); - cache_.erase(iter++); - } else { - iter++; - } - } - - for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { - if (frameCount_ - iter->second->lastUsedFrame > 100) { - sizeCache_.erase(iter++); - } else { - iter++; - } - } - } -} - #endif diff --git a/Common/Render/Text/draw_text_qt.cpp b/Common/Render/Text/draw_text_qt.cpp index 4bc823d1c9..0e3a112184 100644 --- a/Common/Render/Text/draw_text_qt.cpp +++ b/Common/Render/Text/draw_text_qt.cpp @@ -135,45 +135,4 @@ void TextDrawerQt::ClearFonts() { fontHash_ = 0; } -void TextDrawerQt::ClearCache() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); -} - -void TextDrawerQt::OncePerFrame() { - frameCount_++; - // If DPI changed (small-mode, future proper monitor DPI support), drop everything. - float newDpiScale = CalculateDPIScale(); - if (newDpiScale != dpiScale_) { - dpiScale_ = newDpiScale; - ClearCache(); - ClearFonts(); - } - - // 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) { - if (iter->second->texture) - iter->second->texture->Release(); - cache_.erase(iter++); - } else { - iter++; - } - } - - for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { - if (frameCount_ - iter->second->lastUsedFrame > 100) { - sizeCache_.erase(iter++); - } else { - iter++; - } - } - } -} - #endif diff --git a/Common/Render/Text/draw_text_qt.h b/Common/Render/Text/draw_text_qt.h index b0cadadc88..62c3bdf5b2 100644 --- a/Common/Render/Text/draw_text_qt.h +++ b/Common/Render/Text/draw_text_qt.h @@ -18,14 +18,11 @@ public: void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. void MeasureString(std::string_view str, float *w, float *h) override; bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) override; - // Use for housekeeping like throwing out old strings. - void OncePerFrame() override; protected: bool SupportsColorEmoji() const override { return false; } void ClearFonts() override; - void ClearCache() override; std::map fontMap_; }; diff --git a/Common/Render/Text/draw_text_sdl.cpp b/Common/Render/Text/draw_text_sdl.cpp index 8cfb253df0..a1da471141 100644 --- a/Common/Render/Text/draw_text_sdl.cpp +++ b/Common/Render/Text/draw_text_sdl.cpp @@ -405,46 +405,4 @@ void TextDrawerSDL::ClearFonts() { fallbackFonts_.clear(); } -void TextDrawerSDL::OncePerFrame() { - // Reset everything if DPI changes - float newDpiScale = CalculateDPIScale(); - if (newDpiScale != dpiScale_) { - dpiScale_ = newDpiScale; - ClearCache(); - ClearFonts(); - } - - // 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) { - if (iter->second->texture) - iter->second->texture->Release(); - cache_.erase(iter++); - } else { - iter++; - } - } - - for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { - if (frameCount_ - iter->second->lastUsedFrame > 100) { - sizeCache_.erase(iter++); - } else { - iter++; - } - } - } -} - -void TextDrawerSDL::ClearCache() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); - - fontHash_ = 0; -} - #endif diff --git a/Common/Render/Text/draw_text_sdl.h b/Common/Render/Text/draw_text_sdl.h index 0eb7599a0b..b6a2003f0b 100644 --- a/Common/Render/Text/draw_text_sdl.h +++ b/Common/Render/Text/draw_text_sdl.h @@ -21,13 +21,9 @@ public: void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. void MeasureString(std::string_view str, float *w, float *h) override; bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) override; - // Use for housekeeping like throwing out old strings. - void OncePerFrame() override; protected: bool SupportsColorEmoji() const override { return false; } - - void ClearCache() override; void ClearFonts() override; private: diff --git a/Common/Render/Text/draw_text_uwp.cpp b/Common/Render/Text/draw_text_uwp.cpp index 8434ae1cb5..b1139569b7 100644 --- a/Common/Render/Text/draw_text_uwp.cpp +++ b/Common/Render/Text/draw_text_uwp.cpp @@ -384,45 +384,4 @@ void TextDrawerUWP::ClearFonts() { fontMap_.clear(); } -void TextDrawerUWP::ClearCache() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); -} - -void TextDrawerUWP::OncePerFrame() { - frameCount_++; - // If DPI changed (small-mode, future proper monitor DPI support), drop everything. - float newDpiScale = CalculateDPIScale(); - if (newDpiScale != dpiScale_) { - dpiScale_ = newDpiScale; - ClearCache(); - ClearFonts(); - } - - // 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) { - if (iter->second->texture) - iter->second->texture->Release(); - cache_.erase(iter++); - } else { - iter++; - } - } - - for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { - if (frameCount_ - iter->second->lastUsedFrame > 100) { - sizeCache_.erase(iter++); - } else { - iter++; - } - } - } -} - #endif diff --git a/Common/Render/Text/draw_text_uwp.h b/Common/Render/Text/draw_text_uwp.h index 49e7e93ff9..58f8fdaf71 100644 --- a/Common/Render/Text/draw_text_uwp.h +++ b/Common/Render/Text/draw_text_uwp.h @@ -23,13 +23,9 @@ public: void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. void MeasureString(std::string_view str, float *w, float *h) override; bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) override; - // Use for housekeeping like throwing out old strings. - void OncePerFrame() override; protected: bool SupportsColorEmoji() const override { return true; } - - void ClearCache() override; void ClearFonts() override; TextDrawerContext *ctx_; diff --git a/Common/Render/Text/draw_text_win.cpp b/Common/Render/Text/draw_text_win.cpp index f7fcde04ea..05b4fe354a 100644 --- a/Common/Render/Text/draw_text_win.cpp +++ b/Common/Render/Text/draw_text_win.cpp @@ -261,45 +261,4 @@ void TextDrawerWin32::ClearFonts() { fontMap_.clear(); } -void TextDrawerWin32::ClearCache() { - for (auto &iter : cache_) { - if (iter.second->texture) - iter.second->texture->Release(); - } - cache_.clear(); - sizeCache_.clear(); -} - -void TextDrawerWin32::OncePerFrame() { - frameCount_++; - // If DPI changed (small-mode, future proper monitor DPI support), drop everything. - float newDpiScale = CalculateDPIScale(); - if (newDpiScale != dpiScale_) { - dpiScale_ = newDpiScale; - ClearCache(); - ClearFonts(); - } - - // 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) { - if (iter->second->texture) - iter->second->texture->Release(); - cache_.erase(iter++); - } else { - iter++; - } - } - - for (auto iter = sizeCache_.begin(); iter != sizeCache_.end(); ) { - if (frameCount_ - iter->second->lastUsedFrame > 100) { - sizeCache_.erase(iter++); - } else { - iter++; - } - } - } -} - #endif diff --git a/Common/Render/Text/draw_text_win.h b/Common/Render/Text/draw_text_win.h index 16196d5405..24a84a5aef 100644 --- a/Common/Render/Text/draw_text_win.h +++ b/Common/Render/Text/draw_text_win.h @@ -23,13 +23,9 @@ public: void SetFont(uint32_t fontHandle) override; // Shortcut once you've set the font once. void MeasureString(std::string_view str, float *w, float *h) override; bool DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, std::string_view str, int align, bool fullColor) override; - // Use for housekeeping like throwing out old strings. - void OncePerFrame() override; protected: bool SupportsColorEmoji() const override { return false; } - - void ClearCache() override; void ClearFonts() override; TextDrawerContext *ctx_;