Add caching to CurrentColorsService

This commit is contained in:
Dávid Szabó 2016-08-30 14:07:43 +02:00 committed by Julian Descottes
parent 502eb1aac4
commit ac3e43bf61
3 changed files with 44 additions and 11 deletions

View File

@ -90,25 +90,46 @@
ns.CurrentColorsService.prototype.updateCurrentColors_ = function () { ns.CurrentColorsService.prototype.updateCurrentColors_ = function () {
var layers = this.piskelController.getLayers(); var layers = this.piskelController.getLayers();
var frames = layers.map(function (l) {return l.getFrames();}).reduce(function (p, n) {return p.concat(n);}); var frames = layers.map(function (l) {return l.getFrames();}).reduce(function (p, n) {return p.concat(n);});
var job = function (frame) { var job = function (frame) {
return this.cachedFrameProcessor.get(frame); return this.cachedFrameProcessor.get(frame);
}.bind(this); }.bind(this);
// TODO: Cache frame -> color var colors = {};
for (var i = 0, length = frames.length; i < length; i++) { var framesToBatch = [];
for (var i = 0, length = frames.length; i < length; ++i) {
var frame = frames[i]; var frame = frames[i];
var hash = frame.getHash(); var hash = frame.getHash();
if (frameCache[hash]) {
colors = Object.assign(colors, frameCache[hash]);
var hashParts = hash.split('-');
var hashVersion = parseInt(hashParts.pop());
if (hashVersion > 0) {
var lastColors = frameCache[hashParts.join('-')+'-'+(hashVersion - 1)];
if (lastColors) {
Object.keys(lastColors).forEach(function(color) {
if (!colors[color]) {
delete colors[color];
}
});
}
}
} else {
framesToBatch.push(frame);
}
} }
batchAll(frames, job).then(function (results) { var batchAllThen = function (results) {
var colors = {}; var colors = {};
results.forEach(function (result) { results.forEach(function (result) {
Object.keys(result).forEach(function (color) { Object.keys(result).forEach(function (color) {
colors[color] = true; colors[color] = true;
}); });
}); });
// Remove transparent color from used colors // Remove transparent color from used colors
delete colors[pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR)]; delete colors[pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR)];
@ -118,7 +139,13 @@
} }
this.setCurrentColors(hexColors); this.setCurrentColors(hexColors);
}.bind(this)); }.bind(this)
if (framesToBatch.length == 0) {
batchAllThen([colors]);
} else {
batchAll(framesToBatch, job).then(batchAllThen);
}
}; };
ns.CurrentColorsService.prototype.isCurrentColorsPaletteSelected_ = function () { ns.CurrentColorsService.prototype.isCurrentColorsPaletteSelected_ = function () {
@ -138,7 +165,7 @@
ns.CurrentColorsService.prototype.getFrameColors_ = function (frame, processorCallback) { ns.CurrentColorsService.prototype.getFrameColors_ = function (frame, processorCallback) {
var frameColorsWorker = new pskl.worker.framecolors.FrameColors(frame, var frameColorsWorker = new pskl.worker.framecolors.FrameColors(frame,
function (event) {processorCallback(event.data.colors);}, function (event) {frameCache[frame.getHash()] = event.data.colors; processorCallback(event.data.colors);},
function () {}, function () {},
function (event) {processorCallback({});} function (event) {processorCallback({});}
); );

View File

@ -13,7 +13,7 @@
}; };
ns.FrameColors.prototype.process = function () { ns.FrameColors.prototype.process = function () {
this.worker.postMessage(this.pixels); this.worker.postMessage([pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR), Constants.MAX_PALETTE_COLORS, this.pixels]);
}; };
ns.FrameColors.prototype.onWorkerMessage = function (event) { ns.FrameColors.prototype.onWorkerMessage = function (event) {

View File

@ -47,11 +47,15 @@
var getFrameColors = function (frame) { var getFrameColors = function (frame) {
var frameColors = {}; var frameColors = {};
var transparentColorInt = 0; // TODO: Fix magic number var transparentColorInt = this.TRANSPARENT_COLOR;
for (var i = 0; i < frame.length; i++) { var colors = 0;
for (var i = 0, length = frame.length; i < length && colors < this.MAX_PALETTE_COLORS; i++) {
var color = frame[i]; var color = frame[i];
if (color !== transparentColorInt) { if (color !== transparentColorInt) {
frameColors[color] = true; if (!frameColors[color]) {
frameColors[color] = true;
colors++;
}
} }
} }
return frameColors; return frameColors;
@ -59,7 +63,9 @@
this.onmessage = function(event) { this.onmessage = function(event) {
try { try {
var frame = event.data; this.TRANSPARENT_COLOR = event.data[0];
this.MAX_PALETTE_COLORS = event.data[1];
var frame = event.data[2];
var colors = getFrameColors(frame); var colors = getFrameColors(frame);
this.postMessage({ this.postMessage({
type : 'SUCCESS', type : 'SUCCESS',