Skip to content

Commit a1237b9

Browse files
committed
impl
1 parent eae31cf commit a1237b9

23 files changed

Lines changed: 727 additions & 492 deletions

File tree

sparse_strips/vello_bench/src/glyph.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use parley::{
99
PositionedLayoutItem,
1010
};
1111
use vello_common::pixmap::Pixmap;
12-
use vello_cpu::{Glyph, RenderContext, RenderMode, RenderSettings, Resources};
12+
use vello_cpu::{Glyph, RasterizerSettings, RenderContext, RenderSettings, Resources};
1313

1414
pub fn glyph(c: &mut Criterion) {
1515
let mut g = c.benchmark_group("glyph");
@@ -30,10 +30,7 @@ pub fn glyph(c: &mut Criterion) {
3030
layout
3131
};
3232

33-
let settings = RenderSettings {
34-
render_mode: RenderMode::OptimizeSpeed,
35-
..Default::default()
36-
};
33+
let settings = RenderSettings::default();
3734
let layout = layout_for(TEXT, 1.0);
3835

3936
for (hint_name, hint) in [("hinted", true), ("unhinted", false)] {
@@ -114,9 +111,11 @@ fn render_layout(
114111
}
115112
}
116113

117-
renderer
118-
.ctx
119-
.render_to_pixmap(&mut renderer.resources, &mut renderer.pixmap);
114+
renderer.ctx.render(
115+
&mut renderer.pixmap,
116+
&mut renderer.resources,
117+
RasterizerSettings::default(),
118+
);
120119
}
121120

122121
fn render_glyph_run(

sparse_strips/vello_bench/src/integration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use vello_common::peniko::ImageSampler;
1212
use vello_common::peniko::{Extend, ImageQuality};
1313
use vello_common::pixmap::Pixmap;
1414
use vello_cpu::color::AlphaColor;
15-
use vello_cpu::{RenderContext, Resources};
15+
use vello_cpu::{RasterizerSettings, RenderContext, Resources};
1616

1717
/// Image scene rendering benchmark.
1818
pub fn images(c: &mut Criterion) {
@@ -57,7 +57,7 @@ pub fn images(c: &mut Criterion) {
5757
}
5858

5959
renderer.flush();
60-
renderer.render_to_pixmap(&mut resources, &mut pixmap);
60+
renderer.render(&mut pixmap, &mut resources, RasterizerSettings::default());
6161
std::hint::black_box(&pixmap);
6262
});
6363
});

sparse_strips/vello_common/src/pixmap.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,51 @@ pub struct Pixmap {
2929
may_have_transparency: bool,
3030
}
3131

32+
/// A mutable view into premultiplied RGBA8 pixmap data.
33+
#[derive(Debug)]
34+
pub struct PixmapMut<'a> {
35+
/// Width of the pixmap in pixels.
36+
width: u16,
37+
/// Height of the pixmap in pixels.
38+
height: u16,
39+
/// Buffer of the pixmap in RGBA8 format.
40+
buf: &'a mut [u8],
41+
}
42+
43+
impl<'a> PixmapMut<'a> {
44+
/// Create a new mutable pixmap view.
45+
///
46+
/// Returns `None` if `buf` is not exactly `width * height * 4` bytes long.
47+
pub fn new(width: u16, height: u16, buf: &'a mut [u8]) -> Option<Self> {
48+
if buf.len() == usize::from(width) * usize::from(height) * 4 {
49+
Some(Self { width, height, buf })
50+
} else {
51+
None
52+
}
53+
}
54+
55+
/// Return the width of the pixmap.
56+
pub fn width(&self) -> u16 {
57+
self.width
58+
}
59+
60+
/// Return the height of the pixmap.
61+
pub fn height(&self) -> u16 {
62+
self.height
63+
}
64+
65+
/// Returns a mutable reference to the underlying data as premultiplied RGBA8 bytes.
66+
pub fn data_mut(&mut self) -> &mut [u8] {
67+
self.buf
68+
}
69+
}
70+
71+
impl<'a> From<&'a mut Pixmap> for PixmapMut<'a> {
72+
fn from(pixmap: &'a mut Pixmap) -> Self {
73+
pixmap.as_mut()
74+
}
75+
}
76+
3277
impl Pixmap {
3378
/// Create a new pixmap with the given width and height in pixels.
3479
///
@@ -293,6 +338,10 @@ impl Pixmap {
293338
&self.buf
294339
}
295340

341+
// TODO: Now that we have `as_mut`, maybe we don't need the
342+
// mutable methods. If we add a `PixmapRef` we can also remove the
343+
// non-mutable ones.
344+
296345
/// Returns a mutable reference to the underlying data as premultiplied RGBA8.
297346
///
298347
/// The pixels are in row-major order.
@@ -316,6 +365,15 @@ impl Pixmap {
316365
bytemuck::cast_slice_mut(&mut self.buf)
317366
}
318367

368+
/// Return a mutable view into this pixmap's pixel data.
369+
pub fn as_mut(&mut self) -> PixmapMut<'_> {
370+
PixmapMut {
371+
width: self.width,
372+
height: self.height,
373+
buf: bytemuck::cast_slice_mut(&mut self.buf),
374+
}
375+
}
376+
319377
/// Sample a pixel from the pixmap.
320378
///
321379
/// The pixel data is [premultiplied RGBA8][PremulRgba8].

sparse_strips/vello_cpu/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ See https://linebender.org/blog/doc-include/ for related discussion. -->
2929
[RenderContext::glyph_run]: https://docs.rs/vello_cpu/latest/vello_cpu/struct.RenderContext.html#method.glyph_run
3030
[RenderMode::OptimizeSpeed]: https://docs.rs/vello_cpu/latest/vello_cpu/enum.RenderMode.html#variant.OptimizeSpeed
3131
[RenderMode::OptimizeQuality]: https://docs.rs/vello_cpu/latest/vello_cpu/enum.RenderMode.html#variant.OptimizeQuality
32-
[`RenderContext::render_to_pixmap`]: https://docs.rs/vello_cpu/latest/vello_cpu/struct.RenderContext.html#method.render_to_pixmap
32+
[`RenderContext::render`]: https://docs.rs/vello_cpu/latest/vello_cpu/struct.RenderContext.html#method.render
3333
[`Pixmap`]: https://docs.rs/vello_cpu/latest/vello_cpu/struct.Pixmap.html
3434

3535
<!-- cargo-rdme start -->
@@ -43,15 +43,15 @@ Vello CPU is being developed as part of work to address shortcomings in Vello.
4343

4444
To use Vello CPU, you need to:
4545

46-
- Create a [`RenderContext`][], a 2D drawing context for a fixed-size target area.
46+
- Create a [`RenderContext`][], a 2D drawing context for a fixed-size scene area.
4747
- For each object in your scene:
4848
- Set how the object will be painted, using [`set_paint`][RenderContext::set_paint].
4949
- Set the shape to be drawn for that object, using methods like [`fill_path`][RenderContext::fill_path],
5050
[`stroke_path`][RenderContext::stroke_path], or [`glyph_run`][RenderContext::glyph_run].
51-
- Render it to an image using [`RenderContext::render_to_pixmap`][].
51+
- Render it to an image using [`RenderContext::render`][].
5252

5353
```rust
54-
use vello_cpu::{RenderContext, Resources, Pixmap, RenderMode};
54+
use vello_cpu::{RasterizerSettings, RenderContext, Resources, Pixmap};
5555
use vello_cpu::{color::{palette::css, PremulRgba8}, kurbo::Rect};
5656
let width = 10;
5757
let height = 5;
@@ -64,7 +64,7 @@ let mut target = Pixmap::new(width, height);
6464
// While calling `flush` is only strictly necessary if you are rendering using
6565
// multiple threads, it is recommended to always do this.
6666
context.flush();
67-
context.render_to_pixmap(&mut resources, &mut target);
67+
context.render(&mut target, &mut resources, RasterizerSettings::default());
6868

6969
let expected_render = b"\
7070
0000000000\

sparse_strips/vello_cpu/examples/basic.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use vello_cpu::color::palette::css::YELLOW;
99
use vello_cpu::kurbo::Affine;
1010
use vello_cpu::{
11-
Level, Pixmap, RenderContext, RenderMode, RenderSettings, Resources,
11+
Level, Pixmap, RasterizerSettings, RenderContext, RenderMode, RenderSettings, Resources,
1212
color::palette::css::{BLUE, GREEN, RED},
1313
kurbo::{Circle, Rect, Shape},
1414
};
@@ -52,14 +52,17 @@ fn main() {
5252
// using 4+ threads might result in diminishing results, depending on
5353
// the workload.
5454
num_threads: 0,
55+
};
56+
let rasterizer_settings = RasterizerSettings {
5557
// Define whether the renderer should prioritize speed or quality
5658
// during rendering. Currently, the only difference is that
5759
// `OptimizeSpeed` will use u8/u16 for the rasterization
5860
// while `OptimizeQuality` uses f32. The former is much faster, but
5961
// has the disadvantage that the overall color accuracy might be slightly
6062
// worse due to quantization. Unless you really care about that, it is
6163
// highly recommended to use the `OptimizeSpeed` rendering mode.
62-
render_mode: RenderMode::OptimizeSpeed,
64+
quality: RenderMode::OptimizeSpeed,
65+
..Default::default()
6366
};
6467

6568
// Vello CPU embraces a slightly different paradigm than a lot of other 2D
@@ -126,12 +129,12 @@ fn main() {
126129

127130
// Now the second step is to copy the results of the render context into the
128131
// pixmap. We do this by creating a new pixmap (or reusing an existing one).
129-
// Please note that the pixmap and the render context need to have the same
130-
// dimensions! Otherwise, the renderer will panic.
132+
// The pixmap and render context can have different dimensions. See the documentation
133+
// of the `render` method for more information.
131134
let mut pixmap_1 = Pixmap::new(100, 100);
132135
// Now, simply extract the results from the render context into the
133136
// pixmap.
134-
ctx.render_to_pixmap(&mut resources, &mut pixmap_1);
137+
ctx.render(&mut pixmap_1, &mut resources, rasterizer_settings);
135138

136139
// Now you can do whatever you want with the pixmap, which provides raw
137140
// access to the premultiplied RGBA pixels of the image. If you have enabled
@@ -155,11 +158,11 @@ fn main() {
155158

156159
// Once again, we render the results into a pixmap again. If you can,
157160
// you can just reuse existing pixmaps (assuming they have the correct
158-
// dimension), since all previous pixels in the pixmap will be
159-
// discarded. In our case, we need to create a new one since our call
160-
// to `into_png` consumed the pixmap.
161+
// dimension), since all previous pixels in the pixmap will be cleared by
162+
// the default replace mode. In our case, we need to create a new one since
163+
// our call to `into_png` consumed the pixmap.
161164
let mut pixmap_2 = Pixmap::new(100, 100);
162-
ctx.render_to_pixmap(&mut resources, &mut pixmap_2);
165+
ctx.render(&mut pixmap_2, &mut resources, rasterizer_settings);
163166
let png_2 = pixmap_2.into_png().unwrap();
164167
std::fs::write("example_basic2.png", png_2).unwrap();
165168
}

sparse_strips/vello_cpu/examples/clipping.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Applying clip paths using Vello CPU.
55
66
use vello_cpu::Pixmap;
7+
use vello_cpu::RasterizerSettings;
78
use vello_cpu::RenderContext;
89
use vello_cpu::Resources;
910
use vello_cpu::color::palette::css::{BLUE, RED, WHITE};
@@ -128,7 +129,7 @@ fn main() {
128129
fn save_pixmap(ctx: &RenderContext, filename: &str) {
129130
let mut resources = Resources::new();
130131
let mut pixmap = Pixmap::new(ctx.width(), ctx.height());
131-
ctx.render_to_pixmap(&mut resources, &mut pixmap);
132+
ctx.render(&mut pixmap, &mut resources, RasterizerSettings::default());
132133
let png = pixmap.into_png().unwrap();
133134
std::fs::write(format!("{filename}.png"), png).unwrap();
134135
}

sparse_strips/vello_cpu/examples/masking.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use vello_cpu::color::palette::css::{BLUE, RED, WHITE};
77
use vello_cpu::kurbo::Rect;
8-
use vello_cpu::{Mask, Pixmap, RenderContext, Resources};
8+
use vello_cpu::{Mask, Pixmap, RasterizerSettings, RenderContext, Resources};
99

1010
const SIZE: u16 = 200;
1111

@@ -25,7 +25,11 @@ fn main() {
2525
mask_ctx.set_paint(RED);
2626
mask_ctx.fill_rect(&Rect::new(30.0, 30.0, 170.0, 170.0));
2727
mask_ctx.flush();
28-
mask_ctx.render_to_pixmap(&mut mask_resources, &mut pixmap);
28+
mask_ctx.render(
29+
&mut pixmap,
30+
&mut mask_resources,
31+
RasterizerSettings::default(),
32+
);
2933

3034
Mask::new_luminance(&pixmap)
3135
};
@@ -94,7 +98,7 @@ fn main() {
9498
fn save_pixmap(ctx: &RenderContext, filename: &str) {
9599
let mut resources = Resources::new();
96100
let mut pixmap = Pixmap::new(ctx.width(), ctx.height());
97-
ctx.render_to_pixmap(&mut resources, &mut pixmap);
101+
ctx.render(&mut pixmap, &mut resources, RasterizerSettings::default());
98102
let png = pixmap.into_png().unwrap();
99103
std::fs::write(format!("{filename}.png"), png).unwrap();
100104
}

sparse_strips/vello_cpu/examples/paints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::sync::Arc;
77
use std::{io::Cursor, path::Path};
88
use vello_cpu::{
9-
Image, ImageSource, Pixmap, RenderContext, Resources,
9+
Image, ImageSource, Pixmap, RasterizerSettings, RenderContext, Resources,
1010
color::palette::css::{BLUE, CYAN, DEEP_PINK, MAGENTA, NAVY, ORANGE, PURPLE, WHITE, YELLOW},
1111
kurbo::{Affine, Point, Rect},
1212
peniko::{
@@ -135,7 +135,7 @@ fn pattern() -> Image {
135135
fn save_pixmap(ctx: &RenderContext, filename: &str) {
136136
let mut resources = Resources::new();
137137
let mut pixmap = Pixmap::new(ctx.width(), ctx.height());
138-
ctx.render_to_pixmap(&mut resources, &mut pixmap);
138+
ctx.render(&mut pixmap, &mut resources, RasterizerSettings::default());
139139
let png = pixmap.into_png().unwrap();
140140
std::fs::write(format!("{filename}.png"), png).unwrap();
141141
}

sparse_strips/vello_cpu/examples/wasm_cpu/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::rc::Rc;
1313
use std::{cell::RefCell, sync::Arc};
1414
use vello_common::kurbo::{Affine, Vec2};
1515
use vello_common::paint::ImageSource;
16-
use vello_cpu::RenderContext;
16+
use vello_cpu::{RasterizerSettings, RenderContext};
1717
use vello_example_scenes::{AnyScene, image::ImageScene};
1818
use wasm_bindgen::prelude::*;
1919
use web_sys::{Event, HtmlCanvasElement, KeyboardEvent, MouseEvent, WheelEvent};
@@ -72,9 +72,10 @@ impl AppState {
7272
self.scenes[self.current_scene].render(&mut self.renderer, self.transform);
7373

7474
// Render the current scene with transform
75-
self.renderer.render_to_pixmap(
76-
self.scenes[self.current_scene].resources_mut(),
75+
self.renderer.render(
7776
&mut self.pixmap,
77+
self.scenes[self.current_scene].resources_mut(),
78+
RasterizerSettings::default(),
7879
);
7980
let rgba_bytes = self.pixmap.data_as_u8_slice();
8081
let image_data = web_sys::ImageData::new_with_u8_clamped_array_and_sh(

sparse_strips/vello_cpu/examples/winit/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::time::Instant;
1616
use vello_common::kurbo::{Affine, Point};
1717
use vello_common::paint::ImageSource;
1818
use vello_common::pixmap::Pixmap;
19-
use vello_cpu::{RenderContext, RenderSettings};
19+
use vello_cpu::{RasterizerSettings, RenderContext, RenderSettings};
2020
use vello_example_scenes::image::ImageScene;
2121
use vello_example_scenes::{AnyScene, Capabilities, get_example_scenes};
2222
use winit::{
@@ -457,9 +457,10 @@ impl ApplicationHandler for App {
457457

458458
self.scenes[self.current_scene].render(&mut self.renderer, self.transform);
459459
self.renderer.flush();
460-
self.renderer.render_to_pixmap(
461-
self.scenes[self.current_scene].resources_mut(),
460+
self.renderer.render(
462461
&mut self.pixmap,
462+
self.scenes[self.current_scene].resources_mut(),
463+
RasterizerSettings::default(),
463464
);
464465

465466
// Copy pixmap to window surface

0 commit comments

Comments
 (0)