Draw fills offscreen to support blend mode when multiple fills

This commit is contained in:
Belén Albeza 2024-11-14 16:21:51 +01:00
parent 3eb24e7f5f
commit 263d7eb313
3 changed files with 64 additions and 38 deletions

View file

@ -28,7 +28,7 @@ impl GpuState {
}
/// Create a Skia surface that will be used for rendering.
fn create_surface(&mut self, width: i32, height: i32) -> skia::Surface {
fn create_target_surface(&mut self, width: i32, height: i32) -> skia::Surface {
let backend_render_target =
gpu::backend_render_targets::make_gl((width, height), 1, 8, self.framebuffer_info);
@ -46,39 +46,53 @@ impl GpuState {
pub(crate) struct RenderState {
gpu_state: GpuState,
pub surface: skia::Surface,
pub final_surface: skia::Surface,
pub drawing_surface: skia::Surface,
}
impl RenderState {
pub fn new(width: i32, height: i32) -> RenderState {
// This needs to be done once per WebGL context.
let mut gpu_state = GpuState::new();
let surface = gpu_state.create_surface(width, height);
let mut final_surface = gpu_state.create_target_surface(width, height);
let drawing_surface = final_surface
.new_surface_with_dimensions((width, height))
.unwrap();
RenderState { gpu_state, surface }
RenderState {
gpu_state,
final_surface,
drawing_surface,
}
}
pub fn resize(&mut self, width: i32, height: i32) {
let surface = self.gpu_state.create_surface(width, height);
self.surface = surface;
let surface = self.gpu_state.create_target_surface(width, height);
self.final_surface = surface;
self.drawing_surface = self
.final_surface
.new_surface_with_dimensions((width, height))
.unwrap();
}
pub fn flush(&mut self) {
self.gpu_state
.context
.flush_and_submit_surface(&mut self.surface, None)
.flush_and_submit_surface(&mut self.final_surface, None)
}
pub fn translate(&mut self, dx: f32, dy: f32) {
self.surface.canvas().translate((dx, dy));
self.drawing_surface.canvas().translate((dx, dy));
}
pub fn scale(&mut self, sx: f32, sy: f32) {
self.surface.canvas().scale((sx, sy));
self.drawing_surface.canvas().scale((sx, sy));
}
pub fn reset_canvas(&mut self) {
self.surface.canvas().clear(skia_safe::Color::TRANSPARENT);
self.surface.canvas().reset_matrix();
self.drawing_surface
.canvas()
.clear(skia_safe::Color::TRANSPARENT);
self.drawing_surface.canvas().reset_matrix();
}
}