diff --git a/components/script/dom/gpucanvascontext.rs b/components/script/dom/gpucanvascontext.rs index 097b89880d9..7282cac5ba9 100644 --- a/components/script/dom/gpucanvascontext.rs +++ b/components/script/dom/gpucanvascontext.rs @@ -2,8 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::cell::Cell; - use arrayvec::ArrayVec; use dom_struct::dom_struct; use euclid::default::Size2D; @@ -98,7 +96,7 @@ pub struct GPUCanvasContext { // TODO: can we have wgpu surface that is hw accelerated inside wr ... #[ignore_malloc_size_of = "Defined in webrender"] #[no_trace] - webrender_image: Cell>, + webrender_image: ImageKey, #[no_trace] context_id: WebGPUContextId, /// @@ -111,12 +109,12 @@ impl GPUCanvasContext { if let Err(e) = channel.0.send(WebGPURequest::CreateContext(sender)) { warn!("Failed to send CreateContext ({:?})", e); } - let external_id = receiver.recv().unwrap(); + let (external_id, webrender_image) = receiver.recv().unwrap(); Self { reflector_: Reflector::new(), channel, canvas, - webrender_image: Cell::new(None), + webrender_image, context_id: WebGPUContextId(external_id.0), texture: MutNullableDom::default(), } @@ -135,12 +133,7 @@ impl GPUCanvasContext { impl GPUCanvasContext { fn layout_handle(&self) -> HTMLCanvasDataSource { - let image_key = if self.webrender_image.get().is_some() { - self.webrender_image.get().unwrap() - } else { - ImageKey::DUMMY - }; - HTMLCanvasDataSource::WebGPU(image_key) + HTMLCanvasDataSource::WebGPU(self.webrender_image) } pub fn send_swap_chain_present(&self) { @@ -251,8 +244,6 @@ impl GPUCanvasContextMethods for GPUCanvasContext { }; // Step 8 - let (sender, receiver) = ipc::channel().unwrap(); - let mut buffer_ids = ArrayVec::::new(); for _ in 0..PRESENTATION_BUFFER_COUNT { buffer_ids.push(self.global().wgpu_id_hub().create_buffer_id()); @@ -265,7 +256,7 @@ impl GPUCanvasContextMethods for GPUCanvasContext { queue_id: configuration.device.GetQueue().id().0, buffer_ids, context_id: self.context_id, - sender, + image_key: self.webrender_image, format, size: units::DeviceIntSize::new(size.width as i32, size.height as i32), }) @@ -275,24 +266,23 @@ impl GPUCanvasContextMethods for GPUCanvasContext { &configuration.device.CreateTexture(&text_desc).unwrap(), )); - self.webrender_image.set(Some(receiver.recv().unwrap())); Ok(()) } /// fn Unconfigure(&self) { - if let Some(image_key) = self.webrender_image.take() { + if let Some(texture) = self.texture.take() { if let Err(e) = self.channel.0.send(WebGPURequest::DestroySwapChain { context_id: self.context_id, - image_key, + image_key: self.webrender_image, }) { warn!( "Failed to send DestroySwapChain-ImageKey({:?}) ({})", - image_key, e + self.webrender_image, e ); } + drop(texture); } - self.texture.take(); } /// diff --git a/components/webgpu/ipc_messages/recv.rs b/components/webgpu/ipc_messages/recv.rs index 528a1e97581..2b70e10b605 100644 --- a/components/webgpu/ipc_messages/recv.rs +++ b/components/webgpu/ipc_messages/recv.rs @@ -103,7 +103,7 @@ pub enum WebGPURequest { /// present only on ASYNC versions async_sender: Option>, }, - CreateContext(IpcSender), + CreateContext(IpcSender<(WebGPUContextId, ImageKey)>), CreatePipelineLayout { device_id: id::DeviceId, pipeline_layout_id: id::PipelineLayoutId, @@ -134,7 +134,7 @@ pub enum WebGPURequest { queue_id: id::QueueId, buffer_ids: ArrayVec, context_id: WebGPUContextId, - sender: IpcSender, + image_key: ImageKey, format: ImageFormat, size: DeviceIntSize, }, diff --git a/components/webgpu/swapchain.rs b/components/webgpu/swapchain.rs index 6af661b087e..006ff993be8 100644 --- a/components/webgpu/swapchain.rs +++ b/components/webgpu/swapchain.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; use std::ops::ControlFlow; use std::ptr::NonNull; use std::slice; -use std::sync::{Arc, Mutex, MutexGuard}; +use std::sync::{Arc, Mutex}; use arrayvec::ArrayVec; use euclid::default::Size2D; @@ -234,7 +234,6 @@ impl crate::WGPU { format: ImageFormat, size: DeviceIntSize, image_key: ImageKey, - mut wr: MutexGuard, ) { let image_desc = ImageDescriptor { format, @@ -265,7 +264,10 @@ impl crate::WGPU { let mut txn = Transaction::new(); txn.add_image(image_key, image_desc, image_data, None); - wr.send_transaction(self.webrender_document, txn); + self.webrender_api + .lock() + .unwrap() + .send_transaction(self.webrender_document, txn); } /// Copies data async from provided texture using encoder_id to available staging presentation buffer diff --git a/components/webgpu/wgpu_thread.rs b/components/webgpu/wgpu_thread.rs index ac8385f25cb..22eba8d9aa3 100644 --- a/components/webgpu/wgpu_thread.rs +++ b/components/webgpu/wgpu_thread.rs @@ -410,7 +410,8 @@ impl WGPU { .lock() .expect("Lock poisoned?") .next_id(WebrenderImageHandlerType::WebGPU); - if let Err(e) = sender.send(WebGPUContextId(id.0)) { + let image_key = self.webrender_api.lock().unwrap().generate_image_key(); + if let Err(e) = sender.send((WebGPUContextId(id.0), image_key)) { warn!("Failed to send ExternalImageId to new context ({})", e); }; }, @@ -517,20 +518,12 @@ impl WGPU { queue_id, buffer_ids, context_id, - sender, + image_key, size, format, - } => { - let wr = self.webrender_api.lock().unwrap(); - let image_key = wr.generate_image_key(); - if let Err(e) = sender.send(image_key) { - warn!("Failed to send ImageKey ({})", e); - } - self.create_swapchain( - device_id, queue_id, buffer_ids, context_id, format, size, image_key, - wr, - ) - }, + } => self.create_swapchain( + device_id, queue_id, buffer_ids, context_id, format, size, image_key, + ), WebGPURequest::CreateTexture { device_id, texture_id,