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.

fix(canvas): make control selection rollback idempotent

joshistoaste398b06794bd
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts
index 91cc0c169f..29b252e5e5 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts
@@ -12619,6 +12619,58 @@ describe('engine selection: fill / erase', () => {
h.engine.dispose();
});
+ it('keeps materialized no-effect rollback authoritative when growth restoration throws once', async () => {
+ const h = createControlSelectionHarness({
+ source: { image: { height: 10, imageName: 'control-image', width: 10 }, type: 'image' },
+ transform: { rotation: 0, scaleX: 2, scaleY: 2, x: 5, y: 6 },
+ });
+ await h.publishInitialCache();
+ const beforeDocument = structuredClone(h.engine.getDocument());
+ const beforeCache = await snapshotLayerCache(h.engine, 'control');
+ const beforeThumbnailVersion = h.engine.stores.thumbnailVersion.get('control');
+ const createSurface = h.backend.createSurface.bind(h.backend);
+ let didThrow = false;
+ vi.spyOn(h.backend, 'createSurface').mockImplementation((width, height) => {
+ const surface = createSurface(width, height);
+ const resize = surface.resize.bind(surface);
+ surface.resize = (nextWidth, nextHeight) => {
+ if (!didThrow && surface.width === 100 && nextWidth === 20 && nextHeight === 20) {
+ didThrow = true;
+ throw new Error('selection rollback restoration failed');
+ }
+ resize(nextWidth, nextHeight);
+ };
+ return surface;
+ });
+ h.engine.selectAll();
+ h.setSelectionPixelWrites(false);
+
+ expect(() => h.engine.fillSelection()).toThrow('selection rollback restoration failed');
+
+ expect(didThrow).toBe(true);
+ expect(h.engine.getDocument()).toEqual(beforeDocument);
+ expect(h.engine.stores.canUndo.get()).toBe(false);
+ expect(h.bitmapStore.markLayerDirty).not.toHaveBeenCalled();
+ expect(h.bitmapStore.suspendLayer).toHaveBeenCalledOnce();
+ expect(h.bitmapStore.releaseSuspendedLayer).toHaveBeenCalledOnce();
+ expect(h.engine.stores.thumbnailVersion.get('control')).toBe(beforeThumbnailVersion);
+ const restored = await h.engine.exportLayerPixels('control', { includeDisabled: true });
+ expect(restored.status).toBe('ok');
+ if (restored.status === 'ok') {
+ expect(restored.surface).toBe(beforeCache.surface);
+ expect(restored.rect).toEqual(beforeCache.rect);
+ expect(restored.guard.cacheVersion).toBe(beforeCache.version);
+ }
+
+ vi.mocked(h.backend.createSurface).mockRestore();
+ h.setSelectionPixelWrites(true);
+ h.engine.fillSelection();
+ expect(h.engine.stores.canUndo.get()).toBe(true);
+ expect(h.bitmapStore.suspendLayer).toHaveBeenCalledTimes(2);
+ expect(h.bitmapStore.releaseSuspendedLayer).toHaveBeenCalledTimes(2);
+ h.engine.dispose();
+ });
+
it('rolls back a direct control selection edit when masked compositing fails', async () => {
const h = createControlSelectionHarness({
source: { bitmap: { height: 10, imageName: 'paint-bitmap', width: 10 }, type: 'paint' },
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
index ea9b013334..f286339a61 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
@@ -6568,6 +6568,7 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngine => {
let editRect: Rect | null = null;
let editSurface: RasterSurface | null = null;
let controlCommitStarted = false;
+ let controlRollbackStarted = false;
let controlGrowthSnapshot:
| {
hasPublishedPixels: boolean;
@@ -6581,32 +6582,47 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngine => {
| null
| undefined;
const rollbackControlEdit = (): void => {
- if (target.kind !== 'control') {
+ if (target.kind !== 'control' || controlRollbackStarted) {
return;
}
+ controlRollbackStarted = true;
+ const growthSnapshot = controlGrowthSnapshot;
+ const rollbackBefore = before;
+ const rollbackOrigin = editOrigin;
+ const rollbackRect = editRect;
+ const rollbackSurface = editSurface;
+ controlGrowthSnapshot = undefined;
+ before = null;
+ editOrigin = null;
+ editRect = null;
+ editSurface = null;
try {
- if (controlGrowthSnapshot !== undefined) {
- if (controlGrowthSnapshot === null) {
+ if (growthSnapshot !== undefined) {
+ if (growthSnapshot === null) {
layerCache.delete(layerId);
} else {
const current = layerCache.get(layerId);
if (current) {
- controlGrowthSnapshot.surface.resize(controlGrowthSnapshot.rect.width, controlGrowthSnapshot.rect.height);
- if (controlGrowthSnapshot.pixels) {
- controlGrowthSnapshot.surface.ctx.putImageData(controlGrowthSnapshot.pixels, 0, 0);
+ growthSnapshot.surface.resize(growthSnapshot.rect.width, growthSnapshot.rect.height);
+ if (growthSnapshot.pixels) {
+ growthSnapshot.surface.ctx.putImageData(growthSnapshot.pixels, 0, 0);
}
- current.hasPublishedPixels = controlGrowthSnapshot.hasPublishedPixels;
- current.lastUsed = controlGrowthSnapshot.lastUsed;
- current.rect = { ...controlGrowthSnapshot.rect };
- current.stale = controlGrowthSnapshot.stale;
- current.surface = controlGrowthSnapshot.surface;
- current.version = controlGrowthSnapshot.version;
+ current.hasPublishedPixels = growthSnapshot.hasPublishedPixels;
+ current.lastUsed = growthSnapshot.lastUsed;
+ current.rect = { ...growthSnapshot.rect };
+ current.stale = growthSnapshot.stale;
+ current.surface = growthSnapshot.surface;
+ current.version = growthSnapshot.version;
}
}
adjustedSurfaceCache.delete(layerId);
scheduler.invalidate({ layers: [layerId] });
- } else if (before && editOrigin && editRect && editSurface) {
- editSurface.ctx.putImageData(before, editRect.x - editOrigin.x, editRect.y - editOrigin.y);
+ } else if (rollbackBefore && rollbackOrigin && rollbackRect && rollbackSurface) {
+ rollbackSurface.ctx.putImageData(
+ rollbackBefore,
+ rollbackRect.x - rollbackOrigin.x,
+ rollbackRect.y - rollbackOrigin.y
+ );
adjustedSurfaceCache.delete(layerId);
scheduler.invalidate({ layers: [layerId] });
}