Merge pull request #64481 from jamie-pate/master

Improve performance of screen_get_dpi() in Javascript
This commit is contained in:
Fabio Alessandrelli 2022-08-20 13:36:04 +02:00 committed by GitHub
commit 1e044da15c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -336,26 +336,12 @@ const GodotDisplay = {
$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'],
$GodotDisplay: {
window_icon: '',
findDPI: function () {
function testDPI(dpi) {
return window.matchMedia(`(max-resolution: ${dpi}dpi)`).matches;
}
function bisect(low, high, func) {
const mid = parseInt(((high - low) / 2) + low, 10);
if (high - low <= 1) {
return func(high) ? high : low;
}
if (func(mid)) {
return bisect(low, mid, func);
}
return bisect(mid, high, func);
}
try {
const dpi = bisect(0, 800, testDPI);
return dpi >= 96 ? dpi : 96;
} catch (e) {
return 96;
}
getDPI: function () {
// devicePixelRatio is given in dppx
// https://drafts.csswg.org/css-values/#resolution
// > due to the 1:96 fixed ratio of CSS *in* to CSS *px*, 1dppx is equivalent to 96dpi.
const dpi = Math.round(window.devicePixelRatio * 96);
return dpi >= 96 ? dpi : 96;
},
},
@ -461,7 +447,7 @@ const GodotDisplay = {
godot_js_display_screen_dpi_get__sig: 'i',
godot_js_display_screen_dpi_get: function () {
return GodotDisplay.findDPI();
return GodotDisplay.getDPI();
},
godot_js_display_pixel_ratio_get__sig: 'f',