2 Simple DirectMedia Layer
3 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_EMSCRIPTEN
25 #include "SDL_emscriptenvideo.h"
26 #include "SDL_emscriptenframebuffer.h"
29 int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
32 const Uint32 surface_format = SDL_PIXELFORMAT_BGR888;
35 Uint32 Rmask, Gmask, Bmask, Amask;
37 /* Free the old framebuffer surface */
38 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
39 surface = data->surface;
40 SDL_FreeSurface(surface);
42 /* Create a new one */
43 SDL_PixelFormatEnumToMasks(surface_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
44 SDL_GetWindowSize(window, &w, &h);
46 surface = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
51 /* Save the info and return! */
52 data->surface = surface;
53 *format = surface_format;
54 *pixels = surface->pixels;
55 *pitch = surface->pitch;
59 int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
63 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
64 surface = data->surface;
66 return SDL_SetError("Couldn't find framebuffer surface for window");
69 /* Send the data to the display */
76 if (!Module['SDL2']) Module['SDL2'] = {};
77 var SDL2 = Module['SDL2'];
78 if (SDL2.ctxCanvas !== Module['canvas']) {
79 SDL2.ctx = Module['createContext'](Module['canvas'], false, true);
80 SDL2.ctxCanvas = Module['canvas'];
82 if (SDL2.w !== w || SDL2.h !== h || SDL2.imageCtx !== SDL2.ctx) {
83 SDL2.image = SDL2.ctx.createImageData(w, h);
86 SDL2.imageCtx = SDL2.ctx;
88 var data = SDL2.image.data;
89 var src = pixels >> 2;
92 if (typeof CanvasPixelArray !== 'undefined' && data instanceof CanvasPixelArray) {
93 // IE10/IE11: ImageData objects are backed by the deprecated CanvasPixelArray,
94 // not UInt8ClampedArray. These don't have buffers, so we need to revert
95 // to copying a byte at a time. We do the undefined check because modern
96 // browsers do not define CanvasPixelArray anymore.
99 var val = HEAP32[src]; // This is optimized. Instead, we could do {{{ makeGetValue('buffer', 'dst', 'i32') }}};
100 data[dst ] = val & 0xff;
101 data[dst+1] = (val >> 8) & 0xff;
102 data[dst+2] = (val >> 16) & 0xff;
108 if (SDL2.data32Data !== data) {
109 SDL2.data32 = new Int32Array(data.buffer);
110 SDL2.data8 = new Uint8Array(data.buffer);
112 var data32 = SDL2.data32;
114 // logically we need to do
115 // while (dst < num) {
116 // data32[dst++] = HEAP32[src++] | 0xff000000
118 // the following code is faster though, because
119 // .set() is almost free - easily 10x faster due to
120 // native memcpy efficiencies, and the remaining loop
121 // just stores, not load + store, so it is faster
122 data32.set(HEAP32.subarray(src, src + num));
123 var data8 = SDL2.data8;
127 // unrolling gives big speedups
154 SDL2.ctx.putImageData(SDL2.image, 0, 0);
156 }, surface->w, surface->h, surface->pixels);
158 /*if (SDL_getenv("SDL_VIDEO_Emscripten_SAVE_FRAMES")) {
159 static int frame_number = 0;
161 SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
162 SDL_GetWindowID(window), ++frame_number);
163 SDL_SaveBMP(surface, file);
168 void Emscripten_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
170 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
172 SDL_FreeSurface(data->surface);
173 data->surface = NULL;
176 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
178 /* vi: set ts=4 sw=4 expandtab: */