Skip to content

Commit

Permalink
reuse gpu impl in test
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Oct 2, 2024
1 parent 2db9423 commit d75b451
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 15 additions & 14 deletions dev-helper/src/image_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Mutex;

use ribir_geom::Transform;
use ribir_painter::{image::ColorFormat, PixelImage};

Expand Down Expand Up @@ -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<Option<WgpuImpl>> = 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
}
3 changes: 3 additions & 0 deletions gpu/src/gpu_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d75b451

Please sign in to comment.