Skip to content

Commit 4e73efd

Browse files
committed
dont use softbuffer on wasm due to performance issues
1 parent 8b4f563 commit 4e73efd

6 files changed

Lines changed: 186 additions & 103 deletions

File tree

.github/workflows/wasm.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ jobs:
2121

2222
- name: Install wasm-pack
2323
run: cargo install wasm-pack
24-
24+
- name: Install Binaryen (wasm-opt)
25+
run: |
26+
BINARYEN_VERSION=version_123
27+
curl -L -O https://github.com/WebAssembly/binaryen/releases/download/${BINARYEN_VERSION}/binaryen-${BINARYEN_VERSION}-x86_64-linux.tar.gz
28+
tar -xzf binaryen-${BINARYEN_VERSION}-x86_64-linux.tar.gz
29+
echo "$(pwd)/binaryen-${BINARYEN_VERSION}/bin" >> $GITHUB_PATH
2530
- name: depends
2631
run: |
2732
cd ..

Cargo.lock

Lines changed: 0 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ crate-type = ["cdylib", "rlib"]
2727
wasm-opt = false
2828

2929
[features]
30-
default=["skia-vulkan", "rug","fastnum", "vulkano-x11","x11", "wayland", "rayon", "bincode", "arboard"]
30+
default=["tiny-skia","softbuffer-x11", "rug","fastnum", "vulkano-x11","x11", "wayland", "rayon", "bincode", "arboard"]
3131
#default=["egui","rug","fastnum","rayon", "bincode", "arboard"]
3232
#default=["skia-vulkan","rug","fastnum","wayland", "rayon", "bincode", "arboard"]
3333
#default=["skia","rug","fastnum","wayland","softbuffer","softbuffer-wayland", "rayon", "bincode", "arboard"]
3434
#default=["tiny-skia","rug","fastnum", "wayland", "softbuffer", "softbuffer-wayland"]
3535
egui=["dep:egui","dep:eframe","rupl/egui"]
3636
skia=["rupl/skia" ,"rupl/winit", "dep:winit"]
37-
tiny-skia=["rupl/tiny-skia" ,"rupl/winit", "dep:winit", "softbuffer", "dep:tiny-skia", "rupl/tiny-skia-png", "rupl/tiny-skia-text"]
37+
tiny-skia=["rupl/tiny-skia" ,"rupl/winit", "dep:winit", "dep:tiny-skia", "rupl/tiny-skia-png", "rupl/tiny-skia-text"]
3838
softbuffer = ["dep:softbuffer"]
3939
rayon=["dep:rayon", "rupl/rayon", "kalc-lib?/rayon"]
4040
bincode=["dep:bitcode", "dep:serde", "rupl/serde", "dep:dirs", "kalc-lib?/serde"]
@@ -56,7 +56,7 @@ wasm=["dep:wasm-bindgen", "dep:console_error_panic_hook", "dep:web-sys"]
5656
egui={version="0.31.1",default-features = false,optional=true}
5757
eframe={version="0.31.1",optional=true}
5858
winit={version="0.30.11", default-features=false, features = ["rwh_06"],optional=true}
59-
softbuffer={version="0.4.6",default-features = false,features = ["kms"],optional=true}
59+
softbuffer={version="0.4.6",default-features = false,optional=true}
6060
rayon={version="1.10.0", optional=true}
6161
dirs={version="6.0.0", optional=true}
6262
bitcode = {version="0.6.6",features = ["serde"],optional = true}
@@ -65,5 +65,5 @@ rupl={version = "0.1.2",path="../rupl",default-features = false}
6565
kalc-lib={version="1.5.1",default-features=false,features = ["fastrand"],path="../kalc-lib",optional = true}
6666
wasm-bindgen = {version="0.2.100",optional = true}
6767
console_error_panic_hook = { version = "0.1.7", optional = true }
68-
web-sys = { version = "0.3.77", optional = true }
68+
web-sys = { version = "0.3.77", features = ["CanvasRenderingContext2d"], optional = true }
6969
tiny-skia = {version = "0.11.4", default-features = false, optional = true}

src/app.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ impl App {
191191
data,
192192
#[cfg(feature = "bincode")]
193193
tiny,
194-
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
195-
#[cfg(not(feature = "skia-vulkan"))]
194+
#[cfg(feature = "wasm")]
195+
window: None,
196+
#[cfg(not(feature = "wasm"))]
196197
surface_state: None,
197198
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
198199
input_state: rupl::types::InputState::default(),
@@ -247,6 +248,7 @@ impl App {
247248
}
248249
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
249250
#[cfg(not(feature = "skia-vulkan"))]
251+
#[cfg(not(feature = "wasm"))]
250252
pub(crate) fn main(&mut self, width: u32, height: u32) {
251253
let mut b = false;
252254
if let Some(buffer) = &mut self.surface_state {
@@ -281,4 +283,66 @@ impl App {
281283
}
282284
}
283285
}
286+
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
287+
#[cfg(not(feature = "skia-vulkan"))]
288+
#[cfg(feature = "wasm")]
289+
pub(crate) fn main(&mut self, width: u32, height: u32) {
290+
let mut b = false;
291+
self.plot.keybinds(&self.input_state);
292+
self.plot
293+
.set_screen(width as f64, height as f64, true, true);
294+
#[cfg(feature = "bincode")]
295+
if let Some(tiny) = std::mem::take(&mut self.tiny) {
296+
self.plot.apply_tiny(tiny);
297+
}
298+
if let Some(n) = self.data.update(&mut self.plot) {
299+
b = true;
300+
self.name = n;
301+
};
302+
let mut v = Vec::new();
303+
#[cfg(not(feature = "tiny-skia"))]
304+
self.plot.update(width, height, &mut v);
305+
#[cfg(feature = "tiny-skia")]
306+
{
307+
self.canvas = Some(self.plot.update(
308+
width,
309+
height,
310+
&mut v,
311+
std::mem::take(&mut self.canvas).unwrap(),
312+
));
313+
}
314+
draw_buffer_web(self.window.as_ref().unwrap(), self.canvas.as_ref().unwrap());
315+
if b {
316+
let name = self.name.clone();
317+
if let Some(w) = self.window() {
318+
if name.is_empty() {
319+
w.set_title("kalc-plot");
320+
} else {
321+
w.set_title(&name);
322+
}
323+
}
324+
}
325+
}
326+
}
327+
#[cfg(feature = "wasm")]
328+
fn draw_buffer_web(win: &winit::window::Window, pixmap: &tiny_skia::Pixmap) {
329+
use wasm_bindgen::prelude::*;
330+
let canvas = get_a_canvas(win);
331+
let ctx: web_sys::CanvasRenderingContext2d = canvas
332+
.get_context("2d")
333+
.expect("Failed to get 2d context")
334+
.expect("Failed to get 2d context")
335+
.dyn_into()
336+
.expect("Failed to convert to CanvasRenderingContext2d");
337+
let width = pixmap.width();
338+
let clamped = wasm_bindgen::Clamped(pixmap.data());
339+
let image = web_sys::ImageData::new_with_u8_clamped_array(clamped, width)
340+
.expect("Failed to create image data");
341+
ctx.put_image_data(&image, 0.0, 0.0)
342+
.expect("Failed to put image data");
343+
344+
fn get_a_canvas(win: &winit::window::Window) -> web_sys::HtmlCanvasElement {
345+
use winit::platform::web::WindowExtWebSys;
346+
win.canvas().expect("Failed to get canvas")
347+
}
284348
}

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,19 @@ struct App {
161161
tiny: Option<rupl::types::GraphTiny>,
162162
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
163163
#[cfg(not(feature = "skia-vulkan"))]
164-
#[cfg_attr(feature = "bincode", serde(skip_serializing, skip_deserializing))]
164+
#[cfg_attr(feature = "bincode", serde(skip))]
165+
#[cfg(not(feature = "wasm"))]
165166
surface_state: Option<
166167
softbuffer::Surface<
167168
std::sync::Arc<winit::window::Window>,
168169
std::sync::Arc<winit::window::Window>,
169170
>,
170171
>,
172+
#[cfg_attr(feature = "bincode", serde(skip))]
173+
#[cfg(feature = "wasm")]
174+
window: Option<winit::window::Window>,
171175
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
172-
#[cfg_attr(feature = "bincode", serde(skip_serializing, skip_deserializing))]
176+
#[cfg_attr(feature = "bincode", serde(skip))]
173177
input_state: rupl::types::InputState,
174178
#[cfg(any(feature = "skia", feature = "tiny-skia"))]
175179
name: String,

0 commit comments

Comments
 (0)