SDL_emscriptenframebuffer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_EMSCRIPTEN
  20. #include "SDL_emscriptenvideo.h"
  21. #include "SDL_emscriptenframebuffer.h"
  22. #include "SDL_hints.h"
  23. #include <emscripten/threading.h>
  24. int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
  25. {
  26. SDL_Surface *surface;
  27. const Uint32 surface_format = SDL_PIXELFORMAT_BGR888;
  28. int w, h;
  29. int bpp;
  30. Uint32 Rmask, Gmask, Bmask, Amask;
  31. /* Free the old framebuffer surface */
  32. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  33. surface = data->surface;
  34. SDL_FreeSurface(surface);
  35. /* Create a new one */
  36. SDL_PixelFormatEnumToMasks(surface_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
  37. SDL_GetWindowSize(window, &w, &h);
  38. surface = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
  39. if (!surface) {
  40. return -1;
  41. }
  42. /* Save the info and return! */
  43. data->surface = surface;
  44. *format = surface_format;
  45. *pixels = surface->pixels;
  46. *pitch = surface->pitch;
  47. return 0;
  48. }
  49. static void
  50. Emscripten_UpdateWindowFramebufferWorker(SDL_Surface* surface)
  51. {
  52. EM_ASM_INT({
  53. var w = $0;
  54. var h = $1;
  55. var pixels = $2;
  56. if (!Module['SDL2']) Module['SDL2'] = {};
  57. var SDL2 = Module['SDL2'];
  58. if (SDL2.ctxCanvas !== Module['canvas']) {
  59. SDL2.ctx = Module['createContext'](Module['canvas'], false, true);
  60. SDL2.ctxCanvas = Module['canvas'];
  61. }
  62. if (SDL2.w !== w || SDL2.h !== h || SDL2.imageCtx !== SDL2.ctx) {
  63. SDL2.image = SDL2.ctx.createImageData(w, h);
  64. SDL2.w = w;
  65. SDL2.h = h;
  66. SDL2.imageCtx = SDL2.ctx;
  67. }
  68. var data = SDL2.image.data;
  69. var src = pixels >> 2;
  70. var dst = 0;
  71. var num;
  72. if (typeof CanvasPixelArray !== 'undefined' && data instanceof CanvasPixelArray) {
  73. // IE10/IE11: ImageData objects are backed by the deprecated CanvasPixelArray,
  74. // not UInt8ClampedArray. These don't have buffers, so we need to revert
  75. // to copying a byte at a time. We do the undefined check because modern
  76. // browsers do not define CanvasPixelArray anymore.
  77. num = data.length;
  78. while (dst < num) {
  79. var val = HEAP32[src]; // This is optimized. Instead, we could do {{{ makeGetValue('buffer', 'dst', 'i32') }}};
  80. data[dst ] = val & 0xff;
  81. data[dst+1] = (val >> 8) & 0xff;
  82. data[dst+2] = (val >> 16) & 0xff;
  83. data[dst+3] = 0xff;
  84. src++;
  85. dst += 4;
  86. }
  87. } else {
  88. if (SDL2.data32Data !== data) {
  89. SDL2.data32 = new Int32Array(data.buffer);
  90. SDL2.data8 = new Uint8Array(data.buffer);
  91. SDL2.data32Data = data;
  92. }
  93. var data32 = SDL2.data32;
  94. num = data32.length;
  95. // logically we need to do
  96. // while (dst < num) {
  97. // data32[dst++] = HEAP32[src++] | 0xff000000
  98. // }
  99. // the following code is faster though, because
  100. // .set() is almost free - easily 10x faster due to
  101. // native SDL_memcpy efficiencies, and the remaining loop
  102. // just stores, not load + store, so it is faster
  103. data32.set(HEAP32.subarray(src, src + num));
  104. var data8 = SDL2.data8;
  105. var i = 3;
  106. var j = i + 4*num;
  107. if (num % 8 == 0) {
  108. // unrolling gives big speedups
  109. while (i < j) {
  110. data8[i] = 0xff;
  111. i = i + 4 | 0;
  112. data8[i] = 0xff;
  113. i = i + 4 | 0;
  114. data8[i] = 0xff;
  115. i = i + 4 | 0;
  116. data8[i] = 0xff;
  117. i = i + 4 | 0;
  118. data8[i] = 0xff;
  119. i = i + 4 | 0;
  120. data8[i] = 0xff;
  121. i = i + 4 | 0;
  122. data8[i] = 0xff;
  123. i = i + 4 | 0;
  124. data8[i] = 0xff;
  125. i = i + 4 | 0;
  126. }
  127. } else {
  128. while (i < j) {
  129. data8[i] = 0xff;
  130. i = i + 4 | 0;
  131. }
  132. }
  133. }
  134. SDL2.ctx.putImageData(SDL2.image, 0, 0);
  135. return 0;
  136. }, surface->w, surface->h, surface->pixels);
  137. }
  138. int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
  139. {
  140. SDL_Surface *surface;
  141. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  142. surface = data->surface;
  143. if (!surface) {
  144. return SDL_SetError("Couldn't find framebuffer surface for window");
  145. }
  146. /* Send the data to the display */
  147. if (emscripten_is_main_runtime_thread()) {
  148. Emscripten_UpdateWindowFramebufferWorker(surface);
  149. } else {
  150. emscripten_sync_run_in_main_runtime_thread(
  151. EM_FUNC_SIG_VI,
  152. Emscripten_UpdateWindowFramebufferWorker,
  153. (uint32_t)surface
  154. );
  155. }
  156. if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
  157. /* give back control to browser for screen refresh */
  158. emscripten_sleep(0);
  159. }
  160. return 0;
  161. }
  162. void Emscripten_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
  163. {
  164. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  165. SDL_FreeSurface(data->surface);
  166. data->surface = NULL;
  167. }
  168. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
  169. /* vi: set ts=4 sw=4 expandtab: */