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. See also:6cff589b5b (r81273660)
(cherry picked from commitc2e3971013
)
This commit is contained in:
parent
c8810395a2
commit
562d7c616d
@ -335,26 +335,12 @@ const GodotDisplay = {
|
|||||||
$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'],
|
$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'],
|
||||||
$GodotDisplay: {
|
$GodotDisplay: {
|
||||||
window_icon: '',
|
window_icon: '',
|
||||||
findDPI: function () {
|
getDPI: function () {
|
||||||
function testDPI(dpi) {
|
// devicePixelRatio is given in dppx
|
||||||
return window.matchMedia(`(max-resolution: ${dpi}dpi)`).matches;
|
// 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.
|
||||||
function bisect(low, high, func) {
|
const dpi = Math.round(window.devicePixelRatio * 96);
|
||||||
const mid = parseInt(((high - low) / 2) + low, 10);
|
return dpi >= 96 ? dpi : 96;
|
||||||
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;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -472,7 +458,7 @@ const GodotDisplay = {
|
|||||||
|
|
||||||
godot_js_display_screen_dpi_get__sig: 'i',
|
godot_js_display_screen_dpi_get__sig: 'i',
|
||||||
godot_js_display_screen_dpi_get: function () {
|
godot_js_display_screen_dpi_get: function () {
|
||||||
return GodotDisplay.findDPI();
|
return GodotDisplay.getDPI();
|
||||||
},
|
},
|
||||||
|
|
||||||
godot_js_display_pixel_ratio_get__sig: 'f',
|
godot_js_display_pixel_ratio_get__sig: 'f',
|
||||||
|
Loading…
Reference in New Issue
Block a user