Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
perf(engine-render): reuse shape cache during transforms (#7508)
Univer09a077211db0
diff --git a/packages/engine-render/src/shape/__tests__/shape-cache.spec.ts b/packages/engine-render/src/shape/__tests__/shape-cache.spec.tsindex 921e51242d..9eb5726bfd 100644--- a/packages/engine-render/src/shape/__tests__/shape-cache.spec.ts+++ b/packages/engine-render/src/shape/__tests__/shape-cache.spec.ts@@ -52,4 +52,27 @@ describe('Shape render cache', () => {shape.dispose();mainCanvas.dispose();});++ it('keeps the cached bitmap across drag translation but invalidates it for resize', () => {+ const mainCanvas = new Canvas({ width: 200, height: 100, pixelRatio: 1 });+ const shape = new CachedShape('translated-cached-shape', { width: 100, height: 50 });+ const context = mainCanvas.getContext();+ const bounds = { left: -10, top: -10, right: 110, bottom: 60 };++ shape.drawCached(context, bounds);+ shape.translate(24, 18);+ shape.drawCached(context, bounds);+ expect(shape.drawCount).toBe(1);++ shape.transformByState({ left: 30, top: 20, width: 100, height: 50 });+ shape.drawCached(context, bounds);+ expect(shape.drawCount).toBe(1);++ shape.transformByState({ width: 120 });+ shape.drawCached(context, bounds);+ expect(shape.drawCount).toBe(2);++ shape.dispose();+ mainCanvas.dispose();+ });});diff --git a/packages/engine-render/src/shape/shape.ts b/packages/engine-render/src/shape/shape.tsindex 9cf692a785..f9059783e3 100644--- a/packages/engine-render/src/shape/shape.ts+++ b/packages/engine-render/src/shape/shape.ts@@ -102,6 +102,7 @@ export abstract class Shape<T extends IShapeProps> extends BaseObject {private _renderCacheCanvas: Nullable<Canvas>;private _renderCacheBounds: Nullable<IBoundRectNoAngle>;private _renderCachePixelRatio = 0;+ private _renderCacheTransformSize: Nullable<ISize>;private _hoverCursor: Nullable<string>;@@ -402,6 +403,45 @@ export abstract class Shape<T extends IShapeProps> extends BaseObject {};}+ override transformByState(option: IObjectFullState): this | undefined {+ const previousWidth = this.width;+ const previousHeight = this.height;+ const cachedSize = this._renderCacheTransformSize;+ const hasReusableCache = Boolean(+ this._renderCacheCanvas &&+ this._renderCacheBounds &&+ cachedSize?.width === this.width &&+ cachedSize.height === this.height+ );++ const result = super.transformByState(option);+ const geometryChanged = previousWidth !== this.width || previousHeight !== this.height;+ if (hasReusableCache && !geometryChanged) {+ // Translation and rotation only change the outer transform; the cached local pixels+ // remain valid. Keep filters and 3D effects out of the per-frame drag path.+ this.makeDirty(false);+ } else if (geometryChanged) {+ this._releaseRenderCache();+ }+ return result;+ }++ override translate(x?: number | string, y?: number | string): this {+ const cachedSize = this._renderCacheTransformSize;+ const hasReusableCache = Boolean(+ this._renderCacheCanvas &&+ this._renderCacheBounds &&+ cachedSize?.width === this.width &&+ cachedSize.height === this.height+ );++ super.translate(x, y);+ if (hasReusableCache) {+ this.makeDirty(false);+ }+ return this;+ }+protected _renderWithCache(ctx: UniverRenderingContext,bounds: IBoundRectNoAngle,@@ -449,6 +489,7 @@ export abstract class Shape<T extends IShapeProps> extends BaseObject {cacheContext.restore();this._renderCacheBounds = { ...bounds };this._renderCachePixelRatio = pixelRatio;+ this._renderCacheTransformSize = { width: this.width, height: this.height };}ctx.drawImage(@@ -465,6 +506,7 @@ export abstract class Shape<T extends IShapeProps> extends BaseObject {this._renderCacheCanvas = null;this._renderCacheBounds = null;this._renderCachePixelRatio = 0;+ this._renderCacheTransformSize = null;}protected _draw(ctx: UniverRenderingContext, bounds?: IViewportInfo) {