| // 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; | |
| if (reportResultToServer.reported) 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; | |
| // Allow each pixel R,G,B component to be off by a few units to account for possible shading | |
| // differences between GPUs. | |
| function dampenError(error) { | |
| return Math.max(error-2, 0); | |
| } | |
| 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 += dampenError(Math.abs(expected[y*width*4 + x*4 + 0] - actual[y*width*4 + x*4 + 0])); | |
| total += dampenError(Math.abs(expected[y*width*4 + x*4 + 1] - actual[y*width*4 + x*4 + 1])); | |
| total += dampenError(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'; | |
| }; | |
| Module['postRun'] = doReftest; |