Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ export abstract class CanvasEntityAdapterBase<T extends CanvasEntityState, U ext
this.renderer.syncKonvaCache();
};

Comment thread
lstein marked this conversation as resolved.
invalidateRasterCache = () => {
this.renderer.invalidateRasterCache();
};

/**
* Synchronizes the entity's locked state with the canvas.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export class CanvasEntityObjectRenderer extends CanvasModuleBase {
*/
renderers = new SyncableMap<string, AnyObjectRenderer>();

/**
* Tracks the cache keys used when rasterizing this entity so they can be invalidated on demand.
*/
rasterCacheKeys = new Set<string>();

/**
* A object containing singleton Konva nodes.
*/
Expand Down Expand Up @@ -476,7 +481,7 @@ export class CanvasEntityObjectRenderer extends CanvasModuleBase {
}): Promise<ImageDTO> => {
const rasterizingAdapter = this.manager.stateApi.$rasterizingAdapter.get();
if (rasterizingAdapter) {
assert(false, `Already rasterizing an entity: ${rasterizingAdapter.id}`);
await this.manager.stateApi.waitForRasterizationToFinish();
}
Comment thread
lstein marked this conversation as resolved.

const { rect, replaceObjects, attrs, bg, ignoreCache } = {
Expand All @@ -491,6 +496,7 @@ export class CanvasEntityObjectRenderer extends CanvasModuleBase {
const cachedImageName = this.manager.cache.imageNameCache.get(hash);

if (cachedImageName && !ignoreCache) {
this.rasterCacheKeys.add(hash);
imageDTO = await getImageDTOSafe(cachedImageName);
if (imageDTO) {
Comment thread
lstein marked this conversation as resolved.
Outdated
this.log.trace({ rect, cachedImageName, imageDTO }, 'Using cached rasterized image');
Expand Down Expand Up @@ -524,6 +530,7 @@ export class CanvasEntityObjectRenderer extends CanvasModuleBase {
replaceObjects,
});
this.manager.cache.imageNameCache.set(hash, imageDTO.image_name);
this.rasterCacheKeys.add(hash);
return imageDTO;
} catch (error) {
this.log.error({ rasterizeArgs, error: serializeError(error as Error) }, 'Failed to rasterize entity');
Expand All @@ -533,6 +540,16 @@ export class CanvasEntityObjectRenderer extends CanvasModuleBase {
}
};

Comment thread
lstein marked this conversation as resolved.
invalidateRasterCache = () => {
if (this.rasterCacheKeys.size === 0) {
return;
}
for (const key of this.rasterCacheKeys) {
this.manager.cache.imageNameCache.delete(key);
}
this.rasterCacheKeys.clear();
};

cloneObjectGroup = (arg: { attrs?: GroupConfig } = {}): Konva.Group => {
const { attrs } = arg;
const clone = this.konva.objectGroup.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ export class CanvasManager extends CanvasModuleBase {
];
};

Comment thread
lstein marked this conversation as resolved.
invalidateRegionalGuidanceRasterCache = () => {
for (const adapter of this.adapters.regionMasks.values()) {
adapter.invalidateRasterCache();
}
};

createAdapter = (entityIdentifier: CanvasEntityIdentifier): CanvasEntityAdapter => {
if (isRasterLayerEntityIdentifier(entityIdentifier)) {
const adapter = new CanvasEntityAdapterRasterLayer(entityIdentifier, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,29 @@ export class CanvasStateApiModule extends CanvasModuleBase {
*/
setGenerationBbox = (rect: Rect) => {
this.store.dispatch(bboxChangedFromCanvas(rect));
this.manager.invalidateRegionalGuidanceRasterCache();
};

Comment thread
lstein marked this conversation as resolved.
waitForRasterizationToFinish = async () => {
if (!this.$rasterizingAdapter.get()) {
return;
}

await new Promise<void>((resolve) => {
// Re-check before subscribing to avoid a race where rasterization completes
// between the outer check and listener registration.
if (!this.$rasterizingAdapter.get()) {
resolve();
return;
}

const unsubscribe = this.$rasterizingAdapter.listen((adapter) => {
if (!adapter) {
unsubscribe();
resolve();
}
});
Comment thread
lstein marked this conversation as resolved.
});
};

/**
Expand Down