Skip to content

Commit fdc36d9

Browse files
committed
refactor: update texture processing to use time limit instead of count
1 parent 88ed685 commit fdc36d9

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/core/CoreTextureManager.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { SubTexture } from './textures/SubTexture.js';
2626
import { RenderTexture } from './textures/RenderTexture.js';
2727
import type { Texture } from './textures/Texture.js';
2828
import { EventEmitter } from '../common/EventEmitter.js';
29+
import { getTimeStamp } from './platform.js';
2930

3031
/**
3132
* Augmentable map of texture class types
@@ -437,40 +438,38 @@ export class CoreTextureManager extends EventEmitter {
437438
*
438439
* @param maxItems - The maximum number of items to process
439440
*/
440-
processSome(maxItems = 0): void {
441+
processSome(maxProcessingTime: number): void {
441442
if (this.initialized === false) {
442443
return;
443444
}
444445

445-
let itemsProcessed = 0;
446+
const startTime = getTimeStamp();
446447

447448
// Process priority queue
448449
while (
449450
this.priorityQueue.length > 0 &&
450-
(maxItems === 0 || itemsProcessed < maxItems)
451+
performance.now() - startTime < maxProcessingTime
451452
) {
452453
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
453454
const texture = this.priorityQueue.shift()!;
454455
texture.getTextureData().then(() => {
455456
this.uploadTexture(texture);
456457
});
457-
itemsProcessed++;
458458
}
459459

460460
// Process uploads
461461
while (
462462
this.uploadTextureQueue.length > 0 &&
463-
(maxItems === 0 || itemsProcessed < maxItems)
463+
performance.now() - startTime < maxProcessingTime
464464
) {
465465
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
466466
this.uploadTexture(this.uploadTextureQueue.shift()!);
467-
itemsProcessed++;
468467
}
469468

470469
// Process downloads
471470
while (
472471
this.downloadTextureSourceQueue.length > 0 &&
473-
(maxItems === 0 || itemsProcessed < maxItems)
472+
performance.now() - startTime < maxProcessingTime
474473
) {
475474
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
476475
const texture = this.downloadTextureSourceQueue.shift()!;
@@ -479,8 +478,6 @@ export class CoreTextureManager extends EventEmitter {
479478
this.enqueueUploadTexture(texture);
480479
});
481480
});
482-
483-
itemsProcessed++;
484481
}
485482
}
486483

src/core/Stage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export interface StageOptions {
7575
fontEngines: (typeof CanvasTextRenderer | typeof SdfTextRenderer)[];
7676
inspector: boolean;
7777
strictBounds: boolean;
78-
textureProcessingLimit: number;
78+
textureProcessingTimeLimit: number;
7979
}
8080

8181
export type StageFpsUpdateHandler = (
@@ -365,7 +365,7 @@ export class Stage {
365365

366366
// Process some textures
367367
// TODO this should have a configurable amount
368-
this.txManager.processSome(this.options.textureProcessingLimit);
368+
this.txManager.processSome(this.options.textureProcessingTimeLimit);
369369

370370
// Reset render operations and clear the canvas
371371
renderer.reset();

src/main-api/Renderer.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,16 @@ export interface RendererMainSettings {
269269
strictBounds?: boolean;
270270

271271
/**
272-
* Texture Processing Limit
272+
* Texture Processing Limit (in milliseconds)
273273
*
274274
* @remarks
275-
* The maximum number of textures to process in a single frame. This is used to
276-
* prevent the renderer from processing too many textures in a single frame.
275+
* The maximum amount of time the renderer is allowed to process textures in a
276+
* single frame. If the processing time exceeds this limit, the renderer will
277+
* skip processing the remaining textures and continue rendering the frame.
277278
*
278-
* @defaultValue `0`
279+
* @defaultValue `10`
279280
*/
280-
textureProcessingLimit?: number;
281+
textureProcessingTimeLimit?: number;
281282

282283
/**
283284
* Canvas object to use for rendering
@@ -378,7 +379,7 @@ export class RendererMain extends EventEmitter {
378379
quadBufferSize: settings.quadBufferSize ?? 4 * 1024 * 1024,
379380
fontEngines: settings.fontEngines,
380381
strictBounds: settings.strictBounds ?? true,
381-
textureProcessingLimit: settings.textureProcessingLimit || 0,
382+
textureProcessingTimeLimit: settings.textureProcessingTimeLimit || 10,
382383
canvas: settings.canvas || document.createElement('canvas'),
383384
};
384385
this.settings = resolvedSettings;
@@ -422,7 +423,7 @@ export class RendererMain extends EventEmitter {
422423
fontEngines: this.settings.fontEngines,
423424
inspector: this.settings.inspector !== null,
424425
strictBounds: this.settings.strictBounds,
425-
textureProcessingLimit: this.settings.textureProcessingLimit,
426+
textureProcessingTimeLimit: this.settings.textureProcessingTimeLimit,
426427
});
427428

428429
// Extract the root node

0 commit comments

Comments
 (0)