Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

Commit 5bdbd8a

Browse files
committed
emscripten: framebuffer: proxy canvas access to main thread
1 parent d86e506 commit 5bdbd8a

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

src/video/emscripten/SDL_emscriptenframebuffer.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "SDL_emscriptenvideo.h"
2626
#include "SDL_emscriptenframebuffer.h"
2727

28+
#include <emscripten/threading.h>
29+
2830

2931
int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
3032
{
@@ -56,18 +58,8 @@ int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * form
5658
return 0;
5759
}
5860

59-
int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
61+
static void Emscripten_UpdateWindowFramebufferWorker(SDL_Surface *surface)
6062
{
61-
SDL_Surface *surface;
62-
63-
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
64-
surface = data->surface;
65-
if (!surface) {
66-
return SDL_SetError("Couldn't find framebuffer surface for window");
67-
}
68-
69-
/* Send the data to the display */
70-
7163
EM_ASM_INT({
7264
var w = $0;
7365
var h = $1;
@@ -154,6 +146,29 @@ int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rec
154146
SDL2.ctx.putImageData(SDL2.image, 0, 0);
155147
return 0;
156148
}, surface->w, surface->h, surface->pixels);
149+
}
150+
151+
int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
152+
{
153+
SDL_Surface *surface;
154+
155+
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
156+
surface = data->surface;
157+
if (!surface) {
158+
return SDL_SetError("Couldn't find framebuffer surface for window");
159+
}
160+
161+
/* Send the data to the display */
162+
163+
if (emscripten_is_main_runtime_thread()) {
164+
Emscripten_UpdateWindowFramebufferWorker(surface);
165+
} else {
166+
emscripten_sync_run_in_main_runtime_thread(
167+
EM_FUNC_SIG_VI,
168+
Emscripten_UpdateWindowFramebufferWorker,
169+
(uint32_t)surface
170+
);
171+
}
157172

158173
/*if (SDL_getenv("SDL_VIDEO_Emscripten_SAVE_FRAMES")) {
159174
static int frame_number = 0;

0 commit comments

Comments
 (0)