Skip to content

Commit b05d799

Browse files
committed
chore: updates
1 parent 7441bd1 commit b05d799

34 files changed

+4769
-169
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ impl Context {
8484
frame_buffer.format = GR_GL_RGB565;
8585
}
8686

87-
println!("??? {width} {height}");
88-
8987
let target = gpu::backend_render_targets::make_gl(
9088
(width as i32, height as i32),
9189
Some(0),

crates/canvas-c/src/buffers.rs

+16
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 {

crates/canvas-c/src/text_decoder.rs

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ use crate::c2d::CCow;
77
#[derive(Clone)]
88
pub struct TextDecoder(canvas_2d::context::text_decoder::TextDecoder);
99

10+
impl TextDecoder {
11+
pub fn encoding(&self) -> &str {
12+
self.0.encoding()
13+
}
14+
15+
pub fn decode(&self, data: &[u8]) -> String {
16+
self.0.decode_to_string(data)
17+
}
18+
}
19+
20+
1021
#[no_mangle]
1122
pub extern "C" fn canvas_native_text_decoder_release(value: *mut TextDecoder) {
1223
if value.is_null() {

crates/canvas-c/src/text_encoder.rs

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ use crate::buffers::U8Buffer;
55

66
#[derive(Clone)]
77
pub struct TextEncoder(canvas_2d::context::text_encoder::TextEncoder);
8+
impl TextEncoder {
9+
pub fn encoding(&self) -> &str {
10+
self.0.encoding()
11+
}
12+
13+
pub fn encode(&self, value: &str) -> Vec<u8> {
14+
self.0.encode(value)
15+
}
16+
}
817

918
#[no_mangle]
1019
pub extern "C" fn canvas_native_text_encoder_release(value: *mut TextEncoder) {

crates/canvas-c/src/webgl/gl.rs

+62-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/* GL */
2-
use std::ffi::{CStr, CString};
3-
use std::os::raw::{c_char, c_void};
4-
use std::ptr::NonNull;
5-
use canvas_2d::context::SurfaceEngine::CPU;
62
use canvas_2d::utils::image::from_image_slice;
73
use canvas_core::context_attributes::PowerPreference;
84
use canvas_core::gpu::gl::GLContext;
95
use canvas_webgl::prelude::WebGLVersion;
6+
use std::ffi::{CStr, CString};
7+
use std::os::raw::{c_char, c_void};
8+
use std::ptr::NonNull;
109

1110
use crate::buffers::{F32Buffer, I32Buffer, StringBuffer, U32Buffer, U8Buffer};
1211
use crate::c2d::CanvasRenderingContext2D;
@@ -277,7 +276,7 @@ pub extern "C" fn canvas_native_webgl_state_destroy(state: *mut WebGLState) {
277276
}
278277

279278
impl WebGLState {
280-
#[cfg(target_os = "android")]
279+
#[cfg(target_os = "android")]
281280
pub fn new_with_view(
282281
view: *mut c_void,
283282
width: i32,
@@ -534,6 +533,12 @@ impl WebGLShaderPrecisionFormat {
534533

535534
pub struct WebGLExtension(Option<Box<dyn canvas_webgl::prelude::WebGLExtension>>);
536535

536+
impl WebGLExtension {
537+
pub fn new(extension: Option<Box<dyn canvas_webgl::prelude::WebGLExtension>>) -> Self {
538+
Self(extension)
539+
}
540+
}
541+
537542
#[no_mangle]
538543
pub extern "C" fn canvas_native_webgl_extension_destroy(value: *mut WebGLExtension) {
539544
if value.is_null() {
@@ -1193,6 +1198,21 @@ pub extern "C" fn canvas_native_webgl_result_get_i32_array(
11931198
})))
11941199
}
11951200

1201+
#[no_mangle]
1202+
pub extern "C" fn canvas_native_webgl_result_into_i32_array(
1203+
result: *mut WebGLResult,
1204+
) -> *mut I32Buffer {
1205+
if result.is_null() {
1206+
return std::ptr::null_mut();
1207+
}
1208+
let result = unsafe { *Box::from_raw(result) };
1209+
Box::into_raw(Box::new(I32Buffer::from(match result.0 {
1210+
canvas_webgl::prelude::WebGLResult::I32Array(value) => value,
1211+
_ => Vec::new(),
1212+
})))
1213+
}
1214+
1215+
11961216
#[no_mangle]
11971217
pub extern "C" fn canvas_native_webgl_result_get_u32_array(
11981218
result: *const WebGLResult,
@@ -1204,6 +1224,21 @@ pub extern "C" fn canvas_native_webgl_result_get_u32_array(
12041224
})))
12051225
}
12061226

1227+
#[no_mangle]
1228+
pub extern "C" fn canvas_native_webgl_result_into_u32_array(
1229+
result: *mut WebGLResult,
1230+
) -> *mut U32Buffer {
1231+
if result.is_null() {
1232+
return std::ptr::null_mut();
1233+
}
1234+
1235+
let result = unsafe { *Box::from_raw(result) };
1236+
Box::into_raw(Box::new(U32Buffer::from(match result.0 {
1237+
canvas_webgl::prelude::WebGLResult::U32Array(value) => value,
1238+
_ => Vec::new(),
1239+
})))
1240+
}
1241+
12071242
#[no_mangle]
12081243
pub extern "C" fn canvas_native_webgl_result_get_f32_array(
12091244
result: *const WebGLResult,
@@ -1215,6 +1250,22 @@ pub extern "C" fn canvas_native_webgl_result_get_f32_array(
12151250
})))
12161251
}
12171252

1253+
#[no_mangle]
1254+
pub extern "C" fn canvas_native_webgl_result_into_f32_array(
1255+
result: *mut WebGLResult,
1256+
) -> *mut F32Buffer {
1257+
if result.is_null() {
1258+
return std::ptr::null_mut();
1259+
}
1260+
let result = unsafe { *Box::from_raw(result) };
1261+
1262+
Box::into_raw(Box::new(F32Buffer::from(match result.0 {
1263+
canvas_webgl::prelude::WebGLResult::F32Array(value) => value,
1264+
_ => Vec::new(),
1265+
})))
1266+
}
1267+
1268+
12181269
#[no_mangle]
12191270
pub extern "C" fn canvas_native_webgl_result_get_bool_array(
12201271
result: *const WebGLResult,
@@ -1269,11 +1320,13 @@ pub extern "C" fn canvas_native_webgl_result_get_string(
12691320
return CString::new("WebGL 2.0 (OpenGL ES 3.0 NativeScript)")
12701321
.unwrap()
12711322
.into_raw();
1323+
} else if val.contains("OpenGL ES 2.") {
1324+
ret = CString::new("WebGL 1.0 (OpenGL ES 2.0 NativeScript)")
1325+
.unwrap()
1326+
.into_raw()
1327+
} else {
1328+
ret = result.clone().into_raw()
12721329
}
1273-
1274-
ret = CString::new("WebGL 1.0 (OpenGL ES 2.0 NativeScript)")
1275-
.unwrap()
1276-
.into_raw()
12771330
}
12781331
_ => {
12791332
ret = std::ptr::null_mut();

crates/canvas-core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ash = { version = "0.38.0", optional = true, features = ["libloading"] }
2121
ash-window = { version = "0.13.0", optional = true }
2222

2323
[target.'cfg(any(target_os = "ios", target_os="macos"))'.dependencies]
24-
icrate = { version = "0.1.2", features = ["objc2", "Foundation", "Foundation_NSData"] }
24+
icrate = { version = "0.1.2", features = ["objc2", "Foundation", "Foundation_NSData", "Foundation_NSArray"] }
2525
core-foundation = "0.10.0"
2626
objc2-foundation = { version = "0.2.2", features = ["NSGeometry", "NSData", "NSAutoreleasePool"] }
2727
metal = { version = "0.30.0", optional = true }

crates/canvas-core/src/gpu/gl/mac.rs

+50-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use std::ffi::c_void;
2-
use std::ptr::NonNull;
3-
use std::sync::OnceLock;
4-
1+
use crate::context_attributes::ContextAttributes;
52
use icrate::objc2::rc::Id;
63
use icrate::objc2::{class, msg_send, msg_send_id};
74
use objc2_foundation::NSObject;
85
use raw_window_handle::RawWindowHandle;
9-
10-
use crate::context_attributes::ContextAttributes;
6+
use std::ffi::c_void;
7+
use std::ptr::NonNull;
8+
use std::sync::OnceLock;
119

1210
pub static IS_GL_SYMBOLS_LOADED: OnceLock<bool> = OnceLock::new();
1311

@@ -178,15 +176,16 @@ impl NSOpenGLContext {
178176
pub struct NSOpenGLPixelFormat(Id<NSObject>);
179177

180178
impl NSOpenGLPixelFormat {
181-
pub fn init_with_attributes(attribs: &[u32]) -> Self {
179+
pub fn init_with_attributes(attribs: &[u32]) -> Option<Self> {
182180
let cls = class!(NSOpenGLPixelFormat);
183-
let instance = unsafe { msg_send_id![cls, alloc] };
184-
NSOpenGLPixelFormat(unsafe {
181+
let alloc = unsafe { msg_send_id![cls, alloc] };
182+
let instance: Option<Id<NSObject>> = unsafe {
185183
msg_send_id![
186-
instance,
184+
alloc,
187185
initWithAttributes: attribs.as_ptr()
188186
]
189-
})
187+
};
188+
instance.map(|id| NSOpenGLPixelFormat(id))
190189
}
191190
}
192191

@@ -241,16 +240,18 @@ impl NSOpenGLView {
241240
#[derive(Debug, Default)]
242241
pub struct GLContext(GLContextInner);
243242

244-
fn parse_context_attributes(context_attrs: &ContextAttributes) -> NSOpenGLPixelFormat {
243+
fn parse_context_attributes(context_attrs: &ContextAttributes, accelerated: bool) -> Option<NSOpenGLPixelFormat> {
245244
let mut attributes = vec![
246-
NSOpenGLPixelFormatAttribute::NSOpenGLPFAAccelerated as u32,
247-
NSOpenGLPixelFormatAttribute::NSOpenGLPFADoubleBuffer as u32,
248245
NSOpenGLPixelFormatAttribute::NSOpenGLPFAOpenGLProfile as u32,
249246
NSOpenGLPFAOpenGLProfiles::NSOpenGLProfileVersion3_2Core as u32,
250247
NSOpenGLPixelFormatAttribute::NSOpenGLPFAColorSize as u32,
251248
24,
252249
];
253250

251+
if accelerated {
252+
attributes.push(NSOpenGLPixelFormatAttribute::NSOpenGLPFAAccelerated as u32);
253+
}
254+
254255
if context_attrs.get_alpha() {
255256
attributes.push(NSOpenGLPixelFormatAttribute::NSOpenGLPFAAlphaSize as u32);
256257
attributes.push(8);
@@ -274,6 +275,9 @@ fn parse_context_attributes(context_attrs: &ContextAttributes) -> NSOpenGLPixelF
274275
attributes.push(4);
275276
}
276277

278+
attributes.push(NSOpenGLPixelFormatAttribute::NSOpenGLPFADoubleBuffer as u32);
279+
attributes.push(0);
280+
277281
NSOpenGLPixelFormat::init_with_attributes(attributes.as_slice())
278282
}
279283

@@ -363,14 +367,26 @@ impl GLContext {
363367
NSOpenGLPixelFormatAttribute::NSOpenGLPFAOpenGLProfile as u32,
364368
NSOpenGLPFAOpenGLProfiles::NSOpenGLProfileVersion3_2Core as u32,
365369
0,
366-
]);
370+
])?;
367371
NSOpenGLContext::new(format, None)
368372
}
369373
};
370374

371-
let format = parse_context_attributes(context_attrs);
375+
let mut format = parse_context_attributes(context_attrs, true);
372376

373-
let context = NSOpenGLContext::new(format, share_group.clone());
377+
if format.is_none() {
378+
if context_attrs.get_antialias() {
379+
context_attrs.set_antialias(false);
380+
}
381+
382+
if context_attrs.get_preserve_drawing_buffer() {
383+
context_attrs.set_preserve_drawing_buffer(false);
384+
}
385+
386+
format = parse_context_attributes(context_attrs, false);
387+
}
388+
389+
let context = NSOpenGLContext::new(format?, share_group.clone());
374390

375391

376392
if context.is_none() {
@@ -393,9 +409,24 @@ impl GLContext {
393409
width: i32,
394410
height: i32,
395411
) -> Option<Self> {
396-
let format = parse_context_attributes(context_attrs);
412+
let mut format = parse_context_attributes(context_attrs, true);
413+
414+
if format.is_none() {
415+
if context_attrs.get_antialias() {
416+
context_attrs.set_antialias(false);
417+
}
418+
419+
if context_attrs.get_preserve_drawing_buffer() {
420+
context_attrs.set_preserve_drawing_buffer(false);
421+
}
422+
423+
format = parse_context_attributes(context_attrs, false);
424+
}
425+
426+
397427
let view =
398-
NSOpenGLView::new_with_frame_pixel_format(0., 0., width as f32, height as f32, format);
428+
NSOpenGLView::new_with_frame_pixel_format(0., 0., width as f32, height as f32, format?);
429+
399430

400431
GLContext::create_window_context_with_gl_view(context_attrs, view, None)
401432
}

crates/canvas-webgl/src/prelude.rs

-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@ impl WebGLState {
442442
if !self.context.make_current() {
443443
return false;
444444
}
445-
446445
self.context.swap_buffers()
447446
}
448447

0 commit comments

Comments
 (0)