From 02eda875aaad285821cbe682e5e414cfa250429f Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Fri, 26 Jan 2024 15:04:04 -0800 Subject: [PATCH] Fix resize test for offscreen canvas OffscreenCanvas has different rules than HTMLCanvasElement HTMLCanvasElement can have width or height set to negative numbers and will then set the canvas width or height to its default, 300 for width, 150 for height OffscreenCanvas will throw an error with a negative width or height. For OffscreenCanvas, all 3 major browsers do not reset the canvas if width or height are set to the same value they already are For HTMLCanvasElement, Beacuse there are issues with requestAnimationFrame, ResizeObserver, and compositing the page, it seems like, at least for HTMLCanvasElement, all 3 APIs should behave the same. --- .../canvas/getCurrentTexture.spec.ts | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/webgpu/web_platform/canvas/getCurrentTexture.spec.ts b/src/webgpu/web_platform/canvas/getCurrentTexture.spec.ts index 221f3c08fc72..330dbcc11a5a 100644 --- a/src/webgpu/web_platform/canvas/getCurrentTexture.spec.ts +++ b/src/webgpu/web_platform/canvas/getCurrentTexture.spec.ts @@ -293,28 +293,31 @@ g.test('resize') exp: { R: 0, G: 0, B: 0, A: 0 }, }); - // Ensure canvas goes back to defaults when set to negative numbers. - ctx.canvas.width = -1; - currentTexture = ctx.getCurrentTexture(); - t.expect(currentTexture.width === 300); - t.expect(currentTexture.height === 4); - - ctx.canvas.height = -1; - currentTexture = ctx.getCurrentTexture(); - t.expect(currentTexture.width === 300); - t.expect(currentTexture.height === 150); - prevTexture = currentTexture; - - // Setting the canvas width and height values to their current values should - // still trigger a change in the texture. - const { width, height } = ctx.canvas; - ctx.canvas.width = width; - ctx.canvas.height = height; - - t.expectTextureDestroyed(prevTexture); - - currentTexture = ctx.getCurrentTexture(); - t.expect(prevTexture !== currentTexture); + // HTMLCanvasElement behaves differently than OffscreenCanvas + if (t.params.canvasType === 'onscreen') { + // Ensure canvas goes back to defaults when set to negative numbers. + ctx.canvas.width = -1; + currentTexture = ctx.getCurrentTexture(); + t.expect(currentTexture.width === 300); + t.expect(currentTexture.height === 4); + + ctx.canvas.height = -1; + currentTexture = ctx.getCurrentTexture(); + t.expect(currentTexture.width === 300); + t.expect(currentTexture.height === 150); + + // Setting the canvas width and height values to their current values should + // still trigger a change in the texture. + prevTexture = ctx.getCurrentTexture(); + const { width, height } = ctx.canvas; + ctx.canvas.width = width; + ctx.canvas.height = height; + + t.expectTextureDestroyed(prevTexture); + + currentTexture = ctx.getCurrentTexture(); + t.expect(prevTexture !== currentTexture); + } }); g.test('expiry')