Skip to content

Commit cc02858

Browse files
authored
feat: macOS NAPI (#133)
* feat: macOS NAPI * chore: updates * fix: shadow paint * fix: macos bool * feat: shadow color string * chore: updates * chore: updates * chore: flappyBird * feat: save & restore * chore: updates * chore: update demo * chore: updates * chore: updates * chore: updates * feat: webgl2 * chore: updates * chore: updates * chore: updates * chore: updates * chore: updates * chore(node-demo): start script * chore: node start script * chore: updates * chore: updates * chore: updates * chore: updates * chore: updates * chore: updates * feat: 0.0.1 * feat: 0.0.2 * chore: 0.0.5 * fix: magnify -> wheel event * chore: demo * chore: bump * chore: updates * chore: fix build * chore: move napi into crates
1 parent 42ff74a commit cc02858

File tree

305 files changed

+57515
-630
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

305 files changed

+57515
-630
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"new": "cpp"
4+
}
5+
}

.yarn/releases/yarn-4.5.2.cjs

Lines changed: 934 additions & 0 deletions
Large diffs are not rendered by default.

.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yarnPath: .yarn/releases/yarn-4.5.2.cjs

Cargo.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ members = [
1010
"crates/canvas-ios",
1111
"crates/playground",
1212
"crates/canvas-c",
13-
"crates/canvas-svg"]
13+
"crates/canvas-svg",
14+
"crates/canvas-napi",
15+
]
1416

1517
[profile.release]
1618
panic = "abort"
@@ -25,7 +27,7 @@ strip = true
2527
[workspace.dependencies.wgt]
2628
package = "wgpu-types"
2729
git = "https://github.com/triniwiz/wgpu"
28-
rev = "48f663f70d5b6e99a5b8a708876ce9642d78fbde"
30+
rev = "6f163cdf622bd183fcfc20224294b797312550bb"
2931

3032
[workspace.dependencies]
3133
env_logger = "0.11.5"
@@ -37,9 +39,9 @@ canvas-core = { path = "./crates/canvas-core" }
3739
canvas-webgl = { path = "./crates/canvas-webgl" }
3840
gl-bindings = { path = "./crates/gl-bindings" }
3941
canvas-c = { path = "./crates/canvas-c" }
40-
skia-safe = { version = "0.78.2", features = ["textlayout"] }
42+
skia-safe = { version = "0.80.0", features = ["textlayout"] }
4143
itertools = "0.13.0"
42-
wgpu-core = { git = "https://github.com/triniwiz/wgpu", rev = "48f663f70d5b6e99a5b8a708876ce9642d78fbde", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
43-
wgpu-types = { git = "https://github.com/triniwiz/wgpu", rev = "48f663f70d5b6e99a5b8a708876ce9642d78fbde" }
44+
wgpu-core = { git = "https://github.com/triniwiz/wgpu", rev = "6f163cdf622bd183fcfc20224294b797312550bb", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
45+
wgpu-types = { git = "https://github.com/triniwiz/wgpu", rev = "6f163cdf622bd183fcfc20224294b797312550bb" }
4446
ureq = "2.10.1"
4547
jni = "0.21.1"

crates/canvas-2d/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ raw-window-handle.workspace = true
2626
bitflags = "2.6.0"
2727

2828
[target.'cfg(any(target_os = "ios", target_os="macos"))'.dependencies]
29-
foreign-types-shared = "0.3.1"
29+
foreign-types-shared = "0.3.1"
30+
objc2-foundation = { version = "0.2.2", features = ["NSAutoreleasePool"] }

crates/canvas-2d/src/context/drawing_images/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ impl Context {
147147
}
148148

149149
pub fn draw_image_dx_dy(&mut self, image: &Image, x: f32, y: f32) {
150+
#[cfg(any(target_os = "macos", target_os = "ios"))]
151+
let _ = unsafe { objc2_foundation::NSAutoreleasePool::new() };
150152
#[cfg(feature = "gl")]{
151153
if let Some(ref context) = self.gl_context {
152154
context.make_current();
@@ -174,6 +176,8 @@ impl Context {
174176
}
175177

176178
fn draw_image(&mut self, image: &Image, src_rect: impl Into<Rect>, dst_rect: impl Into<Rect>) {
179+
#[cfg(any(target_os = "macos", target_os = "ios"))]
180+
let _ = unsafe { objc2_foundation::NSAutoreleasePool::new() };
177181
#[cfg(feature = "gl")]{
178182
if let Some(ref context) = self.gl_context {
179183
context.make_current();
@@ -209,7 +213,8 @@ impl Context {
209213
}
210214

211215
fn draw_image_with_rect(&mut self, image: &Image, dst_rect: impl Into<Rect>) {
212-
216+
#[cfg(any(target_os = "macos", target_os = "ios"))]
217+
let _ = unsafe { objc2_foundation::NSAutoreleasePool::new() };
213218
#[cfg(feature = "gl")]{
214219
if let Some(ref context) = self.gl_context {
215220
context.make_current();

crates/canvas-2d/src/context/drawing_paths/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ use std::borrow::BorrowMut;
22

33
use skia_safe::{ClipOp, Matrix, Point};
44

5-
use crate::context::Context;
65
use crate::context::drawing_paths::fill_rule::FillRule;
76
use crate::context::paths::path::Path;
7+
use crate::context::Context;
88

99
pub mod fill_rule;
1010

1111
impl Context {
12-
fn fill_or_stroke(&mut self, is_fill: bool, path: Option<&mut Path>, fill_rule: Option<FillRule>) {
13-
14-
#[cfg(feature = "gl")]{
12+
fn fill_or_stroke(
13+
&mut self,
14+
is_fill: bool,
15+
path: Option<&mut Path>,
16+
fill_rule: Option<FillRule>,
17+
) {
18+
#[cfg(feature = "gl")]
19+
{
1520
if let Some(ref context) = self.gl_context {
1621
context.make_current();
1722
}
@@ -42,6 +47,9 @@ impl Context {
4247
)
4348
};
4449

50+
#[cfg(any(target_os = "macos", target_os = "ios"))]
51+
let pool = unsafe { objc2_foundation::NSAutoreleasePool::new() };
52+
4553
self.render_to_canvas(&paint, |canvas, paint| {
4654
if let Some(paint) = &shadow_paint {
4755
canvas.draw_path(path, paint);

crates/canvas-2d/src/context/fill_and_stroke_styles/paint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl PaintStyle {
5151
blue
5252
}
5353

54+
#[inline]
5455
pub fn new_color_str(color: &str) -> Option<Self> {
5556
parse(color)
5657
.map(|color| {
@@ -201,7 +202,7 @@ impl Paint {
201202
color: Color,
202203
blur: c_float,
203204
) -> Option<skia_safe::Paint> {
204-
if !(color != Color::TRANSPARENT && blur > 0.0) {
205+
if color == Color::TRANSPARENT && blur > 0.0 {
205206
return None;
206207
}
207208
let mut paint = self.fill_paint().clone();
@@ -215,7 +216,7 @@ impl Paint {
215216
color: Color,
216217
blur: c_float,
217218
) -> Option<skia_safe::Paint> {
218-
if !(color != Color::TRANSPARENT && blur > 0.0) {
219+
if color == Color::TRANSPARENT && blur > 0.0 {
219220
return None;
220221
}
221222
let mut paint = self.stroke_paint().clone();

crates/canvas-2d/src/context/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,14 @@ impl Context {
560560
};
561561
}
562562

563+
pub fn reset(&mut self) {
564+
self.reset_state();
565+
self.reset_transform();
566+
self.with_canvas_dirty(|canvas| {
567+
canvas.clear(Color::TRANSPARENT);
568+
});
569+
}
570+
563571
pub fn reset_state(&mut self) {
564572
let direction = self.state.direction;
565573
self.state = State::default();

crates/canvas-2d/src/context/pixel_manipulation/image_data.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct ImageDataInner {
1414
}
1515

1616
unsafe impl Send for ImageDataInner {}
17+
unsafe impl Sync for ImageDataInner {}
1718

1819
#[derive(Debug, Clone)]
1920
pub struct ImageData(ImageDataInner);

crates/canvas-2d/src/context/shadows.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::os::raw::c_float;
33
use skia_safe::Color;
44

55
use crate::context::Context;
6+
use crate::utils::color::parse_color;
67

78
impl Context {
89
pub fn set_shadow_blur(&mut self, blur: c_float) {
@@ -34,6 +35,12 @@ impl Context {
3435
self.state.shadow_color = color;
3536
}
3637

38+
pub fn set_shadow_color_str(&mut self, color: &str) {
39+
if let Some(color) = parse_color(color) {
40+
self.state.shadow_color = color;
41+
}
42+
}
43+
3744
pub fn set_shadow_color_rgba(&mut self, r: u8, g: u8, b: u8, a: u8) {
3845
self.state.shadow_color = Color::from_argb(a, r, g, b);
3946
}

crates/canvas-2d/src/context/surface_gl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl Context {
3434
false,
3535
false,
3636
true,
37+
false,
3738
);
3839

3940
let bounds = skia_safe::Rect::from_wh(width, height);

crates/canvas-2d/src/context/text_styles/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ impl Context {
102102
}
103103

104104
pub fn get_letter_spacing(&self) -> &str {
105-
return self.state.letter_spacing_value.as_str();
105+
self.state.letter_spacing_value.as_str()
106106
}
107107
}

crates/canvas-c/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ futures = "0.3"
2626
raw-window-handle.workspace = true
2727
wgpu-core = { workspace = true, features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
2828
infer = "0.16.0"
29-
image = {version = "0.25.5", optional = true}
29+
image = { version = "0.25.5", optional = true }
30+
3031
[target.'cfg(target_os="ios")'.dependencies]
3132
display-link = { version = "0.2.0" }
3233
wgpu-core = { workspace = true, features = ["wgsl", "metal", "raw-window-handle"] }
3334
objc2-foundation = { version = "0.2.2", features = ["NSGeometry", "NSData", "NSAutoreleasePool"] }
3435

3536
[target.'cfg(target_os="macos")'.dependencies]
37+
metal = { version = "0.30.0"}
3638
#display-link = { git = "https://github.com/servo/display-link", branch = "no-transmute" }
3739
#wgpu-core = { version = "22.0.0", features = ["wgsl", "metal", "raw-window-handle"] }
3840
#objc2-foundation = { version = "0.2.2", features = ["NSGeometry", "NSData", "NSAutoreleasePool"] }

crates/canvas-c/src/buffers.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ impl U16Buffer {
101101
pub fn length(&self) -> usize {
102102
self.0.len()
103103
}
104+
105+
pub fn into_vec(self) -> Vec<u16> {
106+
self.0
107+
}
104108
}
105109

106110
impl Default for U16Buffer {
@@ -133,6 +137,10 @@ impl F32Buffer {
133137
pub fn length(&self) -> usize {
134138
self.0.len()
135139
}
140+
141+
pub fn into_vec(self) -> Vec<f32> {
142+
self.0
143+
}
136144
}
137145

138146
impl Default for F32Buffer {
@@ -164,6 +172,10 @@ impl I32Buffer {
164172
pub fn length(&self) -> usize {
165173
self.0.len()
166174
}
175+
176+
pub fn into_vec(self) -> Vec<i32> {
177+
self.0
178+
}
167179
}
168180

169181
impl Default for I32Buffer {
@@ -196,6 +208,10 @@ impl U32Buffer {
196208
pub fn length(&self) -> usize {
197209
self.0.len()
198210
}
211+
212+
pub fn into_vec(self) -> Vec<u32> {
213+
self.0
214+
}
199215
}
200216

201217
impl Default for U32Buffer {
@@ -210,13 +226,56 @@ impl From<Vec<u32>> for U32Buffer {
210226
}
211227
}
212228

229+
230+
pub struct BoolBuffer(Vec<bool>);
231+
232+
impl BoolBuffer {
233+
pub fn new_with_vec(value: Vec<bool>) -> Self {
234+
Self(value)
235+
}
236+
237+
pub fn get_buffer(&self) -> &[bool] {
238+
self.0.as_slice()
239+
}
240+
241+
pub fn get_buffer_mut(&mut self) -> &mut [bool] {
242+
self.0.as_mut_slice()
243+
}
244+
245+
pub fn length(&self) -> usize {
246+
self.0.len()
247+
}
248+
249+
pub fn into_vec(self) -> Vec<bool> {
250+
self.0
251+
}
252+
}
253+
254+
impl Default for BoolBuffer {
255+
fn default() -> Self {
256+
Self::new_with_vec(Vec::new())
257+
}
258+
}
259+
260+
impl From<Vec<bool>> for BoolBuffer {
261+
fn from(value: Vec<bool>) -> Self {
262+
Self::new_with_vec(value)
263+
}
264+
}
265+
213266
pub struct StringBuffer(Vec<String>);
214267
impl From<Vec<String>> for StringBuffer {
215268
fn from(value: Vec<String>) -> Self {
216269
Self(value)
217270
}
218271
}
219272

273+
impl Into<Vec<String>> for StringBuffer {
274+
fn into(self) -> Vec<String> {
275+
self.0
276+
}
277+
}
278+
220279
// todo use convert StringBuffer to enum
221280
impl From<Vec<&str>> for StringBuffer {
222281
fn from(value: Vec<&str>) -> Self {

0 commit comments

Comments
 (0)