• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html>
3<body>
4<script src="../resources/runner.js"></script>
5<script>
6var canvas2D = document.createElement("canvas");
7var ctx2D = canvas2D.getContext("2d");
8var canvas3D = document.createElement('canvas');
9var gl = canvas3D.getContext('experimental-webgl');
10if (!gl)
11    PerfTestRunner.log("\nWebGL is not supported or enabled on this platform!\n");
12var MEASURE_DRAW_TIMES = 1000;
13var MAX_COUNT = 60000;
14var count = 0;
15
16function setSize(canvas2DWidth, canvas2DHeight, webglWidth, webglHeight) {
17    canvas2D.width = canvas2DWidth;
18    canvas2D.height = canvas2DHeight;
19    canvas3D.width = webglWidth;
20    canvas3D.height = webglHeight;
21}
22
23function rand(range) {
24    return Math.floor(Math.random() * range);
25}
26
27function renderWebGL(gl) {
28    gl.disable(gl.SCISSOR_TEST);
29    gl.clear(gl.COLOR_BUFER_BIT);
30    gl.enable(gl.SCISSOR_TEST);
31    gl.scissor(rand(1024), rand(1024), rand(1024), rand(1024));
32    gl.clearColor(Math.random(), Math.random(), Math.random(), 1);
33    gl.clear(gl.COLOR_BUFFER_BIT);
34}
35
36function drawWebGLToCanvas2D() {
37    if (gl) {
38        var start = PerfTestRunner.now();
39        for (var i = 0; i < MEASURE_DRAW_TIMES; i++) {
40            // draw static WebGL
41            ctx2D.drawImage(canvas3D, 0, 0);
42        }
43        // Calling getImageData() is just to flush out the content when
44        // accelerated 2D canvas is in use. The cost of reading 1x1 pixels is low.
45        ctx2D.getImageData(0, 0, 1, 1);
46        count++;
47
48        var elapsedTime = PerfTestRunner.now() - start;
49        PerfTestRunner.measureValueAsync(MEASURE_DRAW_TIMES * 1000 / elapsedTime);
50    } else
51        PerfTestRunner.measureValueAsync(0);
52    if (count < MAX_COUNT)
53        requestAnimationFrame(drawWebGLToCanvas2D);
54}
55
56function onCompletedRun() {
57    count = MAX_COUNT;
58}
59
60window.onload = function () {
61    PerfTestRunner.prepareToMeasureValuesAsync({done: onCompletedRun, unit: 'runs/s',
62        description: "This bench test checks the speed on drawing static WebGL(1024x1024) to HW accelerated Canvas2D(1024x1024)."});
63    // It should use setMinimumAccelerated2dCanvasSize() to enable accelerated Canvas for a specified size
64    // but this API is not available in JS or WebPage. Assume the threshold size is 256x257
65    // and the dest canvas is HW accelerated Canvas when setting its size to 1024x1024.
66    setSize(1024, 1024, 1024, 1024);
67    if (gl)
68        renderWebGL(gl);
69    drawWebGLToCanvas2D();
70}
71
72</script>
73</body>
74</html>
75