SDL_emscriptenframebuffer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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 <emscripten/threading.h>
  23. int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
  24. {
  25. SDL_Surface *surface;
  26. const Uint32 surface_format = SDL_PIXELFORMAT_BGR888;
  27. int w, h;
  28. /* Free the old framebuffer surface */
  29. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  30. surface = data->surface;
  31. SDL_DestroySurface(surface);
  32. /* Create a new one */
  33. SDL_GetWindowSizeInPixels(window, &w, &h);
  34. surface = SDL_CreateSurface(w, h, surface_format);
  35. if (surface == NULL) {
  36. return -1;
  37. }
  38. /* Save the info and return! */
  39. data->surface = surface;
  40. *format = surface_format;
  41. *pixels = surface->pixels;
  42. *pitch = surface->pitch;
  43. return 0;
  44. }
  45. int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
  46. {
  47. SDL_Surface *surface;
  48. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  49. surface = data->surface;
  50. if (surface == NULL) {
  51. return SDL_SetError("Couldn't find framebuffer surface for window");
  52. }
  53. /* Send the data to the display */
  54. /* *INDENT-OFF* */ /* clang-format off */
  55. MAIN_THREAD_EM_ASM({
  56. var w = $0;
  57. var h = $1;
  58. var pixels = $2;
  59. var canvasId = UTF8ToString($3);
  60. var canvas = document.querySelector(canvasId);
  61. //TODO: this should store a context per canvas
  62. if (!Module['SDL3']) Module['SDL3'] = {};
  63. var SDL3 = Module['SDL3'];
  64. if (SDL3.ctxCanvas !== canvas) {
  65. SDL3.ctx = Module['createContext'](canvas, false, true);
  66. SDL3.ctxCanvas = canvas;
  67. }
  68. if (SDL3.w !== w || SDL3.h !== h || SDL3.imageCtx !== SDL3.ctx) {
  69. SDL3.image = SDL3.ctx.createImageData(w, h);
  70. SDL3.w = w;
  71. SDL3.h = h;
  72. SDL3.imageCtx = SDL3.ctx;
  73. }
  74. var data = SDL3.image.data;
  75. var src = pixels >> 2;
  76. var dst = 0;
  77. var num;
  78. if (SDL3.data32Data !== data) {
  79. SDL3.data32 = new Int32Array(data.buffer);
  80. SDL3.data8 = new Uint8Array(data.buffer);
  81. SDL3.data32Data = data;
  82. }
  83. var data32 = SDL3.data32;
  84. num = data32.length;
  85. // logically we need to do
  86. // while (dst < num) {
  87. // data32[dst++] = HEAP32[src++] | 0xff000000
  88. // }
  89. // the following code is faster though, because
  90. // .set() is almost free - easily 10x faster due to
  91. // native SDL_memcpy efficiencies, and the remaining loop
  92. // just stores, not load + store, so it is faster
  93. data32.set(HEAP32.subarray(src, src + num));
  94. var data8 = SDL3.data8;
  95. var i = 3;
  96. var j = i + 4*num;
  97. if (num % 8 == 0) {
  98. // unrolling gives big speedups
  99. while (i < j) {
  100. data8[i] = 0xff;
  101. i = i + 4 | 0;
  102. data8[i] = 0xff;
  103. i = i + 4 | 0;
  104. data8[i] = 0xff;
  105. i = i + 4 | 0;
  106. data8[i] = 0xff;
  107. i = i + 4 | 0;
  108. data8[i] = 0xff;
  109. i = i + 4 | 0;
  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. }
  117. } else {
  118. while (i < j) {
  119. data8[i] = 0xff;
  120. i = i + 4 | 0;
  121. }
  122. }
  123. SDL3.ctx.putImageData(SDL3.image, 0, 0);
  124. }, surface->w, surface->h, surface->pixels, data->canvas_id);
  125. /* *INDENT-ON* */ /* clang-format on */
  126. if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
  127. /* give back control to browser for screen refresh */
  128. emscripten_sleep(0);
  129. }
  130. return 0;
  131. }
  132. void Emscripten_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
  133. {
  134. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  135. SDL_DestroySurface(data->surface);
  136. data->surface = NULL;
  137. }
  138. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */