Atlas - SDL_emscriptenframebuffer.c
Home / ext / SDL / src / video / emscripten Lines: 1 | Size: 4044 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 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 var SDL3 = Module['SDL3']; 79 if (SDL3.ctxCanvas !== canvas) { 80 SDL3.ctx = Browser.createContext(canvas, false, true); 81 if (!SDL3.ctx) { 82 return false; 83 } 84 SDL3.ctxCanvas = canvas; 85 } 86 if (SDL3.w !== w || SDL3.h !== h || SDL3.imageCtx !== SDL3.ctx) { 87 SDL3.image = SDL3.ctx.createImageData(w, h); 88 SDL3.w = w; 89 SDL3.h = h; 90 SDL3.imageCtx = SDL3.ctx; 91 } 92 var data = SDL3.image.data; 93 var src = pixels / 4; 94 95 if (SDL3.data32Data !== data) { 96 SDL3.data32 = new Int32Array(data.buffer); 97 SDL3.data32Data = data; 98 } 99 var data32 = SDL3.data32; 100 data32.set(HEAP32.subarray(src, src + data32.length)); 101 102 SDL3.ctx.putImageData(SDL3.image, 0, 0); 103 return true; 104 }, surface->w, surface->h, surface->pixels, data->canvas_id); 105 /* *INDENT-ON* */ // clang-format on 106 107 if (!updated) { 108 return SDL_SetError("Couldn't create context for canvas update"); 109 } 110 111 if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, true)) { 112 // give back control to browser for screen refresh 113 emscripten_sleep(0); 114 } 115 116 return true; 117} 118 119void Emscripten_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window) 120{ 121 SDL_WindowData *data = window->internal; 122 123 SDL_DestroySurface(data->surface); 124 data->surface = NULL; 125} 126 127#endif // SDL_VIDEO_DRIVER_EMSCRIPTEN 128[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.