@@ -54,13 +54,12 @@ class CustomTextureSource : public BaseCustomTextureSource
5454 }
5555 }
5656 }
57- bool customTexturesAvailable () override { return custom_textures_available; }
58- bool shouldReplace () const override { return config::CustomTextures; }
59- bool shouldPreload () const override { return config::CustomTextures && config::PreloadCustomTextures; }
60- bool LoadMap () override ;
61- size_t GetTextureCount () const override { return texture_map.size (); }
57+ bool shouldReplace () const override { return config::CustomTextures && custom_textures_available; }
58+ bool shouldPreload () const override { return shouldReplace () && config::PreloadCustomTextures; }
59+ bool loadMap () override ;
60+ size_t getTextureCount () const override { return texture_map.size (); }
6261 void PreloadTextures (TextureCallback callback, std::atomic<bool >* stop_flag) override ;
63- u8 * LoadCustomTexture (u32 hash, int & width, int & height) override ;
62+ u8 * loadCustomTexture (u32 hash, int & width, int & height) override ;
6463 bool isTextureReplaced (u32 hash) override final ;
6564
6665
@@ -71,7 +70,7 @@ class CustomTextureSource : public BaseCustomTextureSource
7170 std::map<u32 , std::string> texture_map;
7271};
7372
74- bool CustomTextureSource::LoadMap ()
73+ bool CustomTextureSource::loadMap ()
7574{
7675 texture_map.clear ();
7776 hostfs::DirectoryTree tree (textures_path);
@@ -98,10 +97,10 @@ void CustomTextureSource::PreloadTextures(TextureCallback callback, std::atomic<
9897{
9998 for (auto const & [hash, path] : texture_map)
10099 {
101- if (stop_flag != nullptr && stop_flag-> load (std::memory_order_relaxed) )
100+ if (stop_flag != nullptr && * stop_flag)
102101 return ;
103102 int w, h;
104- u8 * data = LoadCustomTexture (hash, w, h);
103+ u8 * data = loadCustomTexture (hash, w, h);
105104 if (data != nullptr )
106105 {
107106 size_t size = (size_t )w * h * 4 ;
@@ -116,7 +115,7 @@ void CustomTextureSource::PreloadTextures(TextureCallback callback, std::atomic<
116115 }
117116}
118117
119- u8 * CustomTextureSource::LoadCustomTexture (u32 hash, int & width, int & height)
118+ u8 * CustomTextureSource::loadCustomTexture (u32 hash, int & width, int & height)
120119{
121120 auto it = texture_map.find (hash);
122121 if (it == texture_map.end ())
@@ -173,60 +172,60 @@ std::string CustomTexture::getGameId()
173172 return game_id;
174173}
175174
176- bool CustomTexture::Init ()
175+ bool CustomTexture::init ()
177176{
178177 if (!initialized)
179178 {
180- stop_preload. store ( false , std::memory_order_relaxed) ;
179+ stop_preload = false ;
181180 resetPreloadProgress ();
182- pending_preloads. store ( 0 , std::memory_order_relaxed) ;
181+ pending_preloads = 0 ;
183182 initialized = true ;
184183
185184 std::string game_id = getGameId ();
186185 if (game_id.length () > 0 )
187186 {
188- AddSource (std::make_unique<CustomTextureSource>(game_id));
187+ addSource (std::make_unique<CustomTextureSource>(game_id));
189188 }
190189 }
191190
192191 return loaderThread != nullptr ;
193192}
194193
195194bool CustomTexture::enabled () {
196- return Init () ;
195+ return loaderThread != nullptr ;
197196}
198197
199198bool CustomTexture::preloaded () {
200- return preload_total. load (std::memory_order_relaxed) > 0 ;
199+ return preload_total > 0 ;
201200}
202201
203202bool CustomTexture::isPreloading () {
204- if (pending_preloads. load (std::memory_order_relaxed) > 0 )
203+ if (pending_preloads > 0 )
205204 return true ;
206205
207206 int texLoaded = 0 ;
208207 int texTotal = 0 ;
209208 size_t loaded_size_b = 0 ;
210- GetPreloadProgress (texLoaded, texTotal, loaded_size_b);
209+ getPreloadProgress (texLoaded, texTotal, loaded_size_b);
211210
212211 return (texTotal > 0 && texLoaded < texTotal);
213212}
214213
215- void CustomTexture::AddSource (std::unique_ptr<BaseCustomTextureSource> source)
214+ void CustomTexture::addSource (std::unique_ptr<BaseCustomTextureSource> source)
216215{
217216 BaseCustomTextureSource* ptr = source.get ();
218217 sources.emplace_back (std::move (source));
219218
220219 if (initialized)
221220 {
222- if (!loaderThread && ptr->customTexturesAvailable ())
221+ if (!loaderThread && ptr->shouldReplace ())
223222 {
224223 loaderThread = std::make_unique<WorkerThread>(" CustomTexLoader" );
225224 }
226225 if (loaderThread)
227226 {
228227 if (ptr->shouldPreload ())
229- pending_preloads. fetch_add ( 1 , std::memory_order_relaxed) ;
228+ pending_preloads++ ;
230229 loaderThread->run ([this , ptr]() {
231230 prepareSource (ptr);
232231 });
@@ -235,29 +234,19 @@ void CustomTexture::AddSource(std::unique_ptr<BaseCustomTextureSource> source)
235234}
236235
237236CustomTexture::~CustomTexture () {
238- Terminate ();
237+ terminate ();
239238}
240239
241- void CustomTexture::Terminate ()
240+ void CustomTexture::terminate ()
242241{
243- stop_preload. store ( true , std::memory_order_relaxed) ;
242+ stop_preload = true ;
244243 if (loaderThread)
245244 loaderThread->stop ();
246245 loaderThread.reset ();
247246 for (auto & source : sources)
248- source->Terminate ();
247+ source->terminate ();
249248 sources.clear ();
250- if (!preloaded_textures.empty ())
251- {
252- #ifndef LIBRETRO
253- auto textures_to_free = std::make_shared<std::map<u32 , TextureData>>(std::move (preloaded_textures));
254- std::thread ([textures_to_free]() {
255- textures_to_free->clear ();
256- }).detach ();
257- #else
258- preloaded_textures.clear ();
259- #endif
260- }
249+ preloaded_textures.clear ();
261250 resetPreloadProgress ();
262251 initialized = false ;
263252}
@@ -282,7 +271,7 @@ u8* CustomTexture::loadTexture(u32 hash, int& width, int& height)
282271 auto & source = *it;
283272 if (source->shouldReplace ())
284273 {
285- u8 * data = source->LoadCustomTexture (hash, width, height);
274+ u8 * data = source->loadCustomTexture (hash, width, height);
286275 if (data != nullptr )
287276 return data;
288277 }
@@ -315,9 +304,9 @@ bool CustomTexture::isTextureReplaced(BaseTextureCacheData* texture)
315304 return false ;
316305}
317306
318- void CustomTexture::LoadCustomTextureAsync (BaseTextureCacheData *texture_data)
307+ void CustomTexture::loadCustomTextureAsync (BaseTextureCacheData *texture_data)
319308{
320- if (!Init ())
309+ if (!init ())
321310 return ;
322311
323312 texture_data->custom_load_in_progress ++;
@@ -326,7 +315,7 @@ void CustomTexture::LoadCustomTextureAsync(BaseTextureCacheData *texture_data)
326315 });
327316}
328317
329- void CustomTexture::DumpTexture (BaseTextureCacheData* texture, int w, int h, void *src_buffer)
318+ void CustomTexture::dumpTexture (BaseTextureCacheData* texture, int w, int h, void *src_buffer)
330319{
331320 if (!config::DumpReplacedTextures.get () && isTextureReplaced (texture))
332321 return ;
@@ -463,47 +452,43 @@ void CustomTexture::DumpTexture(BaseTextureCacheData* texture, int w, int h, voi
463452
464453void CustomTexture::prepareSource (BaseCustomTextureSource* source)
465454{
466- struct PreloadGuard {
467- std::atomic<int >& counter;
468- bool shouldPreload;
469- ~PreloadGuard () { if (shouldPreload) counter.fetch_sub (1 , std::memory_order_relaxed); }
470- } guard{ pending_preloads, source->shouldPreload () };
455+ bool should_preload = source->shouldPreload ();
471456
472- if (stop_preload.load (std::memory_order_relaxed))
473- return ;
474-
475- if (source->LoadMap ())
457+ if (!stop_preload && source->loadMap ())
476458 {
477- if (guard. shouldPreload )
459+ if (should_preload )
478460 {
479- int count = static_cast <int >(source->GetTextureCount ());
461+ int count = static_cast <int >(source->getTextureCount ());
480462 if (count > 0 )
481463 {
482- preload_total. fetch_add (count, std::memory_order_relaxed) ;
464+ preload_total += count ;
483465 auto callback = [this ](u32 hash, TextureData&& data) {
484466 size_t size = data.data .size ();
485467 preloaded_textures[hash] = std::move (data);
486- preload_loaded. fetch_add ( 1 , std::memory_order_relaxed) ;
487- preload_loaded_size. fetch_add (size, std::memory_order_relaxed) ;
468+ preload_loaded++ ;
469+ preload_loaded_size += size ;
488470 };
489471 source->PreloadTextures (callback, &stop_preload);
490472 }
491473 }
492474 }
475+
476+ if (should_preload)
477+ pending_preloads--;
493478}
494479
495- void CustomTexture::GetPreloadProgress (int & completed, int & total, size_t & loaded_size) const
480+ void CustomTexture::getPreloadProgress (int & completed, int & total, size_t & loaded_size) const
496481{
497- total = preload_total. load (std::memory_order_relaxed) ;
498- if (total == 0 && pending_preloads. load (std::memory_order_relaxed) > 0 )
482+ total = preload_total;
483+ if (total == 0 && pending_preloads > 0 )
499484 total = -1 ; // Prints Preparing... in UI
500- completed = preload_loaded. load (std::memory_order_relaxed) ;
501- loaded_size = preload_loaded_size. load (std::memory_order_relaxed) ;
485+ completed = preload_loaded;
486+ loaded_size = preload_loaded_size;
502487}
503488
504489void CustomTexture::resetPreloadProgress ()
505490{
506- preload_total. store ( 0 , std::memory_order_relaxed) ;
507- preload_loaded. store ( 0 , std::memory_order_relaxed) ;
508- preload_loaded_size. store ( 0 , std::memory_order_relaxed) ;
491+ preload_total = 0 ;
492+ preload_loaded = 0 ;
493+ preload_loaded_size = 0 ;
509494}
0 commit comments