summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenframebuffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenframebuffer.c')
-rw-r--r--contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenframebuffer.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenframebuffer.c b/contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenframebuffer.c
new file mode 100644
index 0000000..503fac6
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/emscripten/SDL_emscriptenframebuffer.c
@@ -0,0 +1,161 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN
24
25#include "SDL_emscriptenvideo.h"
26#include "SDL_emscriptenframebuffer.h"
27
28#include <emscripten/threading.h>
29
30bool Emscripten_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
31{
32 SDL_Surface *surface;
33 const SDL_PixelFormat surface_format = SDL_PIXELFORMAT_XBGR8888;
34 int w, h;
35
36 // Free the old framebuffer surface
37 SDL_WindowData *data = window->internal;
38 surface = data->surface;
39 SDL_DestroySurface(surface);
40
41 // Create a new one
42 SDL_GetWindowSizeInPixels(window, &w, &h);
43
44 surface = SDL_CreateSurface(w, h, surface_format);
45 if (!surface) {
46 return false;
47 }
48
49 // Save the info and return!
50 data->surface = surface;
51 *format = surface_format;
52 *pixels = surface->pixels;
53 *pitch = surface->pitch;
54 return true;
55}
56
57bool Emscripten_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
58{
59 SDL_Surface *surface;
60
61 SDL_WindowData *data = window->internal;
62 surface = data->surface;
63 if (!surface) {
64 return SDL_SetError("Couldn't find framebuffer surface for window");
65 }
66
67 // Send the data to the display
68
69 /* *INDENT-OFF* */ // clang-format off
70 MAIN_THREAD_EM_ASM({
71 var w = $0;
72 var h = $1;
73 var pixels = $2;
74 var canvasId = UTF8ToString($3);
75 var canvas = document.querySelector(canvasId);
76
77 //TODO: this should store a context per canvas
78 if (!Module['SDL3']) Module['SDL3'] = {};
79 var SDL3 = Module['SDL3'];
80 if (SDL3.ctxCanvas !== canvas) {
81 SDL3.ctx = Module['createContext'](canvas, false, true);
82 SDL3.ctxCanvas = canvas;
83 }
84 if (SDL3.w !== w || SDL3.h !== h || SDL3.imageCtx !== SDL3.ctx) {
85 SDL3.image = SDL3.ctx.createImageData(w, h);
86 SDL3.w = w;
87 SDL3.h = h;
88 SDL3.imageCtx = SDL3.ctx;
89 }
90 var data = SDL3.image.data;
91 var src = pixels / 4;
92 var dst = 0;
93 var num;
94
95 if (SDL3.data32Data !== data) {
96 SDL3.data32 = new Int32Array(data.buffer);
97 SDL3.data8 = new Uint8Array(data.buffer);
98 SDL3.data32Data = data;
99 }
100 var data32 = SDL3.data32;
101 num = data32.length;
102 // logically we need to do
103 // while (dst < num) {
104 // data32[dst++] = HEAP32[src++] | 0xff000000
105 // }
106 // the following code is faster though, because
107 // .set() is almost free - easily 10x faster due to
108 // native SDL_memcpy efficiencies, and the remaining loop
109 // just stores, not load + store, so it is faster
110 data32.set(HEAP32.subarray(src, src + num));
111 var data8 = SDL3.data8;
112 var i = 3;
113 var j = i + 4*num;
114 if (num % 8 == 0) {
115 // unrolling gives big speedups
116 while (i < j) {
117 data8[i] = 0xff;
118 i = i + 4 | 0;
119 data8[i] = 0xff;
120 i = i + 4 | 0;
121 data8[i] = 0xff;
122 i = i + 4 | 0;
123 data8[i] = 0xff;
124 i = i + 4 | 0;
125 data8[i] = 0xff;
126 i = i + 4 | 0;
127 data8[i] = 0xff;
128 i = i + 4 | 0;
129 data8[i] = 0xff;
130 i = i + 4 | 0;
131 data8[i] = 0xff;
132 i = i + 4 | 0;
133 }
134 } else {
135 while (i < j) {
136 data8[i] = 0xff;
137 i = i + 4 | 0;
138 }
139 }
140
141 SDL3.ctx.putImageData(SDL3.image, 0, 0);
142 }, surface->w, surface->h, surface->pixels, data->canvas_id);
143 /* *INDENT-ON* */ // clang-format on
144
145 if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, true)) {
146 // give back control to browser for screen refresh
147 emscripten_sleep(0);
148 }
149
150 return true;
151}
152
153void Emscripten_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
154{
155 SDL_WindowData *data = window->internal;
156
157 SDL_DestroySurface(data->surface);
158 data->surface = NULL;
159}
160
161#endif // SDL_VIDEO_DRIVER_EMSCRIPTEN