use anyhow::{Context as _, Result};
use std::{
collections::HashMap,
sync::{
Arc,
atomic::{AtomicBool, AtomicU32, Ordering},
},
};
use crate::fast::layers::scene::LAYER_TILE_TEXTURE_BASE;
use crate::{
AtlasTextureId, AtlasTextureKind, AtlasTile, Bounds, DevicePixels, Pixels, PolychromeSprite,
TileId, Window, point,
};
const BYTES_PER_PIXEL: usize = 4;
const DYNAMIC_INDEX_BASE: u32 = 0xE000_0000;
const DYNAMIC_ID_LIMIT: u32 = LAYER_TILE_TEXTURE_BASE - DYNAMIC_INDEX_BASE;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DynamicTextureId(u32);
impl DynamicTextureId {
pub fn atlas_texture_id(self) -> AtlasTextureId {
AtlasTextureId {
index: DYNAMIC_INDEX_BASE.wrapping_add(self.0),
kind: AtlasTextureKind::Polychrome,
}
}
pub fn from_atlas_texture_id(id: AtlasTextureId) -> Option<Self> {
(id.kind == AtlasTextureKind::Polychrome
&& id.index >= DYNAMIC_INDEX_BASE
&& id.index - DYNAMIC_INDEX_BASE < DYNAMIC_ID_LIMIT)
.then(|| Self(id.index - DYNAMIC_INDEX_BASE))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DynamicTexture {
id: DynamicTextureId,
size: crate::Size<DevicePixels>,
}
impl DynamicTexture {
pub fn id(&self) -> DynamicTextureId {
self.id
}
pub fn new_gpu(size: crate::Size<DevicePixels>) -> Result<Self> {
anyhow::ensure!(
size.width.0 > 0 && size.height.0 > 0,
"dynamic texture dimensions must be positive"
);
allocate(size, TextureSource::Gpu, |_| Ok(()))
}
pub fn remove(self) {
remove(self);
}
}
pub trait DynamicTexturePlatform: Send + Sync {
fn create_texture(
&self,
id: DynamicTextureId,
size: crate::Size<DevicePixels>,
bytes: &[u8],
) -> Result<()>;
fn update_texture(
&self,
id: DynamicTextureId,
bounds: Bounds<DevicePixels>,
bytes: &[u8],
) -> Result<()>;
fn remove_texture(&self, id: DynamicTextureId);
}
static PLATFORM: parking_lot::RwLock<Option<Arc<dyn DynamicTexturePlatform>>> =
parking_lot::RwLock::new(None);
pub fn set_dynamic_texture_platform(backend: Arc<dyn DynamicTexturePlatform>) {
let mut platform = PLATFORM.write();
if platform.is_none() {
*platform = Some(backend);
}
}
fn platform() -> Result<Arc<dyn DynamicTexturePlatform>> {
require_platform(PLATFORM.read().clone())
}
fn require_platform(
platform: Option<Arc<dyn DynamicTexturePlatform>>,
) -> Result<Arc<dyn DynamicTexturePlatform>> {
platform.context("dynamic textures are not supported on this platform")
}
static NEXT_TEXTURE_ID: AtomicU32 = AtomicU32::new(0);
static IDS_EXHAUSTED: AtomicBool = AtomicBool::new(false);
static LIVE_TEXTURES: parking_lot::Mutex<Option<HashMap<DynamicTextureId, TextureState>>> =
parking_lot::Mutex::new(None);
#[derive(Clone, Copy)]
struct TextureState {
size: crate::Size<DevicePixels>,
source: TextureSource,
version: u32,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum TextureSource {
Upload,
Gpu,
}
fn live_textures() -> parking_lot::MappedMutexGuard<'static, HashMap<DynamicTextureId, TextureState>>
{
parking_lot::MutexGuard::map(LIVE_TEXTURES.lock(), |textures| {
textures.get_or_insert_with(HashMap::new)
})
}
fn painted_version(id: DynamicTextureId) -> u32 {
live_textures()
.get(&id)
.map_or(1, |state| state.version.max(1))
}
pub(crate) fn create(
size: crate::Size<DevicePixels>,
bytes: &[u8],
bytes_per_row: u32,
) -> Result<DynamicTexture> {
let width = usize::try_from(size.width.0).context("negative texture width")?;
let height = usize::try_from(size.height.0).context("negative texture height")?;
anyhow::ensure!(
width > 0 && height > 0,
"dynamic texture dimensions must be positive"
);
let row_bytes = width
.checked_mul(BYTES_PER_PIXEL)
.context("dynamic texture row size overflow")?;
let stride = usize::try_from(bytes_per_row).context("negative bytes per row")?;
anyhow::ensure!(
stride >= row_bytes,
"dynamic texture stride is smaller than one row"
);
let required = checked_payload_len(height, stride, row_bytes)?;
anyhow::ensure!(
bytes.len() >= required,
"dynamic texture payload is shorter than its dimensions"
);
let packed = pack_rows(bytes, stride, row_bytes, height);
let backend = platform()?;
allocate(size, TextureSource::Upload, |id| {
backend.create_texture(id, size, &packed)
})
}
fn allocate(
size: crate::Size<DevicePixels>,
source: TextureSource,
create: impl FnOnce(DynamicTextureId) -> Result<()>,
) -> Result<DynamicTexture> {
let id = DynamicTextureId(NEXT_TEXTURE_ID.fetch_add(1, Ordering::Relaxed));
if IDS_EXHAUSTED.load(Ordering::Relaxed) || id.0 >= DYNAMIC_ID_LIMIT {
IDS_EXHAUSTED.store(true, Ordering::Relaxed);
anyhow::bail!("dynamic texture ids are exhausted");
}
create(id).with_context(|| format!("failed to create dynamic texture {id:?}"))?;
live_textures().insert(
id,
TextureState {
size,
source,
version: 1,
},
);
Ok(DynamicTexture { id, size })
}
pub(crate) fn update(
texture: DynamicTexture,
bounds: Bounds<DevicePixels>,
bytes: &[u8],
bytes_per_row: u32,
) -> Result<()> {
let state = live_textures()
.get(&texture.id)
.copied()
.context("unknown dynamic texture")?;
anyhow::ensure!(
state.source == TextureSource::Upload,
"GPU-written textures must be updated by their producer"
);
let packed = validate_region(state.size, bounds, bytes, bytes_per_row)?;
platform()?
.update_texture(texture.id, bounds, &packed)
.with_context(|| format!("failed to update dynamic texture {texture:?}"))?;
if let Some(state) = live_textures().get_mut(&texture.id) {
state.version = state.version.wrapping_add(1).max(1);
}
Ok(())
}
pub(crate) fn mark_written(texture: DynamicTexture) -> Result<()> {
let mut textures = live_textures();
let state = textures
.get_mut(&texture.id)
.context("unknown dynamic texture")?;
state.version = state.version.wrapping_add(1).max(1);
Ok(())
}
pub(crate) fn paint(
window: &mut Window,
bounds: Bounds<Pixels>,
texture: DynamicTexture,
) -> Result<()> {
window.invalidator.debug_assert_paint();
let texture_size = live_size(texture.id)?;
let tile = AtlasTile {
texture_id: texture.id.atlas_texture_id(),
tile_id: TileId(texture.id.0),
padding: 0,
bounds: Bounds {
origin: point(DevicePixels(0), DevicePixels(0)),
size: texture_size,
},
};
window.next_frame.scene.insert_primitive(PolychromeSprite {
order: 0,
pad: painted_version(texture.id),
grayscale: false.into(),
bounds: window.snap_bounds(bounds),
content_mask: window.snapped_content_mask(),
corner_radii: Default::default(),
tile,
opacity: window.element_opacity(),
});
Ok(())
}
pub(crate) fn remove(texture: DynamicTexture) {
let state = live_textures().remove(&texture.id);
if state.is_some_and(|state| state.source == TextureSource::Upload)
&& let Ok(backend) = platform()
{
backend.remove_texture(texture.id);
}
}
impl Window {
pub fn create_dynamic_texture(
&mut self,
size: crate::Size<DevicePixels>,
bytes: &[u8],
bytes_per_row: u32,
) -> Result<DynamicTexture> {
create(size, bytes, bytes_per_row)
}
pub fn update_dynamic_texture(
&mut self,
texture: DynamicTexture,
bounds: Bounds<DevicePixels>,
bytes: &[u8],
bytes_per_row: u32,
) -> Result<()> {
update(texture, bounds, bytes, bytes_per_row)?;
self.refresh();
Ok(())
}
pub fn refresh_dynamic_texture(&mut self, texture: DynamicTexture) -> Result<()> {
mark_written(texture)?;
self.refresh();
Ok(())
}
pub fn paint_dynamic_texture(
&mut self,
bounds: Bounds<Pixels>,
texture: DynamicTexture,
) -> Result<()> {
paint(self, bounds, texture)
}
pub fn remove_dynamic_texture(&mut self, texture: DynamicTexture) {
remove(texture);
}
}
fn live_size(id: DynamicTextureId) -> Result<crate::Size<DevicePixels>> {
live_textures()
.get(&id)
.map(|state| state.size)
.context("unknown dynamic texture")
}
fn validate_region(
texture_size: crate::Size<DevicePixels>,
bounds: Bounds<DevicePixels>,
bytes: &[u8],
bytes_per_row: u32,
) -> Result<Vec<u8>> {
let (origin_x, origin_y) = (bounds.origin.x.0 as i64, bounds.origin.y.0 as i64);
let (width, height) = (bounds.size.width.0 as i64, bounds.size.height.0 as i64);
anyhow::ensure!(
origin_x >= 0
&& origin_y >= 0
&& width > 0
&& height > 0
&& origin_x + width <= texture_size.width.0 as i64
&& origin_y + height <= texture_size.height.0 as i64,
"dynamic texture update is outside the texture"
);
let (width, height) = (width as usize, height as usize);
let row_bytes = width
.checked_mul(BYTES_PER_PIXEL)
.context("dynamic texture region row size overflow")?;
let stride = usize::try_from(bytes_per_row).context("negative bytes per row")?;
anyhow::ensure!(
stride >= row_bytes,
"dynamic texture stride is smaller than one row"
);
let required = checked_payload_len(height, stride, row_bytes)?;
anyhow::ensure!(
bytes.len() >= required,
"dynamic texture update is shorter than the region"
);
Ok(pack_rows(bytes, stride, row_bytes, height))
}
fn checked_payload_len(height: usize, stride: usize, row_bytes: usize) -> Result<usize> {
(height - 1)
.checked_mul(stride)
.and_then(|length| length.checked_add(row_bytes))
.context("dynamic texture payload size overflow")
}
fn pack_rows(bytes: &[u8], stride: usize, row_bytes: usize, height: usize) -> Vec<u8> {
let mut packed = Vec::with_capacity(row_bytes * height);
for row in 0..height {
let start = row * stride;
packed.extend_from_slice(&bytes[start..start + row_bytes]);
}
packed
}
#[cfg(test)]
mod tests {
use super::{
BYTES_PER_PIXEL, DYNAMIC_INDEX_BASE, DynamicTexture, DynamicTextureId,
DynamicTexturePlatform, create, live_size, mark_written, painted_version, remove,
set_dynamic_texture_platform, update, validate_region,
};
use crate::{
AtlasTextureId, AtlasTextureKind, AtlasTile, Bounds, ContentMask, DevicePixels,
PolychromeSprite, ScaledPixels, Scene, TileId, point, size,
};
use anyhow::Result;
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
struct MockPlatform {
created: parking_lot::Mutex<Vec<(DynamicTextureId, crate::Size<DevicePixels>, usize)>>,
updated: parking_lot::Mutex<Vec<(DynamicTextureId, Bounds<DevicePixels>, usize)>>,
removed: parking_lot::Mutex<Vec<DynamicTextureId>>,
fail_updates: AtomicBool,
}
impl DynamicTexturePlatform for MockPlatform {
fn create_texture(
&self,
id: DynamicTextureId,
size: crate::Size<DevicePixels>,
bytes: &[u8],
) -> Result<()> {
self.created.lock().push((id, size, bytes.len()));
Ok(())
}
fn update_texture(
&self,
id: DynamicTextureId,
bounds: Bounds<DevicePixels>,
bytes: &[u8],
) -> Result<()> {
if self.fail_updates.load(Ordering::SeqCst) {
anyhow::bail!("backend refused the update");
}
self.updated.lock().push((id, bounds, bytes.len()));
Ok(())
}
fn remove_texture(&self, id: DynamicTextureId) {
self.removed.lock().push(id);
}
}
static MOCK_LOCK: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
fn shared_mock() -> &'static Arc<MockPlatform> {
static MOCK: std::sync::OnceLock<Arc<MockPlatform>> = std::sync::OnceLock::new();
MOCK.get_or_init(|| {
let mock = Arc::new(MockPlatform {
created: parking_lot::Mutex::new(Vec::new()),
updated: parking_lot::Mutex::new(Vec::new()),
removed: parking_lot::Mutex::new(Vec::new()),
fail_updates: AtomicBool::new(false),
});
set_dynamic_texture_platform(mock.clone());
mock
})
}
fn fresh_mock() -> (parking_lot::MutexGuard<'static, ()>, Arc<MockPlatform>) {
let lock = MOCK_LOCK.lock();
let mock = shared_mock();
mock.created.lock().clear();
mock.updated.lock().clear();
mock.removed.lock().clear();
mock.fail_updates.store(false, Ordering::SeqCst);
(lock, mock.clone())
}
fn size_of(width: i32, height: i32) -> crate::Size<DevicePixels> {
size(DevicePixels(width), DevicePixels(height))
}
fn bounds_at(origin: (i32, i32), width: i32, height: i32) -> Bounds<DevicePixels> {
Bounds {
origin: point(DevicePixels(origin.0), DevicePixels(origin.1)),
size: size(DevicePixels(width), DevicePixels(height)),
}
}
fn packed_bytes(width: i32, height: i32) -> Vec<u8> {
vec![0u8; width as usize * height as usize * BYTES_PER_PIXEL]
}
#[test]
fn create_reports_missing_platform() {
let error = super::require_platform(None)
.err()
.expect("a missing backend is an error");
assert_eq!(
error.to_string(),
"dynamic textures are not supported on this platform"
);
}
#[test]
fn create_requires_every_row_and_ignores_trailing_bytes() {
let (_lock, mock) = fresh_mock();
let short = create(size_of(4, 4), &packed_bytes(4, 4)[..63], 16);
assert!(short.is_err(), "one byte short must be refused");
let _padded = create(size_of(4, 4), &[0u8; 4 * 16], 16).expect("created");
{
let created = mock.created.lock();
assert_eq!(created.last().expect("created").2, 4 * 4 * BYTES_PER_PIXEL);
}
let empty = create(size_of(4, 4), &[], 16);
assert!(empty.is_err());
let zero = create(size_of(0, 4), &[], 0);
assert!(zero.is_err(), "zero width must be refused");
}
#[test]
fn create_rejects_a_stride_smaller_than_a_row() {
let (_lock, _mock) = fresh_mock();
let result = create(
size_of(4, 4),
&packed_bytes(4, 4),
3 * BYTES_PER_PIXEL as u32,
);
assert!(result.is_err());
}
#[test]
fn create_packs_rows_and_allocates_distinct_ids() {
let (_lock, mock) = fresh_mock();
let stride = (4 * BYTES_PER_PIXEL + 2) as u32;
let mut strided = vec![0u8; stride as usize + 4 * BYTES_PER_PIXEL];
for byte in strided.iter_mut().step_by(stride as usize) {
*byte = 0xFF;
}
let texture = create(size_of(4, 2), &strided, stride).expect("created");
{
let created = mock.created.lock();
let (created_id, created_size, packed_len) =
created.last().expect("create reached the platform");
assert_eq!(*created_id, texture.id);
assert_eq!(*created_size, texture.size);
assert_eq!(
*packed_len,
4 * 2 * BYTES_PER_PIXEL,
"rows must arrive tightly packed"
);
}
let other = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created again");
assert_ne!(texture.id, other.id, "ids must not repeat");
}
#[test]
fn update_validates_the_region_against_the_created_size() {
let (_lock, mock) = fresh_mock();
let texture_size = size_of(8, 8);
let texture = create(texture_size, &packed_bytes(8, 8), 32).expect("created");
let inside = bounds_at((0, 0), 4, 4);
let packed = validate_region(texture_size, inside, &packed_bytes(4, 4), 16)
.expect("a region inside the texture is accepted");
assert_eq!(packed.len(), 4 * 4 * BYTES_PER_PIXEL);
let offset = bounds_at((4, 4), 4, 4);
assert!(validate_region(texture_size, offset, &packed_bytes(4, 4), 16).is_ok());
let too_wide = bounds_at((0, 0), 9, 4);
assert!(
validate_region(texture_size, too_wide, &packed_bytes(9, 4), 36).is_err(),
"a region past the texture must be refused"
);
let negative = bounds_at((-1, 0), 4, 4);
assert!(validate_region(texture_size, negative, &packed_bytes(4, 4), 16).is_err());
let empty = bounds_at((0, 0), 0, 4);
assert!(validate_region(texture_size, empty, &[], 0).is_err());
let unknown = DynamicTexture {
id: DynamicTextureId(u32::MAX),
size: texture_size,
};
assert!(
live_size(unknown.id).is_err(),
"an unknown id must be refused"
);
remove(texture);
assert!(
live_size(texture.id).is_err(),
"a removed texture must be refused"
);
assert_eq!(mock.created.lock().len(), 1);
assert_eq!(mock.updated.lock().len(), 0);
assert_eq!(mock.removed.lock().len(), 1);
}
#[test]
fn update_requires_a_full_final_row() {
let (_lock, _mock) = fresh_mock();
let texture_size = size_of(8, 8);
let bounds = bounds_at((0, 0), 4, 4);
let row_bytes = 4 * BYTES_PER_PIXEL;
let short_by_one = &packed_bytes(4, 4)[..4 * row_bytes - 1];
assert!(
validate_region(texture_size, bounds, short_by_one, row_bytes as u32).is_err(),
"a final row missing bytes must be refused"
);
}
#[test]
fn remove_releases_the_texture_once() {
let (_lock, mock) = fresh_mock();
let texture = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
remove(texture);
remove(texture);
assert_eq!(
mock.removed.lock().len(),
1,
"the platform sees one removal"
);
}
#[test]
fn a_failing_backend_leaves_no_stale_state() {
let (_lock, mock) = fresh_mock();
mock.fail_updates.store(true, Ordering::SeqCst);
let texture = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
let version = painted_version(texture.id);
let result = update(texture, bounds_at((0, 0), 2, 2), &packed_bytes(2, 2), 8);
assert!(result.is_err(), "the backend's error must propagate");
assert!(
live_size(texture.id).is_ok(),
"the texture stays live for the next attempt"
);
assert_eq!(
painted_version(texture.id),
version,
"a refused update must not advance the version the next paint carries"
);
}
#[test]
fn atlas_ids_stay_out_of_the_atlas_range() {
let id = DynamicTextureId(0);
let atlas_id = id.atlas_texture_id();
assert_eq!(
DynamicTextureId::from_atlas_texture_id(atlas_id),
Some(id),
"the mapping must round-trip"
);
let atlas_id = AtlasTextureId {
index: 7,
kind: AtlasTextureKind::Polychrome,
};
assert_eq!(DynamicTextureId::from_atlas_texture_id(atlas_id), None);
let atlas_id = AtlasTextureId {
index: DYNAMIC_INDEX_BASE,
kind: AtlasTextureKind::Monochrome,
};
assert_eq!(
DynamicTextureId::from_atlas_texture_id(atlas_id),
None,
"a dynamic range index in another kind must not match"
);
}
fn scene_with_texture(texture: DynamicTexture, pad: u32) -> Scene {
let mut scene = Scene::default();
scene.insert_primitive(PolychromeSprite {
order: 0,
pad,
tile: AtlasTile {
texture_id: texture.id.atlas_texture_id(),
tile_id: TileId(texture.id.0),
padding: 0,
bounds: Bounds {
origin: point(DevicePixels(0), DevicePixels(0)),
size: texture.size,
},
},
grayscale: false.into(),
opacity: 1.,
bounds: Bounds {
origin: point(ScaledPixels(0.), ScaledPixels(0.)),
size: size(ScaledPixels(64.), ScaledPixels(64.)),
},
content_mask: ContentMask {
bounds: Bounds {
origin: point(ScaledPixels(0.), ScaledPixels(0.)),
size: size(ScaledPixels(128.), ScaledPixels(128.)),
},
},
corner_radii: Default::default(),
});
scene
}
#[test]
fn repainting_an_updated_texture_dirties_its_tiles() {
let (_lock, _mock) = fresh_mock();
let texture = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
let region = Bounds {
origin: point(ScaledPixels(0.), ScaledPixels(0.)),
size: size(ScaledPixels(128.), ScaledPixels(128.)),
};
let before = crate::fast::layers::tiles::tile_hashes(
&scene_with_texture(texture, painted_version(texture.id)),
64,
region,
);
update(texture, bounds_at((0, 0), 2, 2), &packed_bytes(2, 2), 8).expect("updated");
let after = crate::fast::layers::tiles::tile_hashes(
&scene_with_texture(texture, painted_version(texture.id)),
64,
region,
);
assert_ne!(before, after, "tiles showing the texture must be dirtied");
let other = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
let unchanged = crate::fast::layers::tiles::tile_hashes(
&scene_with_texture(other, painted_version(other.id)),
64,
region,
);
assert_eq!(
crate::fast::layers::tiles::tile_hashes(
&scene_with_texture(other, painted_version(other.id)),
64,
region,
),
unchanged,
);
}
#[test]
fn gpu_metadata_never_calls_the_upload_platform() {
let (_lock, mock) = fresh_mock();
assert!(DynamicTexture::new_gpu(size_of(0, 4)).is_err());
let texture = DynamicTexture::new_gpu(size_of(4, 4)).expect("GPU metadata");
assert!(mock.created.lock().is_empty());
let version = painted_version(texture.id);
mark_written(texture).expect("recorded GPU write");
assert_ne!(painted_version(texture.id), version);
assert!(update(texture, bounds_at((0, 0), 4, 4), &packed_bytes(4, 4), 16).is_err());
texture.remove();
assert!(mock.updated.lock().is_empty());
assert!(mock.removed.lock().is_empty());
assert!(mark_written(texture).is_err());
}
}