Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make headless-gl context aware #182

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/native/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,8 @@ NAN_MODULE_INIT(Init) {
Nan::Export(target, "setError", WebGLRenderingContext::SetError);
}

#if NODE_MAJOR_VERSION >= 10
NAN_MODULE_WORKER_ENABLED(webgl, Init)
#else
NODE_MODULE(webgl, Init)
#endif
33 changes: 33 additions & 0 deletions test/context-aware-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

var tape = require('tape')

tape('context-aware-module', function (t) {

const createContext1 = require('..');
const { NativeWebGL: NativeWebGL1 } = require("../src/javascript/native-gl");

const gl1 = createContext1(16, 16);

t.assert(gl1, "gl context returned");

gl1.destroy();
NativeWebGL1.cleanup();

delete require.cache[require.resolve('..')];
delete require.cache[require.resolve('../src/javascript/node-index')];
delete require.cache[require.resolve('../src/javascript/native-gl')];
delete require.cache[require.resolve('../build/Debug/webgl.node')];
delete require.cache[require.resolve('../build/Release/webgl.node')];

const createContext2 = require('..');
const { NativeWebGL: NativeWebGL2 } = require("../src/javascript/native-gl");

const gl2 = createContext2(16, 16);

t.assert(NativeWebGL1 !== NativeWebGL2);
t.assert(createContext1 !== createContext2);
t.assert(gl2, "gl context returned");

t.end()
})
45 changes: 45 additions & 0 deletions test/util/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');

if (isMainThread) {
const createContext = require('../..');

gl(1, 1);

module.exports = function (dimension) {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: dimension,
});

worker.on('message', resolve);

worker.on('error', reject);

worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
});
};
} else {
const createContext = require('../..');
const dimension = workerData;

const width = dimension;
const height = dimension;

const gl = createContext(width, height);

gl.clearColor(0, 1, 0, 0.5)
gl.clear(gl.COLOR_BUFFER_BIT)

var pixels = new Uint8Array(width * height * 4)
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)

gl.destroy()

parentPort.postMessage(pixels);
}
38 changes: 38 additions & 0 deletions test/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

var tape = require('tape')
var glWorker = require("./util/worker");

tape('worker - serial', async function (t) {
const jobsData = [1, 2, 3];

t.plan(jobsData.length);

const results = [];

for (const data of jobsData) {
const result = await glWorker(data);

results.push(result);
}

for (const [index, data] of jobsData.entries()) {
t.equal(results[index].length, data * data * 4);
}

t.end()
})

tape('worker - parallel', async function (t) {
const jobsData = [1, 2, 3];

t.plan(jobsData.length);

const results = await Promise.all(jobsData.map(e => glWorker(e)));

for (const [index, data] of jobsData.entries()) {
t.equal(results[index].length, data * data * 4);
}

t.end()
})