Ported: Improve performance of screen_get_dpi() in Javascript

Replace a bisect with a single multiplication when calling
screen_get_dpi() in Javascript

Tested the value of
window.matchMedia(`(resolution:${(window.devicePixelRatio*96).toFixed(100)}dpi)`).matches
which is true except for values that cause a lot of rounding errors
(e.g. dpr : 0.3  => resolution: 28.799999999999997dpi)

Even in these cases the value matches the result of the previous
`findDPI()` method.
-jamie-pate
562d7c616d
This commit is contained in:
Relintai 2022-09-16 21:15:54 +02:00
parent fefc436645
commit bcf96a5840

View File

@ -335,27 +335,12 @@ const PandemoniumDisplay = {
$PandemoniumDisplay__deps: ['$PandemoniumConfig', '$PandemoniumRuntime', '$PandemoniumDisplayCursor', '$PandemoniumEventListeners', '$PandemoniumDisplayScreen', '$PandemoniumDisplayVK'],
$PandemoniumDisplay: {
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;
},
},
@ -388,7 +373,7 @@ const PandemoniumDisplay = {
pandemonium_js_display_screen_dpi_get__sig: 'i',
pandemonium_js_display_screen_dpi_get: function() {
return PandemoniumDisplay.findDPI();
return PandemoniumDisplay.getDPI();
},
pandemonium_js_display_pixel_ratio_get__sig: 'f',