Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Nov 24, 2024
1 parent f33b4dc commit e22df1f
Show file tree
Hide file tree
Showing 21 changed files with 839 additions and 38 deletions.
2 changes: 2 additions & 0 deletions crates/canvas-2d/src/context/surface_gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl Context {
frame_buffer.format = GR_GL_RGB565;
}

println!("??? {width} {height}");

let target = gpu::backend_render_targets::make_gl(
(width as i32, height as i32),
Some(0),
Expand Down
15 changes: 10 additions & 5 deletions crates/canvas-c/src/c2d/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,10 +2211,11 @@ pub extern "C" fn canvas_native_context_fill(context: *mut CanvasRenderingContex
#[no_mangle]
pub extern "C" fn canvas_native_context_fill_with_path(
context: *mut CanvasRenderingContext2D,
path: &mut Path,
path: *mut Path,
rule: CanvasFillRule,
) {
let context = unsafe { &mut *context };
let path = unsafe { &mut *path };
context.context.fill_rule(Some(&mut path.0), rule.into());
}

Expand Down Expand Up @@ -2312,12 +2313,13 @@ pub extern "C" fn canvas_native_context_is_point_in_path_str(
#[no_mangle]
pub extern "C" fn canvas_native_context_is_point_in_path_with_path_str(
context: *mut CanvasRenderingContext2D,
path: &mut Path,
path: *mut Path,
x: f32,
y: f32,
rule: CanvasFillRule,
) -> bool {
let context = unsafe { &*context };
let path = unsafe { &*path };
context
.context
.point_in_path(Some(&path.0), x, y, rule.into())
Expand All @@ -2337,12 +2339,13 @@ pub extern "C" fn canvas_native_context_is_point_in_path(
#[no_mangle]
pub extern "C" fn canvas_native_context_is_point_in_path_with_path(
context: *mut CanvasRenderingContext2D,
path: &mut Path,
path: *mut Path,
x: f32,
y: f32,
rule: CanvasFillRule,
) -> bool {
let context = unsafe { &*context };
let path = unsafe { &*path };
context
.context
.point_in_path(Some(&path.0), x, y, rule.into())
Expand All @@ -2361,11 +2364,12 @@ pub extern "C" fn canvas_native_context_is_point_in_stroke(
#[no_mangle]
pub extern "C" fn canvas_native_context_is_point_in_stroke_with_path(
context: *mut CanvasRenderingContext2D,
path: &mut Path,
path: *mut Path,
x: f32,
y: f32,
) -> bool {
let context = unsafe { &*context };
let path = unsafe { &*path };
context.context.point_in_stroke(Some(&path.0), x, y)
}

Expand Down Expand Up @@ -2565,9 +2569,10 @@ pub extern "C" fn canvas_native_context_stroke(context: *mut CanvasRenderingCont
#[no_mangle]
pub extern "C" fn canvas_native_context_stroke_with_path(
context: *mut CanvasRenderingContext2D,
path: &mut Path,
path: *mut Path,
) {
let context = unsafe { &mut *context };
let path = unsafe { &mut *path };
context.context.stroke(Some(&mut path.0));
}

Expand Down
8 changes: 8 additions & 0 deletions crates/canvas-c/src/c2d/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use std::os::raw::c_char;
#[derive(Clone)]
pub struct Path(pub(crate) canvas_2d::context::paths::path::Path);

impl Path {
pub fn with_d(d: &str) -> Self {
Path(canvas_2d::context::paths::path::Path::from_str(d))
}
}

impl Default for Path {
fn default() -> Self {
Self(Default::default())
Expand Down Expand Up @@ -205,6 +211,8 @@ pub extern "C" fn canvas_native_path_round_rect(
let radii = unsafe { std::slice::from_raw_parts(radii, size) };
let path = unsafe { &mut *path };

println!("radii: {:?}", radii);

let size = radii.len();
if size == 0 {
return;
Expand Down
4 changes: 3 additions & 1 deletion crates/canvas-c/src/image_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::sync::Arc;
#[derive(Clone)]
pub struct ImageAsset(pub(crate) canvas_core::image_asset::ImageAsset);

unsafe impl Send for ImageAsset {}

impl ImageAsset {
pub fn new(asset: canvas_core::image_asset::ImageAsset) -> Self {
Self(asset)
Expand Down Expand Up @@ -227,7 +229,7 @@ pub extern "C" fn canvas_native_image_asset_load_from_raw(
) -> bool {
let array = unsafe { std::slice::from_raw_parts(array, size) };
let asset = unsafe { &*asset };
asset.load_from_raw_bytes(width, height,4, array.to_vec())
asset.load_from_raw_bytes(width, height, 4, array.to_vec())
}

#[no_mangle]
Expand Down
3 changes: 2 additions & 1 deletion crates/canvas-core/src/gpu/gl/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ impl NSOpenGLView {
}

pub fn flush_buffer(&self) {
let _: () = unsafe { msg_send![&self.0, flushBuffer] };
let context: Id<NSObject> = unsafe { msg_send_id![&self.0, openGLContext] };
let _: () = unsafe { msg_send![&context, flushBuffer] };
}

pub fn open_gl_context(&self) -> NSOpenGLContext {
Expand Down
2 changes: 2 additions & 0 deletions crates/canvas-core/src/image_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ unsafe impl Send for ImageAssetInner {}
#[derive(Clone, Debug, Default)]
pub struct ImageAsset(Arc<parking_lot::Mutex<ImageAssetInner>>);

unsafe impl Send for ImageAsset {}

impl ImageAsset {
pub fn with_bytes_dimension<F>(&self, f: F)
where
Expand Down
6 changes: 3 additions & 3 deletions crates/canvas-ios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name = "canvas-ios"
version = "2.0.0"
edition = "2021"

[lib]
name = "canvasnative"
crate-type = ["staticlib"]
#[lib]
#name = "canvasnative"
#crate-type = ["staticlib"]

[dependencies]
canvas-core = { workspace = true, features = ["2d", "gl", "metal"] }
Expand Down
9 changes: 9 additions & 0 deletions crates/playground/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,13 @@ fn main() {

canvas_c::canvas_native_font_add_family_with_bytes(c"ChaChicle".as_ptr(), ChaChicle.as_ptr(), ChaChicle.len());

canvas_c::canvas_native_webgl_create_no_window(
600, 300, 1, true, false, false, false, 1, true, false, false, false, false, false
);


/*
event_loop.run(move |event, target| {
match event {
Event::NewEvents(_) => {}
Expand Down Expand Up @@ -2121,6 +2128,8 @@ fn main() {
}
});
*/

/*
event_loop.run(move |event, target| {
match event {
Expand Down
2 changes: 1 addition & 1 deletion napi/canvas-napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi = { version = "2.12.2", default-features = false, features = ["napi6", "tokio_full"] }
napi-derive = "2.12.2"
canvas-core = { workspace = true, features = ["2d", "gl", "mtl"] }
canvas-c = { workspace = true, features = ["2d", "webgl", "gl", "metal"] }
Expand Down
31 changes: 31 additions & 0 deletions napi/canvas-napi/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//import { CanvasRenderingContext2D, WebGLRenderingContext } from './index.js';

import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

const { CanvasRenderingContext2D, WebGLRenderingContext } = require('./canvas-napi.darwin-universal.node');

const ctx = CanvasRenderingContext2D.withCpu(300, 150, 1, true, 0, 90, 1);
ctx.fillRect(0, 0, 300, 150);
console.log('ctx', ctx.toDataURL('image/png'));
console.log(ctx.font);
ctx.font = '100px serif';
console.log(ctx.font);

ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 150);
ctx.fillStyle = 'blue';
ctx.fillRect(100, 0, 100, 150);
ctx.fillStyle = 'green';
ctx.fillRect(200, 0, 100, 150);

console.log('ctx', ctx.toDataURL('image/png'));

const gl = WebGLRenderingContext.offscreen(600, 300, 1, true, false, false, false, 1, true, false, false, false, false, false);

gl.clearColor(0, 0, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.flush();

console.log('gl', gl.toDataURL('image/png'));
35 changes: 35 additions & 0 deletions napi/canvas-napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,40 @@

export type JSWebGLRenderingContext = WebGLRenderingContext;
export declare class WebGLRenderingContext {
get drawingBufferWidth(): number;
get drawingBufferHeight(): number;
static withView(view: number, version: number, alpha: boolean, antialias: boolean, depth: boolean, failIfMajorPerformanceCaveat: boolean, powerPreference: number, premultipliedAlpha: boolean, preserveDrawingBuffer: boolean, stencil: boolean, desynchronized: boolean, xrCompatible: boolean): JsWebGlRenderingContext;
static offscreen(width: number, height: number, version: number, alpha: boolean, antialias: boolean, depth: boolean, failIfMajorPerformanceCaveat: boolean, powerPreference: number, premultipliedAlpha: boolean, preserveDrawingBuffer: boolean, stencil: boolean, desynchronized: boolean, xrCompatible: boolean, isCanvas: boolean): JsWebGlRenderingContext;
clearColor(red: number, green: number, blue: number, alpha: number): void;
clear(mask: number): void;
flush(): void;
toDataURL(format: string, encoderOptions?: number | undefined | null): string;
get COLOR_BUFFER_BIT(): number;
}
export type JSPath2D = Path2D;
export declare class Path2D {
constructor(data?: Path2D | string | undefined | null);
addPath(path: Path2D): void;
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: boolean | undefined | null): void;
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void;
bezierCurveTo(cp1X: number, cp1Y: number, cp2X: number, cp2Y: number, x: number, y: number): void;
closePath(): void;
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: boolean | undefined | null): void;
lineTo(x: number, y: number): void;
moveTo(x: number, y: number): void;
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void;
rect(x: number, y: number, width: number, height: number): void;
roundRect(x: number, y: number, width: number, height: number, radii: number | Array<number>): void;
trim(start: number, end: number): void;
toSvg(): string;
}
export type JSCanvasRenderingContext2D = CanvasRenderingContext2D;
export declare class CanvasRenderingContext2D {
static withView(view: number, width: number, height: number, density: number, alpha: boolean, fontColor: number, ppi: number, direction: number): JsCanvasRenderingContext2D;
static withCpu(width: number, height: number, density: number, alpha: boolean, fontColor: number, ppi: number, direction: number): JsCanvasRenderingContext2D;
render(): void;
fill(path?: Path2D | undefined | null, rule?: string | undefined | null): void;
stroke(path?: Path2D | undefined | null): void;
get shadowColor(): string;
set shadowColor(color: string);
get globalAlpha(): number;
Expand All @@ -32,8 +56,19 @@ export declare class CanvasRenderingContext2D {
fillRect(x: number, y: number, width: number, height: number): void;
strokeRect(x: number, y: number, width: number, height: number): void;
toDataURL(format: string, encoderOptions?: number | undefined | null): string;
drawImage(image: JSImageAsset, sx?: number | undefined | null, sy?: number | undefined | null, sWidth?: number | undefined | null, sHeight?: number | undefined | null, dx?: number | undefined | null, dy?: number | undefined | null, dWidth?: number | undefined | null, dHeight?: number | undefined | null): void;
}
export type JSCanvasPattern = CanvasPattern;
export declare class CanvasPattern {}
export type JSCCanvasGradient = CanvasGradient;
export declare class CanvasGradient {}
export type JSImageAsset = ImageAsset;
export declare class ImageAsset {
constructor();
get width(): number;
get height(): number;
get error(): string;
fromUrlSync(url: string): boolean;
fromUrl(url: string): Promise<boolean>;
loadUrlCallback(url: string, callback: (...args: any[]) => any): void;
}
4 changes: 3 additions & 1 deletion napi/canvas-napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`);
}

const { WebGLRenderingContext, CanvasRenderingContext2D, CanvasPattern, CanvasGradient } = nativeBinding;
const { WebGLRenderingContext, Path2D, CanvasRenderingContext2D, CanvasPattern, CanvasGradient, ImageAsset } = nativeBinding;

module.exports.WebGLRenderingContext = WebGLRenderingContext;
module.exports.Path2D = Path2D;
module.exports.CanvasRenderingContext2D = CanvasRenderingContext2D;
module.exports.CanvasPattern = CanvasPattern;
module.exports.CanvasGradient = CanvasGradient;
module.exports.ImageAsset = ImageAsset;
Loading

0 comments on commit e22df1f

Please sign in to comment.