2024-10-25 12:10:59 +02:00
|
|
|
use skia_safe as skia;
|
|
|
|
use skia_safe::gpu::{self, gl::FramebufferInfo, DirectContext};
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
struct GpuState {
|
2024-10-25 12:10:59 +02:00
|
|
|
pub context: DirectContext,
|
|
|
|
framebuffer_info: FramebufferInfo,
|
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
impl GpuState {
|
|
|
|
fn new() -> Self {
|
|
|
|
let interface = skia_safe::gpu::gl::Interface::new_native().unwrap();
|
|
|
|
let context = skia_safe::gpu::direct_contexts::make_gl(interface, None).unwrap();
|
|
|
|
let framebuffer_info = {
|
|
|
|
let mut fboid: gl::types::GLint = 0;
|
|
|
|
unsafe { gl::GetIntegerv(gl::FRAMEBUFFER_BINDING, &mut fboid) };
|
2024-10-25 12:10:59 +02:00
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
FramebufferInfo {
|
|
|
|
fboid: fboid.try_into().unwrap(),
|
|
|
|
format: skia_safe::gpu::gl::Format::RGBA8.into(),
|
|
|
|
protected: skia_safe::gpu::Protected::No,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GpuState {
|
|
|
|
context,
|
|
|
|
framebuffer_info,
|
|
|
|
}
|
2024-10-25 12:10:59 +02:00
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
/// Create a Skia surface that will be used for rendering.
|
2024-11-14 16:21:51 +01:00
|
|
|
fn create_target_surface(&mut self, width: i32, height: i32) -> skia::Surface {
|
2024-11-14 11:47:10 +01:00
|
|
|
let backend_render_target =
|
|
|
|
gpu::backend_render_targets::make_gl((width, height), 1, 8, self.framebuffer_info);
|
|
|
|
|
|
|
|
gpu::surfaces::wrap_backend_render_target(
|
|
|
|
&mut self.context,
|
|
|
|
&backend_render_target,
|
|
|
|
skia_safe::gpu::SurfaceOrigin::BottomLeft,
|
|
|
|
skia_safe::ColorType::RGBA8888,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-10-25 12:10:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
pub(crate) struct RenderState {
|
|
|
|
gpu_state: GpuState,
|
2024-11-14 16:21:51 +01:00
|
|
|
pub final_surface: skia::Surface,
|
|
|
|
pub drawing_surface: skia::Surface,
|
2024-11-14 11:47:10 +01:00
|
|
|
}
|
2024-10-25 12:10:59 +02:00
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
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();
|
2024-11-14 16:21:51 +01:00
|
|
|
let mut final_surface = gpu_state.create_target_surface(width, height);
|
|
|
|
let drawing_surface = final_surface
|
|
|
|
.new_surface_with_dimensions((width, height))
|
|
|
|
.unwrap();
|
2024-10-25 12:10:59 +02:00
|
|
|
|
2024-11-14 16:21:51 +01:00
|
|
|
RenderState {
|
|
|
|
gpu_state,
|
|
|
|
final_surface,
|
|
|
|
drawing_surface,
|
|
|
|
}
|
2024-10-25 12:10:59 +02:00
|
|
|
}
|
|
|
|
|
2024-11-14 11:47:10 +01:00
|
|
|
pub fn resize(&mut self, width: i32, height: i32) {
|
2024-11-14 16:21:51 +01:00
|
|
|
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();
|
2024-11-14 11:47:10 +01:00
|
|
|
}
|
2024-11-13 10:56:00 +01:00
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn flush(&mut self) {
|
|
|
|
self.gpu_state
|
|
|
|
.context
|
2024-11-14 16:21:51 +01:00
|
|
|
.flush_and_submit_surface(&mut self.final_surface, None)
|
2024-11-13 10:56:00 +01:00
|
|
|
}
|
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn translate(&mut self, dx: f32, dy: f32) {
|
2024-11-14 16:21:51 +01:00
|
|
|
self.drawing_surface.canvas().translate((dx, dy));
|
2024-11-14 12:08:50 +01:00
|
|
|
}
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn scale(&mut self, sx: f32, sy: f32) {
|
2024-11-14 16:21:51 +01:00
|
|
|
self.drawing_surface.canvas().scale((sx, sy));
|
2024-11-14 12:08:50 +01:00
|
|
|
}
|
2024-11-12 11:09:50 +01:00
|
|
|
|
2024-11-14 12:08:50 +01:00
|
|
|
pub fn reset_canvas(&mut self) {
|
2024-11-14 16:21:51 +01:00
|
|
|
self.drawing_surface
|
|
|
|
.canvas()
|
|
|
|
.clear(skia_safe::Color::TRANSPARENT);
|
|
|
|
self.drawing_surface.canvas().reset_matrix();
|
2024-11-12 11:09:50 +01:00
|
|
|
}
|
|
|
|
}
|