Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: macOS NAPI #133

Draft
wants to merge 31 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"new": "cpp"
}
}
934 changes: 934 additions & 0 deletions .yarn/releases/yarn-4.5.2.cjs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-4.5.2.cjs
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ members = [
"crates/canvas-ios",
"crates/playground",
"crates/canvas-c",
"crates/canvas-svg"]
"crates/canvas-svg",
"napi/canvas-napi",
]

[profile.release]
panic = "abort"
Expand All @@ -25,7 +27,7 @@ strip = true
[workspace.dependencies.wgt]
package = "wgpu-types"
git = "https://github.com/triniwiz/wgpu"
rev = "48f663f70d5b6e99a5b8a708876ce9642d78fbde"
rev = "6f163cdf622bd183fcfc20224294b797312550bb"

[workspace.dependencies]
env_logger = "0.11.5"
Expand All @@ -37,9 +39,9 @@ canvas-core = { path = "./crates/canvas-core" }
canvas-webgl = { path = "./crates/canvas-webgl" }
gl-bindings = { path = "./crates/gl-bindings" }
canvas-c = { path = "./crates/canvas-c" }
skia-safe = { version = "0.78.2", features = ["textlayout"] }
skia-safe = { version = "0.80.0", features = ["textlayout"] }
itertools = "0.13.0"
wgpu-core = { git = "https://github.com/triniwiz/wgpu", rev = "48f663f70d5b6e99a5b8a708876ce9642d78fbde", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
wgpu-types = { git = "https://github.com/triniwiz/wgpu", rev = "48f663f70d5b6e99a5b8a708876ce9642d78fbde" }
wgpu-core = { git = "https://github.com/triniwiz/wgpu", rev = "6f163cdf622bd183fcfc20224294b797312550bb", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
wgpu-types = { git = "https://github.com/triniwiz/wgpu", rev = "6f163cdf622bd183fcfc20224294b797312550bb" }
ureq = "2.10.1"
jni = "0.21.1"
3 changes: 2 additions & 1 deletion crates/canvas-2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ raw-window-handle.workspace = true
bitflags = "2.6.0"

[target.'cfg(any(target_os = "ios", target_os="macos"))'.dependencies]
foreign-types-shared = "0.3.1"
foreign-types-shared = "0.3.1"
objc2-foundation = { version = "0.2.2", features = ["NSAutoreleasePool"] }
7 changes: 6 additions & 1 deletion crates/canvas-2d/src/context/drawing_images/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl Context {
}

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

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

fn draw_image_with_rect(&mut self, image: &Image, dst_rect: impl Into<Rect>) {

#[cfg(any(target_os = "macos", target_os = "ios"))]
let _ = unsafe { objc2_foundation::NSAutoreleasePool::new() };
#[cfg(feature = "gl")]{
if let Some(ref context) = self.gl_context {
context.make_current();
Expand Down
16 changes: 12 additions & 4 deletions crates/canvas-2d/src/context/drawing_paths/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ use std::borrow::BorrowMut;

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

use crate::context::Context;
use crate::context::drawing_paths::fill_rule::FillRule;
use crate::context::paths::path::Path;
use crate::context::Context;

pub mod fill_rule;

impl Context {
fn fill_or_stroke(&mut self, is_fill: bool, path: Option<&mut Path>, fill_rule: Option<FillRule>) {

#[cfg(feature = "gl")]{
fn fill_or_stroke(
&mut self,
is_fill: bool,
path: Option<&mut Path>,
fill_rule: Option<FillRule>,
) {
#[cfg(feature = "gl")]
{
if let Some(ref context) = self.gl_context {
context.make_current();
}
Expand Down Expand Up @@ -42,6 +47,9 @@ impl Context {
)
};

#[cfg(any(target_os = "macos", target_os = "ios"))]
let pool = unsafe { objc2_foundation::NSAutoreleasePool::new() };

self.render_to_canvas(&paint, |canvas, paint| {
if let Some(paint) = &shadow_paint {
canvas.draw_path(path, paint);
Expand Down
5 changes: 3 additions & 2 deletions crates/canvas-2d/src/context/fill_and_stroke_styles/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl PaintStyle {
blue
}

#[inline]
pub fn new_color_str(color: &str) -> Option<Self> {
parse(color)
.map(|color| {
Expand Down Expand Up @@ -201,7 +202,7 @@ impl Paint {
color: Color,
blur: c_float,
) -> Option<skia_safe::Paint> {
if !(color != Color::TRANSPARENT && blur > 0.0) {
if color == Color::TRANSPARENT && blur > 0.0 {
return None;
}
let mut paint = self.fill_paint().clone();
Expand All @@ -215,7 +216,7 @@ impl Paint {
color: Color,
blur: c_float,
) -> Option<skia_safe::Paint> {
if !(color != Color::TRANSPARENT && blur > 0.0) {
if color == Color::TRANSPARENT && blur > 0.0 {
return None;
}
let mut paint = self.stroke_paint().clone();
Expand Down
8 changes: 8 additions & 0 deletions crates/canvas-2d/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,14 @@ impl Context {
};
}

pub fn reset(&mut self) {
self.reset_state();
self.reset_transform();
self.with_canvas_dirty(|canvas| {
canvas.clear(Color::TRANSPARENT);
});
}

pub fn reset_state(&mut self) {
let direction = self.state.direction;
self.state = State::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct ImageDataInner {
}

unsafe impl Send for ImageDataInner {}
unsafe impl Sync for ImageDataInner {}

#[derive(Debug, Clone)]
pub struct ImageData(ImageDataInner);
Expand Down
7 changes: 7 additions & 0 deletions crates/canvas-2d/src/context/shadows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::os::raw::c_float;
use skia_safe::Color;

use crate::context::Context;
use crate::utils::color::parse_color;

impl Context {
pub fn set_shadow_blur(&mut self, blur: c_float) {
Expand Down Expand Up @@ -34,6 +35,12 @@ impl Context {
self.state.shadow_color = color;
}

pub fn set_shadow_color_str(&mut self, color: &str) {
if let Some(color) = parse_color(color) {
self.state.shadow_color = color;
}
}

pub fn set_shadow_color_rgba(&mut self, r: u8, g: u8, b: u8, a: u8) {
self.state.shadow_color = Color::from_argb(a, r, g, b);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/canvas-2d/src/context/text_styles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ impl Context {
}

pub fn get_letter_spacing(&self) -> &str {
return self.state.letter_spacing_value.as_str();
self.state.letter_spacing_value.as_str()
}
}
4 changes: 3 additions & 1 deletion crates/canvas-c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ futures = "0.3"
raw-window-handle.workspace = true
wgpu-core = { workspace = true, features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
infer = "0.16.0"
image = {version = "0.25.5", optional = true}
image = { version = "0.25.5", optional = true }

[target.'cfg(target_os="ios")'.dependencies]
display-link = { version = "0.2.0" }
wgpu-core = { workspace = true, features = ["wgsl", "metal", "raw-window-handle"] }
objc2-foundation = { version = "0.2.2", features = ["NSGeometry", "NSData", "NSAutoreleasePool"] }

[target.'cfg(target_os="macos")'.dependencies]
metal = { version = "0.30.0"}
#display-link = { git = "https://github.com/servo/display-link", branch = "no-transmute" }
#wgpu-core = { version = "22.0.0", features = ["wgsl", "metal", "raw-window-handle"] }
#objc2-foundation = { version = "0.2.2", features = ["NSGeometry", "NSData", "NSAutoreleasePool"] }
Expand Down
59 changes: 59 additions & 0 deletions crates/canvas-c/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl U16Buffer {
pub fn length(&self) -> usize {
self.0.len()
}

pub fn into_vec(self) -> Vec<u16> {
self.0
}
}

impl Default for U16Buffer {
Expand Down Expand Up @@ -133,6 +137,10 @@ impl F32Buffer {
pub fn length(&self) -> usize {
self.0.len()
}

pub fn into_vec(self) -> Vec<f32> {
self.0
}
}

impl Default for F32Buffer {
Expand Down Expand Up @@ -164,6 +172,10 @@ impl I32Buffer {
pub fn length(&self) -> usize {
self.0.len()
}

pub fn into_vec(self) -> Vec<i32> {
self.0
}
}

impl Default for I32Buffer {
Expand Down Expand Up @@ -196,6 +208,10 @@ impl U32Buffer {
pub fn length(&self) -> usize {
self.0.len()
}

pub fn into_vec(self) -> Vec<u32> {
self.0
}
}

impl Default for U32Buffer {
Expand All @@ -210,13 +226,56 @@ impl From<Vec<u32>> for U32Buffer {
}
}


pub struct BoolBuffer(Vec<bool>);

impl BoolBuffer {
pub fn new_with_vec(value: Vec<bool>) -> Self {
Self(value)
}

pub fn get_buffer(&self) -> &[bool] {
self.0.as_slice()
}

pub fn get_buffer_mut(&mut self) -> &mut [bool] {
self.0.as_mut_slice()
}

pub fn length(&self) -> usize {
self.0.len()
}

pub fn into_vec(self) -> Vec<bool> {
self.0
}
}

impl Default for BoolBuffer {
fn default() -> Self {
Self::new_with_vec(Vec::new())
}
}

impl From<Vec<bool>> for BoolBuffer {
fn from(value: Vec<bool>) -> Self {
Self::new_with_vec(value)
}
}

pub struct StringBuffer(Vec<String>);
impl From<Vec<String>> for StringBuffer {
fn from(value: Vec<String>) -> Self {
Self(value)
}
}

impl Into<Vec<String>> for StringBuffer {
fn into(self) -> Vec<String> {
self.0
}
}

// todo use convert StringBuffer to enum
impl From<Vec<&str>> for StringBuffer {
fn from(value: Vec<&str>) -> Self {
Expand Down
Loading
Loading