Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / extra / buffer-gc-stress.html
1 <!--
2
3 /*
4 ** Copyright (c) 2014 The Khronos Group Inc.
5 **
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
24 */
25
26 -->
27
28 <!DOCTYPE html>
29 <html>
30   <head>
31     <meta charset="utf-8">
32     <title>Float32Array garbage collection test</title>
33     <link rel="stylesheet" href="../resources/js-test-style.css"/>
34     <script src="../resources/js-test-pre.js"></script>
35     <script src="../conformance/resources/webgl-test-utils.js"> </script>
36 </head>
37 <body>
38 <canvas id="canvas" width="40" height="40"></canvas>
39 <div id="description"></div>
40 <div id="console"></div>
41 <script id="vshader" type="x-shader/x-vertex">
42 attribute vec2 inPosition;
43 attribute vec4 inColor;
44
45 varying vec4 color;
46
47 void main()
48 {
49     color = inColor;
50
51     gl_Position = vec4(inPosition, 0.0, 1.0);
52 }
53 </script>
54 <script id="fshader" type="x-shader/x-fragment">
55 precision mediump float;
56
57 varying vec4 color;
58
59 void main()
60 {
61     if (color == vec4(0.0))
62         discard;
63
64     gl_FragColor = color;
65 }
66 </script>
67
68 <script>
69 "use strict";
70
71 description("Allocates a buffer object and then updates it repeatedly using throw-away Float32Array objects. " +
72             "Ideally, this should not result in a browser crash or instability, since GC should be able to collect all Float32Arrays.");
73 var wtu = WebGLTestUtils;
74
75 var vertices = [];
76 var w = 0.25;
77 for (var x = -1; x < 1; x += w) {
78     for (var y = -1; y < 1; y += w) {
79         vertices.push(x + w, y + w);
80         vertices.push(x,     y + w);
81         vertices.push(x,     y    );
82
83         vertices.push(x + w, y + w);
84         vertices.push(x,     y    );
85         vertices.push(x + w, y    );
86     }
87 }
88 var numVertices = (vertices.length / 2);
89
90 var gl;
91 var squareBuffer;
92 var buffer;
93 var updateBufferData;
94 var drawIterationsPerTest = 100;
95
96 function initGL() {
97     gl = wtu.create3DContext("canvas");
98     var attribs = ["inPosition", "inColor"];
99     wtu.setupProgram(gl, ["vshader", "fshader"], attribs);
100     gl.disable(gl.DEPTH_TEST);
101     gl.disable(gl.BLEND);
102
103     squareBuffer = gl.createBuffer();
104     gl.enableVertexAttribArray(0);
105     gl.bindBuffer(gl.ARRAY_BUFFER, squareBuffer);
106     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
107     gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
108
109     buffer = gl.createBuffer();
110     gl.enableVertexAttribArray(1);
111     gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
112     gl.vertexAttribPointer(1, 4, gl.FLOAT, false, 0, 0);
113 }
114
115 var testIndex = -1;
116 var drawIterations = 0;
117 var size = 0;
118 function runNextTest() {
119     ++testIndex;
120     var prevSize = size;
121     size = Math.pow(2, testIndex) * numVertices * 16;
122
123     if (size > 2 * 1024 * 1024 && prevSize <= 2 * 1024 * 1024) {
124         if (!confirm("The following tests can cause unresponsiveness or instability. Press OK to continue.")) {
125             testFailed("Tests aborted");
126             return;
127         }
128     }
129
130     if (size > 64 * 1024 * 1024) {
131         gl.deleteBuffer(buffer);
132         testPassed("Tests finished");
133         return;
134     }
135
136     debug('Initializing buffer with size: ' + size);
137     gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
138     gl.bufferData(gl.ARRAY_BUFFER, size, gl.DYNAMIC_DRAW);
139     updateBufferData = new Float32Array(size / 4);
140
141     debug("Drawing " + drawIterationsPerTest + " times, each time creating a new throw-away Float32Array of size " + size + " and using it to update the buffer");
142     drawIterations = 0;
143     doDraw();
144 };
145
146 var doDraw = function() {
147     gl.clearColor(0, 255, 0, 255);
148     gl.clear(gl.COLOR_BUFFER_BIT);
149
150     // Update the array buffer with a throw-away Float32Array
151     gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
152     gl.bufferSubData(gl.ARRAY_BUFFER, 0, new Float32Array(updateBufferData));
153
154     gl.drawArrays(gl.TRIANGLES, 0, numVertices);
155     var error = gl.getError();
156     if (error !== gl.NO_ERROR) {
157         testFailed("drawArrays failed with error " + wtu.glEnumToString(gl, error));
158         return;
159     }
160     if (drawIterations < drawIterationsPerTest) {
161         ++drawIterations;
162         requestAnimationFrame(doDraw);
163     } else {
164         runNextTest();
165     }
166 };
167
168 initGL();
169 runNextTest();
170
171 var successfullyParsed = true;
172 </script>
173
174 </body>
175 </html>
176