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.

15.1 KB
Raw Blame
import type {
  CanvasBlendMode,
  CanvasControlLayerContract,
  CanvasDocumentContractV3,
  CanvasInpaintMaskLayerContract,
  CanvasLayerContract,
  CanvasMaskFillContract,
  CanvasRasterLayerContractV2,
} from '@workbench/canvas-engine/contracts';
import type { Rect } from '@workbench/canvas-engine/types';

import { stacksFrom } from '@workbench/canvas-engine/document-model/documentFixtures.testStub';
import { identity } from '@workbench/canvas-engine/math/mat2d';
import {
  compositeDocument,
  createCheckerboardTile,
  shouldSmoothAtZoom,
  type CompositeOptions,
} from '@workbench/canvas-engine/render/compositor';
import { createLayerCacheStore } from '@workbench/canvas-engine/render/layerCache';
import { createMaskPatternTile } from '@workbench/canvas-engine/render/maskFill';
import { createDomRasterBackend } from '@workbench/canvas-engine/render/raster';
import { planBaseRasterComposite, renderRasterComposite } from '@workbench/canvas-engine/render/rasterComposite';
import { describe, expect, it } from 'vitest';
import { commands } from 'vitest/browser';

import { EXACT, expectGolden, goldenArtifactPath, INTERPOLATED } from './goldenHarness';

const WIDTH = 160;
const HEIGHT = 120;
const BBOX: Rect = { height: 90, width: 120, x: 20, y: 15 };

const transform = (overrides: Partial<CanvasLayerContract['transform']> = {}): CanvasLayerContract['transform'] => ({
  rotation: 0,
  scaleX: 1,
  scaleY: 1,
  x: 0,
  y: 0,
  ...overrides,
});

const raster = (id: string, overrides: Partial<CanvasRasterLayerContractV2> = {}): CanvasRasterLayerContractV2 => ({
  blendMode: 'normal',
  id,
  isEnabled: true,
  isLocked: false,
  name: id,
  opacity: 1,
  source: { image: { height: 10, imageName: id, width: 10 }, type: 'image' },
  transform: transform(),
  type: 'raster',
  ...overrides,
});

const control = (id: string, overrides: Partial<CanvasControlLayerContract> = {}): CanvasControlLayerContract => ({
  adapter: { beginEndStepPct: [0, 1], controlMode: 'balanced', kind: 'controlnet', model: null, weight: 1 },
  blendMode: 'normal',
  id,
  isEnabled: true,
  isLocked: false,
  name: id,
  opacity: 1,
  source: { bitmap: null, type: 'paint' },
  transform: transform(),
  type: 'control',
  withTransparencyEffect: false,
  ...overrides,
});

const inpaintMask = (id: string, fill: CanvasMaskFillContract): CanvasInpaintMaskLayerContract => ({
  blendMode: 'normal',
  id,
  isEnabled: true,
  isLocked: false,
  mask: { bitmap: null, fill },
  name: id,
  opacity: 1,
  transform: transform(),
  type: 'inpaint_mask',
});

const doc = (layers: CanvasLayerContract[]): CanvasDocumentContractV3 => ({
  background: 'transparent',
  bbox: BBOX,
  height: HEIGHT,
  stacks: stacksFrom(layers),
  selectedLayerId: null,
  version: 3,
  width: WIDTH,
});

type Paint = (ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, rect: Rect) => void;

const solid =
  (color: string): Paint =>
  (ctx, rect) => {
    ctx.fillStyle = color;
    ctx.fillRect(0, 0, rect.width, rect.height);
  };

const stripes: Paint = (ctx, rect) => {
  ctx.fillStyle = '#f2c14e';
  ctx.fillRect(0, 0, rect.width, rect.height);
  ctx.fillStyle = '#2d6a4f';
  for (let x = 0; x < rect.width; x += 16) {
    ctx.fillRect(x, 0, 8, rect.height);
  }
};

const scene = (layers: CanvasLayerContract[], paint: Record<string, { rect: Rect; paint: Paint }>) => {
  const backend = createDomRasterBackend();
  const caches = createLayerCacheStore(backend);
  for (const layer of layers) {
    const entry = paint[layer.id]!;
    caches.growToRect(layer.id, entry.rect);
    const surface = caches.get(layer.id)!.surface;
    surface.ctx.setTransform(1, 0, 0, 1, 0, 0);
    entry.paint(surface.ctx, entry.rect);
    caches.publishPixels(layer.id);
  }
  return { backend, caches };
};

const render = (
  layers: CanvasLayerContract[],
  paint: Parameters<typeof scene>[1],
  options: Partial<CompositeOptions> = {}
) => {
  const { backend, caches } = scene(layers, paint);
  const target = backend.createSurface(WIDTH, HEIGHT);
  compositeDocument(target, doc(layers), caches, identity(), {
    backend,
    checkerboardTile: createCheckerboardTile(backend),
    ...options,
  });
  return target;
};

const BASE = { paint: stripes, rect: { height: 70, width: 100, x: 30, y: 25 } };
const OVERLAY = { paint: solid('#4f8ff2'), rect: { height: 50, width: 60, x: 70, y: 45 } };

describe('compositor goldens', () => {
  it('composites two rasters over the checkerboard', async () => {
    const layers = [raster('overlay'), raster('base')];
    await expectGolden('composition-normal', render(layers, { base: BASE, overlay: OVERLAY }));
  });

  for (const blendMode of ['multiply', 'screen', 'difference'] satisfies CanvasBlendMode[]) {
    it(`blends the upper raster with ${blendMode}`, async () => {
      const layers = [raster('overlay', { blendMode }), raster('base')];
      await expectGolden(`blend-${blendMode}`, render(layers, { base: BASE, overlay: OVERLAY }));
    });
  }

  it('applies layer opacity', async () => {
    const layers = [raster('overlay', { opacity: 0.5 }), raster('base')];
    await expectGolden('opacity-half', render(layers, { base: BASE, overlay: OVERLAY }));
  });

  it('rotates a layer through its transform', async () => {
    const layers = [
      raster('overlay', { transform: transform({ rotation: Math.PI / 6, x: 10, y: -6 }) }),
      raster('base'),
    ];
    await expectGolden('rotation-30', render(layers, { base: BASE, overlay: OVERLAY }), { tolerance: INTERPOLATED });
  });

  it('clips document layers to the clip rect but not the checkerboard', async () => {
    const layers = [raster('overlay'), raster('base')];
    await expectGolden(
      'clip-rect',
      render(layers, { base: BASE, overlay: OVERLAY }, { clipRect: { height: 60, width: 80, x: 40, y: 30 } })
    );
  });

  for (const style of ['solid', 'grid'] satisfies CanvasMaskFillContract['style'][]) {
    it(`colorizes an inpaint mask with a ${style} fill`, async () => {
      const layers = [inpaintMask('mask', { color: '#e07575', style }), raster('base')];
      const { backend, caches } = scene(layers, {
        base: BASE,
        mask: { paint: solid('#000000'), rect: { height: 40, width: 50, x: 50, y: 35 } },
      });
      const target = backend.createSurface(WIDTH, HEIGHT);
      compositeDocument(target, doc(layers), caches, identity(), {
        backend,
        checkerboardTile: createCheckerboardTile(backend),
        maskPatternTile: (fillStyle, color) => createMaskPatternTile(backend, fillStyle as typeof style, color),
      });
      await expectGolden(`mask-${style}`, target);
    });
  }

  it.each(['horizontal', 'vertical', 'diagonal'] as const)('keeps %s hatching sharp at DPR 1', (style) => {
    const mask = inpaintMask('mask', { color: '#e07575', style });
    const { backend, caches } = scene([mask], {
      mask: { paint: solid('#000000'), rect: { height: 30, width: 30, x: 4, y: 4 } },
    });
    const target = backend.createSurface(WIDTH, HEIGHT);
    const scratch = backend.createSurface(WIDTH, HEIGHT);
    const tile = createMaskPatternTile(backend, style, mask.mask.fill.color)!;
    compositeDocument(target, doc([mask]), caches, identity(), {
      backend,
      maskPatternTile: () => tile,
      screenMask: { dpr: 1, getScratch: () => scratch },
    });
    const alpha = (pixels: Uint8ClampedArray) => Array.from(pixels).filter((_, index) => index % 4 === 3);
    const rendered = alpha(target.ctx.getImageData(12, 12, tile.width, tile.height).data);
    const original = alpha(tile.ctx.getImageData(0, 0, tile.width, tile.height).data);
    expect(rendered.sort((a, b) => a - b)).toEqual(original.sort((a, b) => a - b));
  });

  it.each(['diagonal', 'diagonal_left', 'horizontal', 'vertical'] as const)(
    'keeps both colors visible where two opaque %s masks overlap at DPR 1',
    (style) => {
      const layers = [
        inpaintMask('upper', { color: '#ff0000', style }),
        inpaintMask('lower', { color: '#0000ff', style }),
      ];
      const coverage = { paint: solid('#000000'), rect: { height: 30, width: 30, x: 4, y: 4 } };
      const { backend, caches } = scene(layers, { lower: coverage, upper: coverage });
      const target = backend.createSurface(WIDTH, HEIGHT);
      const scratch = backend.createSurface(WIDTH, HEIGHT);
      compositeDocument(
        target,
        doc(layers),
        caches,
        { a: 2, b: 0, c: 0, d: 2, e: 0, f: 0 },
        {
          backend,
          maskPatternTile: (_, color) => createMaskPatternTile(backend, style, color),
          screenMask: { dpr: 1, getScratch: () => scratch },
        }
      );
      const { data } = target.ctx.getImageData(24, 24, 12, 12);
      const red = Array.from(data).filter((_, index) => index % 4 === 0);
      const blue = Array.from(data).filter((_, index) => index % 4 === 2);
      expect(red.some((value, index) => value > blue[index]! + 32 && data[index * 4 + 3]! > 32)).toBe(true);
      expect(blue.some((value, index) => value > red[index]! + 32 && data[index * 4 + 3]! > 32)).toBe(true);
    }
  );

  it.each(['diagonal', 'diagonal_left'] as const)('renders the %s mask at a fixed screen scale', (style) => {
    const layers = [inpaintMask('mask', { color: '#e07575', style })];
    const { backend, caches } = scene(layers, {
      mask: { paint: solid('#000000'), rect: { height: 30, width: 30, x: 4, y: 4 } },
    });
    const target = backend.createSurface(WIDTH, HEIGHT);
    const tile = createMaskPatternTile(backend, style, '#e07575');
    const scratch = backend.createSurface(WIDTH, HEIGHT);
    const options: CompositeOptions = {
      backend,
      maskPatternTile: () => tile,
      screenMask: { dpr: 2, getScratch: () => scratch },
    };
    const document = doc(layers);
    const read = () => target.ctx.getImageData(0, 0, WIDTH, HEIGHT).data;
    const stripe = (x = 24, y = 24) =>
      Array.from(target.ctx.getImageData(x, y, 24, 1).data).filter((_, i) => i % 4 === 3);

    compositeDocument(
      target,
      document,
      caches,
      { a: 1.5, b: 0, c: 0, d: 1.5, e: 0, f: 0 },
      {
        ...options,
        imageSmoothing: shouldSmoothAtZoom(0.75),
      }
    );
    const baseline = stripe();
    const nextRow = stripe(24, 26);
    const peakShift = nextRow.indexOf(Math.max(...nextRow)) - baseline.indexOf(Math.max(...baseline));
    expect(Math.abs(peakShift - (style === 'diagonal' ? -2 : 2))).toBeLessThanOrEqual(1);
    expect(Math.max(...baseline)).toBeGreaterThan(0);
    expect(Math.min(...baseline)).toBe(0);
    expect(baseline.slice(0, 12)).toEqual(baseline.slice(12, 24));
    expect(baseline.slice(0, 6)).not.toEqual(baseline.slice(6, 12));

    const zoomedView = { a: 4, b: 0, c: 0, d: 4, e: 3, f: -2 };
    const zoomedOptions = { ...options, imageSmoothing: shouldSmoothAtZoom(2) };
    compositeDocument(target, document, caches, zoomedView, zoomedOptions);
    expect(stripe()).toEqual(baseline);
    expect(target.ctx.getImageData(16, 24, 1, 1).data[3]).toBe(0);

    const fullFrame = read();
    solid('#00ff00')(scratch.ctx, { height: HEIGHT, width: WIDTH, x: 0, y: 0 });
    compositeDocument(target, document, caches, zoomedView, {
      ...zoomedOptions,
      damage: { kind: 'regions', regions: [{ layerId: 'mask', rect: { height: 4, width: 4, x: 5, y: 5 } }] },
    });
    expect(read()).toEqual(fullFrame);
    // A small repaint draws only its own corner of the retained scratch.
    expect(Array.from(scratch.ctx.getImageData(80, 60, 1, 1).data)).toEqual([0, 255, 0, 255]);
  });

  it('draws a mask being edited over a staged candidate, which covers the raster beneath', async () => {
    const layers = [inpaintMask('mask', { color: '#e07575', style: 'solid' }), raster('base')];
    const { backend, caches } = scene(layers, {
      base: BASE,
      mask: { paint: solid('#000000'), rect: { height: 40, width: 50, x: 50, y: 35 } },
    });
    const candidate = backend.createSurface(60, 50);
    solid('#4f8ff2')(candidate.ctx, { height: 50, width: 60, x: 0, y: 0 });
    const target = backend.createSurface(WIDTH, HEIGHT);
    compositeDocument(target, doc(layers), caches, identity(), {
      backend,
      checkerboardTile: createCheckerboardTile(backend),
      maskPatternTile: (fillStyle, color) => createMaskPatternTile(backend, fillStyle as 'solid', color),
      stagedPreview: { rect: { height: 50, width: 60, x: 70, y: 45 }, surface: candidate },
    });
    await expectGolden('staged-candidate-mask-edited', target);
  });

  it('skips a hidden control layer normally but draws it while isolated', async () => {
    const layers = [control('control', { isHidden: true }), raster('base')];
    const paint = { base: BASE, control: { paint: solid('#8a2be2'), rect: { height: 30, width: 90, x: 35, y: 30 } } };
    await expectGolden('hidden-overlay', render(layers, paint));
    await expectGolden('hidden-overlay-isolated', render(layers, paint, { isolationLayerId: 'control' }));
  });

  it('renders the generation base from enabled raster layers only', async () => {
    const layers = [
      control('control'),
      raster('overlay', { opacity: 0.75 }),
      raster('disabled', { isEnabled: false }),
      raster('base'),
    ];
    const { backend, caches } = scene(layers, {
      base: BASE,
      control: { paint: solid('#8a2be2'), rect: { height: 30, width: 90, x: 35, y: 30 } },
      disabled: { paint: solid('#ff0000'), rect: { height: 90, width: 120, x: 20, y: 15 } },
      overlay: OVERLAY,
    });
    const surface = await renderRasterComposite(planBaseRasterComposite(doc(layers), BBOX), {
      backend,
      getLayerSurface: (layerId) => {
        const entry = caches.get(layerId)!;
        return Promise.resolve({ rect: entry.rect, release: () => undefined, surface: entry.surface });
      },
    });
    await expectGolden('generation-base', surface, { tolerance: EXACT });
  });
});

describe('golden harness', () => {
  it.skipIf(__CANVAS_GOLDEN_UPDATE__)('refuses a frame without a reviewed baseline', async () => {
    const target = render([raster('base')], { base: BASE });
    await expect(expectGolden('unreviewed-frame', target)).rejects.toThrow(/Missing golden "unreviewed-frame"/);
  });

  it.skipIf(__CANVAS_GOLDEN_UPDATE__)(
    'reports a differing frame and writes expected, actual and diff artifacts',
    async () => {
      const altered = render([raster('overlay'), raster('base')], {
        base: BASE,
        overlay: { ...OVERLAY, paint: solid('#f24f4f') },
      });
      await expect(
        expectGolden('composition-normal', altered, { artifactName: 'negative-composition' })
      ).rejects.toThrow(/Golden "composition-normal" differs/);
      for (const kind of ['expected', 'actual', 'diff'] as const) {
        const png = await commands.readFile(goldenArtifactPath('negative-composition', kind), 'base64');
        expect(png.length).toBeGreaterThan(0);
      }
    }
  );

  it.skipIf(__CANVAS_GOLDEN_UPDATE__)('accepts interpolation noise only within the declared tolerance', async () => {
    const shifted = render(
      [raster('overlay', { transform: transform({ rotation: Math.PI / 6 + 0.002, x: 10, y: -6 }) }), raster('base')],
      { base: BASE, overlay: OVERLAY }
    );
    await expect(
      expectGolden('rotation-30', shifted, { artifactName: 'negative-rotation', tolerance: INTERPOLATED })
    ).rejects.toThrow(/differs/);
  });
});