From d75b4512d0f8e83929d7e1a8b641eb1c7f05cebe Mon Sep 17 00:00:00 2001 From: Adoo Date: Wed, 2 Oct 2024 23:34:45 +0800 Subject: [PATCH] reuse gpu impl in test --- .github/workflows/ci.yml | 1 + dev-helper/src/image_test.rs | 29 +++++++++++++++-------------- gpu/src/gpu_backend.rs | 3 +++ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 726cb32bc..408e7b66e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -258,6 +258,7 @@ jobs: - name: compile to wasm run: cargo build --workspace --target wasm32-unknown-unknown --exclude ribir_dev_helper wasm-test: + needs: lint name: wasm test runs-on: ubuntu-latest steps: diff --git a/dev-helper/src/image_test.rs b/dev-helper/src/image_test.rs index 542fccbe1..3c459d9da 100644 --- a/dev-helper/src/image_test.rs +++ b/dev-helper/src/image_test.rs @@ -1,5 +1,3 @@ -use std::sync::Mutex; - use ribir_geom::Transform; use ribir_painter::{image::ColorFormat, PixelImage}; @@ -180,29 +178,32 @@ pub fn wgpu_render_commands( commands: &[ribir_painter::PaintCommand], viewport: ribir_geom::DeviceRect, surface: ribir_painter::Color, ) -> PixelImage { + use std::sync::Mutex; + use futures::executor::block_on; use ribir_geom::{DeviceRect, DeviceSize}; - use ribir_gpu::{GPUBackend, GPUBackendImpl, Texture}; + use ribir_gpu::{GPUBackend, GPUBackendImpl, Texture, WgpuImpl}; use ribir_painter::PainterBackend; - static LOCK: Mutex<()> = Mutex::new(()); + static WGPU_IMPL: Mutex> = Mutex::new(None); - // Let's ensure that the GPU tests run in single threads as the resources of the - // CI machine may not support multiple tests simultaneously. - let _single = LOCK.lock(); - - let mut gpu_impl = block_on(ribir_gpu::WgpuImpl::headless()); + let mut container = WGPU_IMPL.lock().unwrap(); + let wgpu_impl = container + .take() + .unwrap_or_else(|| block_on(ribir_gpu::WgpuImpl::headless())); + let mut backend = GPUBackend::new(wgpu_impl); let rect = DeviceRect::from_size(DeviceSize::new(viewport.max_x() + 2, viewport.max_y() + 2)); - let mut texture = gpu_impl.new_texture(rect.size, ColorFormat::Rgba8); - let mut backend = GPUBackend::new(gpu_impl); - + let mut texture = backend + .get_impl_mut() + .new_texture(rect.size, ColorFormat::Rgba8); backend.begin_frame(surface); backend.draw_commands(rect, commands, &Transform::identity(), &mut texture); let img = texture.copy_as_image(&rect, backend.get_impl_mut()); backend.end_frame(); - let img = block_on(img).unwrap(); - drop(backend); + + *container = Some(backend.into_impl()); + img } diff --git a/gpu/src/gpu_backend.rs b/gpu/src/gpu_backend.rs index 1844ab066..2d8f7bdc5 100644 --- a/gpu/src/gpu_backend.rs +++ b/gpu/src/gpu_backend.rs @@ -153,6 +153,9 @@ where #[inline] pub fn get_impl_mut(&mut self) -> &mut Impl { &mut self.gpu_impl } + #[inline] + pub fn into_impl(self) -> Impl { self.gpu_impl } + fn draw_command( &mut self, cmd: &PaintCommand, global_matrix: &Transform, output_tex_size: DeviceSize, output: &mut Impl::Texture,