[go: up one dir, main page]

Skip to content

Commit

Permalink
fix the extension for firefox > v34
Browse files Browse the repository at this point in the history
Notes:
 * There is a serious memory leak that I have not yet located.
 * Tutorial 3 and 4 from the Nokia Research website do not work due to the asynchronous nature of webcl events. I am assuming the turorial is ill-formed, as it does work for the ray tracing demo.
 * Extension is not signed yet, so it won't work with release versions of firefox>=v42.
  • Loading branch information
vhdirk committed Sep 3, 2015
1 parent cb4fbb2 commit f79b04b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 107 deletions.
94 changes: 20 additions & 74 deletions firefox-js/content/modules/common.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Cu.import ("chrome://nrcwebcl/content/modules/lib_ocl/ocl_exception.jsm");
Cu.import ("resource://gre/modules/ctypes.jsm");


var console = Cu.import("resource://gre/modules/devtools/Console.jsm", {}).console;


function getRuntimeABI ()
{
var xulRuntime = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
Expand All @@ -49,7 +52,7 @@ function getRuntimeOS ()
function typedArrayToCTypesPtr (value)
{
var ptr = null, size = 0;

if (value.wrappedJSObject) value = value.wrappedJSObject;

// NOTE: instanceof doesn't work when the object originates from different
Expand All @@ -63,80 +66,23 @@ function typedArrayToCTypesPtr (value)
className = className.substring(0, className.lastIndexOf("]"));
}

//if (value instanceof Int8Array)
if (className == "Int8Array")
{
//ptr = ctypes.int8_t.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Uint8Array)
else if (className == "Uint8Array")
{
//ptr = ctypes.uint8_t.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Uint8ClampedArray)
else if (className == "Uint8ClampedArray")
{
//ptr = ctypes.uint8_t.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Int16Array)
else if (className == "Int16Array")
{
//ptr = ctypes.int16_t.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Uint16Array)
else if (className == "Uint16Array")
{
//ptr = ctypes.uint16_t.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Int32Array)
else if (className == "Int32Array")
{
//ptr = ctypes.int32_t.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Uint32Array)
else if (className == "Uint32Array")
{
//ptr = ctypes.uint32_t.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Float32Array)
else if (className == "Float32Array")
{
//ptr = ctypes.float.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof Float64Array)
else if (className == "Float64Array")
{
//ptr = ctypes.double.ptr (value);
ptr = ctypes.voidptr_t (value.buffer);
size = value.length * value.BYTES_PER_ELEMENT;
}
//else if (value instanceof ArrayBuffer)
else if (className == "ArrayBuffer")
{
ptr = T.voidptr_t (value);
size = value.byteLength;
}
else
{
throw new CLInvalidArgument ("");
switch(className) {
case "Int8Array":
case "Uint8Array":
case "Uint8ClampedArray":
case "Int16Array":
case "Uint16Array":
case "Int32Array":
case "Uint32Array":
case "Float32Array":
case "Float64Array":
case "ArrayBuffer":
ptr = value;
size = value.byteLength;
break;
default:
throw new CLInvalidArgument ("value", null, "Kernel.setArg");
}

return { ptr: ptr, size: size };
}

25 changes: 7 additions & 18 deletions firefox-js/content/modules/lib_ocl/kernel.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ try {
const Cu = Components.utils;

Cu.import ("chrome://nrcwebcl/content/modules/logger.jsm");
Cu.import ("chrome://nrcwebcl/content/modules/common.jsm");
Cu.import ("resource://gre/modules/ctypes.jsm");
Cu.import ("chrome://nrcwebcl/content/modules/lib_ocl/ocl_types.jsm");
Cu.import ("chrome://nrcwebcl/content/modules/lib_ocl/ocl_symbols.jsm");
Expand Down Expand Up @@ -204,25 +205,13 @@ Kernel.prototype.setArg = function (index, value)
ptr = value._internal.address();
size = T.cl_sampler.size;
}
else switch(className) {
case "Int8Array":
case "Uint8Array":
case "Uint8ClampedArray":
case "Int16Array":
case "Uint16Array":
case "Int32Array":
case "Uint32Array":
case "Float32Array":
case "Float64Array":
ptr = ctypes.voidptr_t (value.buffer);
size = value.byteLength;
break;
default:
throw new CLInvalidArgument ("value", null, "Kernel.setArg");
else {
let tmp = typedArrayToCTypesPtr(value);
ptr = tmp.ptr;
size = tmp.size;
}

var err = this._lib.clSetKernelArg (this._internal, +index, size,
ctypes.cast(ptr, ctypes.voidptr_t));

var err = this._lib.clSetKernelArg (this._internal, +index, size, ptr);
if (err) throw new CLError (err, null, "Kernel.setArg");
};

Expand Down
74 changes: 59 additions & 15 deletions firefox-js/content/workers/webclcallbackworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
try {


var ENABLE_DEBUG = false;
var ENABLE_CONSOLE = false;
var ENABLE_DEBUG = true;
var ENABLE_CONSOLE = true;


var puts = null;
Expand All @@ -39,6 +39,8 @@ try {

var libHandle = null;
var clCallSetEventCallback = null;
var clWaitForEvents = null;

var T_clEventCallback = ctypes.FunctionType(ctypes.default_abi,
ctypes.void_t,
[ ctypes.voidptr_t,
Expand All @@ -60,6 +62,12 @@ try {
ctypes.int32_t,
ctypes.voidptr_t, ctypes.int32_t,
T_clEventCallback.ptr, ctypes.voidptr_t);

clWaitForEvents = libHandle.declare ("clWaitForEvents",
ctypes.default_abi,
ctypes.int32_t,
ctypes.uint32_t, ctypes.voidptr_t);

}


Expand All @@ -69,24 +77,39 @@ try {
libHandle = null;
}


function notifyCallback (event, execStatus, userData)
function notifyCallback(event, execStatus, userData)
{
DEBUG('notifyCallback pre');

let id = String(ctypes.cast(userData, ctypes.uint32_t).value);
postMessage ({ cmd: "callback",
execStatus: execStatus,
id: id });
DEBUG('notifyCallback post');

}

function callSetEventCallback (event, commandExecCallbackType, intUserData)
{
DEBUG('callSetEventCallback: event: ' + event + ' commandExecCallbackType: ' + commandExecCallbackType + ' intUserData: ' + intUserData);
try {
let clEvent = ctypes.cast(ctypes.intptr_t(event), ctypes.voidptr_t),
clCallbackType = ctypes.int32_t(+commandExecCallbackType),
clNotify = T_clEventCallback.ptr(notifyCallback),
clUserData = ctypes.cast(ctypes.intptr_t(+intUserData), ctypes.voidptr_t);
let clEvent = ctypes.cast(ctypes.intptr_t(event), ctypes.voidptr_t);
let clCallbackType = ctypes.int32_t(+commandExecCallbackType);

DEBUG('T_clEventCallback.ptr(notifyCallback) pre');
//FIXME: crashes here with JS_AbortIfWrongThread
//make this a void pointer just to test
let clNotify = T_clEventCallback.ptr(notifyCallback);
DEBUG('T_clEventCallback.ptr(notifyCallback) post');

let clUserData = ctypes.cast(ctypes.intptr_t(+intUserData), ctypes.voidptr_t);

return clCallSetEventCallback (clEvent, clCallbackType, clNotify, clUserData);
DEBUG('clCallSetEventCallback pre');

var ret = clCallSetEventCallback (clEvent, clCallbackType, clNotify, clUserData);
DEBUG('clCallSetEventCallback post');

return ret;
}
catch (e) {
let s = (e&&e.stack ? "\n"+e.stack : "");
Expand All @@ -106,6 +129,7 @@ try {
return;
}


switch (event.data.cmd)
{
case "load":
Expand All @@ -129,15 +153,36 @@ try {
case "setEventCallback":
try
{
let rv = callSetEventCallback (event.data.event,
event.data.callbackType,
event.data.callbackId);
DEBUG('callSetEventCallback pre');

// let rv = callSetEventCallback (event.data.event,
// event.data.callbackType,
// event.data.callbackId);
DEBUG('callSetEventCallback post');

event.data.rv = rv;
event.data.rv = 0;//rv;
postMessage (event.data);

let clNumEvents = 1;
let clEventArray = ctypes.voidptr_t.array(clNumEvents)();
let clEvent = ctypes.cast(ctypes.intptr_t(event.data.event), ctypes.voidptr_t);
clEventArray[0] = clEvent;
let clEventArrayPtr = ctypes.cast (clEventArray.address(), ctypes.voidptr_t);
let ret = clWaitForEvents(clNumEvents, clEventArrayPtr);

//TODO: call callback if event status matches

if(true) // execStatus == event.data.callbackType;
{
let clUserData = ctypes.cast(ctypes.intptr_t(+event.data.callbackId), ctypes.voidptr_t);
notifyCallback(event, event.data.callbackType, clUserData);
DEBUG('callSetEventCallback after');
}
}
catch (e)
{
ERROR (e+"\n"+e.stack);

event.data.err = e;
postMessage (event.data);
}
Expand All @@ -152,7 +197,7 @@ try {
}
catch (e)
{
ERROR (e);
ERROR (e+"\n"+e.stack);
event.data.err = String(e);
postMessage (event.data);
}
Expand All @@ -161,4 +206,3 @@ try {


} catch(e) { ERROR (e+"\n"+e.stack); }

0 comments on commit f79b04b

Please sign in to comment.