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(webv2): render floating selections through the layer's display effects

joshistoast355dfcfe24d2
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.test.ts
index 1c9e9f3531..bd85ab9b3a 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.test.ts
@@ -162,6 +162,63 @@ describe('FloatingSelectionController: lift', () => {
});
});
+describe('FloatingSelectionController: display effects', () => {
+ const controlLayer = (withTransparencyEffect: boolean): CanvasLayerContract => ({
+ adapter: { beginEndStepPct: [0, 1], controlMode: 'balanced', kind: 'controlnet', model: null, weight: 1 },
+ blendMode: 'normal',
+ id: 'a',
+ isEnabled: true,
+ isLocked: false,
+ name: 'a',
+ opacity: 1,
+ source: { image: { height: 100, imageName: 'a', width: 100 }, type: 'image' },
+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
+ type: 'control',
+ withTransparencyEffect,
+ });
+
+ it('bakes a display copy for a control layer with the transparency effect', () => {
+ // Without it the float would show the control map's raw, opaque black
+ // background — the effect the layer itself renders away.
+ const h = createHarness({ layer: controlLayer(true) });
+ h.controller.lift('a');
+
+ const float = h.controller.get()!;
+ expect(float.display).not.toBeNull();
+ expect(float.display).not.toBe(float.pixels.surface);
+ // Same extent as the lifted pixels, so it blits at the same rect.
+ expect({ height: float.display!.height, width: float.display!.width }).toEqual({
+ height: float.pixels.rect.height,
+ width: float.pixels.rect.width,
+ });
+ });
+
+ it('leaves the display copy null when the layer has no effect', () => {
+ const h = createHarness({ layer: controlLayer(false) });
+ h.controller.lift('a');
+ expect(h.controller.get()!.display).toBeNull();
+ });
+
+ it('bakes back the RAW pixels, never the display copy', () => {
+ // The effect is display-only; burning it into the document would darken the
+ // layer a little more on every move.
+ const h = createHarness({ layer: controlLayer(true) });
+ h.controller.lift('a');
+ const float = h.controller.get()!;
+ const raw = float.pixels.surface;
+
+ move(h.controller, 40, 0);
+ h.controller.commit();
+
+ const entry = h.layers.get('a')!;
+ const drawn = (entry.surface as unknown as { callLog: { op: string; args: unknown[] }[] }).callLog.filter(
+ (call) => call.op === 'drawImage'
+ );
+ expect(drawn.some((call) => call.args[0] === raw.canvas)).toBe(true);
+ expect(drawn.some((call) => call.args[0] === float.display!.canvas)).toBe(false);
+ });
+});
+
describe('FloatingSelectionController: commit', () => {
it('pushes exactly one undoable entry for the whole move, however many drags', () => {
const h = createHarness();
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.ts
index 7c26a2f1cf..f22f6a3505 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/floatingSelectionController.ts
@@ -11,6 +11,7 @@ import type { Rect } from '@workbench/canvas-engine/types';
import { isLayerPixelEditEligible } from '@workbench/canvas-engine/editing/controlPixelEdit';
import { createImagePatchEntry } from '@workbench/canvas-engine/history/imagePatch';
import { isEmpty, roundOut, transformBounds, union } from '@workbench/canvas-engine/math/rect';
+import { renderLayerDisplayEffect } from '@workbench/canvas-engine/render/layerDisplayEffect';
import {
floatDocumentMatrix,
liftSelectedPixels,
@@ -132,6 +133,10 @@ export class FloatingSelectionController {
this.float = {
before: { data: before, rect: region },
+ // Baked now, not per frame: floating a control map must show the same
+ // darkness-dropped-out pixels the layer itself shows, never its raw
+ // opaque background.
+ display: renderLayerDisplayEffect(this.deps.backend, layer, lifted.pixels.surface),
layerId,
mask: { rect: mask.rect, surface: mask.surface },
pixels: lifted.pixels,
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
index 02f9f913bd..2611a17ddd 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.ts
@@ -1007,7 +1007,9 @@ export const createCanvasEngine = (opts: CanvasEngineOptions): CanvasEngineCoreC
layerId: float.layerId,
matrix,
rect: float.pixels.rect,
- surface: float.pixels.surface,
+ // The display copy when the layer has an effect; the raw pixels
+ // otherwise. Only the bake ever touches `pixels`.
+ surface: float.display ?? float.pixels.surface,
},
}
: null;
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/layerDisplayEffect.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/layerDisplayEffect.test.ts
new file mode 100644
index 0000000000..b24221e132
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/layerDisplayEffect.test.ts
@@ -0,0 +1,110 @@
+import type { CanvasAdjustmentsContract, CanvasLayerContract } from '@workbench/canvas-engine/contracts';
+
+import { describe, expect, it } from 'vitest';
+
+import { hasLayerDisplayEffect, renderLayerDisplayEffect } from './layerDisplayEffect';
+import { createTestStubRasterBackend } from './raster.testStub';
+
+const backend = createTestStubRasterBackend();
+
+const controlLayer = (withTransparencyEffect: boolean): CanvasLayerContract => ({
+ adapter: { beginEndStepPct: [0, 1], controlMode: 'balanced', kind: 'controlnet', model: null, weight: 1 },
+ blendMode: 'normal',
+ id: 'c',
+ isEnabled: true,
+ isLocked: false,
+ name: 'c',
+ opacity: 1,
+ source: { image: { height: 10, imageName: 'c', width: 10 }, type: 'image' },
+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
+ type: 'control',
+ withTransparencyEffect,
+});
+
+const rasterLayer = (adjustments?: CanvasAdjustmentsContract) =>
+ ({
+ adjustments,
+ blendMode: 'normal',
+ id: 'r',
+ isEnabled: true,
+ isLocked: false,
+ name: 'r',
+ opacity: 1,
+ source: { bitmap: null, offset: { x: 0, y: 0 }, type: 'paint' },
+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
+ type: 'raster',
+ }) as CanvasLayerContract;
+
+const maskLayer = (): CanvasLayerContract => ({
+ blendMode: 'normal',
+ id: 'm',
+ isEnabled: true,
+ isLocked: false,
+ mask: { bitmap: null, fill: { color: '#f00', style: 'solid' } },
+ name: 'm',
+ opacity: 1,
+ transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
+ type: 'inpaint_mask',
+});
+
+describe('hasLayerDisplayEffect', () => {
+ it('is true for a control layer with the transparency effect', () => {
+ expect(hasLayerDisplayEffect(controlLayer(true))).toBe(true);
+ });
+
+ it('is false for a control layer without it', () => {
+ expect(hasLayerDisplayEffect(controlLayer(false))).toBe(false);
+ });
+
+ it('is true for a raster layer with non-identity adjustments', () => {
+ expect(hasLayerDisplayEffect(rasterLayer({ brightness: 0.4, contrast: 0, saturation: 0 }))).toBe(true);
+ });
+
+ it('is false for a raster layer with identity or absent adjustments', () => {
+ expect(hasLayerDisplayEffect(rasterLayer({ brightness: 0, contrast: 0, saturation: 0 }))).toBe(false);
+ expect(hasLayerDisplayEffect(rasterLayer(undefined))).toBe(false);
+ });
+
+ it('is false for mask layers — their fill is applied by the compositor, not per-pixel here', () => {
+ expect(hasLayerDisplayEffect(maskLayer())).toBe(false);
+ });
+});
+
+describe('renderLayerDisplayEffect', () => {
+ it('returns null when the layer has no display effect, allocating nothing', () => {
+ const source = backend.createSurface(4, 4);
+ expect(renderLayerDisplayEffect(backend, controlLayer(false), source)).toBeNull();
+ });
+
+ it('returns a same-sized copy for a control layer with the transparency effect', () => {
+ const source = backend.createSurface(6, 5);
+ const out = renderLayerDisplayEffect(backend, controlLayer(true), source);
+
+ expect(out).not.toBeNull();
+ expect(out).not.toBe(source);
+ expect({ height: out!.height, width: out!.width }).toEqual({ height: 5, width: 6 });
+ });
+
+ it('reads the source and writes the transformed pixels back', () => {
+ const source = backend.createSurface(6, 5);
+ const out = renderLayerDisplayEffect(backend, controlLayer(true), source)!;
+
+ const log = (out as unknown as { callLog: { op: string; args: unknown[] }[] }).callLog;
+ expect(log.some((entry) => entry.op === 'drawImage' && entry.args[0] === source.canvas)).toBe(true);
+ expect(log.some((entry) => entry.op === 'getImageData')).toBe(true);
+ expect(log.some((entry) => entry.op === 'putImageData')).toBe(true);
+ });
+
+ it('returns null for a zero-sized source', () => {
+ expect(renderLayerDisplayEffect(backend, controlLayer(true), backend.createSurface(0, 0))).toBeNull();
+ });
+
+ it('returns a copy for a raster layer with adjustments', () => {
+ const out = renderLayerDisplayEffect(
+ backend,
+ rasterLayer({ brightness: 0.4, contrast: 0, saturation: 0 }),
+ backend.createSurface(4, 4)
+ );
+ expect(out).not.toBeNull();
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/layerDisplayEffect.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/layerDisplayEffect.ts
new file mode 100644
index 0000000000..5e2726fce4
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/layerDisplayEffect.ts
@@ -0,0 +1,67 @@
+/**
+ * The display-only effects a layer's pixels pass through on the way to screen:
+ * raster **adjustments** (brightness / contrast / saturation / curves) and the
+ * control-layer **transparency effect** (lightness→alpha, so a dark control map
+ * drops its background out).
+ *
+ * The compositor applies these to a layer's cache every frame through memoizing
+ * caches. This module exists for the one case that has pixels of its own outside
+ * that cache: a floating selection. Its pixels were cut from the layer, so they
+ * must render exactly as the layer's own pixels do — otherwise floating a region
+ * of a control map shows the raw, opaque black background the effect normally
+ * removes.
+ *
+ * Both effects are strictly PER-PIXEL, so applying one to a cut-out region gives
+ * exactly the same result as applying it to the whole layer and then cutting.
+ * That is what lets the float bake its display copy ONCE at lift time rather
+ * than re-deriving it every frame: nothing about its transform can change the
+ * outcome, and a change to the layer's display properties cancels the float.
+ *
+ * Zero React, zero import-time side effects.
+ */
+
+import type { CanvasLayerContract } from '@workbench/canvas-engine/contracts';
+
+import { applyAdjustments, isIdentityAdjustments } from '@workbench/canvas-engine/render/adjustments';
+import { applyLightnessToAlpha } from '@workbench/canvas-engine/render/controlTransparency';
+
+import type { RasterBackend, RasterSurface } from './raster';
+
+/** Whether `layer` renders its pixels through any display-only transform. */
+export const hasLayerDisplayEffect = (layer: CanvasLayerContract): boolean => {
+ if (layer.type === 'control') {
+ return layer.withTransparencyEffect;
+ }
+ return layer.type === 'raster' && !isIdentityAdjustments(layer.adjustments);
+};
+
+/**
+ * A copy of `source` with `layer`'s display effects baked in, or `null` when the
+ * layer has none — the caller then draws `source` directly, which is the common
+ * case and allocates nothing.
+ */
+export const renderLayerDisplayEffect = (
+ backend: RasterBackend,
+ layer: CanvasLayerContract,
+ source: RasterSurface
+): RasterSurface | null => {
+ if (!hasLayerDisplayEffect(layer) || source.width <= 0 || source.height <= 0) {
+ return null;
+ }
+ const out = backend.createSurface(source.width, source.height);
+ const ctx = out.ctx;
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ ctx.globalAlpha = 1;
+ ctx.globalCompositeOperation = 'source-over';
+ ctx.clearRect(0, 0, source.width, source.height);
+ ctx.drawImage(source.canvas, 0, 0);
+
+ const imageData = ctx.getImageData(0, 0, source.width, source.height);
+ if (layer.type === 'raster') {
+ applyAdjustments(imageData, layer.adjustments);
+ } else {
+ applyLightnessToAlpha(imageData.data);
+ }
+ ctx.putImageData(imageData, 0, 0);
+ return out;
+};
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/floatingSelection.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/floatingSelection.ts
index f42923f9d0..6f72d62a52 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/floatingSelection.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/selection/floatingSelection.ts
@@ -29,7 +29,7 @@
* Zero React, zero import-time side effects.
*/
-import type { RasterBackend } from '@workbench/canvas-engine/render/raster';
+import type { RasterBackend, RasterSurface } from '@workbench/canvas-engine/render/raster';
import type { LayerTransform } from '@workbench/canvas-engine/transform/transformMath';
import type { Mat2d, PlacedSurface, Rect, Vec2 } from '@workbench/canvas-engine/types';
@@ -42,6 +42,15 @@ export interface FloatingSelection {
readonly layerId: string;
/** The lifted pixels, placed at their lift-time rect in LAYER-LOCAL space. */
readonly pixels: PlacedSurface;
+ /**
+ * `pixels` with the layer's display-only effects baked in (control
+ * transparency / raster adjustments), or `null` when the layer has none. The
+ * compositor draws THIS; the bake writes back the untouched `pixels`, so the
+ * effect is never burned into the document. Computed once at lift: the effects
+ * are per-pixel, so the float's transform cannot change them, and a change to
+ * the layer's display properties cancels the float outright.
+ */
+ readonly display: RasterSurface | null;
/** The layer cache's pre-lift pixels over the cut region, for cancel and the undo entry. */
readonly before: { rect: Rect; data: ImageData };
/** The selection mask at lift time, in DOCUMENT space — the ants follow it through the float's transform. */