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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(canvas): add layer snapshot history
joshistoastb7c20e352131
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/history/layerSnapshot.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/history/layerSnapshot.test.tsnew file mode 100644index 0000000000..39de4f79f4--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/history/layerSnapshot.test.ts@@ -0,0 +1,99 @@+import type { CanvasControlLayerContract } from '@workbench/types';++import { describe, expect, it, vi } from 'vitest';++import { createLayerSnapshotEntry, type LayerPixelSnapshot } from './layerSnapshot';++const fakeImageData = (width: number, height: number): ImageData =>+ ({+ colorSpace: 'srgb',+ data: new Uint8ClampedArray(width * height * 4),+ height,+ width,+ }) as unknown as ImageData;++const layer = (source: CanvasControlLayerContract['source']): CanvasControlLayerContract => ({+ adapter: { beginEndStepPct: [0, 1], controlMode: 'balanced', kind: 'controlnet', model: 'm', weight: 1 },+ blendMode: 'normal',+ id: 'control',+ isEnabled: true,+ isLocked: false,+ name: 'Control',+ opacity: 1,+ source,+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },+ type: 'control',+ withTransparencyEffect: true,+});++const snapshot = (which: 'before' | 'after'): LayerPixelSnapshot => ({+ layer: layer(+ which === 'before'+ ? { image: { height: 2, imageName: 'before', width: 3 }, type: 'image' }+ : { bitmap: null, offset: { x: 4, y: 5 }, type: 'paint' }+ ),+ pixels: fakeImageData(3, 2),+ rect: { height: 2, width: 3, x: which === 'before' ? 0 : 4, y: which === 'before' ? 0 : 5 },+});++describe('createLayerSnapshotEntry', () => {+ it('replays exact before/after snapshots and accounts for both buffers', () => {+ const before = snapshot('before');+ const after = snapshot('after');+ const apply = vi.fn();+ const entry = createLayerSnapshotEntry({ after, apply, before, label: 'Brush stroke' });++ expect(entry.bytes).toBe(before.pixels!.data.byteLength + after.pixels!.data.byteLength + 256);+ expect(entry.replayFailureAtomic).toBe(true);+ entry.undo();+ entry.redo();+ expect(apply).toHaveBeenNthCalledWith(1, before);+ expect(apply).toHaveBeenNthCalledWith(2, after);+ });++ it('rejects mismatched layer ids', () => {+ const before = snapshot('before');+ const after = { ...snapshot('after'), layer: { ...snapshot('after').layer, id: 'other' } };+ expect(() => createLayerSnapshotEntry({ after, apply: vi.fn(), before, label: 'Edit' })).toThrow(+ 'layerSnapshot: before/after layer ids differ'+ );+ });++ it('rejects pixel dimensions that do not match the rect', () => {+ const before = { ...snapshot('before'), pixels: fakeImageData(1, 1) };+ expect(() => createLayerSnapshotEntry({ after: snapshot('after'), apply: vi.fn(), before, label: 'Edit' })).toThrow(+ 'layerSnapshot: before pixels do not match rect'+ );+ });++ it('accepts null pixels only for an empty cache extent', () => {+ const before = { ...snapshot('before'), pixels: null, rect: { height: 0, width: 0, x: 0, y: 0 } };+ expect(() =>+ createLayerSnapshotEntry({ after: snapshot('after'), apply: vi.fn(), before, label: 'First stroke' })+ ).not.toThrow();+ expect(() =>+ createLayerSnapshotEntry({+ after: snapshot('after'),+ apply: vi.fn(),+ before: { ...snapshot('before'), pixels: null },+ label: 'Edit',+ })+ ).toThrow('layerSnapshot: before non-empty rect requires pixels');+ });++ it('protects contract and rect snapshots from later caller mutation', () => {+ const before = snapshot('before');+ const after = snapshot('after');+ const apply = vi.fn();+ const entry = createLayerSnapshotEntry({ after, apply, before, label: 'Edit' });+ before.layer.name = 'mutated';+ before.rect.x = 999;+ entry.undo();+ expect(apply).toHaveBeenCalledWith(+ expect.objectContaining({+ layer: expect.objectContaining({ name: 'Control' }),+ rect: expect.objectContaining({ x: 0 }),+ })+ );+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/history/layerSnapshot.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/history/layerSnapshot.tsnew file mode 100644index 0000000000..7a407444a4--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/history/layerSnapshot.ts@@ -0,0 +1,65 @@+import type { Rect } from '@workbench/canvas-engine/types';+import type { CanvasControlLayerContract } from '@workbench/types';++import type { HistoryEntry } from './history';++export interface LayerPixelSnapshot {+ layer: CanvasControlLayerContract;+ rect: Rect;+ /** Null represents the exact pixel state of a zero-width or zero-height cache. */+ pixels: ImageData | null;+}++export type LayerPixelSnapshotApply = (snapshot: LayerPixelSnapshot) => void;++export interface CreateLayerSnapshotEntryOptions {+ before: LayerPixelSnapshot;+ after: LayerPixelSnapshot;+ label: string;+ apply: LayerPixelSnapshotApply;+}++const assertSnapshot = (snapshot: LayerPixelSnapshot, which: 'before' | 'after'): void => {+ if (snapshot.rect.width === 0 || snapshot.rect.height === 0) {+ if (snapshot.pixels !== null) {+ throw new Error(`layerSnapshot: ${which} empty rect requires null pixels`);+ }+ return;+ }+ if (snapshot.pixels === null) {+ throw new Error(`layerSnapshot: ${which} non-empty rect requires pixels`);+ }+ if (snapshot.pixels.width !== snapshot.rect.width || snapshot.pixels.height !== snapshot.rect.height) {+ throw new Error(`layerSnapshot: ${which} pixels do not match rect`);+ }+};++export const createLayerSnapshotEntry = ({+ after,+ apply,+ before,+ label,+}: CreateLayerSnapshotEntryOptions): HistoryEntry => {+ if (before.layer.id !== after.layer.id) {+ throw new Error('layerSnapshot: before/after layer ids differ');+ }+ assertSnapshot(before, 'before');+ assertSnapshot(after, 'after');+ const beforeSnapshot: LayerPixelSnapshot = {+ layer: structuredClone(before.layer),+ pixels: before.pixels,+ rect: { ...before.rect },+ };+ const afterSnapshot: LayerPixelSnapshot = {+ layer: structuredClone(after.layer),+ pixels: after.pixels,+ rect: { ...after.rect },+ };+ return {+ bytes: (before.pixels?.data.byteLength ?? 0) + (after.pixels?.data.byteLength ?? 0) + 256,+ label,+ redo: () => apply(afterSnapshot),+ replayFailureAtomic: true,+ undo: () => apply(beforeSnapshot),+ };+};