implements PLN-0111
This commit is contained in:
@@ -13,8 +13,8 @@ pub use crate::audio::{Audio, AudioCommand, Channel, MAX_CHANNELS, OUTPUT_SAMPLE
|
||||
pub use crate::frame_composer::{ComposerBuffer, FrameComposer, SceneStatus, SpriteController};
|
||||
pub use crate::gfx::Gfx;
|
||||
pub use crate::memory_banks::{
|
||||
GlyphBankPoolAccess, GlyphBankPoolInstaller, MemoryBanks, RenderResourceAccess,
|
||||
SceneBankPoolAccess, SceneBankPoolInstaller, SoundBankPoolAccess, SoundBankPoolInstaller,
|
||||
GlyphBankPoolAccess, GlyphBankPoolInstaller, MemoryBanks, SceneBankPoolAccess,
|
||||
SceneBankPoolInstaller, SoundBankPoolAccess, SoundBankPoolInstaller,
|
||||
};
|
||||
pub use crate::pad::Pad;
|
||||
pub use crate::test_platform::TestPlatform;
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
//!
|
||||
//! Render consumers must cross this boundary through small, read-only traits.
|
||||
//! Frame packets carry resource IDs; large resident resources stay in banks and
|
||||
//! are returned as `Arc` snapshots of the slot contents. Installation and slot
|
||||
//! replacement remain separate owner-side operations and are not part of the
|
||||
//! render read surface.
|
||||
//! are read in place through a narrow HAL callback contract. Installation and
|
||||
//! slot replacement remain separate owner-side operations and are not part of
|
||||
//! the render read surface.
|
||||
|
||||
use prometeu_hal::glyph_bank::GlyphBank;
|
||||
use prometeu_hal::render_resources::RenderResourceAccess as HalRenderResourceAccess;
|
||||
use prometeu_hal::scene_bank::SceneBank;
|
||||
use prometeu_hal::sound_bank::SoundBank;
|
||||
use std::sync::{Arc, RwLock};
|
||||
@@ -53,15 +54,6 @@ pub trait SceneBankPoolInstaller: Send + Sync {
|
||||
fn install_scene_bank(&self, slot: usize, bank: Arc<SceneBank>);
|
||||
}
|
||||
|
||||
/// Read-only resource surface needed by render consumers.
|
||||
///
|
||||
/// This is intentionally narrower than `MemoryBanks`: render code can resolve
|
||||
/// stable packet IDs to resident glyph/scene banks, but cannot install or swap
|
||||
/// resources through this trait.
|
||||
pub trait RenderResourceAccess: GlyphBankPoolAccess + SceneBankPoolAccess {}
|
||||
|
||||
impl<T> RenderResourceAccess for T where T: GlyphBankPoolAccess + SceneBankPoolAccess {}
|
||||
|
||||
/// Centralized container for all hardware memory banks.
|
||||
///
|
||||
/// MemoryBanks represent the actual hardware slot state.
|
||||
@@ -150,6 +142,28 @@ impl SceneBankPoolInstaller for MemoryBanks {
|
||||
}
|
||||
}
|
||||
|
||||
impl HalRenderResourceAccess for MemoryBanks {
|
||||
fn with_glyph_bank<R>(&self, bank_id: usize, read: impl FnOnce(&GlyphBank) -> R) -> Option<R> {
|
||||
let pool = self.glyph_bank_pool.read().unwrap();
|
||||
let bank = pool.get(bank_id)?.as_deref()?;
|
||||
Some(read(bank))
|
||||
}
|
||||
|
||||
fn glyph_bank_count(&self) -> usize {
|
||||
16
|
||||
}
|
||||
|
||||
fn with_scene_bank<R>(&self, bank_id: usize, read: impl FnOnce(&SceneBank) -> R) -> Option<R> {
|
||||
let pool = self.scene_bank_pool.read().unwrap();
|
||||
let bank = pool.get(bank_id)?.as_deref()?;
|
||||
Some(read(bank))
|
||||
}
|
||||
|
||||
fn scene_bank_count(&self) -> usize {
|
||||
16
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -193,24 +207,24 @@ mod tests {
|
||||
assert_send_sync::<MemoryBanks>();
|
||||
assert_send_sync::<Arc<dyn GlyphBankPoolAccess>>();
|
||||
assert_send_sync::<Arc<dyn SceneBankPoolAccess>>();
|
||||
assert_send_sync::<Arc<dyn RenderResourceAccess>>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_resource_access_exposes_read_only_glyph_and_scene_banks() {
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
fn render_resource_access_exposes_read_only_glyph_and_scene_banks_without_arc_contract() {
|
||||
let banks = MemoryBanks::new();
|
||||
banks.install_glyph_bank(0, Arc::new(make_glyph_bank()));
|
||||
banks.install_scene_bank(1, Arc::new(make_scene_bank()));
|
||||
|
||||
let render_resources: Arc<dyn RenderResourceAccess> = banks;
|
||||
let glyph_bank =
|
||||
render_resources.glyph_bank_slot(0).expect("glyph bank slot should be readable");
|
||||
let scene_bank =
|
||||
render_resources.scene_bank_slot(1).expect("scene bank slot should be readable");
|
||||
let glyph_tile_size =
|
||||
HalRenderResourceAccess::with_glyph_bank(&banks, 0, |bank| bank.tile_size);
|
||||
let scene_tile_size =
|
||||
HalRenderResourceAccess::with_scene_bank(&banks, 1, |bank| bank.layers[0].tile_size);
|
||||
|
||||
assert_eq!(glyph_bank.tile_size, TileSize::Size8);
|
||||
assert_eq!(scene_bank.layers[0].tile_size, TileSize::Size8);
|
||||
assert!(render_resources.glyph_bank_slot(15).is_none());
|
||||
assert!(render_resources.scene_bank_slot(15).is_none());
|
||||
assert_eq!(glyph_tile_size, Some(TileSize::Size8));
|
||||
assert_eq!(scene_tile_size, Some(TileSize::Size8));
|
||||
assert_eq!(HalRenderResourceAccess::with_glyph_bank(&banks, 15, |_| ()), None);
|
||||
assert_eq!(HalRenderResourceAccess::with_scene_bank(&banks, 15, |_| ()), None);
|
||||
assert_eq!(HalRenderResourceAccess::glyph_bank_count(&banks), 16);
|
||||
assert_eq!(HalRenderResourceAccess::scene_bank_count(&banks), 16);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user