1use anyhow::{Context as _, Result};
24use std::{
25 collections::HashMap,
26 sync::{
27 Arc,
28 atomic::{AtomicBool, AtomicU32, Ordering},
29 },
30};
31
32use crate::fast::layers::scene::LAYER_TILE_TEXTURE_BASE;
33use crate::{
34 AtlasTextureId, AtlasTextureKind, AtlasTile, Bounds, DevicePixels, Pixels, PolychromeSprite,
35 TileId, Window, point,
36};
37
38const BYTES_PER_PIXEL: usize = 4;
41
42const DYNAMIC_INDEX_BASE: u32 = 0xE000_0000;
51
52const DYNAMIC_ID_LIMIT: u32 = LAYER_TILE_TEXTURE_BASE - DYNAMIC_INDEX_BASE;
54
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
57pub struct DynamicTextureId(u32);
58
59impl DynamicTextureId {
60 pub fn atlas_texture_id(self) -> AtlasTextureId {
64 AtlasTextureId {
65 index: DYNAMIC_INDEX_BASE.wrapping_add(self.0),
66 kind: AtlasTextureKind::Polychrome,
67 }
68 }
69
70 pub fn from_atlas_texture_id(id: AtlasTextureId) -> Option<Self> {
72 (id.kind == AtlasTextureKind::Polychrome
73 && id.index >= DYNAMIC_INDEX_BASE
74 && id.index - DYNAMIC_INDEX_BASE < DYNAMIC_ID_LIMIT)
75 .then(|| Self(id.index - DYNAMIC_INDEX_BASE))
76 }
77}
78
79#[derive(Clone, Copy, Debug, PartialEq, Eq)]
86pub struct DynamicTexture {
87 id: DynamicTextureId,
88 size: crate::Size<DevicePixels>,
89}
90
91impl DynamicTexture {
92 pub fn id(&self) -> DynamicTextureId {
95 self.id
96 }
97
98 pub fn new_gpu(size: crate::Size<DevicePixels>) -> Result<Self> {
104 anyhow::ensure!(
105 size.width.0 > 0 && size.height.0 > 0,
106 "dynamic texture dimensions must be positive"
107 );
108 allocate(size, TextureSource::Gpu, |_| Ok(()))
109 }
110
111 pub fn remove(self) {
117 remove(self);
118 }
119}
120
121pub trait DynamicTexturePlatform: Send + Sync {
127 fn create_texture(
129 &self,
130 id: DynamicTextureId,
131 size: crate::Size<DevicePixels>,
132 bytes: &[u8],
133 ) -> Result<()>;
134
135 fn update_texture(
142 &self,
143 id: DynamicTextureId,
144 bounds: Bounds<DevicePixels>,
145 bytes: &[u8],
146 ) -> Result<()>;
147
148 fn remove_texture(&self, id: DynamicTextureId);
150}
151
152static PLATFORM: parking_lot::RwLock<Option<Arc<dyn DynamicTexturePlatform>>> =
153 parking_lot::RwLock::new(None);
154
155pub fn set_dynamic_texture_platform(backend: Arc<dyn DynamicTexturePlatform>) {
161 let mut platform = PLATFORM.write();
162 if platform.is_none() {
163 *platform = Some(backend);
164 }
165}
166
167fn platform() -> Result<Arc<dyn DynamicTexturePlatform>> {
168 require_platform(PLATFORM.read().clone())
169}
170
171fn require_platform(
177 platform: Option<Arc<dyn DynamicTexturePlatform>>,
178) -> Result<Arc<dyn DynamicTexturePlatform>> {
179 platform.context("dynamic textures are not supported on this platform")
180}
181
182static NEXT_TEXTURE_ID: AtomicU32 = AtomicU32::new(0);
183
184static IDS_EXHAUSTED: AtomicBool = AtomicBool::new(false);
189
190static LIVE_TEXTURES: parking_lot::Mutex<Option<HashMap<DynamicTextureId, TextureState>>> =
193 parking_lot::Mutex::new(None);
194
195#[derive(Clone, Copy)]
197struct TextureState {
198 size: crate::Size<DevicePixels>,
199 source: TextureSource,
200 version: u32,
205}
206
207#[derive(Clone, Copy, PartialEq, Eq)]
208enum TextureSource {
209 Upload,
210 Gpu,
211}
212
213fn live_textures() -> parking_lot::MappedMutexGuard<'static, HashMap<DynamicTextureId, TextureState>>
214{
215 parking_lot::MutexGuard::map(LIVE_TEXTURES.lock(), |textures| {
216 textures.get_or_insert_with(HashMap::new)
217 })
218}
219
220fn painted_version(id: DynamicTextureId) -> u32 {
224 live_textures()
225 .get(&id)
226 .map_or(1, |state| state.version.max(1))
227}
228
229pub(crate) fn create(
237 size: crate::Size<DevicePixels>,
238 bytes: &[u8],
239 bytes_per_row: u32,
240) -> Result<DynamicTexture> {
241 let width = usize::try_from(size.width.0).context("negative texture width")?;
242 let height = usize::try_from(size.height.0).context("negative texture height")?;
243 anyhow::ensure!(
244 width > 0 && height > 0,
245 "dynamic texture dimensions must be positive"
246 );
247
248 let row_bytes = width
249 .checked_mul(BYTES_PER_PIXEL)
250 .context("dynamic texture row size overflow")?;
251 let stride = usize::try_from(bytes_per_row).context("negative bytes per row")?;
252 anyhow::ensure!(
253 stride >= row_bytes,
254 "dynamic texture stride is smaller than one row"
255 );
256
257 let required = checked_payload_len(height, stride, row_bytes)?;
258 anyhow::ensure!(
259 bytes.len() >= required,
260 "dynamic texture payload is shorter than its dimensions"
261 );
262
263 let packed = pack_rows(bytes, stride, row_bytes, height);
264 let backend = platform()?;
265 allocate(size, TextureSource::Upload, |id| {
266 backend.create_texture(id, size, &packed)
267 })
268}
269
270fn allocate(
273 size: crate::Size<DevicePixels>,
274 source: TextureSource,
275 create: impl FnOnce(DynamicTextureId) -> Result<()>,
276) -> Result<DynamicTexture> {
277 let id = DynamicTextureId(NEXT_TEXTURE_ID.fetch_add(1, Ordering::Relaxed));
278 if IDS_EXHAUSTED.load(Ordering::Relaxed) || id.0 >= DYNAMIC_ID_LIMIT {
283 IDS_EXHAUSTED.store(true, Ordering::Relaxed);
284 anyhow::bail!("dynamic texture ids are exhausted");
285 }
286 create(id).with_context(|| format!("failed to create dynamic texture {id:?}"))?;
287 live_textures().insert(
288 id,
289 TextureState {
290 size,
291 source,
292 version: 1,
293 },
294 );
295
296 Ok(DynamicTexture { id, size })
297}
298
299pub(crate) fn update(
306 texture: DynamicTexture,
307 bounds: Bounds<DevicePixels>,
308 bytes: &[u8],
309 bytes_per_row: u32,
310) -> Result<()> {
311 let state = live_textures()
312 .get(&texture.id)
313 .copied()
314 .context("unknown dynamic texture")?;
315 anyhow::ensure!(
316 state.source == TextureSource::Upload,
317 "GPU-written textures must be updated by their producer"
318 );
319 let packed = validate_region(state.size, bounds, bytes, bytes_per_row)?;
320
321 platform()?
322 .update_texture(texture.id, bounds, &packed)
323 .with_context(|| format!("failed to update dynamic texture {texture:?}"))?;
324 if let Some(state) = live_textures().get_mut(&texture.id) {
325 state.version = state.version.wrapping_add(1).max(1);
326 }
327
328 Ok(())
329}
330
331pub(crate) fn mark_written(texture: DynamicTexture) -> Result<()> {
334 let mut textures = live_textures();
335 let state = textures
336 .get_mut(&texture.id)
337 .context("unknown dynamic texture")?;
338 state.version = state.version.wrapping_add(1).max(1);
339 Ok(())
340}
341
342pub(crate) fn paint(
346 window: &mut Window,
347 bounds: Bounds<Pixels>,
348 texture: DynamicTexture,
349) -> Result<()> {
350 window.invalidator.debug_assert_paint();
351
352 let texture_size = live_size(texture.id)?;
353
354 let tile = AtlasTile {
360 texture_id: texture.id.atlas_texture_id(),
361 tile_id: TileId(texture.id.0),
362 padding: 0,
363 bounds: Bounds {
364 origin: point(DevicePixels(0), DevicePixels(0)),
365 size: texture_size,
366 },
367 };
368 window.next_frame.scene.insert_primitive(PolychromeSprite {
369 order: 0,
370 pad: painted_version(texture.id),
371 grayscale: false.into(),
372 bounds: window.snap_bounds(bounds),
373 content_mask: window.snapped_content_mask(),
374 corner_radii: Default::default(),
375 tile,
376 opacity: window.element_opacity(),
377 });
378
379 Ok(())
380}
381
382pub(crate) fn remove(texture: DynamicTexture) {
384 let state = live_textures().remove(&texture.id);
385 if state.is_some_and(|state| state.source == TextureSource::Upload)
386 && let Ok(backend) = platform()
387 {
388 backend.remove_texture(texture.id);
389 }
390}
391
392impl Window {
393 pub fn create_dynamic_texture(
403 &mut self,
404 size: crate::Size<DevicePixels>,
405 bytes: &[u8],
406 bytes_per_row: u32,
407 ) -> Result<DynamicTexture> {
408 create(size, bytes, bytes_per_row)
409 }
410
411 pub fn update_dynamic_texture(
429 &mut self,
430 texture: DynamicTexture,
431 bounds: Bounds<DevicePixels>,
432 bytes: &[u8],
433 bytes_per_row: u32,
434 ) -> Result<()> {
435 update(texture, bounds, bytes, bytes_per_row)?;
436 self.refresh();
437 Ok(())
438 }
439
440 pub fn refresh_dynamic_texture(&mut self, texture: DynamicTexture) -> Result<()> {
447 mark_written(texture)?;
448 self.refresh();
449 Ok(())
450 }
451
452 pub fn paint_dynamic_texture(
457 &mut self,
458 bounds: Bounds<Pixels>,
459 texture: DynamicTexture,
460 ) -> Result<()> {
461 paint(self, bounds, texture)
462 }
463
464 pub fn remove_dynamic_texture(&mut self, texture: DynamicTexture) {
466 remove(texture);
467 }
468}
469
470fn live_size(id: DynamicTextureId) -> Result<crate::Size<DevicePixels>> {
472 live_textures()
473 .get(&id)
474 .map(|state| state.size)
475 .context("unknown dynamic texture")
476}
477
478fn validate_region(
481 texture_size: crate::Size<DevicePixels>,
482 bounds: Bounds<DevicePixels>,
483 bytes: &[u8],
484 bytes_per_row: u32,
485) -> Result<Vec<u8>> {
486 let (origin_x, origin_y) = (bounds.origin.x.0 as i64, bounds.origin.y.0 as i64);
489 let (width, height) = (bounds.size.width.0 as i64, bounds.size.height.0 as i64);
490 anyhow::ensure!(
491 origin_x >= 0
492 && origin_y >= 0
493 && width > 0
494 && height > 0
495 && origin_x + width <= texture_size.width.0 as i64
496 && origin_y + height <= texture_size.height.0 as i64,
497 "dynamic texture update is outside the texture"
498 );
499
500 let (width, height) = (width as usize, height as usize);
503 let row_bytes = width
504 .checked_mul(BYTES_PER_PIXEL)
505 .context("dynamic texture region row size overflow")?;
506 let stride = usize::try_from(bytes_per_row).context("negative bytes per row")?;
507 anyhow::ensure!(
508 stride >= row_bytes,
509 "dynamic texture stride is smaller than one row"
510 );
511
512 let required = checked_payload_len(height, stride, row_bytes)?;
513 anyhow::ensure!(
514 bytes.len() >= required,
515 "dynamic texture update is shorter than the region"
516 );
517
518 Ok(pack_rows(bytes, stride, row_bytes, height))
519}
520
521fn checked_payload_len(height: usize, stride: usize, row_bytes: usize) -> Result<usize> {
524 (height - 1)
525 .checked_mul(stride)
526 .and_then(|length| length.checked_add(row_bytes))
527 .context("dynamic texture payload size overflow")
528}
529
530fn pack_rows(bytes: &[u8], stride: usize, row_bytes: usize, height: usize) -> Vec<u8> {
532 let mut packed = Vec::with_capacity(row_bytes * height);
533 for row in 0..height {
534 let start = row * stride;
535 packed.extend_from_slice(&bytes[start..start + row_bytes]);
536 }
537 packed
538}
539
540#[cfg(test)]
541mod tests {
542 use super::{
543 BYTES_PER_PIXEL, DYNAMIC_INDEX_BASE, DynamicTexture, DynamicTextureId,
544 DynamicTexturePlatform, create, live_size, mark_written, painted_version, remove,
545 set_dynamic_texture_platform, update, validate_region,
546 };
547 use crate::{
548 AtlasTextureId, AtlasTextureKind, AtlasTile, Bounds, ContentMask, DevicePixels,
549 PolychromeSprite, ScaledPixels, Scene, TileId, point, size,
550 };
551 use anyhow::Result;
552 use std::sync::{
553 Arc,
554 atomic::{AtomicBool, Ordering},
555 };
556
557 struct MockPlatform {
562 created: parking_lot::Mutex<Vec<(DynamicTextureId, crate::Size<DevicePixels>, usize)>>,
563 updated: parking_lot::Mutex<Vec<(DynamicTextureId, Bounds<DevicePixels>, usize)>>,
564 removed: parking_lot::Mutex<Vec<DynamicTextureId>>,
565 fail_updates: AtomicBool,
566 }
567
568 impl DynamicTexturePlatform for MockPlatform {
569 fn create_texture(
570 &self,
571 id: DynamicTextureId,
572 size: crate::Size<DevicePixels>,
573 bytes: &[u8],
574 ) -> Result<()> {
575 self.created.lock().push((id, size, bytes.len()));
576 Ok(())
577 }
578
579 fn update_texture(
580 &self,
581 id: DynamicTextureId,
582 bounds: Bounds<DevicePixels>,
583 bytes: &[u8],
584 ) -> Result<()> {
585 if self.fail_updates.load(Ordering::SeqCst) {
586 anyhow::bail!("backend refused the update");
587 }
588 self.updated.lock().push((id, bounds, bytes.len()));
589 Ok(())
590 }
591
592 fn remove_texture(&self, id: DynamicTextureId) {
593 self.removed.lock().push(id);
594 }
595 }
596
597 static MOCK_LOCK: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
599
600 fn shared_mock() -> &'static Arc<MockPlatform> {
602 static MOCK: std::sync::OnceLock<Arc<MockPlatform>> = std::sync::OnceLock::new();
603 MOCK.get_or_init(|| {
604 let mock = Arc::new(MockPlatform {
605 created: parking_lot::Mutex::new(Vec::new()),
606 updated: parking_lot::Mutex::new(Vec::new()),
607 removed: parking_lot::Mutex::new(Vec::new()),
608 fail_updates: AtomicBool::new(false),
609 });
610 set_dynamic_texture_platform(mock.clone());
611 mock
612 })
613 }
614
615 fn fresh_mock() -> (parking_lot::MutexGuard<'static, ()>, Arc<MockPlatform>) {
617 let lock = MOCK_LOCK.lock();
618 let mock = shared_mock();
619 mock.created.lock().clear();
620 mock.updated.lock().clear();
621 mock.removed.lock().clear();
622 mock.fail_updates.store(false, Ordering::SeqCst);
623 (lock, mock.clone())
624 }
625
626 fn size_of(width: i32, height: i32) -> crate::Size<DevicePixels> {
627 size(DevicePixels(width), DevicePixels(height))
628 }
629
630 fn bounds_at(origin: (i32, i32), width: i32, height: i32) -> Bounds<DevicePixels> {
631 Bounds {
632 origin: point(DevicePixels(origin.0), DevicePixels(origin.1)),
633 size: size(DevicePixels(width), DevicePixels(height)),
634 }
635 }
636
637 fn packed_bytes(width: i32, height: i32) -> Vec<u8> {
639 vec![0u8; width as usize * height as usize * BYTES_PER_PIXEL]
640 }
641
642 #[test]
643 fn create_reports_missing_platform() {
644 let error = super::require_platform(None)
648 .err()
649 .expect("a missing backend is an error");
650 assert_eq!(
651 error.to_string(),
652 "dynamic textures are not supported on this platform"
653 );
654 }
655
656 #[test]
657 fn create_requires_every_row_and_ignores_trailing_bytes() {
658 let (_lock, mock) = fresh_mock();
659
660 let short = create(size_of(4, 4), &packed_bytes(4, 4)[..63], 16);
661 assert!(short.is_err(), "one byte short must be refused");
662
663 let _padded = create(size_of(4, 4), &[0u8; 4 * 16], 16).expect("created");
666 {
667 let created = mock.created.lock();
668 assert_eq!(created.last().expect("created").2, 4 * 4 * BYTES_PER_PIXEL);
669 }
670
671 let empty = create(size_of(4, 4), &[], 16);
672 assert!(empty.is_err());
673
674 let zero = create(size_of(0, 4), &[], 0);
675 assert!(zero.is_err(), "zero width must be refused");
676 }
677
678 #[test]
679 fn create_rejects_a_stride_smaller_than_a_row() {
680 let (_lock, _mock) = fresh_mock();
681
682 let result = create(
683 size_of(4, 4),
684 &packed_bytes(4, 4),
685 3 * BYTES_PER_PIXEL as u32,
686 );
687 assert!(result.is_err());
688 }
689
690 #[test]
691 fn create_packs_rows_and_allocates_distinct_ids() {
692 let (_lock, mock) = fresh_mock();
693
694 let stride = (4 * BYTES_PER_PIXEL + 2) as u32;
698 let mut strided = vec![0u8; stride as usize + 4 * BYTES_PER_PIXEL];
699 for byte in strided.iter_mut().step_by(stride as usize) {
700 *byte = 0xFF;
701 }
702
703 let texture = create(size_of(4, 2), &strided, stride).expect("created");
704 {
705 let created = mock.created.lock();
706 let (created_id, created_size, packed_len) =
707 created.last().expect("create reached the platform");
708 assert_eq!(*created_id, texture.id);
709 assert_eq!(*created_size, texture.size);
710 assert_eq!(
711 *packed_len,
712 4 * 2 * BYTES_PER_PIXEL,
713 "rows must arrive tightly packed"
714 );
715 }
716
717 let other = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created again");
720 assert_ne!(texture.id, other.id, "ids must not repeat");
721 }
722
723 #[test]
724 fn update_validates_the_region_against_the_created_size() {
725 let (_lock, mock) = fresh_mock();
726
727 let texture_size = size_of(8, 8);
728 let texture = create(texture_size, &packed_bytes(8, 8), 32).expect("created");
729
730 let inside = bounds_at((0, 0), 4, 4);
731 let packed = validate_region(texture_size, inside, &packed_bytes(4, 4), 16)
732 .expect("a region inside the texture is accepted");
733 assert_eq!(packed.len(), 4 * 4 * BYTES_PER_PIXEL);
734
735 let offset = bounds_at((4, 4), 4, 4);
736 assert!(validate_region(texture_size, offset, &packed_bytes(4, 4), 16).is_ok());
737
738 let too_wide = bounds_at((0, 0), 9, 4);
739 assert!(
740 validate_region(texture_size, too_wide, &packed_bytes(9, 4), 36).is_err(),
741 "a region past the texture must be refused"
742 );
743
744 let negative = bounds_at((-1, 0), 4, 4);
745 assert!(validate_region(texture_size, negative, &packed_bytes(4, 4), 16).is_err());
746
747 let empty = bounds_at((0, 0), 0, 4);
748 assert!(validate_region(texture_size, empty, &[], 0).is_err());
749
750 let unknown = DynamicTexture {
751 id: DynamicTextureId(u32::MAX),
752 size: texture_size,
753 };
754 assert!(
755 live_size(unknown.id).is_err(),
756 "an unknown id must be refused"
757 );
758
759 remove(texture);
760 assert!(
761 live_size(texture.id).is_err(),
762 "a removed texture must be refused"
763 );
764 assert_eq!(mock.created.lock().len(), 1);
765 assert_eq!(mock.updated.lock().len(), 0);
766 assert_eq!(mock.removed.lock().len(), 1);
767 }
768
769 #[test]
770 fn update_requires_a_full_final_row() {
771 let (_lock, _mock) = fresh_mock();
772
773 let texture_size = size_of(8, 8);
774 let bounds = bounds_at((0, 0), 4, 4);
775 let row_bytes = 4 * BYTES_PER_PIXEL;
776
777 let short_by_one = &packed_bytes(4, 4)[..4 * row_bytes - 1];
778 assert!(
779 validate_region(texture_size, bounds, short_by_one, row_bytes as u32).is_err(),
780 "a final row missing bytes must be refused"
781 );
782 }
783
784 #[test]
785 fn remove_releases_the_texture_once() {
786 let (_lock, mock) = fresh_mock();
787
788 let texture = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
789 remove(texture);
790 remove(texture);
791
792 assert_eq!(
793 mock.removed.lock().len(),
794 1,
795 "the platform sees one removal"
796 );
797 }
798
799 #[test]
800 fn a_failing_backend_leaves_no_stale_state() {
801 let (_lock, mock) = fresh_mock();
802 mock.fail_updates.store(true, Ordering::SeqCst);
803
804 let texture = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
805 let version = painted_version(texture.id);
806 let result = update(texture, bounds_at((0, 0), 2, 2), &packed_bytes(2, 2), 8);
807 assert!(result.is_err(), "the backend's error must propagate");
808 assert!(
809 live_size(texture.id).is_ok(),
810 "the texture stays live for the next attempt"
811 );
812 assert_eq!(
813 painted_version(texture.id),
814 version,
815 "a refused update must not advance the version the next paint carries"
816 );
817 }
818
819 #[test]
820 fn atlas_ids_stay_out_of_the_atlas_range() {
821 let id = DynamicTextureId(0);
822 let atlas_id = id.atlas_texture_id();
823 assert_eq!(
824 DynamicTextureId::from_atlas_texture_id(atlas_id),
825 Some(id),
826 "the mapping must round-trip"
827 );
828
829 let atlas_id = AtlasTextureId {
830 index: 7,
831 kind: AtlasTextureKind::Polychrome,
832 };
833 assert_eq!(DynamicTextureId::from_atlas_texture_id(atlas_id), None);
834
835 let atlas_id = AtlasTextureId {
836 index: DYNAMIC_INDEX_BASE,
837 kind: AtlasTextureKind::Monochrome,
838 };
839 assert_eq!(
840 DynamicTextureId::from_atlas_texture_id(atlas_id),
841 None,
842 "a dynamic range index in another kind must not match"
843 );
844 }
845
846 fn scene_with_texture(texture: DynamicTexture, pad: u32) -> Scene {
850 let mut scene = Scene::default();
851 scene.insert_primitive(PolychromeSprite {
852 order: 0,
853 pad,
854 tile: AtlasTile {
855 texture_id: texture.id.atlas_texture_id(),
856 tile_id: TileId(texture.id.0),
857 padding: 0,
858 bounds: Bounds {
859 origin: point(DevicePixels(0), DevicePixels(0)),
860 size: texture.size,
861 },
862 },
863 grayscale: false.into(),
864 opacity: 1.,
865 bounds: Bounds {
866 origin: point(ScaledPixels(0.), ScaledPixels(0.)),
867 size: size(ScaledPixels(64.), ScaledPixels(64.)),
868 },
869 content_mask: ContentMask {
870 bounds: Bounds {
871 origin: point(ScaledPixels(0.), ScaledPixels(0.)),
872 size: size(ScaledPixels(128.), ScaledPixels(128.)),
873 },
874 },
875 corner_radii: Default::default(),
876 });
877 scene
878 }
879
880 #[test]
881 fn repainting_an_updated_texture_dirties_its_tiles() {
882 let (_lock, _mock) = fresh_mock();
883
884 let texture = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
885 let region = Bounds {
886 origin: point(ScaledPixels(0.), ScaledPixels(0.)),
887 size: size(ScaledPixels(128.), ScaledPixels(128.)),
888 };
889
890 let before = crate::fast::layers::tiles::tile_hashes(
891 &scene_with_texture(texture, painted_version(texture.id)),
892 64,
893 region,
894 );
895
896 update(texture, bounds_at((0, 0), 2, 2), &packed_bytes(2, 2), 8).expect("updated");
899
900 let after = crate::fast::layers::tiles::tile_hashes(
901 &scene_with_texture(texture, painted_version(texture.id)),
902 64,
903 region,
904 );
905 assert_ne!(before, after, "tiles showing the texture must be dirtied");
906
907 let other = create(size_of(4, 4), &packed_bytes(4, 4), 16).expect("created");
910 let unchanged = crate::fast::layers::tiles::tile_hashes(
911 &scene_with_texture(other, painted_version(other.id)),
912 64,
913 region,
914 );
915 assert_eq!(
916 crate::fast::layers::tiles::tile_hashes(
917 &scene_with_texture(other, painted_version(other.id)),
918 64,
919 region,
920 ),
921 unchanged,
922 );
923 }
924
925 #[test]
926 fn gpu_metadata_never_calls_the_upload_platform() {
927 let (_lock, mock) = fresh_mock();
928 assert!(DynamicTexture::new_gpu(size_of(0, 4)).is_err());
929 let texture = DynamicTexture::new_gpu(size_of(4, 4)).expect("GPU metadata");
930 assert!(mock.created.lock().is_empty());
931 let version = painted_version(texture.id);
932 mark_written(texture).expect("recorded GPU write");
933 assert_ne!(painted_version(texture.id), version);
934 assert!(update(texture, bounds_at((0, 0), 4, 4), &packed_bytes(4, 4), 16).is_err());
935 texture.remove();
936 assert!(mock.updated.lock().is_empty());
937 assert!(mock.removed.lock().is_empty());
938 assert!(mark_written(texture).is_err());
939 }
940}