Skip to content

Commit

Permalink
chore: webgpu.36
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Oct 28, 2024
1 parent c3d890a commit 63479bf
Show file tree
Hide file tree
Showing 180 changed files with 13,109 additions and 9,125 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ strip = true
[workspace.dependencies.wgt]
package = "wgpu-types"
git = "https://github.com/gfx-rs/wgpu"
rev = "4f1a9b53deb053ca65f6a57e2e4c2b59e34ed1c2"
rev = "64a61ee5c69569bbb3db03563997e88a229eba17"

[workspace.dependencies]
env_logger = "0.11.5"
Expand All @@ -39,6 +39,6 @@ gl-bindings = { path = "./crates/gl-bindings" }
canvas-c = { path = "./crates/canvas-c" }
skia-safe = { version = "0.78.2", features = ["textlayout"] }
itertools = "0.13.0"
wgpu-core = { git = "https://github.com/gfx-rs/wgpu", rev = "4f1a9b53deb053ca65f6a57e2e4c2b59e34ed1c2", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
wgpu-types = { git = "https://github.com/gfx-rs/wgpu", rev = "4f1a9b53deb053ca65f6a57e2e4c2b59e34ed1c2" }
wgpu-core = { git = "https://github.com/gfx-rs/wgpu", rev = "64a61ee5c69569bbb3db03563997e88a229eba17", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
wgpu-types = { git = "https://github.com/gfx-rs/wgpu", rev = "64a61ee5c69569bbb3db03563997e88a229eba17" }
ureq = "2.10.1"
36 changes: 31 additions & 5 deletions crates/canvas-2d/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use text_styles::{
use crate::context::drawing_text::typography::Font;

use bitflags::bitflags;
use bytes::Buf;

pub mod drawing_images;
pub mod drawing_text;
Expand Down Expand Up @@ -216,10 +217,11 @@ impl Context {
pub fn get_pixels(&mut self, buffer: &mut [u8], origin: impl Into<IPoint>, size: impl Into<ISize>) {
let origin = origin.into();
let size = size.into();
let info = ImageInfo::new(size, ColorType::RGBA8888, AlphaType::Unpremul, None);
let mut info = ImageInfo::new(size, ColorType::RGBA8888, AlphaType::Unpremul, None);

if let Some(img) = self.get_image() {
img.read_pixels(&info, buffer, info.min_row_bytes(), origin, CachingHint::Allow);
let row_bytes = (info.width() * 4) as usize;
img.read_pixels(&info, buffer, row_bytes, origin, CachingHint::Allow);
}
}

Expand Down Expand Up @@ -352,7 +354,7 @@ impl Context {
self.flush();

let snapshot = self.surface.image_snapshot();
if self.surface_data.engine == SurfaceEngine::GL {
if self.surface_data.engine == SurfaceEngine::GL || self.surface_data.engine == SurfaceEngine::Vulkan || self.surface_data.engine == SurfaceEngine::Metal {
snapshot.make_raster_image(
self.direct_context.as_mut(),
Some(CachingHint::Allow),
Expand All @@ -361,6 +363,25 @@ impl Context {
Some(snapshot)
}
}

pub fn get_image_no_flush(&mut self) -> Option<Image> {
#[cfg(feature = "gl")]{
if let Some(ref context) = self.gl_context {
context.make_current();
}
}

let snapshot = self.surface.image_snapshot();
if self.surface_data.engine == SurfaceEngine::GL || self.surface_data.engine == SurfaceEngine::Vulkan || self.surface_data.engine == SurfaceEngine::Metal {
snapshot.make_raster_image(
self.direct_context.as_mut(),
Some(CachingHint::Allow),
)
} else {
Some(snapshot)
}
}

pub fn as_data(&mut self) -> Vec<u8> {
let bounds = self.surface_data.bounds;
if bounds.is_empty() {
Expand All @@ -384,6 +405,11 @@ impl Context {
IPoint::new(0, 0),
CachingHint::Allow,
);

if image.image_info().color_type() == ColorType::BGRA8888 {
canvas_core::image_asset::ImageAsset::bgra_to_rgba_in_place(pixels.as_mut_slice());
}

pixels
};

Expand Down Expand Up @@ -427,7 +453,7 @@ impl Context {
},
quality,
);
return match data {
match data {
Some(data) => {
let encoded_data =
base64::engine::general_purpose::STANDARD.encode(data.as_bytes());
Expand All @@ -441,7 +467,7 @@ impl Context {
encoded
}
_ => "data:,".to_string(),
};
}
}

pub fn render_to_canvas<F>(&mut self, paint: &skia_safe::Paint, f: F)
Expand Down
2 changes: 1 addition & 1 deletion crates/canvas-2d/src/image_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub(crate) fn create_image_bitmap_internal(

let image_info = skia_safe::ImageInfo::new(
(source_rect.width() as i32, source_rect.height() as i32),
skia_safe::ColorType::N32,
skia_safe::ColorType::RGBA8888,
ImageBitmapPremultiplyAlpha::from(premultiply_alpha).into(),
ImageBitmapColorSpaceConversion::from(color_space_conversion).to_color_space(),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/canvas-2d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn bytes_to_data_url(
encoded_prefix.push_str("data:");
encoded_prefix.push_str(format);
encoded_prefix.push_str(";base64,");
let image_info = ImageInfo::new((width, height), ColorType::N32, AlphaType::Unpremul, None);
let image_info = ImageInfo::new((width, height), ColorType::RGBA8888, AlphaType::Unpremul, None);
if let Some(image) = images::raster_from_data(&image_info, data, (width * 4) as usize) {
let mut quality = quality;
if quality > 100 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use canvas_c::webgpu::gpu::CanvasWebGPUInstance;
use canvas_c::WebGLState;
use canvas_core::context_attributes::PowerPreference;
use canvas_core::gpu::gl::GLContext;
use canvas_core::gpu::vulkan::VulkanContext;
use jni::objects::{JClass, JIntArray, JObject};
use jni::sys::{jboolean, jfloat, jint, jintArray, jlong, jobject, JNI_FALSE, JNI_TRUE};
use jni::sys::{jboolean, jfloat, jint, jlong, jobject, JNI_FALSE, JNI_TRUE};
use jni::JNIEnv;
use ndk::native_window::NativeWindow;
use raw_window_handle::RawWindowHandle;
Expand Down Expand Up @@ -250,6 +249,8 @@ pub extern "system" fn nativeInitWebGLNoSurface(
pub extern "system" fn nativeCreate2DContext(
env: JNIEnv,
_: JClass,
width: jint,
height: jint,
surface: jobject,
alpha: jboolean,
density: jfloat,
Expand All @@ -258,7 +259,27 @@ pub extern "system" fn nativeCreate2DContext(
direction: jint,
) -> jlong {
unsafe {
if let Some(window) = NativeWindow::from_surface(env.get_native_interface(), surface) {
if surface.is_null() {
let ctx_2d = canvas_c::CanvasRenderingContext2D::new_gl(
canvas_2d::context::Context::new_gl(
ptr::null_mut(),
width as f32,
height as f32,
density,
alpha == JNI_TRUE,
font_color,
ppi,
canvas_2d::context::text_styles::text_direction::TextDirection::from(
direction as u32,
),
),
alpha == JNI_TRUE,
);

drop(env);
return Box::into_raw(Box::new(ctx_2d)) as jlong;
}
return if let Some(window) = NativeWindow::from_surface(env.get_native_interface(), surface) {
let width = window.width();
let height = window.height();

Expand All @@ -279,7 +300,26 @@ pub extern "system" fn nativeCreate2DContext(
);

drop(env);
return Box::into_raw(Box::new(ctx_2d)) as jlong;
Box::into_raw(Box::new(ctx_2d)) as jlong
} else {
let ctx_2d = canvas_c::CanvasRenderingContext2D::new_gl(
canvas_2d::context::Context::new_gl(
ptr::null_mut(),
width as f32,
height as f32,
density,
alpha == JNI_TRUE,
font_color,
ppi,
canvas_2d::context::text_styles::text_direction::TextDirection::from(
direction as u32,
),
),
alpha == JNI_TRUE,
);

drop(env);
Box::into_raw(Box::new(ctx_2d)) as jlong
}
}

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

Expand Down Expand Up @@ -111,7 +111,7 @@ pub extern "system" fn nativeLoadFromUrl(
JNI_FALSE
}
Err(error) => {
let asset = unsafe { &*asset };
let asset = unsafe { &*asset };
let error = error.to_string();
asset.set_error(error.as_str());
JNI_FALSE
Expand All @@ -124,6 +124,8 @@ pub unsafe extern "system" fn nativeLoadFromBytes(
mut env: JNIEnv,
_: JClass,
asset: jlong,
width: jint,
height: jint,
byteArray: JByteArray,
) -> jboolean {
if asset == 0 {
Expand All @@ -137,7 +139,7 @@ pub unsafe extern "system" fn nativeLoadFromBytes(
let size = bytes.len();
let slice = std::slice::from_raw_parts_mut(bytes.as_ptr() as *mut u8, size);
let asset = unsafe { &*asset };
if asset.load_from_bytes(slice) {
if asset.load_from_raw_bytes(width as u32, height as u32, 4, slice.to_vec()) {
return JNI_TRUE;
}
JNI_FALSE
Expand All @@ -148,6 +150,62 @@ pub unsafe extern "system" fn nativeLoadFromBytes(

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

let asset = asset as *const 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 { &*asset };
if asset.load_from_raw_bytes(width as u32, height as u32, 4, slice.to_vec()) {
return JNI_TRUE;
}
}
JNI_FALSE
}


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

let asset = asset as *const 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 { &*asset };
if asset.load_from_bytes(slice) {
return JNI_TRUE;
}
JNI_FALSE
}
Err(_) => JNI_FALSE,
}
}

#[no_mangle]
pub extern "system" fn nativeLoadFromEncodedBuffer(
env: JNIEnv,
_: JClass,
asset: jlong,
Expand Down
14 changes: 11 additions & 3 deletions crates/canvas-android/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::jni_compat::org_nativescript_canvas_NSCCanvas::{nativeCreate2dContext

use crate::jni_compat::org_nativescript_canvas_NSCCanvas::{nativeContext2DPathTest, nativeContext2DPathTestNormal, nativeContext2DRender, nativeContext2DTest, nativeContext2DTestNormal, nativeCreate2DContext, nativeCustomWithBitmapFlush, nativeInitWebGL, nativeInitWebGLNoSurface, nativeInitWebGPU, nativeMakeWebGLCurrent, nativeMakeWebGLCurrentNormal, nativeReleaseWebGL, nativeReleaseWebGLNormal, nativeResizeWebGPU, nativeUpdate2DSurface, nativeUpdate2DSurfaceNoSurface, nativeUpdate2DSurfaceNoSurfaceNormal, nativeUpdateGLNoSurface, nativeUpdateWebGLNoSurfaceNormal, nativeUpdateWebGLSurface, nativeWebGLC2DRender, nativeWriteCurrentWebGLContextToBitmap, nativeContext2DConicTest};
use crate::jni_compat::org_nativescript_canvas_NSCCanvasRenderingContext2D::{nativeCreatePattern, nativeDrawAtlasWithBitmap, nativeDrawImageDxDyDwDhWithAsset, nativeDrawImageDxDyDwDhWithBitmap, nativeDrawImageDxDyWithAsset, nativeDrawImageDxDyWithBitmap, nativeDrawImageWithAsset, nativeDrawImageWithBitmap, nativeScale};
use crate::jni_compat::org_nativescript_canvas_NSCImageAsset::{nativeCreateImageAsset, nativeDestroyImageAsset, nativeGetDimensions, nativeGetError, nativeLoadFromBitmap, nativeLoadFromBuffer, nativeLoadFromBytes, nativeLoadFromPath, nativeLoadFromUrl};
use crate::jni_compat::org_nativescript_canvas_NSCImageAsset::{nativeCreateImageAsset, nativeDestroyImageAsset, nativeGetDimensions, nativeGetError, nativeLoadFromBitmap, nativeLoadFromBuffer, nativeLoadFromBytes, nativeLoadFromEncodedBuffer, nativeLoadFromEncodedBytes, nativeLoadFromPath, nativeLoadFromUrl};
use crate::jni_compat::org_nativescript_canvas_NSCImageBitmap::{nativeLoadBitmapFromBuffer, nativeLoadBitmapFromBufferOptions, nativeLoadBitmapFromBufferRectOptions, nativeLoadBitmapFromBytes, nativeLoadBitmapFromBytesOptions, nativeLoadBitmapFromBytesRectOptions};
use crate::jni_compat::org_nativescript_canvas_NSCWebGLRenderingContext::{
nativeTexImage2D, nativeTexSubImage2D,
Expand Down Expand Up @@ -100,7 +100,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
let mut ret = vec![
"(Landroid/view/Surface;ZZZZIZZZZZI)J",
"(IIZZZZIZZZZZI)J",
"(Landroid/view/Surface;ZFIFI)J",
"(IILandroid/view/Surface;ZFIFI)J",
"(Landroid/view/Surface;J)V",
"(Landroid/view/Surface;IIJ)V",
"(IIJ)V",
Expand Down Expand Up @@ -130,7 +130,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
let mut ret = vec![
"!(Landroid/view/Surface;ZZZZIZZZZZI)J",
"!(IIZZZZIZZZZZI)J",
"!(Landroid/view/Surface;ZFIFI)J",
"!(IILandroid/view/Surface;ZFIFI)J",
"!(Landroid/view/Surface;J)V",
"!(Landroid/view/Surface;IIJ)V",
"!(IIJ)V",
Expand Down Expand Up @@ -327,6 +327,8 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"nativeLoadFromUrl",
"nativeLoadFromBytes",
"nativeLoadFromBuffer",
"nativeLoadFromEncodedBytes",
"nativeLoadFromEncodedBuffer",
];

let image_asset_signatures = if ret >= ANDROID_O {
Expand All @@ -338,6 +340,8 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"(JLjava/lang/String;)Z",
"(J)Ljava/lang/String;",
"(JLjava/lang/String;)Z",
"(JII[B)Z",
"(JIILjava/nio/ByteBuffer;)Z",
"(J[B)Z",
"(JLjava/nio/ByteBuffer;)Z",
]
Expand All @@ -350,6 +354,8 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"!(JLjava/lang/String;)Z",
"!(J)Ljava/lang/String;",
"!(JLjava/lang/String;)Z",
"!(JII[B)Z",
"!(JIILjava/nio/ByteBuffer;)Z",
"!(J[B)Z",
"!(JLjava/nio/ByteBuffer;)Z",
]
Expand All @@ -365,6 +371,8 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
nativeLoadFromUrl as *mut c_void,
nativeLoadFromBytes as *mut c_void,
nativeLoadFromBuffer as *mut c_void,
nativeLoadFromEncodedBytes as *mut c_void,
nativeLoadFromEncodedBuffer as *mut c_void,
];

let image_asset_native_methods: Vec<NativeMethod> = izip!(
Expand Down
1 change: 1 addition & 0 deletions crates/canvas-c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ wgt = { workspace = true, package = "wgpu-types" }
futures = "0.3"
raw-window-handle.workspace = true
wgpu-core = { workspace = true, features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
infer = "0.16.0"

[target.'cfg(target_os="ios")'.dependencies]
display-link = { version = "0.2.0" }
Expand Down
Loading

0 comments on commit 63479bf

Please sign in to comment.