|
| 1 | +const TILE_SIZE = 3; |
| 2 | +const SCALE = 20; |
| 3 | +function createTileCanvas() { |
| 4 | + const canvas = document.createElement('canvas'); |
| 5 | + canvas.width = TILE_SIZE; |
| 6 | + canvas.height = TILE_SIZE; |
| 7 | + return canvas; |
| 8 | +} |
| 9 | + |
| 10 | +function fillUnsupported(canvas) { |
| 11 | + const ctx = canvas.getContext('2d'); |
| 12 | + ctx.fillStyle = 'rgb(255, 0, 255)'; |
| 13 | + ctx.fillRect(0, 0, TILE_SIZE, TILE_SIZE); |
| 14 | +} |
| 15 | + |
| 16 | +function fillError() { |
| 17 | + const output = document.getElementById('output'); |
| 18 | + output.width = 40; |
| 19 | + output.height = 40; |
| 20 | + const ctx = output.getContext('2d'); |
| 21 | + ctx.fillStyle = 'rgb(255, 0, 0)'; |
| 22 | + ctx.fillRect(0, 0, output.width, output.height); |
| 23 | +} |
| 24 | + |
| 25 | +async function loadImage(src) { |
| 26 | + const image = new Image(); |
| 27 | + await new Promise((resolve, reject) => { |
| 28 | + image.onload = resolve; |
| 29 | + image.onerror = () => reject(new Error(`image load failed: ${src}`)); |
| 30 | + image.src = src; |
| 31 | + }); |
| 32 | + return image; |
| 33 | +} |
| 34 | + |
| 35 | +async function renderDrawImageTile(image) { |
| 36 | + const canvas = createTileCanvas(); |
| 37 | + const ctx = canvas.getContext('2d'); |
| 38 | + ctx.drawImage(image, 0, 0); |
| 39 | + return canvas; |
| 40 | +} |
| 41 | + |
| 42 | +async function renderImageBitmapTile(src) { |
| 43 | + const canvas = createTileCanvas(); |
| 44 | + const ctx = canvas.getContext('2d'); |
| 45 | + |
| 46 | + try { |
| 47 | + const response = await fetch(src); |
| 48 | + const blob = await response.blob(); |
| 49 | + const bitmap = await createImageBitmap(blob); |
| 50 | + ctx.drawImage(bitmap, 0, 0); |
| 51 | + bitmap.close(); |
| 52 | + return canvas; |
| 53 | + } catch (error) { |
| 54 | + fillUnsupported(canvas); |
| 55 | + return canvas; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function makeImageDataFromWebGLPixels(pixels) { |
| 60 | + const flipped = new Uint8ClampedArray(pixels.length); |
| 61 | + const rowWidth = TILE_SIZE * 4; |
| 62 | + for (let y = 0; y < TILE_SIZE; ++y) { |
| 63 | + const sourceOffset = (TILE_SIZE - 1 - y) * rowWidth; |
| 64 | + const destinationOffset = y * rowWidth; |
| 65 | + flipped.set(pixels.subarray(sourceOffset, sourceOffset + rowWidth), |
| 66 | + destinationOffset); |
| 67 | + } |
| 68 | + return new ImageData(flipped, TILE_SIZE, TILE_SIZE); |
| 69 | +} |
| 70 | + |
| 71 | +function renderWebGLTile(image) { |
| 72 | + const outputCanvas = createTileCanvas(); |
| 73 | + const outputContext = outputCanvas.getContext('2d'); |
| 74 | + |
| 75 | + const webglCanvas = createTileCanvas(); |
| 76 | + const gl = webglCanvas.getContext('webgl'); |
| 77 | + if (!gl) { |
| 78 | + fillUnsupported(outputCanvas); |
| 79 | + return outputCanvas; |
| 80 | + } |
| 81 | + |
| 82 | + const texture = gl.createTexture(); |
| 83 | + const framebuffer = gl.createFramebuffer(); |
| 84 | + if (!texture || !framebuffer) { |
| 85 | + fillUnsupported(outputCanvas); |
| 86 | + return outputCanvas; |
| 87 | + } |
| 88 | + |
| 89 | + gl.bindTexture(gl.TEXTURE_2D, texture); |
| 90 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 91 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 92 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 93 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 94 | + |
| 95 | + try { |
| 96 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image); |
| 97 | + } catch (error) { |
| 98 | + fillUnsupported(outputCanvas); |
| 99 | + gl.deleteFramebuffer(framebuffer); |
| 100 | + gl.deleteTexture(texture); |
| 101 | + return outputCanvas; |
| 102 | + } |
| 103 | + |
| 104 | + gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer); |
| 105 | + gl.framebufferTexture2D( |
| 106 | + gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0); |
| 107 | + |
| 108 | + if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) { |
| 109 | + fillUnsupported(outputCanvas); |
| 110 | + gl.deleteFramebuffer(framebuffer); |
| 111 | + gl.deleteTexture(texture); |
| 112 | + return outputCanvas; |
| 113 | + } |
| 114 | + |
| 115 | + const pixels = new Uint8Array(TILE_SIZE * TILE_SIZE * 4); |
| 116 | + gl.readPixels(0, 0, TILE_SIZE, TILE_SIZE, gl.RGBA, gl.UNSIGNED_BYTE, pixels); |
| 117 | + |
| 118 | + const imageData = makeImageDataFromWebGLPixels(pixels); |
| 119 | + outputContext.putImageData(imageData, 0, 0); |
| 120 | + |
| 121 | + gl.deleteFramebuffer(framebuffer); |
| 122 | + gl.deleteTexture(texture); |
| 123 | + return outputCanvas; |
| 124 | +} |
| 125 | + |
| 126 | +function convertFloatChannelToByte(value) { |
| 127 | + if (!Number.isFinite(value)) { |
| 128 | + return 0; |
| 129 | + } |
| 130 | + if (value <= 0) { |
| 131 | + return 0; |
| 132 | + } |
| 133 | + if (value >= 1) { |
| 134 | + return 255; |
| 135 | + } |
| 136 | + return Math.round(value * 255); |
| 137 | +} |
| 138 | + |
| 139 | +function renderFloat16Tile(image) { |
| 140 | + const canvas = createTileCanvas(); |
| 141 | + const ctx = canvas.getContext('2d'); |
| 142 | + ctx.drawImage(image, 0, 0); |
| 143 | + |
| 144 | + let floatData; |
| 145 | + try { |
| 146 | + floatData = |
| 147 | + ctx.getImageData(0, 0, TILE_SIZE, TILE_SIZE, |
| 148 | + {pixelFormat: 'rgba-float16'}).data; |
| 149 | + } catch (error) { |
| 150 | + fillUnsupported(canvas); |
| 151 | + return canvas; |
| 152 | + } |
| 153 | + |
| 154 | + if (!(floatData instanceof Float16Array)) { |
| 155 | + fillUnsupported(canvas); |
| 156 | + return canvas; |
| 157 | + } |
| 158 | + |
| 159 | + const converted = new Uint8ClampedArray(floatData.length); |
| 160 | + for (let i = 0; i < floatData.length; ++i) { |
| 161 | + converted[i] = convertFloatChannelToByte(floatData[i]); |
| 162 | + } |
| 163 | + |
| 164 | + ctx.putImageData(new ImageData(converted, TILE_SIZE, TILE_SIZE), 0, 0); |
| 165 | + return canvas; |
| 166 | +} |
| 167 | + |
| 168 | +function drawOutputTiles(tiles) { |
| 169 | + const output = document.getElementById('output'); |
| 170 | + output.width = TILE_SIZE * SCALE * tiles.length; |
| 171 | + output.height = TILE_SIZE * SCALE; |
| 172 | + |
| 173 | + const ctx = output.getContext('2d'); |
| 174 | + ctx.imageSmoothingEnabled = false; |
| 175 | + |
| 176 | + const scaledTileSize = TILE_SIZE * SCALE; |
| 177 | + for (let i = 0; i < tiles.length; ++i) { |
| 178 | + ctx.drawImage( |
| 179 | + tiles[i], |
| 180 | + 0, |
| 181 | + 0, |
| 182 | + TILE_SIZE, |
| 183 | + TILE_SIZE, |
| 184 | + i * scaledTileSize, |
| 185 | + 0, |
| 186 | + scaledTileSize, |
| 187 | + scaledTileSize); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +async function runCanvasDecodePathReftest(source) { |
| 192 | + try { |
| 193 | + const image = await loadImage(source); |
| 194 | + const drawImageTile = await renderDrawImageTile(image); |
| 195 | + const imageBitmapTile = await renderImageBitmapTile(source); |
| 196 | + const webglTile = renderWebGLTile(image); |
| 197 | + const float16Tile = renderFloat16Tile(image); |
| 198 | + drawOutputTiles([drawImageTile, imageBitmapTile, webglTile, float16Tile]); |
| 199 | + } catch (error) { |
| 200 | + fillError(); |
| 201 | + } finally { |
| 202 | + document.documentElement.classList.remove('reftest-wait'); |
| 203 | + } |
| 204 | +} |
0 commit comments