summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/canvas/drawingbuffer-hd-dpi-test.html
blob: 93edeacc0f27544e22f1c8516361a697fb093564 (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
<!--
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.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL DrawingBuffer dimensions on HD-DPI machines test</title>
<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>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script id="vshaderGrid" type="x-shader/x-vertex">
attribute vec4 a_position;
void main()
{
  gl_Position = a_position;
}
</script>

<script id="fshaderGrid" type="x-shader/x-fragment">
precision mediump float;
void main()
{
  float r = mod(gl_FragCoord.x, 2.0) < 1.0 ? 0.0 : 1.0;
  float g = mod(gl_FragCoord.y, 2.0) < 1.0 ? 0.0 : 1.0;
  gl_FragColor = vec4(r, g, 0, 1);
}
</script>
<script>
"use strict";
description();
debug("");

var gl;
var canvas;

function checkDimensions() {
  // We expect that for the sizes being testing drawingBufferWidth and drawingBufferHeight
  // will match canvas.width and canvas.height.

  // We need to test that devicePixelRatio does not effect the backbuffer size of a canvas.
  shouldBe('gl.drawingBufferWidth', 'canvas.width');
  shouldBe('gl.drawingBufferHeight', 'canvas.height');
}

// This uses gl_FragCoord to draw a device pixel relavent pattern.
// If drawBufferWidth or drawBufferHeight are not in device pixels
// this test should fail.
function checkGrid(gl, width, height) {
  var program = wtu.setupProgram(gl, ["vshaderGrid", "fshaderGrid"], ["a_position"]);
  wtu.setupUnitQuad(gl);
  gl.useProgram(program);
  shouldBe('gl.getError()', 'gl.NO_ERROR');

  wtu.clearAndDrawUnitQuad(gl);

  var pixels = new Uint8Array(width * height * 4);
  gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);

  var colors = [
    [ { color: [0,   0, 0, 255], name: "black" },  { color: [255,   0, 0, 255], name: "red"    } ],
    [ { color: [0, 255, 0, 255], name: "green" },  { color: [255, 255, 0, 255], name: "yellow" } ],
  ];

  for (var yy = 0; yy < height; ++yy) {
    for (var xx = 0; xx < width; ++xx) {
      var info = colors[yy % 2][xx % 2];
      var color = info.color;
      var offset = (yy * width + xx) * 4;
      for (var jj = 0; jj < 4; ++jj) {
        if (pixels[offset + jj] != color[jj]) {
          var actual = [pixels[offset], pixels[offset + 1], pixels[offset + 2], pixels[offset + 3]];
          testFailed("at " + xx + ", " + yy + " expected " + color + "(" + info.name + ") was " + actual);
          return;
        }
      }
    }
  }
  testPassed("grid rendered correctly");
}

// This passes device coordinate vertices in to make sure gl.viewport is not being mucked with.
function checkQuad(gl, width, height) {
  var deviceToClipSpace = function(value, range) {
    return value / range * 2 - 1;
  }

  var program = wtu.setupColorQuad(gl);

  // draw a small green square in the top right corner.
  var deviceX1 = width - 4;
  var deviceX2 = width;
  var deviceY1 = height - 4;
  var deviceY2 = height;

  var clipX1 = deviceToClipSpace(deviceX1, width);
  var clipX2 = deviceToClipSpace(deviceX2, width);
  var clipY1 = deviceToClipSpace(deviceY1, height);
  var clipY2 = deviceToClipSpace(deviceY2, height);

  var vertexObject = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
  gl.bufferData(
      gl.ARRAY_BUFFER,
      new Float32Array(
        [ clipX2, clipY2,
          clipX1, clipY2,
          clipX1, clipY1,
          clipX2, clipY2,
          clipX1, clipY1,
          clipX2, clipY1]),
      gl.STATIC_DRAW);
  gl.enableVertexAttribArray(0);
  gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);

  var green = [0, 255, 0, 255];
  var black = [0, 0, 0, 0];
  gl.clearColor(0, 0, 0, 0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  wtu.drawUByteColorQuad(gl, [0, 255, 0, 255]);

  var squareWidth = deviceX2 - deviceX1;
  var squareHeight = deviceY2 - deviceY1;

  // check the square.
  wtu.checkCanvasRect(gl, deviceX1, deviceY1, squareWidth, squareHeight, green, "should be green");
  // check outside the square.
  wtu.checkCanvasRect(gl, 0, 0, width, height - squareHeight, black, "should be black");
  wtu.checkCanvasRect(gl, 0, height - squareHeight, width - squareWidth, squareHeight, black, "should be black");
}


function test(desiredWidth, desiredHeight) {
  debug("");
  debug("testing canvas width = " + desiredWidth + ", height = " + desiredHeight);

  // Make a fresh canvas.
  canvas = document.createElement("canvas");
  canvas.width = desiredWidth;
  canvas.height = desiredHeight;

  // This 'gl' must be global for shouldBe to work.
  gl = wtu.create3DContext(canvas, {antialias: false});
  if (!gl) {
    testFailed("context does not exist");
  } else {
    testPassed("context exists");

    // Check the dimensions are correct.
    checkDimensions();

    // Draw a pixel grid using a shader that draws in device coordinates
    checkGrid(gl, desiredWidth, desiredHeight);

    // Draw a quad in the top right corner.
    checkQuad(gl, desiredWidth, desiredHeight);

    shouldBe('gl.getError()', 'gl.NO_ERROR');

    debug("");
    debug("testing resizing canvas to width = " + desiredWidth + ", height = " + desiredHeight);

    var oldViewport = gl.getParameter(gl.VIEWPORT);

    // flip width and height
    canvas.width = desiredHeight;
    canvas.height = desiredWidth;

    // fix the viewport
    gl.viewport(0, 0, desiredHeight, desiredWidth);

    // Check the dimensions are correct.
    checkDimensions();

    // Draw a pixel grid using a shader that draws in device coordinates
    checkGrid(gl, desiredHeight, desiredWidth);

    // Draw a quad in the top right corner.
    checkQuad(gl, desiredHeight, desiredWidth);

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

var wtu = WebGLTestUtils;

// test a few sizes
test(32, 16);
test(128, 64);
test(256, 512);

debug("")
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>