SDL_emscriptenframebuffer.c (6468B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 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 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 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 } 113 var data32 = SDL2.data32; 114 num = data32.length; 115 // logically we need to do 116 // while (dst < num) { 117 // data32[dst++] = HEAP32[src++] | 0xff000000 118 // } 119 // the following code is faster though, because 120 // .set() is almost free - easily 10x faster due to 121 // native memcpy efficiencies, and the remaining loop 122 // just stores, not load + store, so it is faster 123 data32.set(HEAP32.subarray(src, src + num)); 124 var data8 = SDL2.data8; 125 var i = 3; 126 var j = i + 4*num; 127 if (num % 8 == 0) { 128 // unrolling gives big speedups 129 while (i < j) { 130 data8[i] = 0xff; 131 i = i + 4 | 0; 132 data8[i] = 0xff; 133 i = i + 4 | 0; 134 data8[i] = 0xff; 135 i = i + 4 | 0; 136 data8[i] = 0xff; 137 i = i + 4 | 0; 138 data8[i] = 0xff; 139 i = i + 4 | 0; 140 data8[i] = 0xff; 141 i = i + 4 | 0; 142 data8[i] = 0xff; 143 i = i + 4 | 0; 144 data8[i] = 0xff; 145 i = i + 4 | 0; 146 } 147 } else { 148 while (i < j) { 149 data8[i] = 0xff; 150 i = i + 4 | 0; 151 } 152 } 153 } 154 155 SDL2.ctx.putImageData(SDL2.image, 0, 0); 156 return 0; 157 }, surface->w, surface->h, surface->pixels); 158 159 /*if (SDL_getenv("SDL_VIDEO_Emscripten_SAVE_FRAMES")) { 160 static int frame_number = 0; 161 char file[128]; 162 SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp", 163 SDL_GetWindowID(window), ++frame_number); 164 SDL_SaveBMP(surface, file); 165 }*/ 166 167 if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) { 168 /* give back control to browser for screen refresh */ 169 emscripten_sleep(0); 170 } 171 172 return 0; 173 } 174 175 void Emscripten_DestroyWindowFramebuffer(_THIS, SDL_Window * window) 176 { 177 SDL_WindowData *data = (SDL_WindowData *) window->driverdata; 178 179 SDL_FreeSurface(data->surface); 180 data->surface = NULL; 181 } 182 183 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */ 184 185 /* vi: set ts=4 sw=4 expandtab: */