1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-20 02:18:20 +01:00

use web assembly and web worker for data decoder

This commit is contained in:
Xuan Sang LE 2018-09-21 20:01:41 +02:00
parent 50a354ec73
commit 4b941039da
7 changed files with 117 additions and 263 deletions

View File

@ -6,9 +6,8 @@ class WVNC extends window.classes.BaseObject
@uri = @args[0] if @args and @args.length > 0 @uri = @args[0] if @args and @args.length > 0
@canvas = undefined @canvas = undefined
@canvas = ($ @args[1])[0] if @args and @args.length > 1 @canvas = ($ @args[1])[0] if @args and @args.length > 1
@buffer = $("<canvas>")[0]
@lastPose = { x: 0, y: 0 } @lastPose = { x: 0, y: 0 }
@scale = 0.8 @scale = 1.0
@decoder = new Worker('/assets/scripts/decoder.js') @decoder = new Worker('/assets/scripts/decoder.js')
me = @ me = @
@mouseMask = 0 @mouseMask = 0
@ -40,7 +39,7 @@ class WVNC extends window.classes.BaseObject
me.sendPointEvent p.x, p.y, me.mouseMask me.sendPointEvent p.x, p.y, me.mouseMask
return unless me.canvas return unless me.canvas
($ me.canvas).css "cursor", "none" #($ me.canvas).css "cursor", "none"
($ me.canvas).contextmenu (e) -> ($ me.canvas).contextmenu (e) ->
e.preventDefault() e.preventDefault()
return false return false
@ -91,47 +90,30 @@ class WVNC extends window.classes.BaseObject
initCanvas: (w, h , d) -> initCanvas: (w, h , d) ->
me = @ me = @
@depth = d @depth = d
@buffer.width = w @canvas.width = w
@buffer.height = h @canvas.height = h
@resolution = @resolution =
w: w, w: w,
h: h, h: h,
depth: @depth depth: @depth
@decoder.postMessage @resolution @decoder.postMessage @resolution
ctx = @buffer.getContext('2d')
data = ctx.createImageData w, h
ctx.putImageData data, 0, 0
process: (data) -> process: (msg) ->
data.pixels = new Uint8ClampedArray data.pixels if not @socket
data.pixels = data.pixels.subarray 10 if data.flag is 0 and @resolution.depth is 32 return
ctx = @buffer.getContext('2d') data = new Uint8Array msg.buffer
imgData = ctx.createImageData data.w, data.h #w = @buffer.width * @scale
imgData.data.set data.pixels #h = @buffer.height * @scale
ctx.putImageData imgData, data.x, data.y ctx = @canvas.getContext "2d"
imgData = ctx.createImageData msg.w, msg.h
@draw() if data.x isnt @lastPose.x or data.y > @resolution.h - 10 imgData.data.set data
@lastPose = { x: data.x, y: data.y } ctx.putImageData imgData, msg.x, msg.y
setScale: (n) -> setScale: (n) ->
@scale = n @scale = n
@draw() @draw()
draw: () ->
if not @socket
return
w = @buffer.width * @scale
h = @buffer.height * @scale
@canvas.width = w
@canvas.height = h
ctx = @canvas.getContext "2d"
ctx.save()
ctx.scale @scale, @scale
ctx.clearRect 0, 0, w, h
ctx.drawImage @buffer, 0, 0
ctx.restore()
openSession: () -> openSession: () ->
me = @ me = @
@ -152,7 +134,7 @@ class WVNC extends window.classes.BaseObject
initConnection: () -> initConnection: () ->
vncserver = "localhost:5901" vncserver = "localhost:5901"
data = new Uint8Array vncserver.length + 5 data = new Uint8Array vncserver.length + 5
data[0] = 16 # bbp data[0] = 32 # bbp
### ###
flag: flag:
0: raw data no compress 0: raw data no compress
@ -160,7 +142,7 @@ class WVNC extends window.classes.BaseObject
2: raw data compressed by zlib 2: raw data compressed by zlib
3: jpeg data compressed by zlib 3: jpeg data compressed by zlib
### ###
data[1] = 2 data[1] = 3
data[2] = 50 # jpeg quality data[2] = 50 # jpeg quality
## rate in milisecond ## rate in milisecond
rate = 30 rate = 30
@ -237,11 +219,8 @@ class WVNC extends window.classes.BaseObject
# status command for ack # status command for ack
@socket.send(@buildCommand 0x04, 1) @socket.send(@buildCommand 0x04, 1)
when 0x84 when 0x84
#console.log "update" # send data to web assembly for decoding
@decoder.postMessage data.buffer, [data.buffer] @decoder.postMessage data.buffer, [data.buffer]
#@decodeFB d
# ack
#@socket.send(@buildCommand 0x04, 1)
else else
console.log cmd console.log cmd

View File

@ -1,84 +1,48 @@
#zlib library #zlib library
importScripts('pako.min.js') importScripts('wvnc_asm.js')
# jpeg library api = {}
importScripts('jpeg-decoder.js')
resolution = undefined resolution = undefined
pixelValue = (value, depth) -> #frame_buffer = undefined
pixel = Module.onRuntimeInitialized = () ->
r: 255 api =
g: 255 {
b: 255 createBuffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
a: 255 destroyBuffer: Module.cwrap('destroy_buffer', '', ['number']),
#console.log("len is" + arr.length) updateBuffer: Module.cwrap("update", 'number', ['number', 'number', 'number', 'number', 'number', 'number']),
if depth is 24 or depth is 32 decodeBuffer: Module.cwrap("decode",'number', ['number', 'number', 'number','number'] )
pixel.r = value & 0xFF }
pixel.g = (value >> 8) & 0xFF
pixel.b = (value >> 16) & 0xFF
else if depth is 16
pixel.r = (value & 0x1F) * (255 / 31)
pixel.g = ((value >> 5) & 0x3F) * (255 / 63)
pixel.b = ((value >> 11) & 0x1F) * (255 / 31)
#console.log pixel
return pixel
getImageData = (d) ->
return d.pixels if resolution.depth is 32
step = resolution.depth / 8
npixels = d.pixels.length / step
data = new Uint8ClampedArray d.w * d.h * 4
for i in [0..npixels - 1]
value = 0
value = value | d.pixels[i * step + j] << (j * 8) for j in [0..step - 1]
pixel = pixelValue value, resolution.depth
data[i * 4] = pixel.r
data[i * 4 + 1] = pixel.g
data[i * 4 + 2] = pixel.b
data[i * 4 + 3] = pixel.a
return data
decodeRaw = (d) ->
d.pixels = getImageData d
return d
decodeJPEG = (d) ->
raw = decode d.pixels, { useTArray: true, colorTransform: true }
d.pixels = raw.data
return d
###
blob = new Blob [d.pixels], { type: "image/jpeg" }
reader = new FileReader()
reader.onloadend = () ->
d.pixels = reader.result
postMessage d
reader.readAsDataURL blob
###
update = (msg) ->
d = {}
data = new Uint8Array msg
d.x = data[1] | (data[2]<<8)
d.y = data[3] | (data[4]<<8)
d.w = data[5] | (data[6]<<8)
d.h = data[7] | (data[8]<<8)
d.flag = data[9]
d.pixels = data.subarray 10
# the zlib is slower than expected
switch d.flag
when 0x0 # raw data
raw = decodeRaw d
when 0x1 # jpeg data
raw = decodeJPEG(d)
when 0x2 # raw compress in zlib format
d.pixels = pako.inflate(d.pixels)
raw = decodeRaw d
when 0x3 # jpeg compress in zlib format
d.pixels = pako.inflate(d.pixels)
raw = decodeJPEG(d)
return unless raw
raw.pixels = raw.pixels.buffer
# fill the rectangle
postMessage raw, [raw.pixels]
onmessage = (e) -> onmessage = (e) ->
return resolution = e.data if e.data.depth if e.data.depth
update e.data resolution = e.data
#api.destroyBuffer frame_buffer if frame_buffer
#frame_buffer = api.createBuffer resolution.w * resolution.h * 4
#else if e.data.cleanup
# api.destroyBuffer frame_buffer if frame_buffer
else
datain = new Uint8Array e.data
x = datain[1] | (datain[2] << 8)
y = datain[3] | (datain[4] << 8)
w = datain[5] | (datain[6] << 8)
h = datain[7] | (datain[8] << 8)
flag = datain[9]
p = api.createBuffer datain.length
Module.HEAP8.set datain, p
size = w * h * 4
po = api.decodeBuffer p, datain.length, resolution.depth, size
#api.updateBuffer frame_buffer, p, datain.length, resolution.w, resolution.h, resolution.depth
# create buffer array and send back to main
dataout = new Uint8Array Module.HEAP8.buffer, po, size
# console.log dataout
msg = {}
tmp = new Uint8Array size
tmp.set dataout, 0
msg.buffer = tmp.buffer
msg.x = x
msg.y = y
msg.w = w
msg.h = h
postMessage msg, [msg.buffer]
api.destroyBuffer p
if flag isnt 0x0 or resolution.depth isnt 32
api.destroyBuffer po

View File

@ -1,114 +1,50 @@
// Generated by CoffeeScript 1.9.3 // Generated by CoffeeScript 1.9.3
var decodeJPEG, decodeRaw, getImageData, onmessage, pixelValue, resolution, update; var api, onmessage, resolution;
importScripts('pako.min.js'); importScripts('wvnc_asm.js');
importScripts('jpeg-decoder.js'); api = {};
resolution = void 0; resolution = void 0;
pixelValue = function(value, depth) { Module.onRuntimeInitialized = function() {
var pixel; return api = {
pixel = { createBuffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
r: 255, destroyBuffer: Module.cwrap('destroy_buffer', '', ['number']),
g: 255, updateBuffer: Module.cwrap("update", 'number', ['number', 'number', 'number', 'number', 'number', 'number']),
b: 255, decodeBuffer: Module.cwrap("decode", 'number', ['number', 'number', 'number', 'number'])
a: 255
}; };
if (depth === 24 || depth === 32) {
pixel.r = value & 0xFF;
pixel.g = (value >> 8) & 0xFF;
pixel.b = (value >> 16) & 0xFF;
} else if (depth === 16) {
pixel.r = (value & 0x1F) * (255 / 31);
pixel.g = ((value >> 5) & 0x3F) * (255 / 63);
pixel.b = ((value >> 11) & 0x1F) * (255 / 31);
}
return pixel;
};
getImageData = function(d) {
var data, i, j, k, l, npixels, pixel, ref, ref1, step, value;
if (resolution.depth === 32) {
return d.pixels;
}
step = resolution.depth / 8;
npixels = d.pixels.length / step;
data = new Uint8ClampedArray(d.w * d.h * 4);
for (i = k = 0, ref = npixels - 1; 0 <= ref ? k <= ref : k >= ref; i = 0 <= ref ? ++k : --k) {
value = 0;
for (j = l = 0, ref1 = step - 1; 0 <= ref1 ? l <= ref1 : l >= ref1; j = 0 <= ref1 ? ++l : --l) {
value = value | d.pixels[i * step + j] << (j * 8);
}
pixel = pixelValue(value, resolution.depth);
data[i * 4] = pixel.r;
data[i * 4 + 1] = pixel.g;
data[i * 4 + 2] = pixel.b;
data[i * 4 + 3] = pixel.a;
}
return data;
};
decodeRaw = function(d) {
d.pixels = getImageData(d);
return d;
};
decodeJPEG = function(d) {
var raw;
raw = decode(d.pixels, {
useTArray: true,
colorTransform: true
});
d.pixels = raw.data;
return d;
/*
blob = new Blob [d.pixels], { type: "image/jpeg" }
reader = new FileReader()
reader.onloadend = () ->
d.pixels = reader.result
postMessage d
reader.readAsDataURL blob
*/
};
update = function(msg) {
var d, data, raw;
d = {};
data = new Uint8Array(msg);
d.x = data[1] | (data[2] << 8);
d.y = data[3] | (data[4] << 8);
d.w = data[5] | (data[6] << 8);
d.h = data[7] | (data[8] << 8);
d.flag = data[9];
d.pixels = data.subarray(10);
switch (d.flag) {
case 0x0:
raw = decodeRaw(d);
break;
case 0x1:
raw = decodeJPEG(d);
break;
case 0x2:
d.pixels = pako.inflate(d.pixels);
raw = decodeRaw(d);
break;
case 0x3:
d.pixels = pako.inflate(d.pixels);
raw = decodeJPEG(d);
}
if (!raw) {
return;
}
raw.pixels = raw.pixels.buffer;
return postMessage(raw, [raw.pixels]);
}; };
onmessage = function(e) { onmessage = function(e) {
var datain, dataout, flag, h, msg, p, po, size, tmp, w, x, y;
if (e.data.depth) { if (e.data.depth) {
return resolution = e.data; return resolution = e.data;
} else {
datain = new Uint8Array(e.data);
x = datain[1] | (datain[2] << 8);
y = datain[3] | (datain[4] << 8);
w = datain[5] | (datain[6] << 8);
h = datain[7] | (datain[8] << 8);
flag = datain[9];
p = api.createBuffer(datain.length);
Module.HEAP8.set(datain, p);
size = w * h * 4;
po = api.decodeBuffer(p, datain.length, resolution.depth, size);
dataout = new Uint8Array(Module.HEAP8.buffer, po, size);
msg = {};
tmp = new Uint8Array(size);
tmp.set(dataout, 0);
msg.buffer = tmp.buffer;
msg.x = x;
msg.y = y;
msg.w = w;
msg.h = h;
postMessage(msg, [msg.buffer]);
api.destroyBuffer(p);
if (flag !== 0x0 || resolution.depth !== 32) {
return api.destroyBuffer(po);
}
} }
return update(e.data);
}; };

View File

@ -176,12 +176,11 @@
if (this.args && this.args.length > 1) { if (this.args && this.args.length > 1) {
this.canvas = ($(this.args[1]))[0]; this.canvas = ($(this.args[1]))[0];
} }
this.buffer = $("<canvas>")[0];
this.lastPose = { this.lastPose = {
x: 0, x: 0,
y: 0 y: 0
}; };
this.scale = 0.8; this.scale = 1.0;
this.decoder = new Worker('/assets/scripts/decoder.js'); this.decoder = new Worker('/assets/scripts/decoder.js');
me = this; me = this;
this.mouseMask = 0; this.mouseMask = 0;
@ -228,7 +227,6 @@
if (!me.canvas) { if (!me.canvas) {
return; return;
} }
($(me.canvas)).css("cursor", "none");
($(me.canvas)).contextmenu(function(e) { ($(me.canvas)).contextmenu(function(e) {
e.preventDefault(); e.preventDefault();
return false; return false;
@ -283,39 +281,29 @@
}; };
WVNC.prototype.initCanvas = function(w, h, d) { WVNC.prototype.initCanvas = function(w, h, d) {
var ctx, data, me; var me;
me = this; me = this;
this.depth = d; this.depth = d;
this.buffer.width = w; this.canvas.width = w;
this.buffer.height = h; this.canvas.height = h;
this.resolution = { this.resolution = {
w: w, w: w,
h: h, h: h,
depth: this.depth depth: this.depth
}; };
this.decoder.postMessage(this.resolution); return this.decoder.postMessage(this.resolution);
ctx = this.buffer.getContext('2d');
data = ctx.createImageData(w, h);
return ctx.putImageData(data, 0, 0);
}; };
WVNC.prototype.process = function(data) { WVNC.prototype.process = function(msg) {
var ctx, imgData; var ctx, data, imgData;
data.pixels = new Uint8ClampedArray(data.pixels); if (!this.socket) {
if (data.flag === 0 && this.resolution.depth === 32) { return;
data.pixels = data.pixels.subarray(10);
} }
ctx = this.buffer.getContext('2d'); data = new Uint8Array(msg.buffer);
imgData = ctx.createImageData(data.w, data.h); ctx = this.canvas.getContext("2d");
imgData.data.set(data.pixels); imgData = ctx.createImageData(msg.w, msg.h);
ctx.putImageData(imgData, data.x, data.y); imgData.data.set(data);
if (data.x !== this.lastPose.x || data.y > this.resolution.h - 10) { return ctx.putImageData(imgData, msg.x, msg.y);
this.draw();
}
return this.lastPose = {
x: data.x,
y: data.y
};
}; };
WVNC.prototype.setScale = function(n) { WVNC.prototype.setScale = function(n) {
@ -323,23 +311,6 @@
return this.draw(); return this.draw();
}; };
WVNC.prototype.draw = function() {
var ctx, h, w;
if (!this.socket) {
return;
}
w = this.buffer.width * this.scale;
h = this.buffer.height * this.scale;
this.canvas.width = w;
this.canvas.height = h;
ctx = this.canvas.getContext("2d");
ctx.save();
ctx.scale(this.scale, this.scale);
ctx.clearRect(0, 0, w, h);
ctx.drawImage(this.buffer, 0, 0);
return ctx.restore();
};
WVNC.prototype.openSession = function() { WVNC.prototype.openSession = function() {
var me; var me;
me = this; me = this;
@ -368,7 +339,7 @@
var data, rate, vncserver; var data, rate, vncserver;
vncserver = "localhost:5901"; vncserver = "localhost:5901";
data = new Uint8Array(vncserver.length + 5); data = new Uint8Array(vncserver.length + 5);
data[0] = 16; data[0] = 32;
/* /*
flag: flag:
@ -377,7 +348,7 @@
2: raw data compressed by zlib 2: raw data compressed by zlib
3: jpeg data compressed by zlib 3: jpeg data compressed by zlib
*/ */
data[1] = 2; data[1] = 3;
data[2] = 50; data[2] = 50;
rate = 30; rate = 30;
data[3] = rate & 0xFF; data[3] = rate & 0xFF;

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -17,7 +17,7 @@ LOG_ROOT = WWW_ROOT..DIR_SEP.."logs"
-- require needed library -- require needed library
require(BASE_FRW.."silk.api") require(BASE_FRW.."silk.api")
POLICY.mimes["application/wasm"] = true
-- registry object store global variables -- registry object store global variables
local REGISTRY = {} local REGISTRY = {}
-- set logging level -- set logging level