我正在尝试使用网络 worker 来渲染动画曼德尔布罗缩放器的部分帧,因为涉及大量计算,并且由于这可以很容易地分成 block ,这应该是并行处理的理想情况.
但无论我如何尝试,我都没有获得任何性能来换取工作人员使用的额外 cpu。与非工作版本相比,在 Chrome 中我的基准测试有点慢,在 Firefox 中则慢得多。
我的猜测是,将图像数据传输给网络 worker 非常昂贵,我尝试只接收原始数据并使用它来渲染帧,但结果大致相同。我不认为这是向工作人员发送和接收图像数据的理想方式(事实上我只需要接收它,但我无法在工作人员内部创建可直接用于 Canvas 的缓冲区) 。因此,发送任何大量数据都会造成真正的瓶颈。
亲爱的 stackoverflow,请帮我回答这两个问题:我在这里做错了什么,还有哪些可以改进的地方?
可以找到演示 here对于 worker ,以及引用 jsfiddle 上的非 worker 版本.
代码如下:
"use strict";
/*global $*/
$(function() {
var mandelbrot = new Mandelbrot();
});
var Mandelbrot = function() {
// set some values
this.width = 500;
this.height = 500;
this.x_center = -1.407566731001088;
this.y_center = 2.741525895538953e-10;
this.iterations = 250;
this.escape = 4,
this.zoom = 10;
this.count = 0;
this.worker_size = 10;
this.received = 0;
this.refresh = true;
//let's go - create canvas, image data and workers
this.init();
//start animation loop
this.animate();
};
Mandelbrot.prototype = {
init: function() {
var self = this;
//create main canvas and append it to div
var container = $("#content");
this.canvas = document.createElement("canvas");
this.canvas.width = this.width;
this.canvas.height = this.height;
container.append(this.canvas);
//create imagedata
this.context = this.canvas.getContext("2d");
this.image = this.context.getImageData(0, 0, this.width, this.height);
this.data = new Int32Array(this.image.data.buffer);
//create imagedata for webworkers
this.worker_data = this.context.getImageData(0, 0, this.width, this.height / this.worker_size);
//create webworkers drop them in array
this.pool = [];
for (var i = 0; i < this.worker_size; i++) {
this.pool[i] = new Worker("js/worker.js");
this.pool[i].idle = true;
this.pool[i].id = i;
//on webworker finished
this.pool[i].onmessage = function(e) {
self.context.putImageData(e.data, 0, self.height / self.worker_size * e.target.id);
self.received++;
};
}
},
iterate: function() {
for (var i = 0; i < this.pool.length; i++) {
this.pool[i].postMessage({
image: this.worker_data,
id: this.pool[i].id,
worker_size: this.worker_size,
width: this.width,
height: this.height,
x_center: this.x_center,
y_center: this.y_center,
iterations: this.iterations,
escape: this.escape,
zoom: this.zoom
});
}
},
animate: function() {
requestAnimationFrame(this.animate.bind(this));
//poor man's benchmark over 250 frames
if (this.count === 0) {
console.time("timer");
}
if (this.count === 250) {
console.timeEnd("timer");
}
//refresh at init, then refresh when all webworkers are done and reset
if (this.received === this.worker_size | this.refresh) {
this.received = 0;
this.refresh = false;
this.count++;
this.zoom *= 0.95;
this.iterate();
}
}
};
和worker.js:
self.onmessage = function(e) {
"use strict";
var x_step = e.data.zoom / e.data.width;
var y_step = e.data.zoom / e.data.height;
var y_start = e.data.height / e.data.worker_size * e.data.id;
var y_end = e.data.height / e.data.worker_size;
var data = new Int32Array(e.data.image.data.buffer);
for (var y = 0; y < y_end; y++) {
var iy = e.data.y_center - e.data.zoom / 2 + (y + y_start) * y_step;
for (var x = 0; x < e.data.width; x++) {
var rx = e.data.x_center - e.data.zoom / 2 + x * x_step;
var zx = rx;
var zy = iy;
var zx2 = 0;
var zy2 = 0;
for (var i = 0; zx2 + zy2 < e.data.escape && i < e.data.iterations; ++i) {
zx2 = zx * zx;
zy2 = zy * zy;
zy = (zx + zx) * zy + iy;
zx = zx2 - zy2 + rx;
}
data[y * e.data.width + x] = (255 << 24) | (i << 16) | (i << 8) | i;
}
}
self.postMessage(e.data.image);
};
最佳答案
问题在于您正在迭代父图片中的每个像素。如果将迭代限制为两个图像中较小的一个,事情会快得多。此外,如果平铺绘图,则每个平铺都可以在单独的 Web Worker 中处理,从而增加图像每个部分的托盘化。我写了这个:http://robertleeplummerjr.github.io/CanvasWorker/这正是你想要的。
关于javascript - Webworker Canvas 性能很糟糕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15987760/