Skip to content

Commit

Permalink
Merge pull request #402 from gpujs/401-cpu-canvas-check
Browse files Browse the repository at this point in the history
fix: cpu canvas check
  • Loading branch information
robertleeplummerjr authored Nov 18, 2018
2 parents e4dd9d5 + cb4d4f1 commit 3aba32e
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 28 deletions.
4 changes: 2 additions & 2 deletions bin/gpu-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 1.10.3
* @date Sun Nov 18 2018 14:38:47 GMT-0500 (EST)
* @version 1.10.4
* @date Sun Nov 18 2018 15:47:22 GMT-0500 (EST)
*
* @license MIT
* The MIT License
Expand Down
4 changes: 2 additions & 2 deletions bin/gpu-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions bin/gpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 1.10.3
* @date Sun Nov 18 2018 14:38:47 GMT-0500 (EST)
* @version 1.10.4
* @date Sun Nov 18 2018 15:47:22 GMT-0500 (EST)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -890,16 +890,22 @@ module.exports = function (_KernelBase) {
this.setupParams(arguments);
this.validateOptions();
var canvas = this._canvas;
this._canvasCtx = canvas.getContext('2d');
if (canvas) {
this._canvasCtx = canvas.getContext('2d');
}
var threadDim = this.threadDim = utils.clone(this.output);

while (threadDim.length < 3) {
threadDim.push(1);
}

if (this.graphical) {
canvas.width = threadDim[0];
canvas.height = threadDim[1];
var _canvas = this._canvas;
if (!_canvas) {
throw new Error('no canvas available for using graphical output');
}
_canvas.width = threadDim[0];
_canvas.height = threadDim[1];
this._imageData = this._canvasCtx.createImageData(threadDim[0], threadDim[1]);
this._colorData = new Uint8ClampedArray(threadDim[0] * threadDim[1] * 4);
}
Expand Down
8 changes: 4 additions & 4 deletions bin/gpu.min.js

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions dist/backend/cpu/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (_KernelBase) {
* @constructor CPUKernel
*
* @desc Kernel Implementation for CPU.
*
*
* <p>Instantiates properties to the CPU Kernel.</p>
*
* @extends KernelBase
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = function (_KernelBase) {
* @function
* @name validateOptions
*
* @desc Validate options related to CPU Kernel, such as
* @desc Validate options related to CPU Kernel, such as
* dimensions size, and auto dimension support.
*
*/
Expand Down Expand Up @@ -92,8 +92,8 @@ module.exports = function (_KernelBase) {
* @function
* @name build
*
* @desc Builds the Kernel, by generating the kernel
* string using thread dimensions, and arguments
* @desc Builds the Kernel, by generating the kernel
* string using thread dimensions, and arguments
* supplied to the kernel.
*
* <p>If the graphical flag is enabled, canvas is used.</p>
Expand All @@ -107,16 +107,23 @@ module.exports = function (_KernelBase) {
this.setupParams(arguments);
this.validateOptions();
var canvas = this._canvas;
this._canvasCtx = canvas.getContext('2d');
if (canvas) {
// if node or canvas is not found, don't die
this._canvasCtx = canvas.getContext('2d');
}
var threadDim = this.threadDim = utils.clone(this.output);

while (threadDim.length < 3) {
threadDim.push(1);
}

if (this.graphical) {
canvas.width = threadDim[0];
canvas.height = threadDim[1];
var _canvas = this._canvas;
if (!_canvas) {
throw new Error('no canvas available for using graphical output');
}
_canvas.width = threadDim[0];
_canvas.height = threadDim[1];
this._imageData = this._canvasCtx.createImageData(threadDim[0], threadDim[1]);
this._colorData = new Uint8ClampedArray(threadDim[0] * threadDim[1] * 4);
}
Expand Down Expand Up @@ -165,7 +172,7 @@ module.exports = function (_KernelBase) {
* @name getKernelString
*
* @desc Generates kernel string for this kernel program.
*
*
* <p>If sub-kernels are supplied, they are also factored in.
* This string can be saved by calling the `toString` method
* and then can be reused later.</p>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "1.10.3",
"version": "1.10.4",
"description": "GPU Accelerated JavaScript",
"main": "./dist/index.js",
"files": [
Expand Down
19 changes: 13 additions & 6 deletions src/backend/cpu/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = class CPUKernel extends KernelBase {
* @constructor CPUKernel
*
* @desc Kernel Implementation for CPU.
*
*
* <p>Instantiates properties to the CPU Kernel.</p>
*
* @extends KernelBase
Expand Down Expand Up @@ -47,7 +47,7 @@ module.exports = class CPUKernel extends KernelBase {
* @function
* @name validateOptions
*
* @desc Validate options related to CPU Kernel, such as
* @desc Validate options related to CPU Kernel, such as
* dimensions size, and auto dimension support.
*
*/
Expand Down Expand Up @@ -75,8 +75,8 @@ module.exports = class CPUKernel extends KernelBase {
* @function
* @name build
*
* @desc Builds the Kernel, by generating the kernel
* string using thread dimensions, and arguments
* @desc Builds the Kernel, by generating the kernel
* string using thread dimensions, and arguments
* supplied to the kernel.
*
* <p>If the graphical flag is enabled, canvas is used.</p>
Expand All @@ -87,14 +87,21 @@ module.exports = class CPUKernel extends KernelBase {
this.setupParams(arguments);
this.validateOptions();
const canvas = this._canvas;
this._canvasCtx = canvas.getContext('2d');
if (canvas) {
// if node or canvas is not found, don't die
this._canvasCtx = canvas.getContext('2d');
}
const threadDim = this.threadDim = utils.clone(this.output);

while (threadDim.length < 3) {
threadDim.push(1);
}

if (this.graphical) {
const canvas = this._canvas;
if (!canvas) {
throw new Error('no canvas available for using graphical output');
}
canvas.width = threadDim[0];
canvas.height = threadDim[1];
this._imageData = this._canvasCtx.createImageData(threadDim[0], threadDim[1]);
Expand Down Expand Up @@ -144,7 +151,7 @@ module.exports = class CPUKernel extends KernelBase {
* @name getKernelString
*
* @desc Generates kernel string for this kernel program.
*
*
* <p>If sub-kernels are supplied, they are also factored in.
* This string can be saved by calling the `toString` method
* and then can be reused later.</p>
Expand Down
1 change: 1 addition & 0 deletions test/all.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@
<script src="issues/382-bad-constant.js"></script>
<script src="issues/390-thread-assignment.js"></script>
<script src="issues/399-double-definition.js"></script>
<script src="issues/401-cpu-canvas-check.js"></script>
</body>
</html>
30 changes: 30 additions & 0 deletions test/issues/401-cpu-canvas-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(function() {
QUnit.test('Issue #401 - cpu no canvas graphical', function(assert) {
assert.throws(function() {
GPU.CPUKernel.prototype.build.apply({
setupConstants: function() {},
setupParams: function() {},
validateOptions: function() {},
getKernelString: function() {},
graphical: true,
output: [1],
_canvas: null
}, []);
},
new Error('no canvas available for using graphical output'),
'throws when canvas is not available and using graphical output');
});

QUnit.test('Issue #401 - cpu no canvas', function(assert) {
GPU.CPUKernel.prototype.build.apply({
setupConstants: function() {},
setupParams: function() {},
validateOptions: function() {},
getKernelString: function() {},
graphical: false,
output: [1],
_canvas: null
}, []);
assert.equal(true, true, 'ok when canvas is not available and not using graphical output');
});
})();

0 comments on commit 3aba32e

Please sign in to comment.