From 8c4f8af66e13f87227eac2e5025e16bb9778f56b Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 4 May 2025 10:06:14 +0100 Subject: [PATCH] Codechange: Move std::unique_ptr out of LRUCache implementation. This is an implementation detail of how OpenGLSprites are stored. --- src/misc/lrucache.hpp | 15 ++++++++------- src/video/opengl.cpp | 10 +++++----- src/video/opengl.h | 8 +++++--- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/misc/lrucache.hpp b/src/misc/lrucache.hpp index b5f262a3a7..81ac0ae3a0 100644 --- a/src/misc/lrucache.hpp +++ b/src/misc/lrucache.hpp @@ -16,12 +16,12 @@ /** * Size limited cache with a least recently used eviction strategy. * @tparam Tkey Type of the cache key. - * @tparam Tdata Type of the cache item. The cache will store a pointer of this type. + * @tparam Tdata Type of the cache item. */ template class LRUCache { private: - typedef std::pair> Tpair; + typedef std::pair Tpair; typedef typename std::list::iterator Titer; std::list data; ///< Ordered list of all items. @@ -51,7 +51,7 @@ public: * @param key Key under which the item should be stored. * @param item Item to insert. */ - void Insert(const Tkey key, std::unique_ptr &&item) + void Insert(const Tkey key, Tdata &&item) { if (this->Contains(key)) { /* Replace old value. */ @@ -85,13 +85,14 @@ public: * @return The item value. * @note Throws if item not found. */ - inline Tdata *Get(const Tkey key) + inline const Tdata &Get(const Tkey key) { - if (this->lookup.find(key) == this->lookup.end()) throw std::out_of_range("item not found"); + auto it = this->lookup.find(key); + if (it == this->lookup.end()) throw std::out_of_range("item not found"); /* Move to front if needed. */ - this->data.splice(this->data.begin(), this->data, this->lookup[key]); + this->data.splice(this->data.begin(), this->data, it->second); - return this->data.front().second.get(); + return this->data.front().second; } }; diff --git a/src/video/opengl.cpp b/src/video/opengl.cpp index dec8b118dc..008d3f998d 100644 --- a/src/video/opengl.cpp +++ b/src/video/opengl.cpp @@ -1077,7 +1077,7 @@ void OpenGLBackend::DrawMouseCursor() for (const auto &cs : this->cursor_sprites) { /* Sprites are cached by PopulateCursorCache(). */ if (this->cursor_cache.Contains(cs.image.sprite)) { - OpenGLSprite *spr = this->cursor_cache.Get(cs.image.sprite); + const OpenGLSprite *spr = this->cursor_cache.Get(cs.image.sprite).get(); this->RenderOglSprite(spr, cs.image.pal, this->cursor_pos.x + cs.pos.x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI), @@ -1089,10 +1089,10 @@ void OpenGLBackend::DrawMouseCursor() class OpenGLSpriteAllocator : public SpriteAllocator { public: - LRUCache &lru; + OpenGLSpriteLRUCache &lru; SpriteID sprite; - OpenGLSpriteAllocator(LRUCache &lru, SpriteID sprite) : lru(lru), sprite(sprite) {} + OpenGLSpriteAllocator(OpenGLSpriteLRUCache &lru, SpriteID sprite) : lru(lru), sprite(sprite) {} protected: void *AllocatePtr(size_t) override { NOT_REACHED(); } }; @@ -1274,7 +1274,7 @@ void OpenGLBackend::ReleaseAnimBuffer(const Rect &update_rect) * @param y Y position of the sprite. * @param zoom Zoom level to use. */ -void OpenGLBackend::RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom) +void OpenGLBackend::RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom) { /* Set textures. */ bool rgb = gl_sprite->BindTextures(); @@ -1518,7 +1518,7 @@ inline Dimension OpenGLSprite::GetSize(ZoomLevel level) const * Bind textures for rendering this sprite. * @return True if the sprite has RGBA data. */ -bool OpenGLSprite::BindTextures() +bool OpenGLSprite::BindTextures() const { _glActiveTexture(GL_TEXTURE0); _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA] != 0 ? this->tex[TEX_RGBA] : OpenGLSprite::dummy_tex[TEX_RGBA]); diff --git a/src/video/opengl.h b/src/video/opengl.h index 7d3b59db75..75515f7210 100644 --- a/src/video/opengl.h +++ b/src/video/opengl.h @@ -23,6 +23,8 @@ bool HasStringInExtensionList(std::string_view string, std::string_view substrin class OpenGLSprite; +using OpenGLSpriteLRUCache = LRUCache>; + /** Platform-independent back-end class for OpenGL video drivers. */ class OpenGLBackend : public SpriteEncoder { private: @@ -58,7 +60,7 @@ private: GLint sprite_rgb_loc = 0; ///< Uniform location for RGB mode flag. GLint sprite_crash_loc = 0; ///< Uniform location for crash remap mode flag. - LRUCache cursor_cache; ///< Cache of encoded cursor sprites. + OpenGLSpriteLRUCache cursor_cache; ///< Cache of encoded cursor sprites. PaletteID last_sprite_pal = (PaletteID)-1; ///< Last uploaded remap palette. bool clear_cursor_cache = false; ///< A clear of the cursor cache is pending. @@ -74,7 +76,7 @@ private: void InternalClearCursorCache(); - void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom); + void RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom); public: /** Get singleton instance of this class. */ @@ -134,7 +136,7 @@ private: static bool Create(); static void Destroy(); - bool BindTextures(); + bool BindTextures() const; public: OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite);