Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

1.7 KB
Raw Blame
import type { CanvasDiagnosticsSnapshot } from './capabilities';

export type { CanvasDiagnosticsSnapshot } from './capabilities';

export type CanvasDiagnosticsCounter = keyof CanvasDiagnosticsSnapshot;
type MutableCanvasDiagnosticsSnapshot = { -readonly [K in CanvasDiagnosticsCounter]: number };

export interface CanvasDiagnostics {
  readonly enabled: boolean;
  increment(counter: CanvasDiagnosticsCounter): void;
  add(counter: CanvasDiagnosticsCounter, amount: number): void;
  snapshot(): Readonly<CanvasDiagnosticsSnapshot>;
  reset(): void;
}

const zeroSnapshot = (): MutableCanvasDiagnosticsSnapshot => ({
  allocatedBaseBytes: 0,
  allocatedDerivedBytes: 0,
  compositeFrames: 0,
  derivedCacheEvictions: 0,
  derivedCacheHits: 0,
  derivedCacheMisses: 0,
  groupSurfaceAllocations: 0,
  groupSurfaceRebuilds: 0,
  groupSurfaceRefreshes: 0,
  imageDataReads: 0,
  imageDataWrites: 0,
  layersConsidered: 0,
  layersCulled: 0,
  layersDrawn: 0,
  overlayFrames: 0,
  rasterOverageBytes: 0,
  surfaceCreations: 0,
  surfaceResizes: 0,
});

const DISABLED_SNAPSHOT = Object.freeze(zeroSnapshot());

export const createCanvasDiagnostics = (enabled = false): CanvasDiagnostics => {
  if (!enabled) {
    return {
      add: () => undefined,
      enabled: false,
      increment: () => undefined,
      reset: () => undefined,
      snapshot: () => DISABLED_SNAPSHOT,
    };
  }

  let counters = zeroSnapshot();
  return {
    add: (counter, amount) => {
      counters[counter] += amount;
    },
    enabled: true,
    increment: (counter) => {
      counters[counter] += 1;
    },
    reset: () => {
      counters = zeroSnapshot();
    },
    snapshot: () => Object.freeze({ ...counters }),
  };
};