mirror of
https://github.com/penpot/penpot.git
synced 2025-05-08 00:35:53 +02:00
♻️ Refactor debug options into its own struct, along with dpr
This commit is contained in:
parent
230e011003
commit
3e99de19f5
4 changed files with 62 additions and 31 deletions
|
@ -181,17 +181,17 @@
|
||||||
(defn assign-canvas
|
(defn assign-canvas
|
||||||
[canvas]
|
[canvas]
|
||||||
(let [gl (unchecked-get internal-module "GL")
|
(let [gl (unchecked-get internal-module "GL")
|
||||||
init-fn (unchecked-get internal-module "_init")
|
|
||||||
|
|
||||||
context (.getContext ^js canvas "webgl2" canvas-options)
|
context (.getContext ^js canvas "webgl2" canvas-options)
|
||||||
|
dpr js/window.devicePixelRatio
|
||||||
|
|
||||||
;; Register the context with emscripten
|
;; Register the context with emscripten
|
||||||
handle (.registerContext ^js gl context #js {"majorVersion" 2})]
|
handle (.registerContext ^js gl context #js {"majorVersion" 2})]
|
||||||
(.makeContextCurrent ^js gl handle)
|
(.makeContextCurrent ^js gl handle)
|
||||||
;; Initialize Skia
|
|
||||||
(^function init-fn (.-width ^js canvas)
|
;; Initialize Wasm Render Engine
|
||||||
(.-height ^js canvas)
|
(h/call internal-module "_init" (.-width ^js canvas) (.-height ^js canvas))
|
||||||
1)
|
(h/call internal-module "_set_render_options" 0x01 (or dpr 0))
|
||||||
|
|
||||||
(set! (.-width canvas) (.-clientWidth ^js canvas))
|
(set! (.-width canvas) (.-clientWidth ^js canvas))
|
||||||
(set! (.-height canvas) (.-clientHeight ^js canvas))))
|
(set! (.-height canvas) (.-clientHeight ^js canvas))))
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
pub mod debug;
|
mod debug;
|
||||||
pub mod images;
|
mod images;
|
||||||
pub mod math;
|
mod math;
|
||||||
pub mod render;
|
mod render;
|
||||||
pub mod shapes;
|
mod shapes;
|
||||||
pub mod state;
|
mod state;
|
||||||
pub mod utils;
|
mod utils;
|
||||||
pub mod view;
|
mod view;
|
||||||
|
|
||||||
use skia_safe as skia;
|
use skia_safe as skia;
|
||||||
|
|
||||||
|
@ -31,13 +31,24 @@ fn init_gl() {
|
||||||
|
|
||||||
/// This is called from JS after the WebGL context has been created.
|
/// This is called from JS after the WebGL context has been created.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn init(width: i32, height: i32, debug: u32) {
|
pub extern "C" fn init(width: i32, height: i32) {
|
||||||
let state_box = Box::new(State::with_capacity(width, height, debug, 2048));
|
let state_box = Box::new(State::new(width, height, 2048));
|
||||||
unsafe {
|
unsafe {
|
||||||
STATE = Some(state_box);
|
STATE = Some(state_box);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn set_render_options(debug: u32, dpr: f32) {
|
||||||
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
|
let render_state = state.render_state();
|
||||||
|
|
||||||
|
render_state.set_debug_flags(debug);
|
||||||
|
if dpr > 1.0 {
|
||||||
|
render_state.set_dpr(Some(dpr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn render() {
|
pub unsafe extern "C" fn render() {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
|
|
|
@ -59,12 +59,25 @@ pub(crate) struct CachedSurfaceImage {
|
||||||
pub is_complete: bool,
|
pub is_complete: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Copy, Clone, PartialEq)]
|
||||||
|
struct RenderOptions {
|
||||||
|
debug_flags: u32,
|
||||||
|
dpr: Option<f32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RenderOptions {
|
||||||
|
pub fn is_debug_visible(&self) -> bool {
|
||||||
|
self.debug_flags & debug::DEBUG_VISIBLE == debug::DEBUG_VISIBLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct RenderState {
|
pub(crate) struct RenderState {
|
||||||
gpu_state: GpuState,
|
gpu_state: GpuState,
|
||||||
pub final_surface: skia::Surface,
|
pub final_surface: skia::Surface,
|
||||||
pub drawing_surface: skia::Surface,
|
pub drawing_surface: skia::Surface,
|
||||||
pub debug_surface: skia::Surface,
|
pub debug_surface: skia::Surface,
|
||||||
pub cached_surface_image: Option<CachedSurfaceImage>,
|
pub cached_surface_image: Option<CachedSurfaceImage>,
|
||||||
|
options: RenderOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderState {
|
impl RenderState {
|
||||||
|
@ -85,9 +98,18 @@ impl RenderState {
|
||||||
drawing_surface,
|
drawing_surface,
|
||||||
debug_surface,
|
debug_surface,
|
||||||
cached_surface_image: None,
|
cached_surface_image: None,
|
||||||
|
options: RenderOptions::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_debug_flags(&mut self, debug: u32) {
|
||||||
|
self.options.debug_flags = debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_dpr(&mut self, dpr: Option<f32>) {
|
||||||
|
self.options.dpr = dpr;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, width: i32, height: i32) {
|
pub fn resize(&mut self, width: i32, height: i32) {
|
||||||
let surface = self.gpu_state.create_target_surface(width, height);
|
let surface = self.gpu_state.create_target_surface(width, height);
|
||||||
self.final_surface = surface;
|
self.final_surface = surface;
|
||||||
|
@ -176,7 +198,7 @@ impl RenderState {
|
||||||
.clear(skia::Color::TRANSPARENT);
|
.clear(skia::Color::TRANSPARENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn navigate(&mut self, viewbox: &Viewbox, shapes: &HashMap<Uuid, Shape>, debug: u32) {
|
pub fn navigate(&mut self, viewbox: &Viewbox, shapes: &HashMap<Uuid, Shape>) {
|
||||||
self.reset_canvas();
|
self.reset_canvas();
|
||||||
if let Some(cached_surface_image) = &self.cached_surface_image {
|
if let Some(cached_surface_image) = &self.cached_surface_image {
|
||||||
// If we are drawing something bigger than the visible let's do a redraw
|
// If we are drawing something bigger than the visible let's do a redraw
|
||||||
|
@ -190,7 +212,7 @@ impl RenderState {
|
||||||
> -cached_surface_image.viewbox.y
|
> -cached_surface_image.viewbox.y
|
||||||
+ cached_surface_image.viewbox.area.height()))
|
+ cached_surface_image.viewbox.area.height()))
|
||||||
{
|
{
|
||||||
self.render_all(viewbox, shapes, true, debug);
|
self.render_all(viewbox, shapes, true);
|
||||||
} else {
|
} else {
|
||||||
let image = &cached_surface_image.image;
|
let image = &cached_surface_image.image;
|
||||||
let paint = skia::Paint::default();
|
let paint = skia::Paint::default();
|
||||||
|
@ -226,11 +248,11 @@ impl RenderState {
|
||||||
viewbox: &Viewbox,
|
viewbox: &Viewbox,
|
||||||
shapes: &HashMap<Uuid, Shape>,
|
shapes: &HashMap<Uuid, Shape>,
|
||||||
generate_cached_surface_image: bool,
|
generate_cached_surface_image: bool,
|
||||||
debug: u32, // Debug flags
|
|
||||||
) {
|
) {
|
||||||
self.reset_canvas();
|
self.reset_canvas();
|
||||||
self.scale(viewbox.zoom, viewbox.zoom);
|
self.scale(viewbox.zoom, viewbox.zoom);
|
||||||
self.translate(viewbox.x, viewbox.y);
|
self.translate(viewbox.x, viewbox.y);
|
||||||
|
|
||||||
let is_complete = self.render_shape_tree(&Uuid::nil(), viewbox, shapes);
|
let is_complete = self.render_shape_tree(&Uuid::nil(), viewbox, shapes);
|
||||||
if generate_cached_surface_image || self.cached_surface_image.is_none() {
|
if generate_cached_surface_image || self.cached_surface_image.is_none() {
|
||||||
self.cached_surface_image = Some(CachedSurfaceImage {
|
self.cached_surface_image = Some(CachedSurfaceImage {
|
||||||
|
@ -239,7 +261,8 @@ impl RenderState {
|
||||||
is_complete,
|
is_complete,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if debug & debug::DEBUG_VISIBLE == debug::DEBUG_VISIBLE {
|
|
||||||
|
if self.options.is_debug_visible() {
|
||||||
self.render_debug(viewbox);
|
self.render_debug(viewbox);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,12 +274,14 @@ impl RenderState {
|
||||||
paint.set_style(skia::PaintStyle::Stroke);
|
paint.set_style(skia::PaintStyle::Stroke);
|
||||||
paint.set_color(skia::Color::from_argb(255, 255, 0, 255));
|
paint.set_color(skia::Color::from_argb(255, 255, 0, 255));
|
||||||
paint.set_stroke_width(1.);
|
paint.set_stroke_width(1.);
|
||||||
|
|
||||||
let mut scaled_rect = viewbox.area.clone();
|
let mut scaled_rect = viewbox.area.clone();
|
||||||
let x = 100. + scaled_rect.x() * 0.2;
|
let x = 100. + scaled_rect.x() * 0.2;
|
||||||
let y = 100. + scaled_rect.y() * 0.2;
|
let y = 100. + scaled_rect.y() * 0.2;
|
||||||
let width = scaled_rect.width() * 0.2;
|
let width = scaled_rect.width() * 0.2;
|
||||||
let height = scaled_rect.height() * 0.2;
|
let height = scaled_rect.height() * 0.2;
|
||||||
scaled_rect.set_xywh(x, y, width, height);
|
scaled_rect.set_xywh(x, y, width, height);
|
||||||
|
|
||||||
self.debug_surface.canvas().draw_rect(scaled_rect, &paint);
|
self.debug_surface.canvas().draw_rect(scaled_rect, &paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,12 +294,14 @@ impl RenderState {
|
||||||
skia::Color::from_argb(255, 0, 255, 255)
|
skia::Color::from_argb(255, 0, 255, 255)
|
||||||
});
|
});
|
||||||
paint.set_stroke_width(1.);
|
paint.set_stroke_width(1.);
|
||||||
|
|
||||||
let mut scaled_rect = shape.selrect.clone();
|
let mut scaled_rect = shape.selrect.clone();
|
||||||
let x = 100. + scaled_rect.x() * 0.2;
|
let x = 100. + scaled_rect.x() * 0.2;
|
||||||
let y = 100. + scaled_rect.y() * 0.2;
|
let y = 100. + scaled_rect.y() * 0.2;
|
||||||
let width = scaled_rect.width() * 0.2;
|
let width = scaled_rect.width() * 0.2;
|
||||||
let height = scaled_rect.height() * 0.2;
|
let height = scaled_rect.height() * 0.2;
|
||||||
scaled_rect.set_xywh(x, y, width, height);
|
scaled_rect.set_xywh(x, y, width, height);
|
||||||
|
|
||||||
self.debug_surface.canvas().draw_rect(scaled_rect, &paint);
|
self.debug_surface.canvas().draw_rect(scaled_rect, &paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ use crate::view::Viewbox;
|
||||||
/// Note that rust-skia data structures are not thread safe, so a state
|
/// Note that rust-skia data structures are not thread safe, so a state
|
||||||
/// must not be shared between different Web Workers.
|
/// must not be shared between different Web Workers.
|
||||||
pub(crate) struct State<'a> {
|
pub(crate) struct State<'a> {
|
||||||
pub debug: u32,
|
|
||||||
pub render_state: RenderState,
|
pub render_state: RenderState,
|
||||||
pub current_id: Option<Uuid>,
|
pub current_id: Option<Uuid>,
|
||||||
pub current_shape: Option<&'a mut Shape>,
|
pub current_shape: Option<&'a mut Shape>,
|
||||||
|
@ -21,9 +20,8 @@ pub(crate) struct State<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> State<'a> {
|
impl<'a> State<'a> {
|
||||||
pub fn with_capacity(width: i32, height: i32, debug: u32, capacity: usize) -> Self {
|
pub fn new(width: i32, height: i32, capacity: usize) -> Self {
|
||||||
State {
|
State {
|
||||||
debug,
|
|
||||||
render_state: RenderState::new(width, height),
|
render_state: RenderState::new(width, height),
|
||||||
current_id: None,
|
current_id: None,
|
||||||
current_shape: None,
|
current_shape: None,
|
||||||
|
@ -49,17 +47,12 @@ impl<'a> State<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn navigate(&mut self) {
|
pub fn navigate(&mut self) {
|
||||||
self.render_state
|
self.render_state.navigate(&self.viewbox, &self.shapes);
|
||||||
.navigate(&self.viewbox, &self.shapes, self.debug);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_all(&mut self, generate_cached_surface_image: bool) {
|
pub fn render_all(&mut self, generate_cached_surface_image: bool) {
|
||||||
self.render_state.render_all(
|
self.render_state
|
||||||
&self.viewbox,
|
.render_all(&self.viewbox, &self.shapes, generate_cached_surface_image);
|
||||||
&self.shapes,
|
|
||||||
generate_cached_surface_image,
|
|
||||||
self.debug,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_shape(&'a mut self, id: Uuid) {
|
pub fn use_shape(&'a mut self, id: Uuid) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue