From f2cd54b160c705573909622f93595c100f39a1c2 Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Tue, 22 Dec 2015 15:16:52 -0500 Subject: [PATCH 1/3] Fix ImageData Constructor Support Detection Our support detection for the `ImageData(data, width, height)` constructor would fail in certain browsers because the size of a 1x1 ImageData's Uint8ClampedArray is 4, not 1. --- include/display.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/display.js b/include/display.js index 6bf89bd6..0a3921c2 100644 --- a/include/display.js +++ b/include/display.js @@ -17,7 +17,7 @@ var Display; var SUPPORTS_IMAGEDATA_CONSTRUCTOR = false; try { - new ImageData(new Uint8ClampedArray(1), 1, 1); + new ImageData(new Uint8ClampedArray(4), 1, 1); SUPPORTS_IMAGEDATA_CONSTRUCTOR = true; } catch (ex) { // ignore failure From 8eedab379f87d42222a204f11a6ec66f7583e0bd Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Tue, 22 Dec 2015 15:21:42 -0500 Subject: [PATCH 2/3] [WIP] Optimize ImageData creation This commit changes several places which use `createImageData` over to using the `ImageData(data, width, height)` constructor form. This allows us to use a statically allocated ArrayBuffer to be the underlying storage for the image data, so we only have to create a small Uint8ClmapedArray "pointer", instead of having to create underlying storage each time. --- include/display.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/display.js b/include/display.js index 0a3921c2..af9030f3 100644 --- a/include/display.js +++ b/include/display.js @@ -32,6 +32,7 @@ var Display; // the full frame buffer (logical canvas) size this._fb_width = 0; this._fb_height = 0; + this._fb_cache = null; // the size limit of the viewport (start disabled) this._maxWidth = 0; @@ -335,6 +336,7 @@ var Display; this._fb_width = width; this._fb_height = height; + this._fb_cache = new ArrayBuffer(width * height * 4); this._rescale(this._scale); @@ -678,7 +680,8 @@ var Display; }, _rgbImageData: function (x, y, vx, vy, width, height, arr, offset) { - var img = this._drawCtx.createImageData(width, height); + //var img = this._drawCtx.createImageData(width, height); + var img = new ImageData(new Uint8ClampedArray(this._fb_cache, 0, width * height * 4), width, height); var data = img.data; for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 3) { data[i] = arr[j]; @@ -690,7 +693,8 @@ var Display; }, _bgrxImageData: function (x, y, vx, vy, width, height, arr, offset) { - var img = this._drawCtx.createImageData(width, height); + //var img = this._drawCtx.createImageData(width, height); + var img = new ImageData(new Uint8ClampedArray(this._fb_cache, 0, width * height * 4), width, height); var data = img.data; for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 4) { data[i] = arr[j + 2]; From a842140b51bb178bf22f7c3b648d718cc4f83dad Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Tue, 22 Dec 2015 15:42:09 -0500 Subject: [PATCH 3/3] [WIP] Tweak Pako to reduce object creation This commit tweaks pako slightly to reduce the number of objects it creates unecessarily. --- docs/notes | 1 + include/inflator.js | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/notes b/docs/notes index 036cd510..e886b7a0 100644 --- a/docs/notes +++ b/docs/notes @@ -3,3 +3,4 @@ Rebuilding inflator.js - Download pako from npm - Install browserify using npm - browserify utils/inflator.partial.js -o include/inflator.js -s inflator +- apply changes from utils/inflator.diff.js to include/inflator.js diff --git a/include/inflator.js b/include/inflator.js index 48ede208..d964ac5d 100644 --- a/include/inflator.js +++ b/include/inflator.js @@ -889,7 +889,7 @@ function inflate(strm, flush) { var len; /* length to copy for repeats, bits to drop */ var ret; /* return code */ var hbuf = new utils.Buf8(4); /* buffer for gzip header crc calculation */ - var opts; + var opts = {}; var n; // temporary var for NEED_BITS @@ -1425,7 +1425,7 @@ function inflate(strm, flush) { state.lencode = state.lendyn; state.lenbits = 7; - opts = {bits: state.lenbits}; + opts.bits = state.lenbits; ret = inflate_table(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts); state.lenbits = opts.bits; @@ -1556,7 +1556,7 @@ function inflate(strm, flush) { concerning the ENOUGH constants, which depend on those values */ state.lenbits = 9; - opts = {bits: state.lenbits}; + opts.bits = state.lenbits; ret = inflate_table(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed. // state.next_index = opts.table_index; @@ -1573,7 +1573,7 @@ function inflate(strm, flush) { //state.distcode.copy(state.codes); // Switch to use dynamic table state.distcode = state.distdyn; - opts = {bits: state.distbits}; + opts.bits = state.distbits; ret = inflate_table(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed. // state.next_index = opts.table_index; @@ -2415,4 +2415,4 @@ Inflate.prototype = { module.exports = {Inflate: Inflate}; },{"../node_modules/pako/lib/zlib/inflate.js":5,"../node_modules/pako/lib/zlib/zstream.js":7}]},{},[8])(8) -}); \ No newline at end of file +});