1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2021 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 #if SDL_VIDEO_DRIVER_EMSCRIPTEN
24 
25 #include "SDL_emscriptenvideo.h"
26 #include "SDL_emscriptenframebuffer.h"
27 #include "SDL_hints.h"
28 
29 
Emscripten_CreateWindowFramebuffer(_THIS,SDL_Window * window,Uint32 * format,void ** pixels,int * pitch)30 int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
31 {
32     SDL_Surface *surface;
33     const Uint32 surface_format = SDL_PIXELFORMAT_BGR888;
34     int w, h;
35     int bpp;
36     Uint32 Rmask, Gmask, Bmask, Amask;
37 
38     /* Free the old framebuffer surface */
39     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
40     surface = data->surface;
41     SDL_FreeSurface(surface);
42 
43     /* Create a new one */
44     SDL_PixelFormatEnumToMasks(surface_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
45     SDL_GetWindowSize(window, &w, &h);
46 
47     surface = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
48     if (!surface) {
49         return -1;
50     }
51 
52     /* Save the info and return! */
53     data->surface = surface;
54     *format = surface_format;
55     *pixels = surface->pixels;
56     *pitch = surface->pitch;
57     return 0;
58 }
59 
Emscripten_UpdateWindowFramebuffer(_THIS,SDL_Window * window,const SDL_Rect * rects,int numrects)60 int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
61 {
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 
72     EM_ASM_INT({
73         var w = $0;
74         var h = $1;
75         var pixels = $2;
76 
77         if (!Module['SDL2']) Module['SDL2'] = {};
78         var SDL2 = Module['SDL2'];
79         if (SDL2.ctxCanvas !== Module['canvas']) {
80             SDL2.ctx = Module['createContext'](Module['canvas'], false, true);
81             SDL2.ctxCanvas = Module['canvas'];
82         }
83         if (SDL2.w !== w || SDL2.h !== h || SDL2.imageCtx !== SDL2.ctx) {
84             SDL2.image = SDL2.ctx.createImageData(w, h);
85             SDL2.w = w;
86             SDL2.h = h;
87             SDL2.imageCtx = SDL2.ctx;
88         }
89         var data = SDL2.image.data;
90         var src = pixels >> 2;
91         var dst = 0;
92         var num;
93         if (typeof CanvasPixelArray !== 'undefined' && data instanceof CanvasPixelArray) {
94             // IE10/IE11: ImageData objects are backed by the deprecated CanvasPixelArray,
95             // not UInt8ClampedArray. These don't have buffers, so we need to revert
96             // to copying a byte at a time. We do the undefined check because modern
97             // browsers do not define CanvasPixelArray anymore.
98             num = data.length;
99             while (dst < num) {
100                 var val = HEAP32[src]; // This is optimized. Instead, we could do {{{ makeGetValue('buffer', 'dst', 'i32') }}};
101                 data[dst  ] = val & 0xff;
102                 data[dst+1] = (val >> 8) & 0xff;
103                 data[dst+2] = (val >> 16) & 0xff;
104                 data[dst+3] = 0xff;
105                 src++;
106                 dst += 4;
107             }
108         } else {
109             if (SDL2.data32Data !== data) {
110                 SDL2.data32 = new Int32Array(data.buffer);
111                 SDL2.data8 = new Uint8Array(data.buffer);
112                 SDL2.data32Data = data;
113             }
114             var data32 = SDL2.data32;
115             num = data32.length;
116             // logically we need to do
117             //      while (dst < num) {
118             //          data32[dst++] = HEAP32[src++] | 0xff000000
119             //      }
120             // the following code is faster though, because
121             // .set() is almost free - easily 10x faster due to
122             // native SDL_memcpy efficiencies, and the remaining loop
123             // just stores, not load + store, so it is faster
124             data32.set(HEAP32.subarray(src, src + num));
125             var data8 = SDL2.data8;
126             var i = 3;
127             var j = i + 4*num;
128             if (num % 8 == 0) {
129                 // unrolling gives big speedups
130                 while (i < j) {
131                   data8[i] = 0xff;
132                   i = i + 4 | 0;
133                   data8[i] = 0xff;
134                   i = i + 4 | 0;
135                   data8[i] = 0xff;
136                   i = i + 4 | 0;
137                   data8[i] = 0xff;
138                   i = i + 4 | 0;
139                   data8[i] = 0xff;
140                   i = i + 4 | 0;
141                   data8[i] = 0xff;
142                   i = i + 4 | 0;
143                   data8[i] = 0xff;
144                   i = i + 4 | 0;
145                   data8[i] = 0xff;
146                   i = i + 4 | 0;
147                 }
148              } else {
149                 while (i < j) {
150                   data8[i] = 0xff;
151                   i = i + 4 | 0;
152                 }
153             }
154         }
155 
156         SDL2.ctx.putImageData(SDL2.image, 0, 0);
157         return 0;
158     }, surface->w, surface->h, surface->pixels);
159 
160     if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
161         /* give back control to browser for screen refresh */
162         emscripten_sleep(0);
163     }
164 
165     return 0;
166 }
167 
Emscripten_DestroyWindowFramebuffer(_THIS,SDL_Window * window)168 void Emscripten_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
169 {
170     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
171 
172     SDL_FreeSurface(data->surface);
173     data->surface = NULL;
174 }
175 
176 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
177 
178 /* vi: set ts=4 sw=4 expandtab: */
179