Atlas - SDL_emscriptenframebuffer.c

Home / ext / SDL / src / video / emscripten Lines: 1 | Size: 4094 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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#ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN 24 25#include "SDL_emscriptenvideo.h" 26#include "SDL_emscriptenframebuffer.h" 27 28#include <emscripten/threading.h> 29 30bool Emscripten_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch) 31{ 32 SDL_Surface *surface; 33 const SDL_PixelFormat surface_format = SDL_PIXELFORMAT_RGBA32; 34 int w, h; 35 36 // Free the old framebuffer surface 37 SDL_WindowData *data = window->internal; 38 surface = data->surface; 39 SDL_DestroySurface(surface); 40 41 // Create a new one 42 SDL_GetWindowSizeInPixels(window, &w, &h); 43 44 surface = SDL_CreateSurface(w, h, surface_format); 45 if (!surface) { 46 return false; 47 } 48 49 // Save the info and return! 50 data->surface = surface; 51 *format = surface_format; 52 *pixels = surface->pixels; 53 *pitch = surface->pitch; 54 return true; 55} 56 57bool Emscripten_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects) 58{ 59 SDL_Surface *surface; 60 61 SDL_WindowData *data = window->internal; 62 surface = data->surface; 63 if (!surface) { 64 return SDL_SetError("Couldn't find framebuffer surface for window"); 65 } 66 67 // Send the data to the display 68 69 /* *INDENT-OFF* */ // clang-format off 70 int updated = MAIN_THREAD_EM_ASM_INT({ 71 var w = $0; 72 var h = $1; 73 var pixels = $2; 74 var canvasId = UTF8ToString($3); 75 var canvas = document.querySelector(canvasId); 76 77 //TODO: this should store a context per canvas 78 if (!Module['SDL3']) Module['SDL3'] = {}; 79 var SDL3 = Module['SDL3']; 80 if (SDL3.ctxCanvas !== canvas) { 81 SDL3.ctx = Browser.createContext(canvas, false, true); 82 if (!SDL3.ctx) { 83 return false; 84 } 85 SDL3.ctxCanvas = canvas; 86 } 87 if (SDL3.w !== w || SDL3.h !== h || SDL3.imageCtx !== SDL3.ctx) { 88 SDL3.image = SDL3.ctx.createImageData(w, h); 89 SDL3.w = w; 90 SDL3.h = h; 91 SDL3.imageCtx = SDL3.ctx; 92 } 93 var data = SDL3.image.data; 94 var src = pixels / 4; 95 96 if (SDL3.data32Data !== data) { 97 SDL3.data32 = new Int32Array(data.buffer); 98 SDL3.data32Data = data; 99 } 100 var data32 = SDL3.data32; 101 data32.set(HEAP32.subarray(src, src + data32.length)); 102 103 SDL3.ctx.putImageData(SDL3.image, 0, 0); 104 return true; 105 }, surface->w, surface->h, surface->pixels, data->canvas_id); 106 /* *INDENT-ON* */ // clang-format on 107 108 if (!updated) { 109 return SDL_SetError("Couldn't create context for canvas update"); 110 } 111 112 if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, true)) { 113 // give back control to browser for screen refresh 114 emscripten_sleep(0); 115 } 116 117 return true; 118} 119 120void Emscripten_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window) 121{ 122 SDL_WindowData *data = window->internal; 123 124 SDL_DestroySurface(data->surface); 125 data->surface = NULL; 126} 127 128#endif // SDL_VIDEO_DRIVER_EMSCRIPTEN 129
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.