Skip to content

Commit

Permalink
Fix resize test for offscreen canvas
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greggman committed Jan 27, 2024
1 parent fba0b4b commit ce757c8
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/webgpu/web_platform/canvas/getCurrentTexture.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit ce757c8

Please sign in to comment.