SDL  2.0
SDL_emscriptenframebuffer.c
Go to the documentation of this file.
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"
27 #include "SDL_hints.h"
28 
29 
31 {
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;
42 
43  /* Create a new one */
44  SDL_PixelFormatEnumToMasks(surface_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
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 
61 {
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 
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: */
#define _THIS
#define SDL_SetError
#define SDL_GetWindowSize
#define SDL_PixelFormatEnumToMasks
#define SDL_GetHintBoolean
#define SDL_CreateRGBSurface
#define SDL_FreeSurface
void Emscripten_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
#define SDL_HINT_EMSCRIPTEN_ASYNCIFY
Disable giving back control to the browser automatically when running with asyncify.
Definition: SDL_hints.h:1125
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
GLuint GLfloat * val
GLenum src
GLenum GLenum dst
GLuint num
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
@ SDL_PIXELFORMAT_BGR888
Definition: SDL_pixels.h:253
@ SDL_TRUE
Definition: SDL_stdinc.h:170
uint32_t Uint32
Definition: SDL_stdinc.h:209
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:163
EGLSurface surface
Definition: eglext.h:248
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
EGLSurface EGLint * rects
Definition: eglext.h:282
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_Surface * surface
The type used to identify a window.
Definition: SDL_sysvideo.h:75