1#[cfg(not(target_family = "wasm"))]
2use crate::CompositorGpuHint;
3use crate::{WgpuAtlas, WgpuContext};
4use bytemuck::{Pod, Zeroable};
5use gpui::{
6 AtlasTextureId, Background, Bounds, DevicePixels, GpuSpecs, MonochromeSprite, Path, Point,
7 PolychromeSprite, PrimitiveBatch, Quad, ScaledPixels, Scene, Shadow, Size, SubpixelSprite,
8 Underline, get_gamma_correction_ratios,
…lines 9–94 unchanged (86 lines)
95
96pub struct WgpuRenderer {
97 device: Arc<wgpu::Device>,
98 queue: Arc<wgpu::Queue>,
99 surface: wgpu::Surface<'static>,
100 surface_config: wgpu::SurfaceConfiguration,
101 surface_configured: bool,
102 pipelines: WgpuPipelines,
103 bind_group_layouts: WgpuBindGroupLayouts,
104 atlas: Arc<WgpuAtlas>,
105 atlas_sampler: wgpu::Sampler,
106 globals_buffer: wgpu::Buffer,
107 path_globals_offset: u64,
…lines 108–131 unchanged (24 lines)
132 /// of the returned renderer.
133 #[cfg(not(target_family = "wasm"))]
134 pub fn new<W: HasWindowHandle + HasDisplayHandle>(
135 gpu_context: &mut Option<WgpuContext>,
136 window: &W,
137 config: WgpuSurfaceConfig,
138 compositor_gpu: Option<CompositorGpuHint>,
139 ) -> anyhow::Result<Self> {
140 let window_handle = window
141 .window_handle()
142 .map_err(|e| anyhow::anyhow!("Failed to get window handle: {e}"))?;
143 let display_handle = window
144 .display_handle()
…lines 145–167 unchanged (23 lines)
168
169 let context = match gpu_context {
170 Some(context) => {
171 context.check_compatible_with_surface(&surface)?;
172 context
173 }
174 None => gpu_context.insert(WgpuContext::new(instance, &surface, compositor_gpu)?),
175 };
176
177 Self::new_with_surface(context, surface, config)
178 }
179
180 #[cfg(target_family = "wasm")]
…lines 181–185 unchanged (5 lines)
186 let surface = context
187 .instance
188 .create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
189 .map_err(|e| anyhow::anyhow!("Failed to create surface: {e}"))?;
190 Self::new_with_surface(context, surface, config)
191 }
192
193 fn new_with_surface(
194 context: &WgpuContext,
195 surface: wgpu::Surface<'static>,
196 config: WgpuSurfaceConfig,
197 ) -> anyhow::Result<Self> {
198 let surface_caps = surface.get_capabilities(&context.adapter);
199 let preferred_formats = [
200 wgpu::TextureFormat::Bgra8Unorm,
201 wgpu::TextureFormat::Rgba8Unorm,
202 ];
203 let surface_format = preferred_formats
…lines 204–266 unchanged (63 lines)
267 height: clamped_height.max(1),
268 present_mode: wgpu::PresentMode::Fifo,
269 desired_maximum_frame_latency: 2,
270 alpha_mode,
271 view_formats: vec![],
272 };
273 // Configure the surface immediately. The adapter selection process already validated
274 // that this adapter can successfully configure this surface.
275 surface.configure(&context.device, &surface_config);
276
277 let queue = Arc::clone(&context.queue);
278 let dual_source_blending = context.supports_dual_source_blending();
279
280 let rendering_params = RenderingParameters::new(&context.adapter, surface_format);
…lines 281–368 unchanged (88 lines)
369
370 Ok(Self {
371 device,
372 queue,
373 surface,
374 surface_config,
375 surface_configured: true,
376 pipelines,
377 bind_group_layouts,
378 atlas,
379 atlas_sampler,
380 globals_buffer,
381 path_globals_offset,
…lines 382–860 unchanged (479 lines)
861 if let Some(ref texture) = self.path_msaa_texture {
862 texture.destroy();
863 }
864
865 self.surface_config.width = clamped_width.max(1);
866 self.surface_config.height = clamped_height.max(1);
867 if self.surface_configured {
868 self.surface.configure(&self.device, &self.surface_config);
869 }
870
871 // Invalidate intermediate textures - they will be lazily recreated
872 // in draw() after we confirm the surface is healthy. This avoids
873 // panics when the device/surface is in an invalid state during resize.
874 self.path_intermediate_texture = None;
875 self.path_intermediate_view = None;
…lines 876–913 unchanged (38 lines)
914 } else {
915 self.opaque_alpha_mode
916 };
917
918 if new_alpha_mode != self.surface_config.alpha_mode {
919 self.surface_config.alpha_mode = new_alpha_mode;
920 if self.surface_configured {
921 self.surface.configure(&self.device, &self.surface_config);
922 }
923 self.pipelines = Self::create_pipelines(
924 &self.device,
925 &self.bind_group_layouts,
926 self.surface_config.format,
927 self.surface_config.alpha_mode,
928 self.rendering_params.path_sample_count,
…lines 929–962 unchanged (34 lines)
963 pub fn draw(&mut self, scene: &Scene) {
964 self.atlas.before_frame();
965
966 let frame = match self.surface.get_current_texture() {
967 Ok(frame) => frame,
968 Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
969 self.surface_configured = false;
970 return;
971 }
972 Err(e) => {
973 log::error!("Failed to acquire surface texture: {e}");
974 return;
975 }
…lines 976–1541 unchanged (566 lines)