OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <body> |
| 4 <canvas id="canvas" width="3" height="2"></canvas> |
| 5 <script src="../../resources/js-test.js"></script> |
| 6 <script> |
| 7 jsTestIsAsync = true; |
| 8 var worker = new Worker('./resources/canvas-ImageBitmap-transferable.js'); |
| 9 |
| 10 description("Tests that ImageBitmap is transferable and that the pixel data surv
ives the trip, as well as createImageBitmap(ImageBitmap) works on worker thread"
); |
| 11 |
| 12 var imageWidth = 3; |
| 13 var imageHeight = 2; |
| 14 var bitmapWidth; |
| 15 var bitmapHeight; |
| 16 var newImage; |
| 17 var newImageBitmap; |
| 18 var image = new ImageData(new Uint8ClampedArray( |
| 19 [255, 0, 0, 255, |
| 20 0, 255, 0, 255, |
| 21 255, 255, 255, 127, |
| 22 0, 0, 0, 64, |
| 23 12, 34, 56, 64, |
| 24 12, 34, 56, 127]), |
| 25 imageWidth, imageHeight); |
| 26 var context = document.getElementById("canvas").getContext("2d"); |
| 27 |
| 28 createImageBitmap(image).then(imageBitmap => { |
| 29 bitmapWidth = imageBitmap.width; |
| 30 bitmapHeight = imageBitmap.height; |
| 31 shouldBe("bitmapWidth", "imageWidth"); |
| 32 shouldBe("bitmapHeight", "imageHeight"); |
| 33 |
| 34 worker.postMessage({data: imageBitmap}, [imageBitmap]); |
| 35 |
| 36 // ImageBitmap has been transfered to worker, main thread loses ownership |
| 37 // of it, so the width and height should be 0 now. |
| 38 bitmapWidth = imageBitmap.width; |
| 39 bitmapHeight = imageBitmap.height; |
| 40 shouldBe("bitmapWidth", "0"); |
| 41 shouldBe("bitmapHeight", "0"); |
| 42 }); |
| 43 |
| 44 worker.onmessage = function(e) { |
| 45 newImageBitmap = e.data.data; |
| 46 bitmapWidth = newImageBitmap.width; |
| 47 bitmapHeight = newImageBitmap.height; |
| 48 |
| 49 // Getting the ImageBitmap back from the worker thread |
| 50 shouldBe("bitmapWidth", "imageWidth"); |
| 51 shouldBe("bitmapHeight", "imageHeight"); |
| 52 |
| 53 context.drawImage(newImageBitmap, 0, 0); |
| 54 newImage = context.getImageData(0, 0, imageWidth, imageHeight); |
| 55 |
| 56 // newImage is not necessary the same as imageData because of |
| 57 // multiplying and dividing by alpha during the round trip, but |
| 58 // they should be close. |
| 59 // The alpha channel should be exactly the same. |
| 60 shouldBeCloseTo("newImage.data[0]", "image.data[0]", 5); |
| 61 shouldBeCloseTo("newImage.data[1]", "image.data[1]", 5); |
| 62 shouldBeCloseTo("newImage.data[2]", "image.data[2]", 5); |
| 63 shouldBe("newImage.data[3]", "image.data[3]"); |
| 64 |
| 65 shouldBeCloseTo("newImage.data[4]", "image.data[4]", 5); |
| 66 shouldBeCloseTo("newImage.data[5]", "image.data[5]", 5); |
| 67 shouldBeCloseTo("newImage.data[6]", "image.data[6]", 5); |
| 68 shouldBe("newImage.data[7]", "image.data[7]"); |
| 69 |
| 70 shouldBeCloseTo("newImage.data[8]", "image.data[8]", 5); |
| 71 shouldBeCloseTo("newImage.data[9]", "image.data[9]", 5); |
| 72 shouldBeCloseTo("newImage.data[10]", "image.data[10]", 5); |
| 73 shouldBe("newImage.data[11]", "image.data[11]"); |
| 74 |
| 75 shouldBeCloseTo("newImage.data[12]", "image.data[12]", 5); |
| 76 shouldBeCloseTo("newImage.data[13]", "image.data[13]", 5); |
| 77 shouldBeCloseTo("newImage.data[14]", "image.data[14]", 5); |
| 78 shouldBe("newImage.data[15]", "image.data[15]"); |
| 79 |
| 80 shouldBeCloseTo("newImage.data[16]", "image.data[16]", 5); |
| 81 shouldBeCloseTo("newImage.data[17]", "image.data[17]", 5); |
| 82 shouldBeCloseTo("newImage.data[18]", "image.data[18]", 5); |
| 83 shouldBe("newImage.data[19]", "image.data[19]"); |
| 84 |
| 85 shouldBeCloseTo("newImage.data[20]", "image.data[20]", 5); |
| 86 shouldBeCloseTo("newImage.data[21]", "image.data[21]", 5); |
| 87 shouldBeCloseTo("newImage.data[22]", "image.data[22]", 5); |
| 88 shouldBe("newImage.data[23]", "image.data[23]"); |
| 89 finishJSTest(); |
| 90 } |
| 91 |
| 92 </script> |
| 93 </body> |
| 94 </html> |
OLD | NEW |