Skip to content

Commit

Permalink
feat(painter): 🎸Add support for drawing bundled commands as a single …
Browse files Browse the repository at this point in the history
…command in Painter
  • Loading branch information
M-Adoo committed Jun 4, 2024
1 parent 82ced7d commit 4ede40d
Show file tree
Hide file tree
Showing 24 changed files with 430 additions and 197 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ Check out our Wordle game demo, now running smoothly in your browser!
- **gpu**: Updated the `wgpu` implementation of the GPU backend to support WebGL. (#578, @M-Adoo)
- **ci**: add wasm test (#583 @wjian23)
- **ci**: wasm server watch file change (#586 @wjian23)
- **painter**: Introduced support for `Resource<Path>` for drawing. This indicates that the `Path` may be shared with others, allowing the backend to cache it. (#589 @M-Adoo)
- **painter**: Introduced support for bundled commands, enabling the backend to process these commands as a single entity and cache the resulting output. (#589 @M-Adoo)

### Fixed

- **gpu**: Retrieve the texture limit size from the GPU instead of using a hardcoded value. (#578, @M-Adoo)
- **ribir**: fixed the crash issue when the shell window is zero-sized at startup. (#582, @M-Adoo)

### Changed

Expand All @@ -56,11 +59,8 @@ Check out our Wordle game demo, now running smoothly in your browser!
- **painter**: Removed the AntiAliasing feature from the `painter` package, This responsibility now lies with the painter backend. (#584 @M-Adoo)
- **gpu**: The GPU backend no longer relies on MSAA, which is dependent on the graphics API. Instead, it uses the alpha atlas to provide a solution similar to SSAA.(#584, @M-Adoo)
- **example**: run example in web wasm (#571 @wjian23)


### Fixed

- **ribir**: fixed the crash issue when the shell window is zero-sized at startup. (#582, @M-Adoo)
- **gpu**: The GPU backend now only caches the path command if it is a `Resource`. This change reduces GPU memory usage and accelerates cache detection. (#589 @M-Adoo)
- **text**: Implemented caching of the glyph path as a `Resource` to improve performance. (#589 @M-Adoo)


### Documented
Expand Down
5 changes: 3 additions & 2 deletions core/src/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ impl ShellWindow for TestShellWindow {

fn begin_frame(&mut self, surface: Color) { self.surface_color = surface; }

fn draw_commands(&mut self, viewport: Rect, commands: Vec<PaintCommand>) {
self.last_frame = Some(Frame { commands, viewport, surface: self.surface_color });
fn draw_commands(&mut self, viewport: Rect, commands: &[PaintCommand]) {
self.last_frame =
Some(Frame { commands: commands.to_owned(), viewport, surface: self.surface_color });
}

fn end_frame(&mut self) {}
Expand Down
6 changes: 3 additions & 3 deletions core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub trait ShellWindow {
/// device.
fn device_pixel_ratio(&self) -> f32;
fn begin_frame(&mut self, surface_color: Color);
fn draw_commands(&mut self, viewport: Rect, commands: Vec<PaintCommand>);
fn draw_commands(&mut self, viewport: Rect, commands: &[PaintCommand]);
fn end_frame(&mut self);
}

Expand Down Expand Up @@ -202,8 +202,8 @@ impl Window {

let mut shell = self.shell_wnd.borrow_mut();
let inner_size = shell.inner_size();
let paint_cmds = self.painter.borrow_mut().finish();
shell.draw_commands(Rect::from_size(inner_size), paint_cmds);
let mut painter = self.painter.borrow_mut();
shell.draw_commands(Rect::from_size(inner_size), &painter.finish());

shell.end_frame();
}
Expand Down
7 changes: 4 additions & 3 deletions dev-helper/src/image_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ribir_geom::Transform;
use ribir_painter::{image::ColorFormat, PixelImage};

/// This macro generates image tests for the painter with every backend. Accept
Expand Down Expand Up @@ -26,7 +27,7 @@ macro_rules! painter_backend_eq_image_test {
fn [<wgpu_ $painter_fn>]() {
let mut painter = $painter_fn();
let viewport = painter.viewport().to_i32().cast_unit();
let img = wgpu_render_commands(painter.finish(), viewport, Color::TRANSPARENT);
let img = wgpu_render_commands(&painter.finish(), viewport, Color::TRANSPARENT);
let name = format!("{}_wgpu", std::stringify!($painter_fn));
let file_path = test_case_name!(name, "png");
ImageTest::new(img, &file_path)
Expand Down Expand Up @@ -174,7 +175,7 @@ pub fn assert_texture_eq_png(test_img: PixelImage, ref_path: &std::path::Path) {

/// Render painter by wgpu backend, and return the image.
pub fn wgpu_render_commands(
commands: Vec<ribir_painter::PaintCommand>, viewport: ribir_geom::DeviceRect,
commands: &[ribir_painter::PaintCommand], viewport: ribir_geom::DeviceRect,
surface: ribir_painter::Color,
) -> PixelImage {
use futures::executor::block_on;
Expand All @@ -189,7 +190,7 @@ pub fn wgpu_render_commands(
let mut backend = GPUBackend::new(gpu_impl);

backend.begin_frame(surface);
backend.draw_commands(rect, commands, &mut texture);
backend.draw_commands(rect, commands, &Transform::identity(), &mut texture);
let img = texture.copy_as_image(&rect, backend.get_impl_mut());
backend.end_frame();
block_on(img).unwrap()
Expand Down
4 changes: 2 additions & 2 deletions dev-helper/src/widget_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ macro_rules! widget_image_test {
wnd.draw_frame();
let Frame { commands, viewport, surface} = wnd.take_last_frame().unwrap();
let viewport = viewport.to_i32().cast_unit();
let img = wgpu_render_commands(commands, viewport, surface);
let img = wgpu_render_commands(&commands, viewport, surface);
let name = format!("{}_with_default_by_wgpu", std::stringify!($widget_fn));
let file_path = test_case_name!(name, "png");
ImageTest::new(img, &file_path)
Expand All @@ -241,7 +241,7 @@ macro_rules! widget_image_test {
wnd.draw_frame();
let Frame { commands, viewport, surface} = wnd.take_last_frame().unwrap();
let viewport = viewport.to_i32().cast_unit();
let img = wgpu_render_commands(commands, viewport, surface);
let img = wgpu_render_commands(&commands, viewport, surface);
let name = format!("{}_with_material_by_wgpu", std::stringify!($widget_fn));
let file_path = test_case_name!(name, "png");
ImageTest::new(img, &file_path)
Expand Down
9 changes: 9 additions & 0 deletions geom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ where
Point2D::new(rect.min_x(), rect.max_y()),
]
}

/// Apply the transform to the rect and return the new rect as device unit.
pub fn transform_to_device_rect(rect: &Rect, matrix: &Transform) -> DeviceRect {
matrix
.outer_transformed_rect(rect)
.round_out()
.to_i32()
.cast_unit()
}
Loading

0 comments on commit 4ede40d

Please sign in to comment.