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

HiDPI framebuffer fixes #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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