Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
emscripten: Scale framebuffer if HiDPI is enabled
Browse files Browse the repository at this point in the history
HiDPI doesn't affect the framebuffer surface size, so we can't just copy
it to the canvas.
  • Loading branch information
Daft-Freak committed Aug 14, 2021
1 parent f9a06a3 commit 0989d88
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/video/emscripten/SDL_emscriptenframebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,35 @@ int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rec
var w = $0;
var h = $1;
var pixels = $2;
var pixel_ratio = $3;

if (!Module['SDL2']) Module['SDL2'] = {};
var SDL2 = Module['SDL2'];
if (SDL2.ctxCanvas !== Module['canvas']) {
SDL2.ctx = Module['createContext'](Module['canvas'], false, true);
SDL2.ctxCanvas = Module['canvas'];
}
if (SDL2.w !== w || SDL2.h !== h || SDL2.imageCtx !== SDL2.ctx) {
SDL2.image = SDL2.ctx.createImageData(w, h);

// For HiDPI, create a second canvas/context to scale up
if (pixel_ratio !== 1.0) {
if (!SDL2.scaleCtx) {
SDL2.scaleCtxCanvas = document.createElement('canvas');
SDL2.scaleCtx = SDL2.scaleCtxCanvas.getContext('2d');
}
}

var imageCtx = pixel_ratio == 1.0 ? SDL2.ctx : SDL2.scaleCtx;

if (SDL2.w !== w || SDL2.h !== h || SDL2.imageCtx !== imageCtx) {
SDL2.image = imageCtx.createImageData(w, h);
SDL2.w = w;
SDL2.h = h;
SDL2.imageCtx = SDL2.ctx;
SDL2.imageCtx = imageCtx;

if(SDL2.scaleCtxCanvas) {
SDL2.scaleCtxCanvas.width = w;
SDL2.scaleCtxCanvas.height = h;
}
}
var data = SDL2.image.data;
var src = pixels >> 2;
Expand Down Expand Up @@ -153,9 +170,16 @@ int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rec
}
}

SDL2.ctx.putImageData(SDL2.image, 0, 0);
imageCtx.putImageData(SDL2.image, 0, 0);

// HiDPI scale, no smoothing to match what the renderer-based fallback does by default
if (pixel_ratio !== 1.0) {
SDL2.ctx.imageSmoothingEnabled = false;
SDL2.ctx.drawImage(SDL2.scaleCtxCanvas, 0, 0, w * pixel_ratio, h * pixel_ratio);
}

return 0;
}, surface->w, surface->h, surface->pixels);
}, surface->w, surface->h, surface->pixels, data->pixel_ratio);

if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
/* give back control to browser for screen refresh */
Expand Down

0 comments on commit 0989d88

Please sign in to comment.