OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <script src="../../../resources/testharness.js"></script> |
| 5 <script src="../../../resources/testharnessreport.js"></script> |
| 6 <script src="./resources/webgl-test.js"></script> |
| 7 <script src="./resources/webgl-test-utils-full.js"></script> |
| 8 </head> |
| 9 <body> |
| 10 <script> |
| 11 var wtu = WebGLTestUtils; |
| 12 var canvas; |
| 13 var gl; |
| 14 var WEBGL_lose_context; |
| 15 var new_WEBGL_lose_context; |
| 16 var allowRestore; |
| 17 var contextLostEventFired; |
| 18 var contextRestoredEventFired; |
| 19 var OES_vertex_array_object; |
| 20 var old_OES_vertex_array_object; |
| 21 var OES_texture_float; |
| 22 var newExtension; |
| 23 |
| 24 async_test(function(t) { |
| 25 if (!setupTest()) { |
| 26 assert_true(false, 'Cannot initialize test'); |
| 27 t.done(); |
| 28 } |
| 29 |
| 30 canvas.addEventListener("webglcontextlost", t.step_func(function(e) { |
| 31 testLostContext(e); |
| 32 // restore the context after this event has exited. |
| 33 setTimeout(function() { |
| 34 // we didn't call prevent default so we should not be able to restor
e the context |
| 35 compareGLError(gl.INVALID_OPERATION, "WEBGL_lose_context.restoreCont
ext()"); |
| 36 testLosingAndRestoringContext(t); |
| 37 }, 0); |
| 38 })); |
| 39 canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext)
; |
| 40 allowRestore = false; |
| 41 contextLostEventFired = false; |
| 42 contextRestoredEventFired = false; |
| 43 |
| 44 testOriginalContext(); |
| 45 WEBGL_lose_context.loseContext(); |
| 46 // The context should be lost immediately. |
| 47 assert_true(gl.isContextLost()); |
| 48 assert_equals(gl.getError(), gl.CONTEXT_LOST_WEBGL); |
| 49 assert_equals(gl.getError(), gl.NO_ERROR); |
| 50 // gl methods should be no-ops |
| 51 compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP
)"); |
| 52 // but the event should not have been fired. |
| 53 assert_false(contextLostEventFired); |
| 54 }, 'Test WebGL context restoration with OffscreenCanvas'); |
| 55 |
| 56 function compareGLError(glError, evalStr) |
| 57 { |
| 58 var exception; |
| 59 try { |
| 60 eval(evalStr); |
| 61 } catch (e) { |
| 62 exception = e; |
| 63 } |
| 64 if (exception) { |
| 65 assert_true(false, evalStr + " threw exception " + exception); |
| 66 } else { |
| 67 assert_equals(gl.getError(), glError); |
| 68 } |
| 69 } |
| 70 |
| 71 function setupTest() |
| 72 { |
| 73 canvas = new OffscreenCanvas(10, 10); |
| 74 gl = canvas.getContext('webgl'); |
| 75 WEBGL_lose_context = getExtensionAndAddProperty(gl, "WEBGL_lose_context"); |
| 76 if (!WEBGL_lose_context) |
| 77 return false; |
| 78 |
| 79 // Try to get a few extensions |
| 80 OES_vertex_array_object = getExtensionAndAddProperty(gl, "OES_vertex_array_o
bject"); |
| 81 OES_texture_float = getExtensionAndAddProperty(gl, "OES_texture_float"); |
| 82 |
| 83 return true; |
| 84 } |
| 85 |
| 86 function getExtensionAndAddProperty(gl, name) { |
| 87 var ext = wtu.getExtensionWithKnownPrefixes(gl, name); |
| 88 if (ext) { |
| 89 ext.webglTestProperty = true; |
| 90 } |
| 91 return ext; |
| 92 } |
| 93 |
| 94 function reGetExtensionAndTestForProperty(gl, name, expectProperty) { |
| 95 newExtension = wtu.getExtensionWithKnownPrefixes(gl, name); |
| 96 // NOTE: while getting a extension after context lost/restored is allowed to f
ail |
| 97 // for the purpose the conformance tests it is not. |
| 98 // |
| 99 // Hypothetically the user can switch GPUs live. For example on Windows, insta
ll 2 GPUs, |
| 100 // then in the control panen enable 1, disable the others and visa versa. Sinc
e the GPUs |
| 101 // have different capabilities one or the other may not support a particlar ex
tension. |
| 102 // |
| 103 // But, for the purpose of the conformance tests the context is expected to re
store |
| 104 // on the same GPU and therefore the extensions that succeeded previously shou
ld |
| 105 // succeed on restore. |
| 106 assert_true(newExtension != null); |
| 107 if (expectProperty) { |
| 108 assert_true(newExtension.webglTestProperty === true); |
| 109 } else { |
| 110 assert_true(newExtension.webglTestProperty === undefined); |
| 111 } |
| 112 return newExtension; |
| 113 } |
| 114 |
| 115 function testLosingAndRestoringContext(t) |
| 116 { |
| 117 if (!setupTest()) { |
| 118 assert_true(false, 'Cannot initialize test'); |
| 119 t.done(); |
| 120 } |
| 121 |
| 122 canvas.addEventListener("webglcontextlost", function(e) { |
| 123 testLostContext(e); |
| 124 // restore the context after this event has exited. |
| 125 setTimeout(function() { |
| 126 compareGLError(gl.NO_ERROR, "WEBGL_lose_context.restoreContext()"); |
| 127 // The context should still be lost. It will not get restored until the |
| 128 // webglrestorecontext event is fired. |
| 129 assert_true(gl.isContextLost()); |
| 130 assert_equals(gl.getError(), gl.NO_ERROR); |
| 131 // gl methods should still be no-ops |
| 132 compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE
_MAP)"); |
| 133 }, 0); |
| 134 }); |
| 135 canvas.addEventListener("webglcontextrestored", t.step_func_done(function()
{ |
| 136 testRestoredContext(); |
| 137 })); |
| 138 allowRestore = true; |
| 139 contextLostEventFired = false; |
| 140 contextRestoredEventFired = false; |
| 141 |
| 142 testOriginalContext(); |
| 143 WEBGL_lose_context.loseContext(); |
| 144 // The context should be lost immediately. |
| 145 assert_true(gl.isContextLost()); |
| 146 assert_equals(gl.getError(), gl.CONTEXT_LOST_WEBGL); |
| 147 assert_equals(gl.getError(), gl.NO_ERROR); |
| 148 // gl methods should be no-ops |
| 149 compareGLError(gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP
)"); |
| 150 // but the event should not have been fired. |
| 151 assert_false(contextLostEventFired); |
| 152 } |
| 153 |
| 154 function testOriginalContext() |
| 155 { |
| 156 assert_false(gl.isContextLost()); |
| 157 assert_equals(gl.getError(), gl.NO_ERROR); |
| 158 } |
| 159 |
| 160 function testLostContext(e) |
| 161 { |
| 162 assert_false(contextLostEventFired); |
| 163 contextLostEventFired = true; |
| 164 assert_true(gl.isContextLost()); |
| 165 assert_equals(gl.getError(), gl.NO_ERROR); |
| 166 if (allowRestore) |
| 167 e.preventDefault(); |
| 168 } |
| 169 |
| 170 function testShouldNotRestoreContext(e) |
| 171 { |
| 172 assert_true(false, "Should not restore the context unless preventDefault is
called on the context lost event"); |
| 173 } |
| 174 |
| 175 function testOESTextureFloat() { |
| 176 if (OES_texture_float) { |
| 177 // Extension must still be lost. |
| 178 var tex = gl.createTexture(); |
| 179 gl.bindTexture(gl.TEXTURE_2D, tex); |
| 180 compareGLError(gl.INVALID_ENUM, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1,
1, 0, gl.RGBA, gl.FLOAT, null)"); |
| 181 // Try re-enabling extension |
| 182 OES_texture_float = reGetExtensionAndTestForProperty(gl, "OES_texture_float"
, false); |
| 183 compareGLError(gl, gl.NO_ERROR, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1,
1, 0, gl.RGBA, gl.FLOAT, null)"); |
| 184 } |
| 185 } |
| 186 |
| 187 function testOESVertexArrayObject() { |
| 188 if (OES_vertex_array_object) { |
| 189 // Extension must still be lost. |
| 190 assert_equals(OES_vertex_array_object.createVertexArrayOES(), null); |
| 191 // Try re-enabling extension |
| 192 |
| 193 old_OES_vertex_array_object = OES_vertex_array_object; |
| 194 OES_vertex_array_object = reGetExtensionAndTestForProperty(gl, "OES_vertex_a
rray_object", false); |
| 195 assert_true(OES_vertex_array_object.createVertexArrayOES() != null); |
| 196 assert_true(old_OES_vertex_array_object.createVertexArrayOES() == null); |
| 197 } |
| 198 } |
| 199 |
| 200 function testExtensions() { |
| 201 testOESTextureFloat(); |
| 202 testOESVertexArrayObject(); |
| 203 // Only the WEBGL_lose_context extension should be the same object after conte
xt lost. |
| 204 new_WEBGL_lose_context = reGetExtensionAndTestForProperty(gl, "WEBGL_lose_cont
ext", true); |
| 205 } |
| 206 |
| 207 function testRestoredContext() |
| 208 { |
| 209 assert_false(contextRestoredEventFired); |
| 210 contextRestoredEventFired = true; |
| 211 assert_false(gl.isContextLost()); |
| 212 assert_equals(gl.getError(), gl.NO_ERROR); |
| 213 testExtensions(); |
| 214 } |
| 215 </script> |
| 216 </body> |
| 217 </html> |
OLD | NEW |