forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreftest.js
110 lines (98 loc) · 3.72 KB
/
reftest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Code for performing browser reftest where we compare generated canvas
// image with an expected output image.
//
// This code expects the reftestRebaseline global to exist.
// See make_reftest in test/common.py.
// We have some tests that don't perform rendering during `main` so
// the normal process of performing `doReftest` in `postRun` doesn't
// work. These tests can delay the call to `doReftest` by calling
// `reftestBlock` and then calling `reftestUnblock` once they have
// done their rendering.
var reftestBlocked = false;
function reftestBlock() {
reftestBlocked = true;
}
function reftestUnblock() {
reftestBlocked = false;
doReftest();
}
function doReftest() {
if (reftestBlocked) return;
if (doReftest.done) return;
doReftest.done = true;
var img = new Image();
img.onload = () => {
assert(img.width == Module['canvas'].width, `Invalid width: ${Module['canvas'].width}, should be ${img.width}`);
assert(img.height == Module['canvas'].height, `Invalid height: ${Module['canvas'].height}, should be ${img.height}`);
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
var expected = ctx.getImageData(0, 0, img.width, img.height).data;
var actualUrl = Module['canvas'].toDataURL();
var actualImage = new Image();
actualImage.onload = () => {
/*
document.body.appendChild(img); // for comparisons
var div = document.createElement('div');
div.innerHTML = '^=expected, v=actual';
document.body.appendChild(div);
document.body.appendChild(actualImage); // to grab it for creating the test reference
*/
var actualCanvas = document.createElement('canvas');
actualCanvas.width = actualImage.width;
actualCanvas.height = actualImage.height;
var actualCtx = actualCanvas.getContext('2d');
actualCtx.drawImage(actualImage, 0, 0);
var actual = actualCtx.getImageData(0, 0, actualImage.width, actualImage.height).data;
var total = 0;
var width = img.width;
var height = img.height;
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
total += Math.abs(expected[y*width*4 + x*4 + 0] - actual[y*width*4 + x*4 + 0]);
total += Math.abs(expected[y*width*4 + x*4 + 1] - actual[y*width*4 + x*4 + 1]);
total += Math.abs(expected[y*width*4 + x*4 + 2] - actual[y*width*4 + x*4 + 2]);
}
}
// floor, to allow some margin of error for antialiasing
var wrong = Math.floor(total / (img.width*img.height*3));
if (wrong || reftestRebaseline) {
// Generate a png of the actual rendered image and send it back
// to the server.
Module['canvas'].toBlob((blob) => {
sendFileToServer('actual.png', blob);
reportResultToServer(wrong);
})
} else {
reportResultToServer(wrong);
}
};
actualImage.src = actualUrl;
}
img.src = 'expected.png';
};
function setupRefTest() {
Module['postRun'] = doReftest;
if (typeof WebGLClient !== 'undefined') {
// trigger reftest from RAF as well, needed for workers where there is no pre|postRun on the main thread
var realRAF = window.requestAnimationFrame;
/** @suppress{checkTypes} */
window.requestAnimationFrame = (func) => {
return realRAF(() => {
func();
realRAF(doReftest);
});
};
// trigger reftest from canvas render too, for workers not doing GL
var realWOM = worker.onmessage;
worker.onmessage = (event) => {
realWOM(event);
if (event.data.target === 'canvas' && event.data.op === 'render') {
realRAF(doReftest);
}
};
}
}
setupRefTest();