2015-06-21 17:33:46 +02:00
|
|
|
/*
|
|
|
|
|
Simple DirectMedia Layer
|
2025-01-01 07:45:41 -08:00
|
|
|
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
|
appreciated but is not required.
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*/
|
2022-11-29 18:34:15 -08:00
|
|
|
#include "SDL_internal.h"
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2023-03-29 21:49:01 +00:00
|
|
|
#ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN
|
2015-06-21 17:33:46 +02:00
|
|
|
|
|
|
|
|
#include "SDL_emscriptenvideo.h"
|
|
|
|
|
#include "SDL_emscriptenframebuffer.h"
|
|
|
|
|
|
2022-02-24 21:09:03 +01:00
|
|
|
#include <emscripten/threading.h>
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
bool Emscripten_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
|
|
|
|
SDL_Surface *surface;
|
2024-07-08 14:59:18 -07:00
|
|
|
const SDL_PixelFormat surface_format = SDL_PIXELFORMAT_XBGR8888;
|
2015-06-21 17:33:46 +02:00
|
|
|
int w, h;
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Free the old framebuffer surface
|
2024-07-16 11:09:18 -07:00
|
|
|
SDL_WindowData *data = window->internal;
|
2015-06-21 17:33:46 +02:00
|
|
|
surface = data->surface;
|
2022-12-27 06:36:39 -08:00
|
|
|
SDL_DestroySurface(surface);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Create a new one
|
2023-01-11 19:45:01 -07:00
|
|
|
SDL_GetWindowSizeInPixels(window, &w, &h);
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2022-12-01 17:04:02 +01:00
|
|
|
surface = SDL_CreateSurface(w, h, surface_format);
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!surface) {
|
2024-08-22 17:33:49 -07:00
|
|
|
return false;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Save the info and return!
|
2015-06-21 17:33:46 +02:00
|
|
|
data->surface = surface;
|
|
|
|
|
*format = surface_format;
|
|
|
|
|
*pixels = surface->pixels;
|
|
|
|
|
*pitch = surface->pitch;
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
bool Emscripten_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2022-06-08 14:20:50 +01:00
|
|
|
SDL_Surface *surface;
|
|
|
|
|
|
2024-07-16 11:09:18 -07:00
|
|
|
SDL_WindowData *data = window->internal;
|
2022-06-08 14:20:50 +01:00
|
|
|
surface = data->surface;
|
2023-11-09 22:29:15 +01:00
|
|
|
if (!surface) {
|
2022-06-08 14:20:50 +01:00
|
|
|
return SDL_SetError("Couldn't find framebuffer surface for window");
|
|
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// Send the data to the display
|
2022-06-08 14:20:50 +01:00
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
/* *INDENT-OFF* */ // clang-format off
|
2022-06-08 14:20:50 +01:00
|
|
|
MAIN_THREAD_EM_ASM({
|
2016-09-13 00:03:43 -07:00
|
|
|
var w = $0;
|
|
|
|
|
var h = $1;
|
|
|
|
|
var pixels = $2;
|
2018-04-08 16:54:29 +01:00
|
|
|
var canvasId = UTF8ToString($3);
|
|
|
|
|
var canvas = document.querySelector(canvasId);
|
2016-09-13 00:03:43 -07:00
|
|
|
|
2018-04-08 16:54:29 +01:00
|
|
|
//TODO: this should store a context per canvas
|
2022-11-21 20:28:58 -08:00
|
|
|
if (!Module['SDL3']) Module['SDL3'] = {};
|
|
|
|
|
var SDL3 = Module['SDL3'];
|
2022-11-23 13:22:23 -05:00
|
|
|
if (SDL3.ctxCanvas !== canvas) {
|
2025-05-06 13:04:37 -07:00
|
|
|
SDL3.ctx = Browser.createContext(canvas, false, true);
|
2022-11-23 13:22:23 -05:00
|
|
|
SDL3.ctxCanvas = canvas;
|
2016-09-13 00:03:43 -07:00
|
|
|
}
|
2022-11-21 20:28:58 -08:00
|
|
|
if (SDL3.w !== w || SDL3.h !== h || SDL3.imageCtx !== SDL3.ctx) {
|
|
|
|
|
SDL3.image = SDL3.ctx.createImageData(w, h);
|
|
|
|
|
SDL3.w = w;
|
|
|
|
|
SDL3.h = h;
|
|
|
|
|
SDL3.imageCtx = SDL3.ctx;
|
2016-09-13 00:03:43 -07:00
|
|
|
}
|
2022-11-21 20:28:58 -08:00
|
|
|
var data = SDL3.image.data;
|
2024-10-08 17:41:18 -04:00
|
|
|
var src = pixels / 4;
|
2015-06-21 17:33:46 +02:00
|
|
|
var dst = 0;
|
|
|
|
|
var num;
|
2022-09-30 16:28:57 +01:00
|
|
|
|
2022-11-23 13:46:27 -05:00
|
|
|
if (SDL3.data32Data !== data) {
|
|
|
|
|
SDL3.data32 = new Int32Array(data.buffer);
|
|
|
|
|
SDL3.data8 = new Uint8Array(data.buffer);
|
|
|
|
|
SDL3.data32Data = data;
|
2022-09-30 16:28:57 +01:00
|
|
|
}
|
2022-12-13 12:18:54 +00:00
|
|
|
var data32 = SDL3.data32;
|
2022-09-30 16:28:57 +01:00
|
|
|
num = data32.length;
|
|
|
|
|
// logically we need to do
|
|
|
|
|
// while (dst < num) {
|
|
|
|
|
// data32[dst++] = HEAP32[src++] | 0xff000000
|
|
|
|
|
// }
|
|
|
|
|
// the following code is faster though, because
|
|
|
|
|
// .set() is almost free - easily 10x faster due to
|
|
|
|
|
// native SDL_memcpy efficiencies, and the remaining loop
|
|
|
|
|
// just stores, not load + store, so it is faster
|
|
|
|
|
data32.set(HEAP32.subarray(src, src + num));
|
2022-11-23 13:46:27 -05:00
|
|
|
var data8 = SDL3.data8;
|
2022-09-30 16:28:57 +01:00
|
|
|
var i = 3;
|
2025-06-18 09:26:51 -07:00
|
|
|
var j = i + 4 * num;
|
2022-09-30 16:28:57 +01:00
|
|
|
if (num % 8 == 0) {
|
|
|
|
|
// unrolling gives big speedups
|
|
|
|
|
while (i < j) {
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
2016-09-13 00:03:43 -07:00
|
|
|
}
|
2022-09-30 16:28:57 +01:00
|
|
|
} else {
|
|
|
|
|
while (i < j) {
|
|
|
|
|
data8[i] = 0xff;
|
|
|
|
|
i = i + 4 | 0;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 20:28:58 -08:00
|
|
|
SDL3.ctx.putImageData(SDL3.image, 0, 0);
|
2018-04-08 16:54:29 +01:00
|
|
|
}, surface->w, surface->h, surface->pixels, data->canvas_id);
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
/* *INDENT-ON* */ // clang-format on
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2024-08-22 09:21:26 -07:00
|
|
|
if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, true)) {
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
// give back control to browser for screen refresh
|
2020-06-27 16:22:50 -04:00
|
|
|
emscripten_sleep(0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 17:33:49 -07:00
|
|
|
return true;
|
2015-06-21 17:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-09 12:55:11 +02:00
|
|
|
void Emscripten_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
|
2015-06-21 17:33:46 +02:00
|
|
|
{
|
2024-07-16 11:09:18 -07:00
|
|
|
SDL_WindowData *data = window->internal;
|
2015-06-21 17:33:46 +02:00
|
|
|
|
2022-12-27 06:36:39 -08:00
|
|
|
SDL_DestroySurface(data->surface);
|
2015-06-21 17:33:46 +02:00
|
|
|
data->surface = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
|
|
|
#endif // SDL_VIDEO_DRIVER_EMSCRIPTEN
|