diff --git a/src/render/cesium/globe.test.mjs b/src/render/cesium/globe.test.mjs index 8d30364dd..500bfee27 100644 --- a/src/render/cesium/globe.test.mjs +++ b/src/render/cesium/globe.test.mjs @@ -334,6 +334,29 @@ test('a layer deferring while another has a request in flight asks for a frame', assert.deepEqual(renders, ['imagery']); }); +test('layers sharing an admit wait for each other’s tiles, asking for no frame', (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + const { scene, port, renders } = setup(); + // A budget of one shared by two layers, as Recent Imagery's swipe slots. + let inFlight = 0; + const admit = (_tile, issue) => { + if (inFlight >= 1) return undefined; + inFlight += 1; + return issue(); + }; + port.imagery.add({ kind: 'xyz', url: url('a'), admit }); + port.imagery.add({ kind: 'xyz', url: url('b'), admit }); + const [holder, waiting] = [0, 1].map( + (i) => scene.imageryLayers.get(i).imageryProvider, + ); + Object.getPrototypeOf(holder).requestImage = () => new Promise(() => {}); + assert.ok(holder.requestImage(0, 0, 1), 'in flight'); + renders.length = 0; + assert.equal(waiting.requestImage(0, 0, 1), undefined); + t.mock.timers.tick(TILE_RETRY_DELAY_MS * 4); + assert.deepEqual(renders, [], 'the holder’s settling is the later frame'); +}); + test('fetchTile fetches in place of the provider, with the URL it would fetch', async (t) => { const asked = []; const answers = [{ defer: true }, { missing: true }]; diff --git a/src/render/cesium/imagery.js b/src/render/cesium/imagery.js index c89fe87ea..546449976 100644 --- a/src/render/cesium/imagery.js +++ b/src/render/cesium/imagery.js @@ -250,9 +250,10 @@ function guarded(run) { * `maxConcurrentRequests` are in flight. * * A tile `admit` defers is asked again on a later frame: the one a request - * of the same layer in flight asks for as it settles, or, with none in - * flight (`busy`), the one `retrySoon` asks for. So a layer whose own - * budget is full never keeps the scene drawing while it waits. + * of a layer with the same `admit` in flight asks for as it settles, or, + * with none in flight (`busy`), the one `retrySoon` asks for. So layers + * whose shared budget is full never keep the scene drawing while they + * wait. * @param {object} provider * @param {import('../contract/globe').ImagerySpec} spec * @param {{started: () => void, settled: () => void, retrySoon: () => void, @@ -366,6 +367,8 @@ export function createCesiumImagery( /** Port layers, bottom to top. */ const layers = []; let pending = 0; + /** Tiles in flight by the `admit` their layers share. */ + const admitPending = new Map(); let retryTimer = null; let disposed = false; const waiters = []; @@ -512,17 +515,21 @@ export function createCesiumImagery( }; const signals = new Set(); const requests = new Set(); - let layerPending = 0; + // Layers given one `admit` share its waits: a tile it defers is asked + // again on the frame any of their tiles asks for as it settles. + const shared = spec.admit ?? {}; const ctx = { - busy: () => layerPending > 0, + busy: () => (admitPending.get(shared) ?? 0) > 0, requests, started: () => { pending += 1; - layerPending += 1; + admitPending.set(shared, (admitPending.get(shared) ?? 0) + 1); }, settled: () => { pending -= 1; - layerPending -= 1; + const left = (admitPending.get(shared) ?? 1) - 1; + if (left > 0) admitPending.set(shared, left); + else admitPending.delete(shared); if (!disposed) requestRender('imagery'); }, retrySoon, diff --git a/src/render/contract/globe.d.ts b/src/render/contract/globe.d.ts index da7eff9f8..43c425818 100644 --- a/src/render/contract/globe.d.ts +++ b/src/render/contract/globe.d.ts @@ -111,7 +111,8 @@ export interface ImagerySpec { /** * `imageHooks`: decides each tile request on the main thread: call * `issue()` to fetch it, or return `undefined` to defer it and be asked - * again on a later frame. `fetchTile`'s `defer` does the same anywhere. + * again on a later frame. Layers given the same function share its waits: + * while one of their tiles is in flight, its settling is that frame. `fetchTile`'s `defer` does the same anywhere. * With both, `admit` decides first and an issued request goes through * `fetchTile`; each defers on its own. */