From b03bec267ccc13a019aa8902759134733869f4d4 Mon Sep 17 00:00:00 2001 From: Antoine <20756073+antt0n@users.noreply.github.com> Date: Wed, 4 Nov 2026 20:59:05 +0100 Subject: [PATCH] 2D map: keep the land picture on the graphics card in Firefox on a computer The 2D map paints its land into an offscreen picture. In Firefox that picture was a software canvas (willReadFrequently), because Firefox on Android garbled big paths on its graphics card (#43). Firefox on a computer had it too, and a software canvas is sent to the card again on every frame, at a cost that grows with the picture: the whole world at mid zoom, four screens of it zoomed in. Only Firefox on Android keeps it now (softCanvas()). Measured in Firefox 159 on Windows (RTX 4090, full screen 2560x1440), 2D map, frames per second at zoom 0.6 / 1.1 / 1.6 / 3.2: - big-network fixture, main: 56 / 37 / 34 / 26; with this change: 64 / 49 / 51 / 78; - the 183-POP save, main: 32 / 25 / 25 / 22; with this change: 36 / 26 / 33 / 55. (Measured with the change injected into main's page; the land picture is the same code on this branch.) tests/world-layer-firefox.test.js: which browsers get the software canvas, and that the land is an ordinary one elsewhere. --- CHANGELOG.md | 1 + src/game/96-map/70-world-layer.js | 5 ++++- tests/world-layer-firefox.test.js | 36 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/world-layer-firefox.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 39252dc..32a06d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project are documented here. The project follows - Zooming is smoother on big maps: while you zoom, cities and ships keep their picture and are only redrawn sharp once the zoom stops, instead of being repainted at every small step. - The graphics card draws the coastlines with fewer points when you zoom out, where the extra detail was smaller than a pixel, so the map costs it a lot less work, which helps phones most. - The map leaves far less memory behind it on every frame for the browser to clean up while you play. +- In Firefox on a computer, the 2D map is up to three times as fast when zoomed in: its land picture now stays on the graphics card instead of being sent to it again on every frame. Firefox on Android keeps the old way, which avoids its drawing glitches. - Settings › Viewport info is now Diagnostic info: besides your screen's size, it shows how the map is drawn (by the graphics card or in 2D, and why) and how fast, ready to copy into a bug report. ## [2.9.1] — 2026-11-04 diff --git a/src/game/96-map/70-world-layer.js b/src/game/96-map/70-world-layer.js index 426de70..20a94a8 100644 --- a/src/game/96-map/70-world-layer.js +++ b/src/game/96-map/70-world-layer.js @@ -2,7 +2,10 @@ GPU canvas implementations (Firefox on Android) garble big paths (#43). It is painted into an offscreen image instead: the whole world when that fits in MAXPX, otherwise the visible area plus a margin, repainted when you pan out of it. While a zoom gesture is still moving, the last image is scaled rather than repainted. */ -const SOFT_CANVAS=/Firefox\//.test(navigator.userAgent); +/* Only Firefox on Android gets that image as a software canvas (willReadFrequently). Firefox on a computer drew it + fine on the graphics card, and a software canvas there is sent to the card again on every frame, a cost that grows + with the image: zoomed in, the 2D map ran at half its speed. */ +const softCanvas=ua=>/Android.*Firefox\//.test(ua),SOFT_CANVAS=softCanvas(navigator.userAgent); function wlCovers(z){if(WL.full)return true;const hw=CW/2/z,hh=CH/2/z,w=WL.cv.width/WL.sc,h=WL.cv.height/WL.sc; return cam.x-hw>=WL.ox&&cam.x+hw<=WL.ox+w&&cam.y-hh>=WL.oy&&cam.y+hh<=WL.oy+h;} function worldLayer(z){ diff --git a/tests/world-layer-firefox.test.js b/tests/world-layer-firefox.test.js new file mode 100644 index 0000000..b77ad42 --- /dev/null +++ b/tests/world-layer-firefox.test.js @@ -0,0 +1,36 @@ +// The 2D map's land picture (src/game/96-map/70-world-layer.js) is a software canvas (willReadFrequently) only in +// Firefox on Android, which garbled big paths on its graphics card (#43). Firefox on a computer had it too, and sent +// the whole picture to the card again on every frame: measured in Firefox on Windows, full screen 2560x1440, zoomed in +// the 2D map ran at a third of its speed (zoom 3.2 on the big network: 26 fps, 78 without it). +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { load } = require('./harness'); + +const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8'); + +test('only Firefox on Android gets the land as a software canvas', () => { + const { ev } = load({ seed: 777 }); + const soft = (ua) => ev(`softCanvas(${JSON.stringify(ua)})`); + // Firefox on Android, phone and tablet. + assert.equal(soft('Mozilla/5.0 (Android 14; Mobile; rv:132.0) Gecko/132.0 Firefox/132.0'), true); + assert.equal(soft('Mozilla/5.0 (Android 13; Tablet; rv:128.0) Gecko/128.0 Firefox/128.0'), true); + // Firefox on a computer, and the other browsers. + assert.equal(soft('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:159.0) Gecko/20100101 Firefox/159.0'), false); + assert.equal(soft('Mozilla/5.0 (X11; Linux x86_64; rv:153.0) Gecko/20100101 Firefox/153.0'), false); + assert.equal(soft('Mozilla/5.0 (Macintosh; Intel Mac OS X 14.5; rv:131.0) Gecko/20100101 Firefox/131.0'), false); + assert.equal(soft('Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Mobile Safari/537.36'), false); + assert.equal(soft('Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/130.0 Mobile/15E148 Safari/605.1.15'), false); +}); + +test('elsewhere the land picture is an ordinary canvas, which the browser may keep on the graphics card', () => { + const { ev, errors } = load({ seed: 777 }); + ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();compute();mapGl(false,false); + window.__opts=[];const gc=HTMLCanvasElement.prototype.getContext;HTMLCanvasElement.prototype.getContext=function(t,o){__opts.push(o||null);return gc.call(this,t,o);}; + WL.cv=null;WL.key=null;cam.z=1.6;worldLayer(cam.z);`); + assert.equal(ev('SOFT_CANVAS'), false); + assert.ok(ev('!!WL.cv'), 'the land was painted'); + assert.equal(ev('__opts.some(o=>o&&o.willReadFrequently)'), false); + assert.deepEqual(errors, []); +});