From 398722313716885bf6d2835f86576d2ae6615572 Mon Sep 17 00:00:00 2001 From: Micha White Date: Sun, 20 Nov 2022 12:26:13 -0500 Subject: Allowed queuing of a new priority --- alligator_resources/Cargo.toml | 1 + alligator_resources/src/texture.rs | 39 +++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) (limited to 'alligator_resources') diff --git a/alligator_resources/Cargo.toml b/alligator_resources/Cargo.toml index 74a696b..3ccb084 100644 --- a/alligator_resources/Cargo.toml +++ b/alligator_resources/Cargo.toml @@ -12,3 +12,4 @@ exun = "0.1" texture_packer = { git = "https://github.com/botahamec/piston_texture_packer", branch = "u16" } profiling = "1" bytemuck = { version = "1", features = ["extern_crate_alloc"] } +parking_lot = "0.12" diff --git a/alligator_resources/src/texture.rs b/alligator_resources/src/texture.rs index 2255b3f..4b55f95 100644 --- a/alligator_resources/src/texture.rs +++ b/alligator_resources/src/texture.rs @@ -5,6 +5,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; use image::{GenericImage, ImageBuffer}; +use parking_lot::Mutex; use texture_packer::exporter::ImageExporter; use texture_packer::{Frame, TexturePacker, TexturePackerConfig}; use thiserror::Error; @@ -144,6 +145,7 @@ enum TextureBuffer { struct Texture { priority: Priority, + queued_priority: Arc>>, buffer: TextureBuffer, } @@ -151,6 +153,7 @@ impl Texture { fn from_buffer(texture: Rgba16Texture) -> Self { Self { priority: Priority::Urgent, // indicates that it can't be unloaded + queued_priority: Arc::new(Mutex::new(None)), buffer: TextureBuffer::Memory(Arc::new(texture)), } } @@ -158,6 +161,7 @@ impl Texture { fn from_path(path: impl AsRef, priority: Priority) -> Self { Self { priority, + queued_priority: Arc::new(Mutex::new(None)), buffer: TextureBuffer::Disk(TextureFile::new(path)), } } @@ -166,13 +170,29 @@ impl Texture { self.priority } - fn set_priority(&mut self, priority: Priority) { + fn _set_priority(buffer: &TextureBuffer, src: &mut Priority, priority: Priority) -> bool { // memory textures and textures in use should always be urgent - if let TextureBuffer::Disk(disk) = &self.buffer && !disk.is_used() { - self.priority = priority; + if let TextureBuffer::Disk(disk) = buffer && !disk.is_used() { + *src = priority; + true + } else { + false } } + fn unqueue_priority(&mut self) { + let mut queued_priority = self.queued_priority.lock(); + let unqueued_priority = queued_priority.unwrap_or(Priority::Unnecessary); + + if Self::_set_priority(&self.buffer, &mut self.priority, unqueued_priority) { + *queued_priority = None; + } + } + + fn set_priority(&mut self, priority: Priority) { + Self::_set_priority(&self.buffer, &mut self.priority, priority); + } + fn load_texture(&mut self) -> Result<&Rgba16Texture, LoadError> { match &mut self.buffer { TextureBuffer::Memory(ref texture) => Ok(texture), @@ -199,11 +219,18 @@ impl Texture { TextureBuffer::Disk(file) => file.is_loaded(), } } + + fn unqueue(&mut self) { + if let Some(queued_priority) = *self.queued_priority.lock() { + self.priority = queued_priority; + } + } } pub struct TextureRef<'a> { id: TextureId, texture: Arc, + queued_priority: Arc>>, manager: &'a TextureManager, } @@ -250,6 +277,11 @@ impl<'a> TextureRef<'a> { pub fn height(&self) -> f32 { self.manager.texture_height(self) } + + pub fn queue_priority(&self, priority: Priority) { + let mut queued_priority = self.queued_priority.lock(); + *queued_priority = Some(priority); + } } pub struct TextureManager { @@ -315,6 +347,7 @@ impl TextureManager { let mut total_size = size; for texture in textures { + texture.unqueue_priority(); if total_size + texture.allocated_size() < max_size { total_size += texture.allocated_size(); } else if texture.priority() < priority { -- cgit v1.2.3