summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/context/context-lost-restored.html
blob: 6942e3d1ce4b970b7a5b4971a9b7e86c57e9d653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script>
"use strict";
var wtu = WebGLTestUtils;
var canvas;
var gl;
var shouldGenerateGLError;
var WEBGL_lose_context;
var new_WEBGL_lose_context;
var bufferObjects;
var program;
var texture;
var texColor = [255, 10, 20, 255];
var allowRestore;
var contextLostEventFired;
var contextRestoredEventFired;
var OES_vertex_array_object;
var old_OES_vertex_array_object;
var vertexArrayObject;
var OES_texture_float;
var newExtension;

function init()
{
    enableJSTestPreVerboseLogging();
    description("Tests behavior under a restored context.");

    shouldGenerateGLError = wtu.shouldGenerateGLError;
    testLosingContext();
}

function setupTest()
{
    canvas = document.createElement("canvas");
    canvas.width = 1;
    canvas.height = 1;
    gl = wtu.create3DContext(canvas);
    WEBGL_lose_context = getExtensionAndAddProperty(gl, "WEBGL_lose_context");
    if (!WEBGL_lose_context) {
        debug("Could not find WEBGL_lose_context extension");
        return false;
    }

    // Try to get a few extensions
    OES_vertex_array_object = getExtensionAndAddProperty(gl, "OES_vertex_array_object");
    OES_texture_float = getExtensionAndAddProperty(gl, "OES_texture_float");

    return true;
}

function getExtensionAndAddProperty(gl, name) {
  var ext = wtu.getExtensionWithKnownPrefixes(gl, name);
  if (ext) {
    ext.webglTestProperty = true;
  }
  return ext;
}

function reGetExtensionAndTestForProperty(gl, name, expectProperty) {
  newExtension = wtu.getExtensionWithKnownPrefixes(gl, name);
  // NOTE: while getting a extension after context lost/restored is allowed to fail
  // for the purpose the conformance tests it is not.
  //
  // Hypothetically the user can switch GPUs live. For example on Windows, install 2 GPUs,
  // then in the control panen enable 1, disable the others and visa versa. Since the GPUs
  // have different capabilities one or the other may not support a particlar extension.
  //
  // But, for the purpose of the conformance tests the context is expected to restore
  // on the same GPU and therefore the extensions that succeeded previously should
  // succeed on restore.
  shouldBeTrue("newExtension != null");
  if (expectProperty) {
    shouldBeTrue("newExtension.webglTestProperty === true");
  } else {
    shouldBeTrue("newExtension.webglTestProperty === undefined");
  }
  return newExtension;
}

function testLosingContext()
{
    if (!setupTest()) {
        finishTest();
        return;
    }

    debug("Test losing a context and inability to restore it.");

    canvas.addEventListener("webglcontextlost", function(e) {
       testLostContext(e);
       // restore the context after this event has exited.
       setTimeout(function() {
         // we didn't call prevent default so we should not be able to restore the context
         shouldGenerateGLError(gl, gl.INVALID_OPERATION, "WEBGL_lose_context.restoreContext()");
         testLosingAndRestoringContext();
       }, 0);
    });
    canvas.addEventListener("webglcontextrestored", testShouldNotRestoreContext);
    allowRestore = false;
    contextLostEventFired = false;
    contextRestoredEventFired = false;

    testOriginalContext();
    WEBGL_lose_context.loseContext();
    // The context should be lost immediately.
    shouldBeTrue("gl.isContextLost()");
    shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL");
    shouldBe("gl.getError()", "gl.NO_ERROR");
    // gl methods should be no-ops
    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
    // but the event should not have been fired.
    shouldBeFalse("contextLostEventFired");
}

function testLosingAndRestoringContext()
{
    if (!setupTest())
        finishTest();

    debug("");
    debug("Test losing and restoring a context.");

    canvas.addEventListener("webglcontextlost", function(e) {
      testLostContext(e);
      // restore the context after this event has exited.
      setTimeout(function() {
        shouldGenerateGLError(gl, gl.NO_ERROR, "WEBGL_lose_context.restoreContext()");
        // Calling restoreContext() twice should not cause error or crash
        shouldGenerateGLError(gl, gl.NO_ERROR, "WEBGL_lose_context.restoreContext()");
        // The context should still be lost. It will not get restored until the
        // webglrestorecontext event is fired.
        shouldBeTrue("gl.isContextLost()");
        shouldBe("gl.getError()", "gl.NO_ERROR");
        // gl methods should still be no-ops
        shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
      }, 0);
    });
    canvas.addEventListener("webglcontextrestored", function() {
      testRestoredContext();
      finishTest();
    });
    allowRestore = true;
    contextLostEventFired = false;
    contextRestoredEventFired = false;

    testOriginalContext();
    WEBGL_lose_context.loseContext();
    // The context should be lost immediately.
    shouldBeTrue("gl.isContextLost()");
    shouldBe("gl.getError()", "gl.CONTEXT_LOST_WEBGL");
    shouldBe("gl.getError()", "gl.NO_ERROR");
    // gl methods should be no-ops
    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.blendFunc(gl.TEXTURE_2D, gl.TEXTURE_CUBE_MAP)");
    // but the event should not have been fired.
    shouldBeFalse("contextLostEventFired");
}

function testRendering()
{
    gl.clearColor(0, 0, 0, 255);
    gl.colorMask(1, 1, 1, 0);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    program = wtu.setupSimpleTextureProgram(gl);
    bufferObjects = wtu.setupUnitQuad(gl);
    texture = wtu.createColoredTexture(gl, canvas.width, canvas.height, texColor);

    gl.uniform1i(gl.getUniformLocation(program, "tex"), 0);
    wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);

    var compare = texColor.slice(0, 3);
    wtu.checkCanvasRect(gl, 0, 0, canvas.width, canvas.height, compare, "shouldBe " + compare);

    shouldBe("gl.getError()", "gl.NO_ERROR");
}

function testOriginalContext()
{
    debug("Test valid context");
    shouldBeFalse("gl.isContextLost()");
    shouldBe("gl.getError()", "gl.NO_ERROR");
    testRendering();
    debug("");
}

function testLostContext(e)
{
    debug("Test lost context");
    shouldBeFalse("contextLostEventFired");
    contextLostEventFired = true;
    shouldBeTrue("gl.isContextLost()");
    shouldBe("gl.getError()", "gl.NO_ERROR");
    debug("");
    if (allowRestore)
      e.preventDefault();
}

function testShouldNotRestoreContext(e)
{
    testFailed("Should not restore the context unless preventDefault is called on the context lost event");
    debug("");
}

function testResources(expected)
{
    var tests = [
        "gl.bindTexture(gl.TEXTURE_2D, texture)",
        "gl.useProgram(program)",
        "gl.bindBuffer(gl.ARRAY_BUFFER, bufferObjects[0])",
    ];

    for (var i = 0; i < tests.length; ++i)
        shouldGenerateGLError(gl, expected, tests[i]);
}

function testOESTextureFloat() {
  if (OES_texture_float) {
    // Extension must still be lost.
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    shouldGenerateGLError(gl, gl.INVALID_ENUM, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.FLOAT, null)");
    // Try re-enabling extension
    OES_texture_float = reGetExtensionAndTestForProperty(gl, "OES_texture_float", false);
    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.FLOAT, null)");
  }
}

function testOESVertexArrayObject() {
  if (OES_vertex_array_object) {
    // Extension must still be lost.
    shouldBeNull("OES_vertex_array_object.createVertexArrayOES()");
    // Try re-enabling extension

    old_OES_vertex_array_object = OES_vertex_array_object;
    OES_vertex_array_object = reGetExtensionAndTestForProperty(gl, "OES_vertex_array_object", false);
    shouldBeTrue("OES_vertex_array_object.createVertexArrayOES() != null");
    shouldBeTrue("old_OES_vertex_array_object.createVertexArrayOES() == null");
  }
}

function testExtensions() {
  testOESTextureFloat();
  testOESVertexArrayObject();
  // Only the WEBGL_lose_context extension should be the same object after context lost.
  new_WEBGL_lose_context = reGetExtensionAndTestForProperty(gl, "WEBGL_lose_context", true);
}

function testRestoredContext()
{
    debug("Test restored context");
    shouldBeFalse("contextRestoredEventFired");
    contextRestoredEventFired = true;
    shouldBeFalse("gl.isContextLost()");
    shouldBe("gl.getError()", "gl.NO_ERROR");

    // Validate that using old resources fails.
    testResources(gl.INVALID_OPERATION);

    testRendering();

    // Validate new resources created in testRendering().
    testResources(gl.NO_ERROR);

    testExtensions();

    debug("");
}


</script>
</head>
<body onload="init()">
<div id="description"></div>
<div id="console"></div>
</body>
</html>