mirror of
https://github.com/penpot/penpot.git
synced 2025-01-21 14:12:36 -05:00
Merge pull request #5400 from penpot/superalex-memory-refactor
❇️ Refactor memory management for color linear gradient stops
This commit is contained in:
commit
c688ae2e33
3 changed files with 74 additions and 14 deletions
|
@ -13,6 +13,7 @@
|
||||||
[app.config :as cf]
|
[app.config :as cf]
|
||||||
[app.render-wasm.helpers :as h]
|
[app.render-wasm.helpers :as h]
|
||||||
[app.util.functions :as fns]
|
[app.util.functions :as fns]
|
||||||
|
[goog.object :as gobj]
|
||||||
[promesa.core :as p]))
|
[promesa.core :as p]))
|
||||||
|
|
||||||
(defonce internal-frame-id nil)
|
(defonce internal-frame-id nil)
|
||||||
|
@ -43,6 +44,17 @@
|
||||||
;; rgba >>> 0 so we have an unsigned representation
|
;; rgba >>> 0 so we have an unsigned representation
|
||||||
(unsigned-bit-shift-right (bit-or (bit-shift-left a 24) rgb) 0)))
|
(unsigned-bit-shift-right (bit-or (bit-shift-left a 24) rgb) 0)))
|
||||||
|
|
||||||
|
(defn- rgba-bytes-from-hex
|
||||||
|
"Takes a hex color in #rrggbb format, and an opacity value from 0 to 1 and returns an array with its r g b a values"
|
||||||
|
[hex opacity]
|
||||||
|
(let [rgb (js/parseInt (subs hex 1) 16)
|
||||||
|
a (mth/floor (* (or opacity 1) 0xff))
|
||||||
|
;; rgba >>> 0 so we have an unsigned representation
|
||||||
|
r (bit-shift-right rgb 16)
|
||||||
|
g (bit-and (bit-shift-right rgb 8) 255)
|
||||||
|
b (bit-and rgb 255)]
|
||||||
|
[r g b a]))
|
||||||
|
|
||||||
(defn cancel-render
|
(defn cancel-render
|
||||||
[]
|
[]
|
||||||
(when internal-frame-id
|
(when internal-frame-id
|
||||||
|
@ -108,15 +120,25 @@
|
||||||
(let [rgba (rgba-from-hex color opacity)]
|
(let [rgba (rgba-from-hex color opacity)]
|
||||||
(h/call internal-module "_add_shape_solid_fill" rgba)))
|
(h/call internal-module "_add_shape_solid_fill" rgba)))
|
||||||
(when (and (some? gradient) (= (:type gradient) :linear))
|
(when (and (some? gradient) (= (:type gradient) :linear))
|
||||||
(h/call internal-module "_add_shape_linear_fill"
|
(let [stops (:stops gradient)
|
||||||
(:start-x gradient)
|
n-stops (count stops)
|
||||||
(:start-y gradient)
|
mem-size (* 5 n-stops)
|
||||||
(:end-x gradient)
|
stops-ptr (h/call internal-module "_alloc_bytes" mem-size)
|
||||||
(:end-y gradient)
|
heap (gobj/get ^js internal-module "HEAPU8")
|
||||||
opacity)
|
mem (js/Uint8Array. (.-buffer heap) stops-ptr mem-size)]
|
||||||
(run! (fn [stop]
|
(h/call internal-module "_add_shape_linear_fill"
|
||||||
(let [rgba (rgba-from-hex (:color stop) (:opacity stop))]
|
(:start-x gradient)
|
||||||
(h/call internal-module "_add_shape_fill_stop" rgba (:offset stop)))) (:stops gradient)))))
|
(:start-y gradient)
|
||||||
|
(:end-x gradient)
|
||||||
|
(:end-y gradient)
|
||||||
|
opacity)
|
||||||
|
(.set mem (js/Uint8Array. (clj->js (flatten (map (fn [stop]
|
||||||
|
(let [[r g b a] (rgba-bytes-from-hex (:color stop) (:opacity stop))
|
||||||
|
offset (:offset stop)]
|
||||||
|
[r g b a (* 100 offset)]))
|
||||||
|
stops)))))
|
||||||
|
|
||||||
|
(h/call internal-module "_add_shape_fill_stops" stops-ptr n-stops)))))
|
||||||
fills))
|
fills))
|
||||||
|
|
||||||
(defn- translate-blend-mode
|
(defn- translate-blend-mode
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
mod debug;
|
mod debug;
|
||||||
mod images;
|
mod images;
|
||||||
mod math;
|
mod math;
|
||||||
|
pub mod mem;
|
||||||
mod render;
|
mod render;
|
||||||
mod shapes;
|
mod shapes;
|
||||||
mod state;
|
mod state;
|
||||||
|
@ -176,14 +177,34 @@ pub extern "C" fn add_shape_linear_fill(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct RawStopData {
|
||||||
|
color: [u8; 4],
|
||||||
|
offset: u8,
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_fill_stop(raw_color: u32, offset: f32) {
|
pub extern "C" fn add_shape_fill_stops(ptr: *mut RawStopData, n_stops: i32) {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
if let Some(shape) = state.current_shape() {
|
if let Some(shape) = state.current_shape() {
|
||||||
let color = skia::Color::new(raw_color);
|
unsafe {
|
||||||
shape
|
let buf = Vec::<RawStopData>::from_raw_parts(ptr, n_stops as usize, n_stops as usize);
|
||||||
.add_gradient_stop(color, offset)
|
for raw_stop in buf.iter() {
|
||||||
.expect("got no fill or an invalid one");
|
let color = skia::Color::from_argb(
|
||||||
|
raw_stop.color[3],
|
||||||
|
raw_stop.color[0],
|
||||||
|
raw_stop.color[1],
|
||||||
|
raw_stop.color[2],
|
||||||
|
);
|
||||||
|
shape
|
||||||
|
.add_gradient_stop(color, (raw_stop.offset as f32) / 100.)
|
||||||
|
.expect("got no fill or an invalid one");
|
||||||
|
}
|
||||||
|
mem::free(
|
||||||
|
ptr as *mut u8,
|
||||||
|
n_stops as usize * std::mem::size_of::<RawStopData>(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
17
render-wasm/src/mem.rs
Normal file
17
render-wasm/src/mem.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn alloc_bytes(len: usize) -> *mut u8 {
|
||||||
|
// create a new mutable buffer with capacity `len`
|
||||||
|
let mut buf: Vec<u8> = Vec::with_capacity(len);
|
||||||
|
let ptr = buf.as_mut_ptr();
|
||||||
|
// take ownership of the memory block and ensure the its destructor is not
|
||||||
|
// called when the object goes out of scope at the end of the function
|
||||||
|
std::mem::forget(buf);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn free(ptr: *mut u8, len: usize) {
|
||||||
|
unsafe {
|
||||||
|
let buf = Vec::<u8>::from_raw_parts(ptr, len, len);
|
||||||
|
std::mem::forget(buf);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue