Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Skip sprites whose atlas texture was released instead of panicking (#64623)
Mikayla Maki96e95edac45b
diff --git a/crates/gpui_apple/src/metal_atlas.rs b/crates/gpui_apple/src/metal_atlas.rsindex faba1388093..0e8b800ac4c 100644--- a/crates/gpui_apple/src/metal_atlas.rs+++ b/crates/gpui_apple/src/metal_atlas.rs@@ -21,8 +21,12 @@ impl MetalAtlas {})))}- pub(crate) fn metal_texture(&self, id: AtlasTextureId) -> metal::Texture {- self.0.lock().backend.texture(id).metal_texture.clone()+ /// Returns the GPU texture backing `id`, or `None` once every tile in it+ /// has been removed. A scene can still reference such a texture when a+ /// cached view replays a paint from before the image was dropped, so+ /// callers must skip those sprites rather than assume the texture exists.+ pub(crate) fn metal_texture(&self, id: AtlasTextureId) -> Option<metal::Texture> {+ Some(self.0.lock().backend.texture(id)?.metal_texture.clone())}}@@ -55,7 +59,9 @@ impl AtlasBackend for MetalAtlasTextures {bytes: &[u8],) -> Result<AtlasTile> {let tile = self.allocate(size, kind).context("failed to allocate")?;- let texture = self.texture(tile.texture_id);+ let texture = self+ .texture(tile.texture_id)+ .context("allocated tile refers to a missing texture")?;texture.upload(tile.bounds, bytes);Ok(tile)}@@ -187,13 +193,13 @@ impl MetalAtlasTextures {.unwrap()}- fn texture(&self, id: AtlasTextureId) -> &MetalAtlasTexture {+ fn texture(&self, id: AtlasTextureId) -> Option<&MetalAtlasTexture> {let textures = match id.kind {AtlasTextureKind::Monochrome => &self.monochrome_textures,AtlasTextureKind::Polychrome => &self.polychrome_textures,AtlasTextureKind::Subpixel => unreachable!(),};- textures[id.index as usize].as_ref().unwrap()+ textures.textures.get(id.index as usize)?.as_ref()}}@@ -334,7 +340,30 @@ mod tests {let tile_a2 = insert_tile(&atlas, key_a, small);// The texture must actually exist — this would panic before the fix.- let _texture = atlas.metal_texture(tile_a2.texture_id);+ assert!(atlas.metal_texture(tile_a2.texture_id).is_some());+ }++ #[test]+ fn test_metal_texture_is_none_after_last_tile_removed() {+ let Some(atlas) = create_atlas() else {+ return;+ };++ let key = make_image_key(1, 0);+ let tile = insert_tile(+ &atlas,+ key.clone(),+ Size {+ width: DevicePixels(64),+ height: DevicePixels(64),+ },+ );+ assert!(atlas.metal_texture(tile.texture_id).is_some());++ // A scene built before the removal may still carry `tile`; looking its+ // texture up must report the gap instead of panicking.+ atlas.remove(&key);+ assert!(atlas.metal_texture(tile.texture_id).is_none());}#[test]diff --git a/crates/gpui_apple/src/metal_renderer.rs b/crates/gpui_apple/src/metal_renderer.rsindex 2350718d1bb..93bac52377f 100644--- a/crates/gpui_apple/src/metal_renderer.rs+++ b/crates/gpui_apple/src/metal_renderer.rs@@ -1026,7 +1026,9 @@ impl MetalRenderer {return;}- let texture = self.sprite_atlas.metal_texture(texture_id);+ let Some(texture) = self.sprite_atlas.metal_texture(texture_id) else {+ return;+ };let texture_size = size(DevicePixels(texture.width() as i32),DevicePixels(texture.height() as i32),@@ -1080,7 +1082,9 @@ impl MetalRenderer {return;}- let texture = self.sprite_atlas.metal_texture(texture_id);+ let Some(texture) = self.sprite_atlas.metal_texture(texture_id) else {+ return;+ };let texture_size = size(DevicePixels(texture.width() as i32),DevicePixels(texture.height() as i32),diff --git a/crates/gpui_wgpu/src/wgpu_atlas.rs b/crates/gpui_wgpu/src/wgpu_atlas.rsindex 72aa46ad1ad..b541607ba38 100644--- a/crates/gpui_wgpu/src/wgpu_atlas.rs+++ b/crates/gpui_wgpu/src/wgpu_atlas.rs@@ -75,13 +75,17 @@ impl WgpuAtlas {lock.backend.flush_uploads();}- pub fn get_texture_info(&self, id: AtlasTextureId) -> WgpuTextureInfo {+ /// Returns the view backing `id`, or `None` once every tile in it has been+ /// removed. A scene can still reference such a texture when a cached view+ /// replays a paint from before the image was dropped, so callers must skip+ /// those sprites rather than assume the texture exists.+ pub fn get_texture_info(&self, id: AtlasTextureId) -> Option<WgpuTextureInfo> {let lock = self.0.lock();- let texture = &lock.backend.storage[id];- WgpuTextureInfo {+ let texture = lock.backend.storage.get(id)?;+ Some(WgpuTextureInfo {view: texture.view.clone(),generation: texture.generation,- }+ })}/// Clears all cached textures and tiles, forcing them to be recreated.@@ -331,20 +335,6 @@ impl WgpuAtlasStorage {}}-impl ops::Index<AtlasTextureId> for WgpuAtlasStorage {- type Output = WgpuAtlasTexture;- fn index(&self, id: AtlasTextureId) -> &Self::Output {- let textures = match id.kind {- AtlasTextureKind::Monochrome => &self.monochrome_textures,- AtlasTextureKind::Subpixel => &self.subpixel_textures,- AtlasTextureKind::Polychrome => &self.polychrome_textures,- };- textures[id.index as usize]- .as_ref()- .expect("texture must exist")- }-}-struct WgpuAtlasTexture {id: AtlasTextureId,generation: u64,@@ -538,11 +528,18 @@ mod tests {let first_key = make_key(1);let first_tile = insert(first_key.clone());- let first_generation = atlas.get_texture_info(first_tile.texture_id).generation;+ let first_generation = atlas+ .get_texture_info(first_tile.texture_id)+ .context("first texture should exist")?+ .generation;atlas.remove(&first_key);+ assert!(atlas.get_texture_info(first_tile.texture_id).is_none());let second_tile = insert(make_key(2));- let second_generation = atlas.get_texture_info(second_tile.texture_id).generation;+ let second_generation = atlas+ .get_texture_info(second_tile.texture_id)+ .context("second texture should exist")?+ .generation;assert_eq!(second_tile.texture_id, first_tile.texture_id);assert_ne!(second_generation, first_generation);diff --git a/crates/gpui_wgpu/src/wgpu_renderer.rs b/crates/gpui_wgpu/src/wgpu_renderer.rsindex d728711a64a..06efecd3247 100644--- a/crates/gpui_wgpu/src/wgpu_renderer.rs+++ b/crates/gpui_wgpu/src/wgpu_renderer.rs@@ -1635,7 +1635,12 @@ impl WgpuRenderer {.retain(|texture_id, _| texture_ids.contains(texture_id));for texture_id in texture_ids {- let texture_info = self.atlas.get_texture_info(texture_id);+ let Some(texture_info) = self.atlas.get_texture_info(texture_id) else {+ self.resources_mut()+ .atlas_texture_bind_groups+ .remove(&texture_id);+ continue;+ };let is_current = self.resources().atlas_texture_bind_groups@@ -1688,10 +1693,11 @@ impl WgpuRenderer {return Ok(());}let resources = self.resources();- let texture = resources- .atlas_texture_bind_groups- .get(&texture_id)- .context("missing atlas texture bind group")?;+ // The atlas has released this texture; the batch belongs to a stale+ // paint that will be replaced once its view re-renders.+ let Some(texture) = resources.atlas_texture_bind_groups.get(&texture_id) else {+ return Ok(());+ };pass.set_pipeline(pipeline);pass.set_bind_group(0, &resources.globals_bind_group, &[]);pass.set_bind_group(1, &sprite_instances.bind_group, &[]);diff --git a/crates/gpui_windows/src/directx_atlas.rs b/crates/gpui_windows/src/directx_atlas.rsindex a4ebd3c9b0c..622c1cb6040 100644--- a/crates/gpui_windows/src/directx_atlas.rs+++ b/crates/gpui_windows/src/directx_atlas.rs@@ -43,13 +43,17 @@ impl DirectXAtlas {})))}+ /// Returns the view backing `id`, or `None` once every tile in it has been+ /// removed. A scene can still reference such a texture when a cached view+ /// replays a paint from before the image was dropped, so callers must skip+ /// those sprites rather than assume the texture exists.pub(crate) fn get_texture_view(&self,id: AtlasTextureId,- ) -> [Option<ID3D11ShaderResourceView>; 1] {+ ) -> Option<[Option<ID3D11ShaderResourceView>; 1]> {let lock = self.0.lock();- let texture = lock.backend.texture(id);- texture.view.clone()+ let texture = lock.backend.texture(id)?;+ Some(texture.view.clone())}pub(crate) fn handle_device_lost(@@ -94,7 +98,9 @@ impl AtlasBackend for DirectXAtlasTextures {let tile = self.allocate(size, kind).ok_or_else(|| anyhow::anyhow!("failed to allocate"))?;- let texture = self.texture(tile.texture_id);+ let texture = self+ .texture(tile.texture_id)+ .ok_or_else(|| anyhow::anyhow!("allocated tile refers to a missing texture"))?;texture.upload(&self.device_context, tile.bounds, bytes);Ok(tile)}@@ -244,18 +250,13 @@ impl DirectXAtlasTextures {}}- fn texture(&self, id: AtlasTextureId) -> &DirectXAtlasTexture {- match id.kind {- AtlasTextureKind::Monochrome => &self.monochrome_textures[id.index as usize]- .as_ref()- .unwrap(),- AtlasTextureKind::Polychrome => &self.polychrome_textures[id.index as usize]- .as_ref()- .unwrap(),- AtlasTextureKind::Subpixel => {- &self.subpixel_textures[id.index as usize].as_ref().unwrap()- }- }+ fn texture(&self, id: AtlasTextureId) -> Option<&DirectXAtlasTexture> {+ let textures = match id.kind {+ AtlasTextureKind::Monochrome => &self.monochrome_textures,+ AtlasTextureKind::Polychrome => &self.polychrome_textures,+ AtlasTextureKind::Subpixel => &self.subpixel_textures,+ };+ textures.textures.get(id.index as usize)?.as_ref()}}diff --git a/crates/gpui_windows/src/directx_renderer.rs b/crates/gpui_windows/src/directx_renderer.rsindex 3f53556e1d1..f8b86bc25bd 100644--- a/crates/gpui_windows/src/directx_renderer.rs+++ b/crates/gpui_windows/src/directx_renderer.rs@@ -745,7 +745,9 @@ impl DirectXRenderer {return Ok(());}let devices = self.devices.as_ref().context("devices missing")?;- let texture_view = self.atlas.get_texture_view(texture_id);+ let Some(texture_view) = self.atlas.get_texture_view(texture_id) else {+ return Ok(());+ };self.pipelines.mono_sprites.draw_range_with_texture(&devices.device_context,&texture_view,@@ -769,7 +771,9 @@ impl DirectXRenderer {return Ok(());}let devices = self.devices.as_ref().context("devices missing")?;- let texture_view = self.atlas.get_texture_view(texture_id);+ let Some(texture_view) = self.atlas.get_texture_view(texture_id) else {+ return Ok(());+ };self.pipelines.subpixel_sprites.draw_range_with_texture(&devices.device_context,&texture_view,@@ -793,7 +797,9 @@ impl DirectXRenderer {return Ok(());}let devices = self.devices.as_ref().context("devices missing")?;- let texture_view = self.atlas.get_texture_view(texture_id);+ let Some(texture_view) = self.atlas.get_texture_view(texture_id) else {+ return Ok(());+ };self.pipelines.poly_sprites.draw_range_with_texture(&devices.device_context,&texture_view,