tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

tcuSurface.js (5416B)


      1 /*-------------------------------------------------------------------------
      2 * drawElements Quality Program OpenGL ES Utilities
      3 * ------------------------------------------------
      4 *
      5 * Copyright 2014 The Android Open Source Project
      6 *
      7 * Licensed under the Apache License, Version 2.0 (the "License");
      8 * you may not use this file except in compliance with the License.
      9 * You may obtain a copy of the License at
     10 *
     11 *      http://www.apache.org/licenses/LICENSE-2.0
     12 *
     13 * Unless required by applicable law or agreed to in writing, software
     14 * distributed under the License is distributed on an "AS IS" BASIS,
     15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 * See the License for the specific language governing permissions and
     17 * limitations under the License.
     18 *
     19 */
     20 
     21 'use strict';
     22 goog.provide('framework.common.tcuSurface');
     23 goog.require('framework.common.tcuTexture');
     24 goog.require('framework.delibs.debase.deMath');
     25 goog.require('framework.opengl.gluTextureUtil');
     26 
     27 goog.scope(function() {
     28 
     29 var tcuSurface = framework.common.tcuSurface;
     30 var tcuTexture = framework.common.tcuTexture;
     31 var deMath = framework.delibs.debase.deMath;
     32 var gluTextureUtil = framework.opengl.gluTextureUtil;
     33 
     34 var DE_ASSERT = function(x) {
     35    if (!x)
     36        throw new Error('Assert failed');
     37 };
     38 
     39 /**
     40 * \brief RGBA8888 surface
     41 *
     42 * tcuSurface.Surface provides basic pixel storage functionality. Only single format
     43 * (RGBA8888) is supported.
     44 *
     45 * PixelBufferAccess (see tcuTexture.h) provides much more flexible API
     46 * for handling various pixel formats. This is mainly a convinience class.
     47 * @constructor
     48 * @param {number=} width
     49 * @param {number=} height
     50 */
     51 tcuSurface.Surface = function(width, height) {
     52    width = width || 0;
     53    height = height || 0;
     54    this.setSize(width, height);
     55 };
     56 
     57 /**
     58 * @param {number} width
     59 * @param {number} height
     60 */
     61 tcuSurface.Surface.prototype.setSize = function(width, height) {
     62    this.m_width = width;
     63    this.m_height = height;
     64    if (width * height > 0) {
     65        this.m_data = new ArrayBuffer(4 * width * height);
     66        this.m_pixels = new Uint8Array(this.m_data);
     67    } else {
     68        this.m_data = null;
     69        this.m_pixels = null;
     70    }
     71 };
     72 
     73 /**
     74 * @return {number}
     75 */
     76 tcuSurface.Surface.prototype.getWidth = function() { return this.m_width; };
     77 
     78 /**
     79 * @return {number}
     80 */
     81 tcuSurface.Surface.prototype.getHeight = function() { return this.m_height; };
     82 
     83 /**
     84 * @param {number} x
     85 * @param {number} y
     86 * @param {Array<number>} color Vec4 color
     87 */
     88 tcuSurface.Surface.prototype.setPixel = function(x, y, color) {
     89    DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width));
     90    DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height));
     91 
     92    var offset = 4 * (x + y * this.m_width);
     93    for (var i = 0; i < 4; i++)
     94        this.m_pixels[offset + i] = color[i];
     95 };
     96 
     97 /**
     98 * @param {number} x
     99 * @param {number} y
    100 * @return {Array<number>}
    101 */
    102 tcuSurface.Surface.prototype.getPixel = function(x, y) {
    103    DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width));
    104    DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height));
    105 
    106    var color = [];
    107    color.length = 4;
    108 
    109    var offset = 4 * (x + y * this.m_width);
    110    for (var i = 0; i < 4; i++)
    111        color[i] = this.m_pixels[offset + i];
    112 
    113    return color;
    114 };
    115 
    116 /**
    117 * @param {number} x
    118 * @param {number} y
    119 * @return {number}
    120 */
    121 tcuSurface.Surface.prototype.getPixelUintRGB8 = function(x, y) {
    122    DE_ASSERT(deMath.deInBounds32(x, 0, this.m_width));
    123    DE_ASSERT(deMath.deInBounds32(y, 0, this.m_height));
    124 
    125    var offset = 4 * (x + y * this.m_width);
    126    return (this.m_pixels[offset] << 16) +
    127        (this.m_pixels[offset + 1] << 8) +
    128        this.m_pixels[offset + 2];
    129 };
    130 
    131 /**
    132 * Read the viewport contents into this surface
    133 * @param {*=} ctx WebGL or ref context
    134 * @param {(Array<number>|{x: number, y: number, width: number, height: number})=} view
    135 */
    136 tcuSurface.Surface.prototype.readViewport = function(ctx, view) {
    137    ctx = ctx || gl;
    138    var v = view || /** @type {Array<number>} */ (ctx.getParameter(gl.VIEWPORT));
    139    /** @type {number} */ var x;
    140    /** @type {number} */ var y;
    141    /** @type {number} */ var width;
    142    /** @type {number} */ var height;
    143    if (v instanceof Array || ArrayBuffer.isView(v)) {
    144        x = v[0];
    145        y = v[1];
    146        width = v[2];
    147        height = v[3];
    148    } else {
    149        x = v.x;
    150        y = v.y;
    151        width = v.width;
    152        height = v.height;
    153    }
    154    if (width != this.m_width || height != this.m_height)
    155        this.setSize(width, height);
    156    ctx.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, this.m_pixels);
    157 };
    158 
    159 /**
    160 * @return {Uint8Array}
    161 */
    162 tcuSurface.Surface.prototype.getPixels = function() { return this.m_pixels || null; };
    163 
    164 /**
    165 * @return {tcuTexture.PixelBufferAccess} Pixel Buffer Access object
    166 */
    167 tcuSurface.Surface.prototype.getAccess = function() {
    168    return new tcuTexture.PixelBufferAccess({
    169                    format: new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA, tcuTexture.ChannelType.UNORM_INT8),
    170                    width: this.m_width,
    171                    height: this.m_height,
    172                    data: this.m_data
    173                });
    174 
    175 };
    176 
    177 tcuSurface.Surface.prototype.getSubAccess = function(x, y, width, height) {
    178    /* TODO: Implement. the deqp getSubAccess() looks broken. It will probably fail if
    179     * x != 0 or width != m_width
    180     */
    181     throw new Error('Unimplemented');
    182 };
    183 
    184 });