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

[WIP] SDL2 thread proxying fixes #127

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 26 additions & 11 deletions src/video/emscripten/SDL_emscriptenframebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "SDL_emscriptenframebuffer.h"
#include "SDL_hints.h"

#include <emscripten/threading.h>


int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
{
Expand Down Expand Up @@ -57,18 +59,8 @@ int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * form
return 0;
}

int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
static void Emscripten_UpdateWindowFramebufferWorker(SDL_Surface *surface)
{
SDL_Surface *surface;

SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
surface = data->surface;
if (!surface) {
return SDL_SetError("Couldn't find framebuffer surface for window");
}

/* Send the data to the display */

EM_ASM_INT({
var w = $0;
var h = $1;
Expand Down Expand Up @@ -155,6 +147,29 @@ int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rec
SDL2.ctx.putImageData(SDL2.image, 0, 0);
return 0;
}, surface->w, surface->h, surface->pixels);
}

int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
{
SDL_Surface *surface;

SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
surface = data->surface;
if (!surface) {
return SDL_SetError("Couldn't find framebuffer surface for window");
}

/* Send the data to the display */

if (emscripten_is_main_runtime_thread()) {
Emscripten_UpdateWindowFramebufferWorker(surface);
} else {
emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_VI,
Emscripten_UpdateWindowFramebufferWorker,
(uint32_t)surface
);
}

/*if (SDL_getenv("SDL_VIDEO_Emscripten_SAVE_FRAMES")) {
static int frame_number = 0;
Expand Down
54 changes: 39 additions & 15 deletions src/video/emscripten/SDL_emscriptenmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <emscripten/threading.h>

#include "SDL_emscriptenmouse.h"
#include "SDL_emscriptenvideo.h"
Expand Down Expand Up @@ -65,19 +66,9 @@ Emscripten_CreateDefaultCursor()
return Emscripten_CreateCursorFromString("default", SDL_FALSE);
}

static SDL_Cursor*
Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
static const char *Emscripten_GetCursorUrl(int w, int h, int hot_x, int hot_y, int pixels)
{
const char *cursor_url = NULL;
SDL_Surface *conv_surf;

conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0);

if (!conv_surf) {
return NULL;
}

cursor_url = (const char *)EM_ASM_INT({
return (const char *)EM_ASM_INT({
var w = $0;
var h = $1;
var hot_x = $2;
Expand Down Expand Up @@ -125,7 +116,40 @@ Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
stringToUTF8(url, urlBuf, url.length + 1);

return urlBuf;
}, surface->w, surface->h, hot_x, hot_y, conv_surf->pixels);
}, w, h, hot_x, hot_y, pixels);
}

static SDL_Cursor*
Emscripten_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
{
const char *cursor_url = NULL;
SDL_Surface *conv_surf;

conv_surf = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR8888, 0);

if (!conv_surf) {
return NULL;
}

if (emscripten_is_main_runtime_thread()) {
cursor_url = Emscripten_GetCursorUrl(
surface->w,
surface->h,
hot_x,
hot_y,
conv_surf->pixels
);
} else {
cursor_url = emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_IIIIIII,
Emscripten_GetCursorUrl,
surface->w,
surface->h,
hot_x,
hot_y,
conv_surf->pixels
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly annoying that we have to proxy this as it just wants to create an image from some data... But I don't think it can be avoided.


SDL_FreeSurface(conv_surf);

Expand Down Expand Up @@ -209,7 +233,7 @@ Emscripten_ShowCursor(SDL_Cursor* cursor)
curdata = (Emscripten_CursorData *) cursor->driverdata;

if(curdata->system_cursor) {
EM_ASM_INT({
MAIN_THREAD_EM_ASM_INT({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch this to MAIN_THREAD_EM_ASM and drop the return. (I think EM_ASM was less flexible when this was written.)

if (Module['canvas']) {
Module['canvas'].style['cursor'] = UTF8ToString($0);
}
Expand All @@ -218,7 +242,7 @@ Emscripten_ShowCursor(SDL_Cursor* cursor)
}
}
else {
EM_ASM(
MAIN_THREAD_EM_ASM(
if (Module['canvas']) {
Module['canvas'].style['cursor'] = 'none';
}
Expand Down
16 changes: 2 additions & 14 deletions src/video/emscripten/SDL_emscriptenvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,7 @@ Emscripten_VideoInit(_THIS)

/* Use a fake 32-bpp desktop mode */
mode.format = SDL_PIXELFORMAT_RGB888;

mode.w = EM_ASM_INT_V({
return screen.width;
});

mode.h = EM_ASM_INT_V({
return screen.height;
});
emscripten_get_screen_size(&mode.w, &mode.h);

mode.refresh_rate = 0;
mode.driverdata = NULL;
Expand Down Expand Up @@ -365,12 +358,7 @@ Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * di

static void
Emscripten_SetWindowTitle(_THIS, SDL_Window * window) {
EM_ASM_INT({
if (typeof setWindowTitle !== 'undefined') {
setWindowTitle(UTF8ToString($0));
}
return 0;
}, window->title);
emscripten_set_window_title(window->title);
}

#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
Expand Down