AI-native design editor. Open-source Figma alternative.
perf(canvas): gate draw call instrumentation
Danila Poyarkov22741a3dc459
diff --git a/packages/core/src/profiler/draw-call-counter.ts b/packages/core/src/profiler/draw-call-counter.tsindex 354d40bd4..6bd6a2340 100644--- a/packages/core/src/profiler/draw-call-counter.ts+++ b/packages/core/src/profiler/draw-call-counter.ts@@ -16,18 +16,32 @@ export class DrawCallCounter {constructor(gl: WebGL2RenderingContext | null) {this.gl = gl- if (!gl) return+ }++ enable(): void {+ const gl = this.gl+ if (!gl || this.originals.size > 0) returnfor (const method of DRAW_METHODS) {- const original = gl[method].bind(gl) as DrawFunction+ const original = gl[method] as DrawFunctionthis.originals.set(method, original);(gl[method] as DrawFunction) = (...args: unknown[]) => {this.count++- original(...args)+ original.apply(gl, args)}}}+ disable(): void {+ const gl = this.gl+ if (!gl || this.originals.size === 0) return++ for (const [method, fn] of this.originals) {+ ;(gl[method] as DrawFunction) = fn+ }+ this.originals.clear()+ }+reset(): number {const prev = this.countthis.count = 0@@ -35,12 +49,7 @@ export class DrawCallCounter {}destroy(): void {- const gl = this.gl- if (!gl) return-- for (const [method, fn] of this.originals) {- ;(gl[method] as DrawFunction) = fn- }- this.originals.clear()+ this.disable()+ this.gl = null}}diff --git a/packages/core/src/profiler/render-profiler.ts b/packages/core/src/profiler/render-profiler.tsindex 558c7a67c..d4e7ed16b 100644--- a/packages/core/src/profiler/render-profiler.ts+++ b/packages/core/src/profiler/render-profiler.ts@@ -37,6 +37,7 @@ export class RenderProfiler {this.hudVisible = !this.hudVisiblethis.enabled = this.hudVisiblethis.phases.enabled = this.enabled+ this.syncInstrumentation()}beginFrame(): void {@@ -83,6 +84,7 @@ export class RenderProfiler {beginCapture(): void {this.capturing = truethis.captureSession = createCaptureSession(now())+ this.syncInstrumentation()}endCapture(): FrameCapture | null {@@ -92,6 +94,7 @@ export class RenderProfiler {const capture = createFrameCapture(this.captureSession, this.stats, this.gpuTimer, now)this.lastCapture = capturethis.captureSession = null+ this.syncInstrumentation()return capture}@@ -123,6 +126,11 @@ export class RenderProfiler {URL.revokeObjectURL(url)}+ private syncInstrumentation(): void {+ if (this.enabled || this.capturing) this.drawCallCounter.enable()+ else this.drawCallCounter.disable()+ }+setTypeface(typeface: Typeface): void {this.hud.setTypeface(typeface)}diff --git a/tests/engine/profiler.test.ts b/tests/engine/profiler.test.tsindex f197a14d4..ba12e4d65 100644--- a/tests/engine/profiler.test.ts+++ b/tests/engine/profiler.test.ts@@ -50,6 +50,34 @@ describe('DrawCallCounter', () => {expect(counter.reset()).toBe(0)counter.destroy()})++ it('wraps draw calls only while enabled', () => {+ let rawDrawCalls = 0+ const gl = {+ drawArrays: () => rawDrawCalls++,+ drawElements: () => rawDrawCalls++,+ drawArraysInstanced: () => rawDrawCalls++,+ drawElementsInstanced: () => rawDrawCalls+++ } as WebGL2RenderingContext+ const originalDrawArrays = gl.drawArrays++ const counter = new DrawCallCounter(gl)+ gl.drawArrays(0, 0, 0)+ expect(counter.count).toBe(0)+ expect(rawDrawCalls).toBe(1)++ counter.enable()+ expect(gl.drawArrays).not.toBe(originalDrawArrays)+ gl.drawArrays(0, 0, 0)+ expect(counter.count).toBe(1)+ expect(rawDrawCalls).toBe(2)++ counter.disable()+ expect(gl.drawArrays).toBe(originalDrawArrays)+ gl.drawArrays(0, 0, 0)+ expect(counter.count).toBe(1)+ expect(rawDrawCalls).toBe(3)+ })})describe('GPUTimer', () => {