Skip to content

Commit 62d03ff

Browse files
Merge pull request #355 from sandeepmistry/offscreen-canvas
Add initial support for OffcreenCanvas
2 parents a55ad11 + aad86a8 commit 62d03ff

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

src/core/utils-core.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,23 @@ class UtilsCore {
3535
* @memberOf UtilsCore
3636
*
3737
*
38-
* @desc Return TRUE, on a valid DOM canvas object
38+
* @desc Return TRUE, on a valid DOM canvas or OffscreenCanvas object
3939
*
4040
* Note: This does just a VERY simply sanity check. And may give false positives.
4141
*
4242
* @param {CanvasDOMObject} canvasObj - Object to validate
4343
*
44-
* @returns {Boolean} TRUE if the object is a DOM canvas
44+
* @returns {Boolean} TRUE if the object is a DOM canvas or OffscreenCanvas
4545
*
4646
*/
4747
static isCanvas(canvasObj) {
4848
return (
4949
canvasObj !== null &&
50-
canvasObj.nodeName &&
51-
canvasObj.getContext &&
52-
canvasObj.nodeName.toUpperCase() === 'CANVAS'
50+
((canvasObj.nodeName &&
51+
canvasObj.getContext &&
52+
canvasObj.nodeName.toUpperCase() === 'CANVAS') ||
53+
(typeof OffscreenCanvas !== 'undefined' &&
54+
canvasObj instanceof OffscreenCanvas))
5355
);
5456
}
5557

@@ -87,7 +89,7 @@ class UtilsCore {
8789
}
8890

8991
// Create a new canvas DOM
90-
const canvas = document.createElement('canvas');
92+
const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(0, 0);
9193

9294
// Default width and height, to fix webgl issue in safari
9395
canvas.width = 2;
@@ -217,10 +219,21 @@ class UtilsCore {
217219
}
218220

219221
// Create a new canvas DOM
220-
const webGl = (
221-
canvasObj.getContext('experimental-webgl', UtilsCore.initWebGlDefaultOptions()) ||
222-
canvasObj.getContext('webgl', UtilsCore.initWebGlDefaultOptions())
223-
);
222+
let webGl = null;
223+
224+
try {
225+
webGl = canvasObj.getContext('experimental-webgl', UtilsCore.initWebGlDefaultOptions());
226+
} catch (e) {
227+
// 'experimental-webgl' is not a supported context type
228+
// fallback to 'webgl2' or 'webgl' below
229+
}
230+
231+
if (webGl === null) {
232+
webGl = (
233+
canvasObj.getContext('webgl2', UtilsCore.initWebGlDefaultOptions()) ||
234+
canvasObj.getContext('webgl', UtilsCore.initWebGlDefaultOptions())
235+
);
236+
}
224237

225238
if (webGl) {
226239
// Get the extension that is needed
@@ -287,7 +300,7 @@ class UtilsCore {
287300
//
288301
//-----------------------------------------------------------------------------
289302

290-
const _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : false;
303+
const _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : typeof OffscreenCanvas !== 'undefined';
291304
const _testingWebGl = UtilsCore.initWebGl(UtilsCore.initCanvas());
292305
const _testingWebGl2 = UtilsCore.initWebGl2(UtilsCore.initCanvas());
293306
const _isWebGlSupported = UtilsCore.isWebGl(_testingWebGl);

test/all.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
<script src="features/read-color-texture.js"></script>
4444
<script src="features/read-from-texture.js"></script>
4545
<script src="features/sum-ab.js"></script>
46+
<script src="features/float-output.js"></script>
47+
<script src="features/offscreen-canvas.js"></script>
4648

4749
<!-- internal -->
4850
<script src="internal/context-inheritance.js"></script>

test/features/offscreen-canvas.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(function() {
2+
if (typeof(document) === 'undefined') {
3+
// inside Worker
4+
window = {};
5+
importScripts('../../bin/gpu.js');
6+
7+
onmessage = function(e) {
8+
const gpu = new window.GPU();
9+
10+
postMessage(gpu.getMode());
11+
};
12+
13+
return;
14+
}
15+
16+
// skip test if browser doesn't support Workers or OffscreenCanvas
17+
var test = (typeof(Worker) === 'undefined') || (typeof(OffscreenCanvas) === 'undefined') ?
18+
QUnit.skip : QUnit.test;
19+
20+
test( 'OffscreenCanvas used in Worker', function(assert) {
21+
var worker = new Worker('features/offscreen-canvas.js');
22+
var done = assert.async();
23+
24+
worker.onmessage = function(e) {
25+
var mode = e.data;
26+
27+
assert.equal( mode, 'gpu', 'GPU mode used in Worker' );
28+
29+
done();
30+
};
31+
32+
worker.postMessage('test');
33+
});
34+
35+
})();

0 commit comments

Comments
 (0)