Skip to content

Commit

Permalink
chore: webgpu.0
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jul 22, 2024
1 parent 9eaa30d commit f104df6
Show file tree
Hide file tree
Showing 87 changed files with 6,231 additions and 4,044 deletions.
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Page xmlns:canvas="@nativescript/canvas" xmlns:ui="@nativescript/canvas/dom/index" xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
<Page.actionBar>
<ActionBar title="canvas 2.0 WebGPU" icon="" class="action-bar">
<ActionBar title="Canvas 2.0" icon="" class="action-bar">
</ActionBar>
</Page.actionBar>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use jni::objects::{JClass, JIntArray, JObject, JString};
use jni::sys::{jboolean, jlong, jobject, JNI_FALSE, JNI_TRUE};
use jni::JNIEnv;
use jni::objects::{JByteArray, JByteBuffer, JClass, JIntArray, JObject, JString, ReleaseMode};
use jni::sys::{jboolean, jlong, JNI_FALSE, JNI_TRUE, jobject};
use ndk::bitmap::BitmapFormat;

use canvas_c::ImageAsset;
Expand Down Expand Up @@ -74,8 +74,8 @@ pub extern "system" fn nativeLoadFromPath(

match env.get_string(&path) {
Ok(path) => {
let path: String = path.into();
if asset.load_from_path(path.as_str()) {
let path = path.to_string_lossy();
if asset.load_from_path(path.as_ref()) {
return JNI_TRUE;
}
JNI_FALSE
Expand All @@ -88,6 +88,90 @@ pub extern "system" fn nativeLoadFromPath(
}
}


#[no_mangle]
pub extern "system" fn nativeLoadFromUrl(
mut env: JNIEnv,
_: JClass,
asset: jlong,
url: JString,
) -> jboolean {
if asset == 0 {
return JNI_FALSE;
}

let asset = asset as *mut ImageAsset;

match env.get_string(&url) {
Ok(path) => {
if canvas_c::canvas_native_image_asset_load_from_url(asset, path.as_ptr()) {
return JNI_TRUE;
}
JNI_FALSE
}
Err(error) => {
let asset = unsafe { &mut *asset };
let error = error.to_string();
asset.set_error(error.as_str());
JNI_FALSE
}
}
}

#[no_mangle]
pub unsafe extern "system" fn nativeLoadFromBytes(
mut env: JNIEnv,
_: JClass,
asset: jlong,
byteArray: JByteArray,
) -> jboolean {
if asset == 0 {
return JNI_FALSE;
}

let asset = asset as *mut ImageAsset;

match env.get_array_elements_critical(&byteArray, ReleaseMode::NoCopyBack) {
Ok(bytes) => {
let size = bytes.len();
let slice = std::slice::from_raw_parts_mut(bytes.as_ptr() as *mut u8, size);
let asset = unsafe { &mut *asset };
if asset.load_from_bytes(slice) {
return JNI_TRUE;
}
JNI_FALSE
}
Err(_) => JNI_FALSE
}
}

#[no_mangle]
pub extern "system" fn nativeLoadFromBuffer(
mut env: JNIEnv,
_: JClass,
asset: jlong,
buffer: JByteBuffer,
) -> jboolean {
if asset == 0 {
return JNI_FALSE;
}

let asset = asset as *mut ImageAsset;

if let (Ok(buf), Ok(size)) = (
env.get_direct_buffer_address(&buffer),
env.get_direct_buffer_capacity(&buffer),
) {
let slice = unsafe { std::slice::from_raw_parts(buf, size) };
let asset = unsafe { &mut *asset };
if asset.load_from_bytes(slice) {
return JNI_TRUE;
}
}
JNI_FALSE
}


#[no_mangle]
pub extern "system" fn nativeGetDimensions(
mut env: JNIEnv,
Expand Down
65 changes: 36 additions & 29 deletions crates/canvas-android/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,41 @@ extern crate log;

use std::os::raw::c_void;

use ::jni::JavaVM;
use ::jni::signature::JavaType;
use ::jni::sys::jint;
use ::jni::JavaVM;
use android_logger::Config;
use itertools::izip;
use jni::{JNIEnv, NativeMethod};
use jni::objects::JClass;
use jni::sys::jlong;
use jni::{JNIEnv, NativeMethod};
use log::LevelFilter;
use once_cell::sync::OnceCell;

use crate::jni_compat::org_nativescript_canvas_NSCCanvas::{
nativeContext2DPathTest, nativeContext2DPathTestNormal, nativeContext2DRender,
nativeContext2DTest, nativeContext2DTestNormal, nativeCreate2DContext,
nativeCreate2DContextNormal, nativeCustomWithBitmapFlush, nativeGLPointerRefCount,
nativeGLPointerRefCountNormal, nativeGetGLPointer, nativeGetGLPointerNormal, nativeInitGL,
nativeInitGLNoSurface, nativeMakeGLCurrent, nativeMakeGLCurrentNormal, nativeReleaseGL,
nativeReleaseGLNormal, nativeReleaseGLPointer, nativeReleaseGLPointerNormal,
nativeUpdate2DSurface, nativeUpdate2DSurfaceNoSurface, nativeUpdate2DSurfaceNoSurfaceNormal,
nativeUpdateGLNoSurface, nativeUpdateGLNoSurfaceNormal, nativeUpdateGLSurface,
nativeWebGLC2DRender, nativeWriteCurrentGLContextToBitmap, nativeInitWebGPU
nativeCreate2DContextNormal, nativeCustomWithBitmapFlush, nativeGetGLPointer,
nativeGetGLPointerNormal, nativeGLPointerRefCount, nativeGLPointerRefCountNormal, nativeInitGL,
nativeInitGLNoSurface, nativeInitWebGPU, nativeMakeGLCurrent, nativeMakeGLCurrentNormal,
nativeReleaseGL, nativeReleaseGLNormal, nativeReleaseGLPointer,
nativeReleaseGLPointerNormal, nativeUpdate2DSurface, nativeUpdate2DSurfaceNoSurface,
nativeUpdate2DSurfaceNoSurfaceNormal, nativeUpdateGLNoSurface, nativeUpdateGLNoSurfaceNormal,
nativeUpdateGLSurface, nativeWebGLC2DRender, nativeWriteCurrentGLContextToBitmap,
};
use crate::jni_compat::org_nativescript_canvas_NSCCanvasRenderingContext2D::{
nativeCreatePattern, nativeDrawAtlasWithBitmap, nativeDrawImageDxDyDwDhWithAsset,
nativeDrawImageDxDyDwDhWithBitmap, nativeDrawImageDxDyWithAsset, nativeDrawImageDxDyWithBitmap,
nativeDrawImageWithAsset, nativeDrawImageWithBitmap,
};
use crate::jni_compat::org_nativescript_canvas_NSCImageAsset::{
nativeCreateImageAsset, nativeDestroyImageAsset, nativeGetDimensions, nativeGetError,
nativeLoadFromBitmap, nativeLoadFromPath,
};
use crate::jni_compat::org_nativescript_canvas_NSCImageAsset::{nativeCreateImageAsset, nativeDestroyImageAsset, nativeGetDimensions, nativeGetError, nativeLoadFromBitmap, nativeLoadFromBuffer, nativeLoadFromBytes, nativeLoadFromPath, nativeLoadFromUrl};
use crate::jni_compat::org_nativescript_canvas_NSCWebGLRenderingContext::{nativeTexImage2D, nativeTexSubImage2D};

use crate::utils::gl::st::{SurfaceTexture, SURFACE_TEXTURE};
use crate::utils::gl::texture_render::nativeDrawFrame;
use crate::utils::{
nativeInitContextWithCustomSurface, nativeInitContextWithCustomSurfaceNormal,
nativeResizeCustomSurface, nativeResizeCustomSurfaceNormal,
};
use crate::utils::gl::st::{SURFACE_TEXTURE, SurfaceTexture};
use crate::utils::gl::texture_render::nativeDrawFrame;

mod jni_compat;
pub mod utils;
Expand Down Expand Up @@ -290,12 +286,12 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
canvas_rendering_context_2d_signatures,
canvas_rendering_context_2d_methods
)
.map(|(name, signature, method)| NativeMethod {
name: name.into(),
sig: signature.into(),
fn_ptr: method,
})
.collect();
.map(|(name, signature, method)| NativeMethod {
name: name.into(),
sig: signature.into(),
fn_ptr: method,
})
.collect();

let _ = env.register_native_methods(
&canvas_rendering_context_2d_class,
Expand All @@ -313,6 +309,9 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"nativeGetDimensions",
"nativeLoadFromPath",
"nativeGetError",
"nativeLoadFromUrl",
"nativeLoadFromBytes",
"nativeLoadFromBuffer"
];

let image_asset_signatures = if ret >= ANDROID_O {
Expand All @@ -323,6 +322,9 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"(J[I)V",
"(JLjava/lang/String;)Z",
"(J)Ljava/lang/String;",
"(JLjava/lang/String;)Z",
"(J[B)Z",
"(JLjava/nio/ByteBuffer;)Z",
]
} else {
[
Expand All @@ -332,6 +334,9 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"!(J[I)V",
"!(JLjava/lang/String;)Z",
"!(J)Ljava/lang/String;",
"!(JLjava/lang/String;)Z",
"!(J[B)Z",
"!(JLjava/nio/ByteBuffer;)Z",
]
};

Expand All @@ -342,25 +347,27 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
nativeGetDimensions as *mut c_void,
nativeLoadFromPath as *mut c_void,
nativeGetError as *mut c_void,
nativeLoadFromUrl as *mut c_void,
nativeLoadFromBytes as *mut c_void,
nativeLoadFromBuffer as *mut c_void
];

let image_asset_native_methods: Vec<NativeMethod> = izip!(
image_asset_method_names,
image_asset_signatures,
image_asset_methods
)
.map(|(name, signature, method)| NativeMethod {
name: name.into(),
sig: signature.into(),
fn_ptr: method,
})
.collect();
.map(|(name, signature, method)| NativeMethod {
name: name.into(),
sig: signature.into(),
fn_ptr: method,
})
.collect();

let _ = env
.register_native_methods(&image_asset_class, image_asset_native_methods.as_slice());



let webgl_rendering_class = env
.find_class("org/nativescript/canvas/NSCWebGLRenderingContext")
.unwrap();
Expand Down
9 changes: 9 additions & 0 deletions crates/canvas-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4480,6 +4480,15 @@ impl ImageAsset {
}
}

#[no_mangle]
pub extern "C" fn canvas_native_image_asset_get_addr(asset: *mut ImageAsset) -> i64 {
if asset.is_null() {
return 0;
}
asset as i64
}


#[no_mangle]
pub extern "C" fn canvas_native_image_asset_create() -> *mut ImageAsset {
Box::into_raw(Box::new(ImageAsset::default()))
Expand Down
2 changes: 1 addition & 1 deletion crates/canvas-c/src/webgpu/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ pub extern "C" fn canvas_native_webgpu_enum_gpu_texture_to_string(
}

#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum CanvasOptionalGPUTextureFormat {
None,
Some(CanvasGPUTextureFormat),
Expand Down
41 changes: 27 additions & 14 deletions crates/canvas-c/src/webgpu/gpu_canvas_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use raw_window_handle::{
AppKitDisplayHandle, RawDisplayHandle, RawWindowHandle,
};

use crate::webgpu::enums::CanvasOptionalGPUTextureFormat;
use crate::webgpu::error::handle_error_fatal;
use crate::webgpu::gpu_adapter::CanvasGPUAdapter;
use crate::webgpu::gpu_device::ErrorSink;
use crate::webgpu::structs::CanvasSurfaceCapabilities;
use crate::webgpu::structs::{CanvasExtent3d, CanvasSurfaceCapabilities};

use super::{
enums::CanvasGPUTextureFormat, gpu::CanvasWebGPUInstance, gpu_device::CanvasGPUDevice,
Expand Down Expand Up @@ -294,6 +295,8 @@ pub struct CanvasGPUSurfaceConfiguration {
pub presentMode: CanvasGPUPresentMode,
pub view_formats: *const CanvasGPUTextureFormat,
pub view_formats_size: usize,
pub size: *const CanvasExtent3d,
pub format: CanvasOptionalGPUTextureFormat,
}

#[no_mangle]
Expand Down Expand Up @@ -327,22 +330,34 @@ pub unsafe extern "C" fn canvas_native_webgpu_context_configure(
};

#[cfg(any(target_os = "ios", target_os = "macos"))]
let format = wgpu_types::TextureFormat::Bgra8Unorm;

let mut format = wgpu_types::TextureFormat::Bgra8Unorm;

#[cfg(any(target_os = "android"))]
let format = wgpu_types::TextureFormat::Rgba8Unorm;
let mut format = wgpu_types::TextureFormat::Rgba8Unorm;

match config.format {
CanvasOptionalGPUTextureFormat::None => {}
CanvasOptionalGPUTextureFormat::Some(value) => {
format = value.into();
}
}

let usage = wgpu_types::TextureUsages::from_bits_truncate(config.usage);

let view_data = context.view_data.lock();
let view_data = if !config.size.is_null() {
let size = &*config.size;
(size.width, size.height)
} else {
let view_data = context.view_data.lock();
(view_data.width, view_data.height)
};

let config = wgpu_types::SurfaceConfiguration::<Vec<wgpu_types::TextureFormat>> {
desired_maximum_frame_latency: 2,
usage,
format,
width: view_data.height,
height: view_data.width,
width: view_data.0,
height: view_data.1,
present_mode: config.presentMode.into(),
alpha_mode: config.alphaMode.into(),
view_formats,
Expand All @@ -363,8 +378,8 @@ pub unsafe extern "C" fn canvas_native_webgpu_context_configure(
usage,
dimension: wgpu_types::TextureDimension::D2,
size: wgpu_types::Extent3d {
width: view_data.width,
height: view_data.height,
width: view_data.0,
height: view_data.1,
depth_or_array_layers: 1,
},
format,
Expand All @@ -388,11 +403,9 @@ pub unsafe extern "C" fn canvas_native_webgpu_context_unconfigure(
return;
}
let context = &*context;
let surface_id = context.surface;
let global = context.instance.global();
let device = &*device;
let device_id = device.device;
let config = &*config;

let mut lock = context.data.lock();
*lock = None;
}

#[no_mangle]
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@nativescript/webpack": "~5.0.5",
"@ngtools/webpack": "^15.0.0",
"@pixi-essentials/svg": "^2.0.1",
"@types/three": "^0.157.0",
"@types/three": "0.166.0",
"@xmldom/xmldom": "0.8.10",
"babylonjs": "^6.0.0",
"babylonjs-materials": "^6.0.0",
Expand Down Expand Up @@ -69,7 +69,8 @@
"rimraf": "^3.0.2",
"rxjs": "~7.8.0",
"svelte-native": "^1.0.5",
"three": "^0.157.0",
"teapot": "^1.0.0",
"three": "~0.166.1",
"typescript": "~4.8.0",
"util": "~0.12.5",
"vexflow": "^4.2.3",
Expand Down
Loading

0 comments on commit f104df6

Please sign in to comment.