videoFrame-drawImage.any.js (3628B)
1 // META: global=window,dedicatedworker 2 // META: script=/webcodecs/utils.js 3 4 function testDrawImageFromVideoFrame( 5 width, height, expectedPixel, canvasOptions, imageSetting) { 6 let vfInit = 7 {format: 'RGBA', timestamp: 0, codedWidth: width, codedHeight: height}; 8 let data = new Uint32Array(vfInit.codedWidth * vfInit.codedHeight); 9 data.fill(0xFF966432); // 'rgb(50, 100, 150)'; 10 let frame = new VideoFrame(data, vfInit); 11 let canvas = new OffscreenCanvas(width, height); 12 let ctx = canvas.getContext('2d', canvasOptions); 13 ctx.drawImage(frame, 0, 0); 14 testCanvas(ctx, width, height, expectedPixel, imageSetting, assert_equals); 15 frame.close(); 16 } 17 18 test(_ => { 19 return testDrawImageFromVideoFrame(48, 36, kSRGBPixel); 20 }, 'drawImage(VideoFrame) with canvas(48x36 srgb uint8).'); 21 22 test(_ => { 23 return testDrawImageFromVideoFrame(480, 360, kSRGBPixel); 24 }, 'drawImage(VideoFrame) with canvas(480x360 srgb uint8).'); 25 26 test(_ => { 27 return testDrawImageFromVideoFrame( 28 48, 36, kP3Pixel, {colorSpace: 'display-p3'}, {colorSpaceConversion: 'none'}, 29 {colorSpace: 'display-p3'}); 30 }, 'drawImage(VideoFrame) with canvas(48x36 display-p3 uint8).'); 31 32 test(_ => { 33 return testDrawImageFromVideoFrame( 34 480, 360, kP3Pixel, {colorSpace: 'display-p3'}, {colorSpaceConversion: 'none'}, 35 {colorSpace: 'display-p3'}); 36 }, 'drawImage(VideoFrame) with canvas(480x360 display-p3 uint8).'); 37 38 test(_ => { 39 let width = 128; 40 let height = 128; 41 let vfInit = 42 {format: 'RGBA', timestamp: 0, codedWidth: width, codedHeight: height}; 43 let data = new Uint32Array(vfInit.codedWidth * vfInit.codedHeight); 44 data.fill(0xFF966432); // 'rgb(50, 100, 150)'; 45 let frame = new VideoFrame(data, vfInit); 46 let canvas = new OffscreenCanvas(width, height); 47 let ctx = canvas.getContext('2d'); 48 49 frame.close(); 50 assert_throws_dom('InvalidStateError', _ => { 51 ctx.drawImage(frame, 0, 0); 52 }, 'drawImage with a closed VideoFrame should throw InvalidStateError.'); 53 }, 'drawImage on a closed VideoFrame throws InvalidStateError.'); 54 55 56 test(_ => { 57 let canvas = new OffscreenCanvas(128, 128); 58 let ctx = canvas.getContext('2d'); 59 60 let init = {alpha: 'discard', timestamp: 33090}; 61 let frame = new VideoFrame(canvas, {timestamp: 0}); 62 let frame2 = new VideoFrame(frame, init); 63 let frame3 = new VideoFrame(frame2, init); 64 65 ctx.drawImage(frame3, 0, 0); 66 frame.close(); 67 frame2.close(); 68 frame3.close(); 69 }, 'drawImage of nested frame works properly'); 70 71 test(_ => { 72 const width = 128; 73 const height = 128; 74 let vfInit = {format: 'RGBA', timestamp: 0, 75 codedWidth: width, codedHeight: height, 76 displayWidth: width / 2, displayHeight: height / 2}; 77 let data = new Uint32Array(vfInit.codedWidth * vfInit.codedHeight); 78 data.fill(0xFF966432); // 'rgb(50, 100, 150)'; 79 let frame = new VideoFrame(data, vfInit); 80 let canvas = new OffscreenCanvas(width, height); 81 let ctx = canvas.getContext('2d'); 82 ctx.fillStyle = "#FFFFFF"; 83 ctx.fillRect(0, 0, width, height); 84 ctx.drawImage(frame, 0, 0); 85 frame.close(); 86 87 function peekPixel(ctx, x, y) { 88 return ctx.getImageData(x, y, 1, 1).data; 89 } 90 91 assert_array_equals(peekPixel(ctx, 0, 0), [50, 100, 150, 255]); 92 assert_array_equals(peekPixel(ctx, width / 2 - 1, height / 2 - 1), 93 [50, 100, 150, 255]); 94 assert_array_equals(peekPixel(ctx, width / 2 + 1, height / 2 + 1), 95 [255, 255, 255, 255]); 96 assert_array_equals(peekPixel(ctx, width - 1, height - 1), 97 [255, 255, 255, 255]); 98 }, 'drawImage with display size != visible size');