Skip to content

Commit

Permalink
Re-upload webgl texture data if context changes.
Browse files Browse the repository at this point in the history
The editor disposes the canvas when going back to the file browser. This causes a new canvas to be created with a new WebGL context. We were caching images and their gl texture handles, which could belong to a previously created context. We now re-upload the texture data if we see the webgl context has changed.

This fixes the issue where the wrong image would be used for the grid pattern behind a transparent artboard.

Diffs=
7777300368 Re-upload webgl texture data if context changes. (#8655)

Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
  • Loading branch information
luigi-rosso and luigi-rosso committed Nov 27, 2024
1 parent b0b8382 commit a76c5aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7986d64d8371531716ea3f038dcbec5da187e6cd
7777300368cc4513c398054fe4df8b01188aec4b
22 changes: 21 additions & 1 deletion wasm/src/bindings_webgl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ EM_JS(void, upload_image, (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE gl, uintptr_t renderI
}
gl = GL.getContext(gl).GLctx;
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
});

EM_JS(void, delete_image, (uintptr_t renderImage), {
var images = Module["images"];
if (!images)
{
return;
}

var image = images.get(renderImage);
if (!image)
{
return;
}
images.delete(renderImage);
});

Expand All @@ -147,6 +161,7 @@ class WebGL2RenderImage : public LITE_RTTI_OVERRIDE(RenderImage, WebGL2RenderIma
~WebGL2RenderImage()
{
ScopedGLContextMakeCurrent makeCurrent(m_contextGL);
delete_image(reinterpret_cast<uintptr_t>(this));
m_renderImage.reset();
}

Expand Down Expand Up @@ -399,9 +414,14 @@ class WebGL2Renderer : public RiveRenderer
RenderImage* WebGL2RenderImage::prep(WebGL2Renderer* webglRenderer,
const EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context)
{
// Only return the existing render image if its from the same context,
// otherwise we need to re-upload.
if (context == m_contextGL && m_renderImage)
{
return m_renderImage.get();
}
if (m_readyToUpload)
{
m_readyToUpload = false;
ScopedGLContextMakeCurrent makeCurrent(m_contextGL = context);
GLuint textureId = 0;
glGenTextures(1, &textureId);
Expand Down

0 comments on commit a76c5aa

Please sign in to comment.