game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_thumbnail_cache.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/game_thumbnail_cache.h
4// ==============================================================================
5// Image cache for the library view. Extracted from MainWindow (MED-7).
6// ==============================================================================
7// Responsibilities:
8// - Resolve the best-available image file path for a Game, with per-type
9// fallback chains (HeroImage -> Header -> Capsule, etc.).
10// - Cache the result of QFile::exists() lookups (imagePathCache_) so we
11// don't re-stat on every paint.
12// - Cache scaled thumbnails and hero images in two separate LRU-evicted
13// QPixmap stores. Thumbnails are the small icons in the game list;
14// hero images are the wide banner on the details panel.
15// - DPR-aware: scaled pixmaps are produced at physical resolution then
16// flagged with QPixmap::setDevicePixelRatio() so layouts read them as
17// their original logical size (standard Qt 6 HiDPI pattern).
18//
19// Threading:
20// - Thumbnail/hero caches are GUI-thread-only.
21// - buildImagePathCache() does its work on a thread pool and marshals the
22// merged result back to the GUI thread via QMetaObject::invokeMethod,
23// because imagePathCache_ is main-thread-only once populated.
24// ==============================================================================
25
26#pragma once
27
28#include <QObject>
29#include <QPixmap>
30#include <QString>
31#include <atomic>
32#include <deque>
33#include <string>
34#include <unordered_map>
35#include <vector>
36
37class Game;
38enum class ImageType;
39
40class GameThumbnailCache : public QObject
41{
42 Q_OBJECT
43
44 public:
45 GameThumbnailCache(size_t maxThumbnails = 300, size_t maxHeroImages = 30,
46 QObject* parent = nullptr);
48
49 // ==========================================================================
50 // Image path resolution
51 // ==========================================================================
52
53 /// Return the best-available image file path for `game`, falling back
54 /// through a type-specific chain when the preferred type is missing.
55 /// Empty string if no image is available. Results are cached.
56 QString findBestImage(const Game* game, ImageType preferred) const;
57
58 // ==========================================================================
59 // Thumbnail cache (game list icons)
60 // ==========================================================================
61
62 /// Get a logical-size thumbnail with DPR-aware backing store. Cached.
63 /// Size is quantised to 16px boundaries internally to raise hit rate
64 /// during zoom/resize.
65 QPixmap getGameThumbnail(const Game* game, const QSize& logicalSize, ImageType preferred);
66
67 // ==========================================================================
68 // Hero image cache (details-panel banner)
69 // ==========================================================================
70
71 /// Get a hero image scaled to `targetWidth` logical pixels (preserving
72 /// aspect ratio). Returns a null QPixmap if no image is available — the
73 /// null result is still cached to avoid repeated lookups.
74 QPixmap getHeroImage(const Game* game, int targetWidth);
75
76 // ==========================================================================
77 // Background pre-population
78 // ==========================================================================
79
80 /// Pre-populate the image-path validation cache by stat-ing every image
81 /// file for every game. Runs asynchronously on QThreadPool and marshals
82 /// the result to the GUI thread. Call at startup with the full game list.
83 /// Abort signal via isShuttingDown().
84 void buildImagePathCache(const std::vector<const Game*>& games);
85
86 /// Mark shutdown — any in-flight buildImagePathCache() task will observe
87 /// this and early-exit without touching the GUI thread.
89 {
90 shuttingDown_.store(true);
91 }
92 bool isShuttingDown() const
93 {
94 return shuttingDown_.load();
95 }
96
97 // ==========================================================================
98 // Invalidation
99 // ==========================================================================
100
101 /// Clear all caches (thumbnails, hero images, path validation). Called
102 /// when new artwork is downloaded or a game is added.
103 void clearAll();
104
105 private:
106 void touchThumbEntry(const std::string& key);
107 void touchHeroEntry(const std::string& key);
108
109 // Thumbnails
110 size_t maxThumbnails_;
111 std::unordered_map<std::string, QPixmap> thumbnailCache_;
112 std::deque<std::string> thumbnailLRU_;
113
114 // Hero images
115 size_t maxHeroImages_;
116 std::unordered_map<std::string, QPixmap> heroCache_;
117 std::deque<std::string> heroLRU_;
118
119 // Path validation. mutable because findBestImage() is const.
120 mutable std::unordered_map<std::string, QString> imagePathCache_;
121
122 std::atomic<bool> shuttingDown_{false};
123};
QPixmap getGameThumbnail(const Game *game, const QSize &logicalSize, ImageType preferred)
Get a logical-size thumbnail with DPR-aware backing store.
~GameThumbnailCache() override
QPixmap getHeroImage(const Game *game, int targetWidth)
Get a hero image scaled to targetWidth logical pixels (preserving aspect ratio).
void clearAll()
Clear all caches (thumbnails, hero images, path validation).
void requestShutdown()
Mark shutdown — any in-flight buildImagePathCache() task will observe this and early-exit without tou...
GameThumbnailCache(size_t maxThumbnails=300, size_t maxHeroImages=30, QObject *parent=nullptr)
void buildImagePathCache(const std::vector< const Game * > &games)
Pre-populate the image-path validation cache by stat-ing every image file for every game.
QString findBestImage(const Game *game, ImageType preferred) const
Return the best-available image file path for game, falling back through a type-specific chain when t...
Complete representation of a game in the library.
Definition game.h:767
ImageType
Types of game images available.
Definition game.h:478