Skip to content

Commit 4dca8f7

Browse files
NeroBurnericculus
authored andcommitted
SDL2 thread proxying fixes
This PR uses new APIs added in [emscripten-core/emscripten#9336](emscripten-core/emscripten#9336) to improve compatibility with USE_PTHREADS=1. Original PR: emscripten-ports/SDL2#127 By: @jakogut Reviewed by: Daft-Freak
1 parent 1c7bf47 commit 4dca8f7

2 files changed

Lines changed: 65 additions & 26 deletions

File tree

src/video/emscripten/SDL_emscriptenframebuffer.c

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

29+
#include <emscripten/threading.h>
30+
2931

3032
int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
3133
{
@@ -57,18 +59,8 @@ int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * form
5759
return 0;
5860
}
5961

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

160175
if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
161176
/* give back control to browser for screen refresh */

src/video/emscripten/SDL_emscriptenmouse.c

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <emscripten/emscripten.h>
2626
#include <emscripten/html5.h>
27+
#include <emscripten/threading.h>
2728

2829
#include "SDL_emscriptenmouse.h"
2930
#include "SDL_emscriptenvideo.h"
@@ -62,19 +63,9 @@ Emscripten_CreateDefaultCursor()
6263
return Emscripten_CreateCursorFromString("default", SDL_FALSE);
6364
}
6465

65-
static SDL_Cursor*
66-
Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
66+
static const char *Emscripten_GetCursorUrl(int w, int h, int hot_x, int hot_y, int pixels)
6767
{
68-
const char *cursor_url = NULL;
69-
SDL_Surface *conv_surf;
70-
71-
conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0);
72-
73-
if (!conv_surf) {
74-
return NULL;
75-
}
76-
77-
cursor_url = (const char *)EM_ASM_INT({
68+
return (const char *)EM_ASM_INT({
7869
var w = $0;
7970
var h = $1;
8071
var hot_x = $2;
@@ -122,7 +113,40 @@ Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
122113
stringToUTF8(url, urlBuf, url.length + 1);
123114

124115
return urlBuf;
125-
}, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels);
116+
}, w, h, hot_x, hot_y, pixels);
117+
}
118+
119+
static SDL_Cursor*
120+
Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
121+
{
122+
const char *cursor_url = NULL;
123+
SDL_Surface *conv_surf;
124+
125+
conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0);
126+
127+
if (!conv_surf) {
128+
return NULL;
129+
}
130+
131+
if (emscripten_is_main_runtime_thread()) {
132+
cursor_url = Emscripten_GetCursorUrl(
133+
surface->w,
134+
surface->h,
135+
hot_x,
136+
hot_y,
137+
conv_surf->pixels
138+
);
139+
} else {
140+
cursor_url = emscripten_sync_run_in_main_runtime_thread(
141+
EM_FUNC_SIG_IIIIIII,
142+
Emscripten_GetCursorUrl,
143+
surface->w,
144+
surface->h,
145+
hot_x,
146+
hot_y,
147+
conv_surf->pixels
148+
);
149+
}
126150

127151
SDL_FreeSurface(conv_surf);
128152

@@ -206,7 +230,7 @@ Emscripten_ShowCursor(SDL_Cursor* cursor)
206230
curdata = (Emscripten_CursorData *) cursor->driverdata;
207231

208232
if(curdata->system_cursor) {
209-
EM_ASM_INT({
233+
MAIN_THREAD_EM_ASM_INT({
210234
if (Module['canvas']) {
211235
Module['canvas'].style['cursor'] = UTF8ToString($0);
212236
}
@@ -215,7 +239,7 @@ Emscripten_ShowCursor(SDL_Cursor* cursor)
215239
}
216240
}
217241
else {
218-
EM_ASM(
242+
MAIN_THREAD_EM_ASM_INT(
219243
if (Module['canvas']) {
220244
Module['canvas'].style['cursor'] = 'none';
221245
}

0 commit comments

Comments
 (0)