Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/audio-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Button text="Pan Left" tap="testPanLeft" class="btn btn-secondary" />
<Button text="Pan Right" tap="testPanRight" class="btn btn-secondary" />
</StackLayout>
<canvas:Canvas id="visualizer" row="1" ready="canvasReady" style="width:100%; height:100%;" />
<canvas:Canvas id="visualizer" row="1" loaded="canvasReady" style="width:100%; height:100%;" />

<StackLayout row="2" padding="8">
<Label text="Volume" class="muted" />
Expand Down
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-babylon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</Page.actionBar>
<StackLayout class="p-20">
<GridLayout loaded="loaded">
<canvas:Canvas id="canvas" loaded="{{loaded}}" unloaded="{{unloaded}}" ready="{{ canvasLoaded }}"/>
<canvas:Canvas id="canvas" loaded="{{ canvasLoaded }}" unloaded="{{unloaded}}"/>
</GridLayout>
</StackLayout>
</Page>
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-chartjs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
</ActionBar>
</Page.actionBar>
<GridLayout height="100%" width="100%">
<canvas:Canvas style="width:100%;height:100%;" ready="{{ ready }}"/>
<canvas:Canvas style="width:100%;height:100%;" loaded="{{ ready }}"/>
</GridLayout>
</Page>
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-perf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ActionBar title="canvas perf" icon="" class="action-bar" />
</Page.actionBar>
<GridLayout rows="*, auto" columns="*" style="width:100%;height:100%">
<canvas:Canvas row="0" col="0" id="canvas" ready="{{ canvasReady }}" style="width:100%;height:100%;" />
<canvas:Canvas row="0" col="0" id="canvas" loaded="{{ canvasReady }}" style="width:100%;height:100%;" />
<Label row="1" col="0" text="{{ status }}" textWrap="true" class="p-10" />
</GridLayout>
</Page>
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-phaser-ce.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</ActionBar>
</Page.actionBar>
<GridLayout iosOverflowSafeArea="false" touch="{{ onTouch }}" pan="{{ onTouch }}">
<canvas:Canvas iosOverflowSafeArea="true" id="canvas" width="100%" height="100%" ready="loaded"/>
<canvas:Canvas iosOverflowSafeArea="true" id="canvas" width="100%" height="100%" loaded="loaded"/>
<ActivityIndicator loaded="indicatorLoaded" busy="{{ isLoading }}"
visibility="{{ isLoading ? 'visible': 'collapsed' }}"/>

Expand Down
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-phaser.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
</ActionBar>
</Page.actionBar>
<StackLayout>
<canvas:Canvas id="canvas" width="100%" height="100%" ready="{{ canvasLoaded }}"/>
<canvas:Canvas id="canvas" width="100%" height="100%" loaded="{{ canvasLoaded }}"/>
</StackLayout>
</Page>
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-pixi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</ActionBar>
</Page.actionBar>
<GridLayout width="100%" height="100%">
<canvas:Canvas style="width:100%; height:auto;" id="canvas" ready="{{canvasLoaded}}" loaded="{{loaded}}"
<canvas:Canvas style="width:100%; height:auto;" id="canvas" loaded="{{canvasLoaded}}"
unloaded="{{unloaded}}"/>
</GridLayout>
</Page>
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-spec.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Label row="0" col="0" text="{{ status }}" textWrap="true" class="p-10" />
<!-- A real on-screen surface for the tests that cannot use an offscreen
canvas (WebGPU needs a window to configure against). -->
<canvas:Canvas row="1" col="0" id="surface" ready="{{ canvasReady }}" style="width:64;height:64;" />
<canvas:Canvas row="1" col="0" id="surface" loaded="{{ canvasReady }}" style="width:64;height:64;" />
<ScrollView row="2" col="0">
<Label text="{{ report }}" textWrap="true" class="p-10" style="font-family:monospace;font-size:11;" />
</ScrollView>
Expand Down
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas-three.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</ActionBar>
</Page.actionBar>
<GridLayout class="bg-black w-full h-full" iosOverflowSafeArea="true">
<canvas:Canvas id="canvas" ready="{{ canvasLoaded }}" loaded="{{ loaded }}"
<canvas:Canvas id="canvas" loaded="{{ canvasLoaded }}"
unloaded="{{unloaded}}"/>
</GridLayout>
</Page>
10 changes: 5 additions & 5 deletions apps/demo/src/plugin-demos/canvas.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ loaded="{{ rectLoaded }}"

<!-- <WebView rowSpan="2" colSpan="2" height="100%" width="100%" loaded="loaded"/>" -->

<canvas:Canvas isUserInteractionEnabled="true" ready="{{ canvasLoaded }}" style="width: 100%;height:100%;"/>
<canvas:Canvas isUserInteractionEnabled="true" loaded="{{ canvasLoaded }}" style="width: 100%;height:100%;"/>

<!-- <Button height="40" text="Draw" tap="{{ draw }}"/> -->

Expand All @@ -188,10 +188,10 @@ loaded="{{ rectLoaded }}"

<!-- Multi Canvas -->

<!-- <canvas:Canvas row="0" col="0" id="canvas1" width="100%" height="100%" ready="{{ canvasLoaded }}"/>
<canvas:Canvas row="0" col="1" id="canvas2" width="100%" height="100%" ready="{{ canvasLoaded }}"/>
<canvas:Canvas row="1" col="0" id="canvas3" width="100%" height="100%" ready="{{ canvasLoaded }}"/>
<canvas:Canvas row="1" col="1" id="canvas4" width="100%" height="100%" ready="{{ canvasLoaded }}"/> -->
<!-- <canvas:Canvas row="0" col="0" id="canvas1" width="100%" height="100%" loaded="{{ canvasLoaded }}"/>
<canvas:Canvas row="0" col="1" id="canvas2" width="100%" height="100%" loaded="{{ canvasLoaded }}"/>
<canvas:Canvas row="1" col="0" id="canvas3" width="100%" height="100%" loaded="{{ canvasLoaded }}"/>
<canvas:Canvas row="1" col="1" id="canvas4" width="100%" height="100%" loaded="{{ canvasLoaded }}"/> -->

<!-- Multi Canvas -->
</GridLayout>
Expand Down
21 changes: 21 additions & 0 deletions crates/canvas-2d/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ pub struct Context {
pub(crate) font_color: Color,
}

impl Drop for Context {
fn drop(&mut self) {
// GrDirectContext frees its GL objects on whichever context is current, which
// may be another canvas's; its names would then delete that canvas's objects.
#[cfg(feature = "gl")]
if let Some(ref context) = self.gl_context {
context.make_current();
}
}
}

impl Context {
pub fn get_recording_context(&mut self) -> Option<RecordingContext> {
self.surface.recording_context()
Expand Down Expand Up @@ -471,6 +482,16 @@ impl Context {
ret
}

pub fn draw_pixels(&mut self, image: &Image) {
let canvas = self.surface.canvas();
canvas.save();
canvas.reset_matrix();
let mut paint = skia_safe::Paint::default();
paint.set_blend_mode(BlendMode::Src);
canvas.draw_image(image, (0., 0.), Some(&paint));
canvas.restore();
}

pub fn get_image_no_flush(&mut self) -> Option<Image> {
#[cfg(feature = "gl")]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,21 @@ pub extern "system" fn nativeInitWebGPU(
}

unsafe {
let interface = env.get_native_interface();
if let Some(window) = NativeWindow::from_surface(interface, surface) {
let Some(ptr) = NonNull::new(window.ptr().as_ptr() as *mut c_void) else {
return 0;
};
let instance: *mut CanvasWebGPUInstance = instance as _;
let ret = canvas_c::webgpu::gpu_canvas_context::canvas_native_webgpu_context_create(
instance,
ptr.as_ptr(),
width as u32,
height as u32,
);

return ret as jlong;
}
let window = if surface.is_null() {
None
} else {
NativeWindow::from_surface(env.get_native_interface(), surface)
};
let ptr = window
.as_ref()
.map_or(ptr::null_mut(), |window| window.ptr().as_ptr() as *mut c_void);
canvas_c::webgpu::gpu_canvas_context::canvas_native_webgpu_context_create(
instance as *mut CanvasWebGPUInstance,
ptr,
width as u32,
height as u32,
) as jlong
}
0
}

#[no_mangle]
Expand Down Expand Up @@ -175,6 +173,7 @@ pub extern "system" fn nativeDetach2DSurface(_: JNIEnv, _: JClass, context: jlon
false,
false,
false,
false,
true,
false,
color_space,
Expand Down Expand Up @@ -431,6 +430,15 @@ pub extern "system" fn nativeUpdate2DSurface(
let context = context.get_context_mut();
let color_space = context.surface_data().color_space();
let alpha = !context.surface_data().is_opaque();
// A new EGL surface starts blank; resize() only clears on a size change.
let pixels = if context.gl_context.is_some()
&& context.surface_data().width() as i32 == width
&& context.surface_data().height() as i32 == height
{
context.get_image()
} else {
None
};
if let Some(context) = context.gl_context.as_mut() {
let mut attr = canvas_core::context_attributes::ContextAttributes::new(
alpha,
Expand All @@ -454,6 +462,9 @@ pub extern "system" fn nativeUpdate2DSurface(
context.set_window_surface(&mut attr, width, height, handle);
context.make_current();
}
if let Some(pixels) = pixels {
context.draw_pixels(&pixels);
}

if let Some(vulkan_context) = context.vulkan_context.as_mut() {
vulkan_context.set_view(
Expand Down
Loading
Loading