Skip to content

Commit

Permalink
fix(editor): turbo renderer stale frame lag on zooming (#10376)
Browse files Browse the repository at this point in the history
Before:

https://github.com/user-attachments/assets/593e91a3-042e-4619-93a0-dca21fa942aa

After:

https://github.com/user-attachments/assets/779d7582-f7b2-4135-a97a-d1f65c7cb467

This is only a bug fix that ensures correct baseline rendering result. There'll be more optimizations specific for zooming TBD.
  • Loading branch information
doodlewind committed Feb 24, 2025
1 parent 5fe4b2b commit 67889d9
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions blocksuite/affine/shared/src/viewport-renderer/viewport-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
);
});

const debounceOptions = { leading: false, trailing: true };
const debouncedRefresh = debounce(
() => {
this.refresh().catch(console.error);
},
1000, // During this period, fallback to DOM
debounceOptions
{ leading: false, trailing: true }
);
this.disposables.add(
this.std.store.slots.blockUpdated.on(() => {
Expand All @@ -97,11 +96,12 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
return this.std.get(GfxControllerIdentifier).viewport;
}

async refresh(force = false) {
if (this.state === 'paused' && !force) return;
async refresh() {
if (this.state === 'paused') return;

this.clearCanvas();
if (this.viewport.zoom > zoomThreshold) {
this.clearCanvas();
return;
} else if (this.canUseBitmapCache()) {
this.drawCachedBitmap(this.layoutCache!);
} else {
Expand All @@ -115,20 +115,16 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
}

invalidate() {
this.clearCache();
this.clearCanvas();
this.layoutCache = null;
this.clearTile();
this.clearCanvas(); // Should clear immediately after content updates
}

private updateLayoutCache() {
const layout = getViewportLayout(this.std.host, this.viewport);
this.layoutCache = layout;
}

private clearCache() {
this.layoutCache = null;
this.clearTile();
}

private clearTile() {
if (this.tile) {
this.tile.bitmap.close();
Expand All @@ -154,25 +150,20 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {

this.worker.onmessage = (e: MessageEvent) => {
if (e.data.type === 'bitmapPainted') {
this.handlePaintedBitmap(e.data.bitmap, layout, resolve);
this.handlePaintedBitmap(e.data.bitmap, resolve);
}
};
});
}

private handlePaintedBitmap(
bitmap: ImageBitmap,
layout: ViewportLayout,
resolve: () => void
) {
private handlePaintedBitmap(bitmap: ImageBitmap, resolve: () => void) {
if (this.tile) {
this.tile.bitmap.close();
}
this.tile = {
bitmap,
zoom: this.viewport.zoom,
};
this.drawCachedBitmap(layout);
resolve();
}

Expand Down

0 comments on commit 67889d9

Please sign in to comment.