Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/anyrender_skia/src/image_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl ImageRenderer for SkiaImageRenderer {
draw_fn(&mut SkiaScenePainter {
inner: surface.canvas(),
cache: &mut self.scene_cache,
base_color: Color::WHITE,
});
timer.record_time("render");

Expand All @@ -89,6 +90,7 @@ impl ImageRenderer for SkiaImageRenderer {
draw_fn(&mut SkiaScenePainter {
inner: surface.canvas(),
cache: &mut self.scene_cache,
base_color: Color::WHITE,
});
timer.record_time("render");

Expand Down
3 changes: 2 additions & 1 deletion crates/anyrender_skia/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Default for SkiaSceneCache {
pub struct SkiaScenePainter<'a> {
pub(crate) inner: &'a Canvas,
pub(crate) cache: &'a mut SkiaSceneCache,
pub(crate) base_color: Color,
}

impl SkiaScenePainter<'_> {
Expand Down Expand Up @@ -364,7 +365,7 @@ impl SkiaScenePainter<'_> {

impl PaintScene for SkiaScenePainter<'_> {
fn reset(&mut self) {
self.inner.clear(Color::WHITE);
self.inner.clear(self.base_color);
}

fn push_layer(
Expand Down
12 changes: 11 additions & 1 deletion crates/anyrender_skia/src/window_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct ActiveRenderState {

pub struct SkiaWindowRenderer {
render_state: RenderState,
base_color: Color,
}

impl Default for SkiaWindowRenderer {
Expand All @@ -37,6 +38,14 @@ impl SkiaWindowRenderer {
pub fn new() -> Self {
Self {
render_state: RenderState::Suspended,
base_color: Color::WHITE,
}
}
pub fn with_base_color(base_color: peniko::Color) -> Self {
let base_color = base_color.to_rgba8();
Self {
render_state: RenderState::Suspended,
base_color: Color::from_argb(base_color.a, base_color.r, base_color.g, base_color.b),
}
}
}
Expand Down Expand Up @@ -92,11 +101,12 @@ impl WindowRenderer for SkiaWindowRenderer {
};

surface.canvas().restore_to_count(1);
surface.canvas().clear(Color::WHITE);
surface.canvas().clear(self.base_color);

draw_fn(&mut SkiaScenePainter {
inner: surface.canvas(),
cache: &mut state.scene_cache,
base_color: self.base_color,
});
timer.record_time("cmd");

Expand Down
8 changes: 7 additions & 1 deletion crates/anyrender_vello/src/window_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ impl WindowRenderer for VelloWindowRenderer {
}

fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
let alpha_mode = if self.config.base_color.components[3] < 1.0 {
wgpu::CompositeAlphaMode::PreMultiplied
} else {
wgpu::CompositeAlphaMode::Auto
};

// Create wgpu_context::SurfaceRenderer
let render_surface = pollster::block_on(self.wgpu_context.create_surface(
window_handle.clone(),
Expand All @@ -138,7 +144,7 @@ impl WindowRenderer for VelloWindowRenderer {
height,
present_mode: PresentMode::AutoVsync,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
alpha_mode,
view_formats: vec![],
},
Some(TextureConfiguration {
Expand Down
21 changes: 19 additions & 2 deletions crates/wgpu_context/src/surface_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{DeviceHandle, WgpuContextError, util::create_texture};
use wgpu::{
CommandEncoderDescriptor, CompositeAlphaMode, Device, PresentMode, Queue, Surface,
SurfaceConfiguration, SurfaceTexture, TextureFormat, TextureUsages, TextureView,
TextureViewDescriptor, util::TextureBlitter,
TextureViewDescriptor,
util::{TextureBlitter, TextureBlitterBuilder},
};

#[derive(Clone)]
Expand Down Expand Up @@ -126,6 +127,22 @@ impl<'s> SurfaceRenderer<'s> {
view_formats: surface_renderer_config.view_formats,
};

// `TextureBlitter::new` only does post-multiplied alpha
let blitter = if surface_renderer_config.alpha_mode == CompositeAlphaMode::PreMultiplied {
TextureBlitterBuilder::new(&device_handle.device, surface_config.format)
.blend_state(wgpu::BlendState {
alpha: wgpu::BlendComponent::REPLACE,
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::Zero,
operation: wgpu::BlendOperation::Add,
},
})
.build()
} else {
TextureBlitter::new(&device_handle.device, surface_config.format)
};

let intermediate_texture = intermediate_texture_config.map(|texture_config| {
Box::new(IntermediateTextureStuff {
config: texture_config.clone(),
Expand All @@ -136,7 +153,7 @@ impl<'s> SurfaceRenderer<'s> {
texture_config.usage,
&device_handle.device,
),
blitter: TextureBlitter::new(&device_handle.device, surface_config.format),
blitter,
})
});

Expand Down