2 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
4 <title>WebGL test: CaptureStream()
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
8 <script src=
"captureStream_common.js">
9 <script src=
"driver-info.js"></script>
10 <script src=
"webgl-util.js"></script>
11 <script id=
"vs" type=
"x-shader/x-vertex">
13 attribute vec2 aVertCoord;
16 gl_Position = vec4(aVertCoord,
0.0,
1.0);
20 <script id=
"fs" type=
"x-shader/x-fragment">
22 precision mediump float;
26 gl_FragColor = uColor;
33 // Globals. Initialized during beginTest().
34 var c
; // Canvas element captured by streams.
35 var gl
; // WebGLContext of |c|.
36 var h
; // CaptureStreamTestHelper holding utility test functions.
37 var vauto
; // Video element with captureStream stream in automatic mode.
38 var vmanual
; // Video element with captureStream stream in manual (fps 0) mode.
39 var vrate
; // Video element with captureStream stream with fixed frame rate.
41 /* Fails the test if there was a GL error */
42 function checkGLError(info
) {
43 var error
= gl
.getError();
44 // Comparing strings for sake of log output in hex format.
45 is("0x" + error
.toString(16), "0x0", "WebGL error [" + info
+ "]");
48 function checkClearColorInitialRed() {
49 info("Checking that clearing to red works for first frame.");
51 h
.clearColor(c
, h
.red
);
53 vauto
.srcObject
= c
.captureStream();
54 vmanual
.srcObject
= c
.captureStream(0);
55 vrate
.srcObject
= c
.captureStream(10);
57 ok(h
.isPixel(h
.getPixel(vauto
), h
.blackTransparent
),
58 "vauto should not be drawn to before stable state");
59 ok(h
.isPixel(h
.getPixel(vrate
), h
.blackTransparent
),
60 "vrate should not be drawn to before stable state");
61 ok(h
.isPixel(h
.getPixel(vmanual
), h
.blackTransparent
),
62 "vmanual should not be drawn to before stable state");
64 return Promise
.resolve()
65 .then(() => h
.pixelMustBecome(vauto
, h
.red
, {
66 infoString
: "should become red automatically",
68 .then(() => h
.pixelMustBecome(vrate
, h
.red
, {
69 infoString
: "should become red automatically",
71 .then(() => h
.pixelMustBecome(vmanual
, h
.red
, {
72 infoString
: "should become red when we get to stable "
73 + "state (first frame)",
77 function checkDrawColorGreen() {
78 info("Checking that drawing green results in green video frames.");
79 var drawing
= h
.startDrawing(h
.drawColor
.bind(h
, c
, h
.green
));
80 checkGLError('after DrawColor');
81 return Promise
.resolve()
82 .then(() => h
.pixelMustBecome(vauto
, h
.green
, {
83 infoString
: "should become green automatically",
85 .then(() => h
.pixelMustBecome(vrate
, h
.green
, {
86 infoString
: "should become green automatically",
88 .then(() => h
.pixelMustBecome(vmanual
, h
.red
, {
89 infoString
: "should still be red",
91 .then(() => h
.requestFrame(vmanual
))
92 .then(() => h
.pixelMustBecome(vmanual
, h
.green
, {
93 infoString
: "should become green after requstFrame()",
95 .then(() => drawing
.stop());
98 function checkClearColorRed() {
99 info("Checking that clearing to red works.");
100 var drawing
= h
.startDrawing(h
.clearColor
.bind(h
, c
, h
.red
));
101 return Promise
.resolve()
102 .then(() => h
.pixelMustBecome(vauto
, h
.red
, {
103 infoString
: "should become red automatically",
105 .then(() => h
.pixelMustBecome(vrate
, h
.red
, {
106 infoString
: "should become red automatically",
108 .then(() => h
.pixelMustBecome(vmanual
, h
.green
, {
109 infoString
: "should still be green",
111 .then(() => h
.requestFrame(vmanual
))
112 .then(() => h
.pixelMustBecome(vmanual
, h
.red
, {
113 infoString
: "should become red after requestFrame()",
115 .then(() => drawing
.stop());
118 function checkRequestFrameOrderGuarantee() {
119 info("Checking that requestFrame() immediately after a draw " +
120 "call results in the expected frame seen in the stream.");
121 return Promise
.resolve()
122 .then(() => h
.pixelMustBecome(vmanual
, h
.red
, 0, "should still be red"))
123 .then(() => h
.drawColor(c
, h
.green
)) // 1. Draw canvas green
124 .then(() => h
.requestFrame(vmanual
)) // 2. Immediately request a frame
125 .then(() => h
.pixelMustBecome(vmanual
, h
.green
, {
126 infoString
: "should become green after call order test",
130 function checkEndedOnStop() {
131 let promises
= [vauto
, vmanual
, vrate
].map(elem
=> {
132 elem
.srcObject
.getTracks()[0].stop();
133 return new Promise(resolve
=>
134 elem
.addEventListener("ended", function endedListener(event
) {
135 ok(true, "Element " + elem
.id
+ " ended.");
137 elem
.removeEventListener("ended", endedListener
);
140 return Promise
.all(promises
);
145 ok(true, 'Test complete.');
149 function beginTest() {
150 h
= new CaptureStreamTestHelperWebGL();
152 c
= h
.createAndAppendElement('canvas', 'c');
153 vauto
= h
.createAndAppendElement('video', 'vauto');
154 vmanual
= h
.createAndAppendElement('video', 'vmanual');
155 vrate
= h
.createAndAppendElement('video', 'vrate');
157 gl
= c
.getContext('webgl');
159 todo(false, 'WebGL is unavailable.');
164 gl
.disable(gl
.DEPTH_TEST
);
166 prog
= WebGLUtil
.createProgramByIds(gl
, 'vs', 'fs');
168 ok(false, 'Program linking should succeed.');
172 // Setup vertex coordinates for drawing a rectangle across the whole canvas.
174 prog
.aVertCoord
= gl
.getAttribLocation(prog
, "aVertCoord");
175 ok(prog
.aVertCoord
>= 0, '`aVertCoord` should be valid.');
177 var vertCoordArr
= new Float32Array([
183 var vertCoordBuff
= gl
.createBuffer();
184 gl
.bindBuffer(gl
.ARRAY_BUFFER
, vertCoordBuff
);
185 gl
.bufferData(gl
.ARRAY_BUFFER
, vertCoordArr
, gl
.STATIC_DRAW
);
188 gl
.enableVertexAttribArray(prog
.aVertCoord
);
189 gl
.vertexAttribPointer(prog
.aVertCoord
, 2, gl
.FLOAT
, false, 0, 0);
191 // Setup the helper with a pointer to how to change fragment color.
193 var uColorLocation
= gl
.getUniformLocation(prog
, "uColor");
194 h
.setFragmentColorLocation(uColorLocation
);
196 checkGLError('after setup');
201 .then(checkClearColorInitialRed
)
202 .then(checkDrawColorGreen
)
203 .then(checkClearColorRed
)
204 .then(checkRequestFrameOrderGuarantee
)
205 .then(checkEndedOnStop
)
209 SimpleTest
.waitForExplicitFinish();