Skip to content

Commit 03dabf9

Browse files
committed
FemtoVG: Fix panic when used with WebGL
In earlier refactorings, passing through the id of the HTML canvas element got "lost". Restore this by handling it in the place where we also have access to the element. This restores rendering as part #9345 , but the size of the canvas is still wrong. But if resized manually you can see rendering now :) cc #9345
1 parent 2faae11 commit 03dabf9

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

internal/renderers/femtovg/lib.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ pub struct FemtoVGRenderer<B: GraphicsBackend> {
8686
rendering_first_time: Cell<bool>,
8787
// Last field, so that it's dropped last and for example the OpenGL context exists and is current when destroying the FemtoVG canvas
8888
graphics_backend: B,
89-
#[cfg(target_arch = "wasm32")]
90-
canvas_id: RefCell<String>,
9189
}
9290

9391
impl<B: GraphicsBackend> FemtoVGRenderer<B> {
@@ -260,29 +258,13 @@ impl<B: GraphicsBackend> FemtoVGRenderer<B> {
260258
Ok(())
261259
}
262260

263-
#[cfg(not(target_arch = "wasm32"))]
264261
fn with_graphics_api(
265262
&self,
266263
callback: impl FnOnce(i_slint_core::api::GraphicsAPI<'_>),
267264
) -> Result<(), PlatformError> {
268265
self.graphics_backend.with_graphics_api(|api| callback(api.unwrap()))
269266
}
270267

271-
#[cfg(target_arch = "wasm32")]
272-
fn with_graphics_api(
273-
&self,
274-
callback: impl FnOnce(i_slint_core::api::GraphicsAPI<'_>),
275-
) -> Result<(), PlatformError> {
276-
use i_slint_core::api::GraphicsAPI;
277-
278-
let canvas_id = self.canvas_id.borrow();
279-
280-
let api =
281-
GraphicsAPI::WebGL { canvas_element_id: canvas_id.as_str(), context_type: "webgl2" };
282-
callback(api);
283-
Ok(())
284-
}
285-
286268
fn window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, PlatformError> {
287269
self.maybe_window_adapter.borrow().as_ref().and_then(|w| w.upgrade()).ok_or_else(|| {
288270
"Renderer must be associated with component before use".to_string().into()
@@ -542,8 +524,6 @@ impl<B: GraphicsBackend> FemtoVGRendererExt for FemtoVGRenderer<B> {
542524
rendering_metrics_collector: Default::default(),
543525
rendering_first_time: Cell::new(true),
544526
graphics_backend: B::new_suspended(),
545-
#[cfg(target_arch = "wasm32")]
546-
canvas_id: Default::default(),
547527
}
548528
}
549529

internal/renderers/femtovg/opengl.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ unsafe impl OpenGLInterface for SuspendedRenderer {
8989

9090
pub struct OpenGLBackend {
9191
opengl_context: RefCell<Box<dyn OpenGLInterface>>,
92+
#[cfg(target_family = "wasm")]
93+
html_canvas: RefCell<Option<web_sys::HtmlCanvasElement>>,
9294
}
9395

9496
impl OpenGLBackend {
@@ -140,6 +142,10 @@ impl OpenGLBackend {
140142
.unwrap();
141143

142144
*self.opengl_context.borrow_mut() = opengl_context;
145+
#[cfg(target_family = "wasm")]
146+
{
147+
*self.html_canvas.borrow_mut() = Some(html_canvas);
148+
}
143149

144150
let canvas = Rc::new(RefCell::new(femtovg_canvas));
145151
renderer.reset_canvas(canvas);
@@ -161,7 +167,11 @@ impl GraphicsBackend for OpenGLBackend {
161167
const NAME: &'static str = "OpenGL";
162168

163169
fn new_suspended() -> Self {
164-
Self { opengl_context: RefCell::new(Box::new(SuspendedRenderer {})) }
170+
Self {
171+
opengl_context: RefCell::new(Box::new(SuspendedRenderer {})),
172+
#[cfg(target_family = "wasm")]
173+
html_canvas: RefCell::new(None),
174+
}
165175
}
166176

167177
fn clear_graphics_context(&self) {
@@ -188,6 +198,7 @@ impl GraphicsBackend for OpenGLBackend {
188198
self.opengl_context.borrow().swap_buffers()
189199
}
190200

201+
#[cfg(not(target_family = "wasm"))]
191202
fn with_graphics_api<R>(
192203
&self,
193204
callback: impl FnOnce(Option<i_slint_core::api::GraphicsAPI<'_>>) -> R,
@@ -201,6 +212,20 @@ impl GraphicsBackend for OpenGLBackend {
201212
Ok(callback(Some(api)))
202213
}
203214

215+
#[cfg(target_family = "wasm")]
216+
fn with_graphics_api<R>(
217+
&self,
218+
callback: impl FnOnce(Option<i_slint_core::api::GraphicsAPI<'_>>) -> R,
219+
) -> Result<R, i_slint_core::platform::PlatformError> {
220+
use i_slint_core::api::GraphicsAPI;
221+
222+
let id =
223+
self.html_canvas.borrow().as_ref().map_or_else(|| String::new(), |canvas| canvas.id());
224+
225+
let api = GraphicsAPI::WebGL { canvas_element_id: &id, context_type: "webgl2" };
226+
Ok(callback(Some(api)))
227+
}
228+
204229
/// This function is called by the renderers when the surface needs to be resized, typically
205230
/// in response to the windowing system notifying of a change in the window system.
206231
/// For most implementations this is a no-op, with the exception for wayland for example.

0 commit comments

Comments
 (0)