Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce platform abstraction layer #422

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/core/CoreTextureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { NoiseTexture } from './textures/NoiseTexture.js';
import { SubTexture } from './textures/SubTexture.js';
import { RenderTexture } from './textures/RenderTexture.js';
import type { Texture } from './textures/Texture.js';
import type { CorePlatform } from './platforms/CorePlatform.js';
import type { WebPlatform } from './platforms/web/WebPlatform.js';

/**
* Augmentable map of texture class types
Expand Down Expand Up @@ -139,6 +141,10 @@ export interface TextureOptions {
}

export class CoreTextureManager {
private platform: CorePlatform | WebPlatform;
public createImageBitmap: typeof createImageBitmap;
public hasCreateImageBitmap = false;

/**
* Map of textures by cache key
*/
Expand All @@ -155,8 +161,8 @@ export class CoreTextureManager {
txConstructors: Partial<TextureMap> = {};

imageWorkerManager: ImageWorkerManager | null = null;
hasCreateImageBitmap = !!self.createImageBitmap;
hasWorker = !!self.Worker;

/**
* Renderer that this texture manager is associated with
*
Expand All @@ -177,7 +183,12 @@ export class CoreTextureManager {
*/
frameTime = 0;

constructor(numImageWorkers: number) {
constructor(numImageWorkers: number, platform: CorePlatform | WebPlatform) {
this.platform = platform;

this.createImageBitmap = platform.createImageBitmap;
this.hasCreateImageBitmap = !!this.createImageBitmap;

// Register default known texture types
if (this.hasCreateImageBitmap && this.hasWorker && numImageWorkers > 0) {
this.imageWorkerManager = new ImageWorkerManager(numImageWorkers);
Expand Down
19 changes: 15 additions & 4 deletions src/core/Stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { startLoop, getTimeStamp } from './platform.js';

import { assertTruthy, setPremultiplyMode } from '../utils.js';
import { AnimationManager } from './animations/AnimationManager.js';
import {
Expand Down Expand Up @@ -52,6 +52,8 @@ import { CoreTextNode, type CoreTextNodeProps } from './CoreTextNode.js';
import { santizeCustomDataMap } from '../main-api/utils.js';
import type { SdfTextRenderer } from './text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js';
import type { CanvasTextRenderer } from './text-rendering/renderers/CanvasTextRenderer.js';
import type { CorePlatform } from './platforms/CorePlatform.js';
import type { WebPlatform } from './platforms/web/WebPlatform.js';

export interface StageOptions {
appWidth: number;
Expand All @@ -71,6 +73,7 @@ export interface StageOptions {
quadBufferSize: number;
fontEngines: (typeof CanvasTextRenderer | typeof SdfTextRenderer)[];
inspector: boolean;
platform: CorePlatform | WebPlatform;
}

export type StageFpsUpdateHandler = (
Expand Down Expand Up @@ -119,6 +122,7 @@ export class Stage {
private frameEventQueue: [name: string, payload: unknown][] = [];
private fontResolveMap: Record<string, CanvasTextRenderer | SdfTextRenderer> =
{};
private platform: CorePlatform | WebPlatform | null = null;

/// Debug data
contextSpy: ContextSpy | null = null;
Expand All @@ -139,10 +143,11 @@ export class Stage {
textureMemory,
renderEngine,
fontEngines,
platform,
} = options;

this.eventBus = options.eventBus;
this.txManager = new CoreTextureManager(numImageWorkers);
this.txManager = new CoreTextureManager(numImageWorkers, platform);
this.txMemManager = new TextureMemoryManager(this, textureMemory);
this.shManager = new CoreShaderManager();
this.animationManager = new AnimationManager();
Expand All @@ -156,8 +161,14 @@ export class Stage {
}
this.boundsMargin = bm;

assertTruthy(
platform !== null,
'A CorePlatform is not provided in the options',
);

const rendererOptions: CoreRendererOptions = {
stage: this,
platform,
canvas,
pixelRatio:
options.devicePhysicalPixelRatio * options.deviceLogicalPixelRatio,
Expand Down Expand Up @@ -252,12 +263,12 @@ export class Stage {

// execute platform start loop
if (autoStart) {
startLoop(this);
platform.startLoop(this);
}
}

updateFrameTime() {
const newFrameTime = getTimeStamp();
const newFrameTime = this.platform?.getTimeStamp() || Date.now();
this.lastFrameTime = this.currentFrameTime;
this.currentFrameTime = newFrameTime;
this.deltaTime = !this.lastFrameTime
Expand Down
34 changes: 34 additions & 0 deletions src/core/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,37 @@ export function convertUrlToAbsolute(url: string): string {
const absoluteUrl = new URL(url, self.location.href);
return absoluteUrl.href;
}

/**
* Compare two arrays for equality.
*
* @remarks
* This function will not try to compare nested arrays or Float32Arrays and
* instead will always return false when they are encountered.
*
* @param a
* @param b
* @returns
*/
export function compareArrays<T>(a: T[], b: T[]): boolean {
if (a.length !== b.length) {
return false;
}

let result = false;
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
result = false;
break;
}

if (a[i] !== b[i]) {
result = false;
break;
}

result = true;
}

return result;
}
61 changes: 0 additions & 61 deletions src/core/platform.ts

This file was deleted.

Loading
Loading