Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

// META: title=OffscreenCanvas getContext(): script that runs while options are converted
'use strict';
// Converting the options argument of getContext() to a dictionary can run script. If that script
// changes the context mode of the OffscreenCanvas, the outer getContext() call has to act on the
// new context mode rather than create a second rendering context.
const contextIds = ['2d', 'bitmaprenderer', 'webgl', 'webgl2'];
const interfaceNames = {
'2d': 'OffscreenCanvasRenderingContext2D',
'bitmaprenderer': 'ImageBitmapRenderingContext',
'webgl': 'WebGLRenderingContext',
'webgl2': 'WebGL2RenderingContext',
};
function loseWebGLContext(context) {
if (context && typeof context.getExtension === 'function') {
context.getExtension('WEBGL_lose_context')?.loseContext();
}
}
const webglSupport = {};
for (const contextId of ['webgl', 'webgl2']) {
const probeContext = new OffscreenCanvas(10, 10).getContext(contextId);
webglSupport[contextId] = probeContext !== null;
loseWebGLContext(probeContext);
}
function requireContextId(contextId) {
if (contextId in webglSupport) {
assert_implements_optional(webglSupport[contextId], `getContext("${contextId}") is supported`);
}
}
// Returns options with non-default values whose getters run callback the first time any of them is
// invoked, while counting how many times each getter is read. Different context types (and
// implementations) read different dictionary members, so multiple members are included.
function optionsWithGetter(callback) {
const state = {
called: false,
alphaCalls: 0,
colorSpaceCalls: 0,
willReadFrequentlyCalls: 0,
};
const run = value => {
if (!state.called) {
state.called = true;
callback();
}
return value;
};
const options = {
get alpha() {
state.alphaCalls++;
return run(false);
},
get colorSpace() {
state.colorSpaceCalls++;
return run('display-p3');
},
get willReadFrequently() {
state.willReadFrequentlyCalls++;
return run(true);
},
};
return { options, state };
}
function assertSingleConversion(state) {
assert_true(state.called, 'an options getter was invoked');
assert_less_than_equal(state.alphaCalls, 1, 'alpha getter is read at most once');
assert_less_than_equal(state.colorSpaceCalls, 1, 'colorSpace getter is read at most once');
assert_less_than_equal(state.willReadFrequentlyCalls, 1,
'willReadFrequently getter is read at most once');
}
function assertContextSettings(contextId, context, expectedNonDefault) {
if (contextId === 'webgl' || contextId === 'webgl2') {
assert_equals(context.getContextAttributes().alpha, !expectedNonDefault,
`${contextId} alpha matches the creating call's settings`);
} else if (contextId === '2d') {
assert_equals(context.getContextAttributes().willReadFrequently, expectedNonDefault,
`2d willReadFrequently matches the creating call's settings`);
}
}
for (const outerId of contextIds) {
for (const innerId of contextIds) {
test(t => {
requireContextId(innerId);
const canvas = new OffscreenCanvas(10, 10);
let innerContext;
const { options, state } = optionsWithGetter(() => {
innerContext = canvas.getContext(innerId);
});
t.add_cleanup(() => loseWebGLContext(innerContext));
const outerContext = canvas.getContext(outerId, options);
if (outerContext !== innerContext) {
t.add_cleanup(() => loseWebGLContext(outerContext));
}
assertSingleConversion(state);
assert_true(innerContext instanceof self[interfaceNames[innerId]],
`inner getContext("${innerId}") returns a ${interfaceNames[innerId]}`);
if (outerId === innerId) {
assert_equals(outerContext, innerContext,
'outer getContext() returns the context created by the inner call');
assertContextSettings(innerId, outerContext, false);
} else {
assert_equals(outerContext, null, 'outer getContext() returns null');
}
assert_equals(canvas.getContext(innerId), innerContext,
'later getContext() calls return the context created by the inner call');
}, `getContext("${outerId}") whose options getter calls getContext("${innerId}")`);
}
}
for (const contextId of contextIds) {
test(t => {
requireContextId(contextId);
const canvas = new OffscreenCanvas(10, 10);
const throwingError = new Error('inner getter');
const { options, state } = optionsWithGetter(() => {
assert_throws_dom('InvalidStateError', () => canvas.transferToImageBitmap(),
'transferToImageBitmap() throws while context mode is still none');
assert_throws_exactly(throwingError, () => canvas.getContext('2d', {
get alpha() { throw throwingError; },
get willReadFrequently() { throw throwingError; },
}));
});
const context = canvas.getContext(contextId, options);
t.add_cleanup(() => loseWebGLContext(context));
assertSingleConversion(state);
assert_true(context instanceof self[interfaceNames[contextId]],
`getContext("${contextId}") returns a ${interfaceNames[contextId]}`);
assertContextSettings(contextId, context, true);
}, `getContext("${contextId}") forwards converted options once when getter leaves context mode as none`);
}
test(() => {
const canvas = new OffscreenCanvas(10, 10);
let innerContext;
let middleContext;
const { options: middleOptions } = optionsWithGetter(() => {
innerContext = canvas.getContext('2d');
});
const { options: outerOptions } = optionsWithGetter(() => {
middleContext = canvas.getContext('bitmaprenderer', middleOptions);
});
const outerContext = canvas.getContext('2d', outerOptions);
assert_equals(middleContext, null, 'middle getContext("bitmaprenderer") returns null');
assert_true(innerContext instanceof OffscreenCanvasRenderingContext2D,
'inner getContext("2d") returns an OffscreenCanvasRenderingContext2D');
assert_equals(outerContext, innerContext,
'outer getContext("2d") returns the context created by the innermost call');
assertContextSettings('2d', outerContext, false);
}, 'Two-level nested re-entrant getContext() dispatches on the final context mode at each nesting level');
for (const contextId of contextIds) {
test(() => {
const canvas = new OffscreenCanvas(10, 10);
let transferredCanvas;
const { options, state } = optionsWithGetter(() => {
transferredCanvas = structuredClone(canvas, { transfer: [canvas] });
});
assert_throws_dom('InvalidStateError', () => canvas.getContext(contextId, options));
assertSingleConversion(state);
assert_throws_dom('InvalidStateError', () => canvas.getContext('2d'),
'OffscreenCanvas remains in detached context mode');
assert_true(transferredCanvas instanceof OffscreenCanvas,
'transferring the OffscreenCanvas succeeds');
assert_true(transferredCanvas.getContext('2d') instanceof OffscreenCanvasRenderingContext2D,
'transferred OffscreenCanvas can create a context');
}, `getContext("${contextId}") whose options getter transfers the OffscreenCanvas`);
}
for (const contextId of contextIds) {
test(() => {
const canvas = new OffscreenCanvas(10, 10);
const error = new Error('options getter');
const { options } = optionsWithGetter(() => {
throw error;
});
assert_throws_exactly(error, () => canvas.getContext(contextId, options));
const otherId = contextId === '2d' ? 'bitmaprenderer' : '2d';
assert_true(canvas.getContext(otherId) instanceof self[interfaceNames[otherId]],
`getContext("${otherId}") returns a ${interfaceNames[otherId]}`);
}, `getContext("${contextId}") whose options getter throws leaves the context mode as none`);
}
for (const [firstId, secondId] of [['webgl', 'webgl2'], ['webgl2', 'webgl']]) {
test(t => {
requireContextId(firstId);
const canvas = new OffscreenCanvas(10, 10);
const error = new Error('options getter');
const { options } = optionsWithGetter(() => {
throw error;
});
assert_throws_exactly(error, () => canvas.getContext(secondId, options));
const firstContext = canvas.getContext(firstId);
t.add_cleanup(() => loseWebGLContext(firstContext));
assert_true(firstContext instanceof self[interfaceNames[firstId]],
`getContext("${firstId}") returns a ${interfaceNames[firstId]}`);
assert_equals(canvas.getContext(secondId), null,
`getContext("${secondId}") returns null when context mode is ${firstId}`);
}, `getContext("${secondId}") returns null after getContext("${firstId}") and an earlier throwing getContext("${secondId}") call`);
}
for (const contextId of contextIds) {
test(t => {
requireContextId(contextId);
const canvas = new OffscreenCanvas(10, 10);
const existing = canvas.getContext(contextId);
t.add_cleanup(() => loseWebGLContext(existing));
const { options: sameOptions, state: sameState } = optionsWithGetter(() => {});
assert_equals(canvas.getContext(contextId, sameOptions), existing,
'returns the existing context after converting options');
assert_true(sameState.called, 'options getter was invoked for same contextId');
const otherId = contextId === '2d' ? 'bitmaprenderer' : '2d';
const { options: otherOptions, state: otherState } = optionsWithGetter(() => {});
assert_equals(canvas.getContext(otherId, otherOptions), null,
'returns null for another contextId after converting options');
assert_true(otherState.called, 'options getter was invoked for mismatched contextId');
}, `getContext() converts options even when context mode is already ${contextId}`);
test(() => {
const canvas = new OffscreenCanvas(10, 10);
structuredClone(canvas, { transfer: [canvas] });
const error = new Error('options getter');
const { options } = optionsWithGetter(() => {
throw error;
});
assert_throws_exactly(error, () => canvas.getContext(contextId, options));
}, `getContext("${contextId}") converts options before checking detached context mode`);
}
test(() => {
const canvas = new OffscreenCanvas(10, 10);
let innerContext;
const contextId = {
toString() {
innerContext = canvas.getContext('2d');
return '2d';
},
};
const { options, state } = optionsWithGetter(() => {});
assert_equals(canvas.getContext(contextId, options), innerContext);
assert_true(state.called, 'options getter was invoked');
}, 'getContext() converts options even when contextId toString() changes context mode');