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.

perf(canvas): blit replacement selection masks instead of copying RGBA

Josh Corbettb38f70d8b8d2
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.browser.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.browser.test.ts
new file mode 100644
index 0000000000..8b77842b92
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.browser.test.ts
@@ -0,0 +1,27 @@
+import { createDomRasterBackend } from '@workbench/canvas-engine/render/raster';
+import { createSelectionState } from '@workbench/canvas-engine/selection/selectionState';
+import { expect, it } from 'vitest';
+
+it('replaceMask keeps exact partial coverage, isolated from later edits to the source surface', () => {
+ const backend = createDomRasterBackend();
+ const selection = createSelectionState({
+ backend,
+ createPath2D: (d) => new Path2D(d),
+ getDocumentSize: () => ({ height: 100, width: 100 }),
+ onChange: () => undefined,
+ });
+ const source = backend.createSurface(2, 2);
+ const pixels = source.ctx.createImageData(2, 2);
+ [0, 64, 255, 0].forEach((alpha, index) => {
+ pixels.data.set([255, 255, 255, alpha], index * 4);
+ });
+ source.ctx.putImageData(pixels, 0, 0);
+
+ selection.replaceMask({ rect: { height: 2, width: 2, x: 7, y: -3 }, surface: source });
+ source.ctx.clearRect(0, 0, 2, 2);
+
+ const mask = selection.mask()!;
+ expect(mask.rect).toEqual({ height: 2, width: 2, x: 7, y: -3 });
+ const alpha = [...mask.surface.ctx.getImageData(0, 0, 2, 2).data].filter((_, index) => index % 4 === 3);
+ expect(alpha).toEqual([0, 64, 255, 0]);
+});
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.test.ts
index 3819ea4dec..11d18b7da0 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.test.ts
@@ -242,14 +242,12 @@ describe('selectionState: replaceMask', () => {
// rather than the replacement's bounding rect.
expect(selection.antsPaths()).toEqual([{ d: 'M 7 -2 L 8 -2 L 8 -1 L 7 -1 Z' } as unknown as Path2D]);
- const put = maskLog(selection).find((entry) => entry.op === 'putImageData');
- const copied = put?.args[0] as ImageData | undefined;
- expect(copied).toBeDefined();
- expect(copied).not.toBe(source.pixels);
- expect([...copied!.data]).toEqual([...source.pixels.data]);
-
- source.pixels.data.fill(0);
- expect(copied!.data.filter((_, index) => index % 4 === 3)).toEqual(new Uint8ClampedArray([0, 64, 255, 0]));
+ // The mask owns a blitted copy; no RGBA readback is written back into it.
+ const log = maskLog(selection);
+ expect(log.filter((entry) => entry.op === 'drawImage').map((entry) => entry.args)).toEqual([
+ [source.placed.surface.canvas, 0, 0],
+ ]);
+ expect(log.some((entry) => entry.op === 'putImageData')).toBe(false);
expect(onChange).toHaveBeenCalledOnce();
});
@@ -300,7 +298,7 @@ describe('selectionState: replaceMask', () => {
const createSurface = backend.createSurface;
vi.spyOn(backend, 'createSurface').mockImplementation((width, height) => {
const surface = createSurface(width, height);
- Object.defineProperty(surface.ctx, 'putImageData', {
+ Object.defineProperty(surface.ctx, 'drawImage', {
value: () => {
throw new Error('selection pixel staging failed');
},
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.ts
index 04dcccd58c..91fff90a59 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/selectionState.ts
@@ -242,19 +242,15 @@ export const createSelectionState = (deps: SelectionStateDeps): SelectionState =
return;
}
- const copiedData = new Uint8ClampedArray(source.data);
- const copied = createImageData(copiedData, source.width, source.height);
// Prepare every fallible replacement artifact before publishing any state.
- // If allocation, pixel upload, or Path2D construction fails, the exact prior
+ // If allocation, the blit, or Path2D construction fails, the exact prior
// selection remains authoritative and the engine may safely report failure.
const nextMask = backend.createSurface(rect.width, rect.height);
- nextMask.ctx.putImageData(copied, 0, 0);
+ nextMask.ctx.drawImage(next.surface.canvas, 0, 0);
// Trace the mask's true edge for the ants; a mask whose alpha never reaches
// the solid threshold still selected something (hasAlpha above), so fall
// back to tracing any non-zero coverage rather than showing no outline.
- const alphaSource = { data: copiedData, height: source.height, width: source.width };
- const outline =
- traceMaskOutlinePath(alphaSource, rect) || traceMaskOutlinePath(alphaSource, rect, 1) || rectPathData(rect);
+ const outline = traceMaskOutlinePath(source, rect) || traceMaskOutlinePath(source, rect, 1) || rectPathData(rect);
const nextPath = createPath2D(outline);
mask = nextMask;