summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/deqp/framework/common/tcuSurface.js
blob: 47d3634aad66a627461a18c426e120ab53f71dc9 (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
/*-------------------------------------------------------------------------
 * drawElements Quality Program OpenGL ES Utilities
 * ------------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

'use strict';
goog.provide('framework.common.tcuSurface');
goog.require('framework.common.tcuTexture');
goog.require('framework.delibs.debase.deMath');
goog.require('framework.opengl.gluTextureUtil');

goog.scope(function() {

var tcuSurface = framework.common.tcuSurface;
var tcuTexture = framework.common.tcuTexture;
var deMath = framework.delibs.debase.deMath;
var gluTextureUtil = framework.opengl.gluTextureUtil;

var DE_ASSERT = function(x) {
    if (!x)
        throw new Error('Assert failed');
};

/**
 * \brief RGBA8888 surface
 *
 * tcuSurface.Surface provides basic pixel storage functionality. Only single format
 * (RGBA8888) is supported.
 *
 * PixelBufferAccess (see tcuTexture.h) provides much more flexible API
 * for handling various pixel formats. This is mainly a convinience class.
 * @constructor
 * @param {number=} width
 * @param {number=} height
 */
tcuSurface.Surface = function(width, height) {
    width = width || 0;
    height = height || 0;
    this.setSize(width, height);
};

/**
 * @param {number} width
 * @param {number} height
 */
tcuSurface.Surface.prototype.setSize = function(width, height) {
    this.m_width = width;
    this.m_height = height;
    if (width * height > 0) {
        this.m_data = new ArrayBuffer(4 * width * height);
        this.m_pixels = new Uint8Array(this.m_data);
    } else {
        this.m_data = null;
        this.m_pixels = null;
    }
};

/**
 * @return {number}
 */
tcuSurface.Surface.prototype.getWidth = function() { return this.m_width; };

/**
 * @return {number}
 */
tcuSurface.Surface.prototype.getHeight = function() { return this.m_height; };

/**
 * @param {number} x
 * @param {number} y
 * @param {Array<number>} color Vec4 color
 */
tcuSurface.Surface.prototype.setPixel = function(x, y, color) {
    DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width));
    DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height));

    var offset = 4 * (x + y * this.m_width);
    for (var i = 0; i < 4; i++)
        this.m_pixels[offset + i] = color[i];
};

/**
 * @param {number} x
 * @param {number} y
 * @return {Array<number>}
 */
tcuSurface.Surface.prototype.getPixel = function(x, y) {
    DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width));
    DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height));

    var color = [];
    color.length = 4;

    var offset = 4 * (x + y * this.m_width);
    for (var i = 0; i < 4; i++)
        color[i] = this.m_pixels[offset + i];

    return color;
};

/**
 * @param {number} x
 * @param {number} y
 * @return {number}
 */
tcuSurface.Surface.prototype.getPixelUintRGB8 = function(x, y) {
    DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width));
    DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height));

    var offset = 4 * (x + y * this.m_width);
    return (this.m_pixels[offset] << 16) +
        (this.m_pixels[offset + 1] << 8) +
        this.m_pixels[offset + 2];
};

/**
 * Read the viewport contents into this surface
 * @param {*=} ctx WebGL or ref context
 * @param {(Array<number>|{x: number, y: number, width: number, height: number})=} view
 */
tcuSurface.Surface.prototype.readViewport = function(ctx, view) {
    ctx = ctx || gl;
    var v = view || /** @type {Array<number>} */ (ctx.getParameter(gl.VIEWPORT));
    /** @type {number} */ var x;
    /** @type {number} */ var y;
    /** @type {number} */ var width;
    /** @type {number} */ var height;
    if (v instanceof Array || ArrayBuffer.isView(v)) {
        x = v[0];
        y = v[1];
        width = v[2];
        height = v[3];
    } else {
        x = v.x;
        y = v.y;
        width = v.width;
        height = v.height;
    }
    if (width != this.m_width || height != this.m_height)
        this.setSize(width, height);
    ctx.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, this.m_pixels);
};

/**
 * @return {Uint8Array}
 */
tcuSurface.Surface.prototype.getPixels = function() { return this.m_pixels || null; };

/**
 * @return {tcuTexture.PixelBufferAccess} Pixel Buffer Access object
 */
tcuSurface.Surface.prototype.getAccess = function() {
    return new tcuTexture.PixelBufferAccess({
                    format: new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8),
                    width: this.m_width,
                    height: this.m_height,
                    data: this.m_data
                });

};

tcuSurface.Surface.prototype.getSubAccess = function(x, y, width, height) {
    /* TODO: Implement. the deqp getSubAccess() looks broken. It will probably fail if
     * x != 0 or width != m_width
     */
     throw new Error('Unimplemented');
};

});