bfdec3b56b39b5aafe185a13b93987b9ef7a91b8
[platform/upstream/SDL.git] / src / video / emscripten / SDL_emscriptenframebuffer.c
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2018 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
28
29 int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
30 {
31     SDL_Surface *surface;
32     const Uint32 surface_format = SDL_PIXELFORMAT_BGR888;
33     int w, h;
34     int bpp;
35     Uint32 Rmask, Gmask, Bmask, Amask;
36
37     /* Free the old framebuffer surface */
38     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
39     surface = data->surface;
40     SDL_FreeSurface(surface);
41
42     /* Create a new one */
43     SDL_PixelFormatEnumToMasks(surface_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
44     SDL_GetWindowSize(window, &w, &h);
45
46     surface = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
47     if (!surface) {
48         return -1;
49     }
50
51     /* Save the info and return! */
52     data->surface = surface;
53     *format = surface_format;
54     *pixels = surface->pixels;
55     *pitch = surface->pitch;
56     return 0;
57 }
58
59 int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
60 {
61     SDL_Surface *surface;
62
63     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
64     surface = data->surface;
65     if (!surface) {
66         return SDL_SetError("Couldn't find framebuffer surface for window");
67     }
68
69     /* Send the data to the display */
70
71     EM_ASM_INT({
72         var w = $0;
73         var h = $1;
74         var pixels = $2;
75
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'];
81         }
82         if (SDL2.w !== w || SDL2.h !== h || SDL2.imageCtx !== SDL2.ctx) {
83             SDL2.image = SDL2.ctx.createImageData(w, h);
84             SDL2.w = w;
85             SDL2.h = h;
86             SDL2.imageCtx = SDL2.ctx;
87         }
88         var data = SDL2.image.data;
89         var src = pixels >> 2;
90         var dst = 0;
91         var num;
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.
97             num = data.length;
98             while (dst < num) {
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;
103                 data[dst+3] = 0xff;
104                 src++;
105                 dst += 4;
106             }
107         } else {
108             if (SDL2.data32Data !== data) {
109                 SDL2.data32 = new Int32Array(data.buffer);
110                 SDL2.data8 = new Uint8Array(data.buffer);
111             }
112             var data32 = SDL2.data32;
113             num = data32.length;
114             // logically we need to do
115             //      while (dst < num) {
116             //          data32[dst++] = HEAP32[src++] | 0xff000000
117             //      }
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;
124             var i = 3;
125             var j = i + 4*num;
126             if (num % 8 == 0) {
127                 // unrolling gives big speedups
128                 while (i < j) {
129                   data8[i] = 0xff;
130                   i = i + 4 | 0;
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                 }
146              } else {
147                 while (i < j) {
148                   data8[i] = 0xff;
149                   i = i + 4 | 0;
150                 }
151             }
152         }
153
154         SDL2.ctx.putImageData(SDL2.image, 0, 0);
155         return 0;
156     }, surface->w, surface->h, surface->pixels);
157
158     /*if (SDL_getenv("SDL_VIDEO_Emscripten_SAVE_FRAMES")) {
159         static int frame_number = 0;
160         char file[128];
161         SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
162                      SDL_GetWindowID(window), ++frame_number);
163         SDL_SaveBMP(surface, file);
164     }*/
165     return 0;
166 }
167
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: */