tor-browser

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

rrMultisamplePixelBufferAccess.js (6866B)


      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.referencerenderer.rrMultisamplePixelBufferAccess');
     23 goog.require('framework.common.tcuTexture');
     24 goog.require('framework.common.tcuTextureUtil');
     25 goog.require('framework.delibs.debase.deMath');
     26 
     27 goog.scope(function() {
     28 
     29 var rrMultisamplePixelBufferAccess = framework.referencerenderer.rrMultisamplePixelBufferAccess;
     30 var tcuTexture = framework.common.tcuTexture;
     31 var deMath = framework.delibs.debase.deMath;
     32 var tcuTextureUtil = framework.common.tcuTextureUtil;
     33 
     34 var DE_ASSERT = function(x) {
     35    if (!x)
     36        throw new Error('Assert failed');
     37 };
     38 
     39 /**
     40 * \brief Read-write pixel data access to multisampled buffers.
     41 *
     42 * Multisampled data access follows the multisampled indexing convention.
     43 *
     44 * Prevents accidental usage of non-multisampled buffer as multisampled
     45 * with PixelBufferAccess.
     46 * @constructor
     47 * @param {tcuTexture.PixelBufferAccess=} rawAccess
     48 */
     49 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess = function(rawAccess) {
     50    this.m_access = rawAccess || new tcuTexture.PixelBufferAccess({
     51                                            width: 0,
     52                                            height: 0});
     53 };
     54 
     55 /**
     56 * @return {tcuTexture.PixelBufferAccess}
     57 */
     58 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.raw = function() { return this.m_access; };
     59 
     60 /**
     61 * @return {boolean}
     62 */
     63 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.isEmpty = function() { return this.m_access.isEmpty(); };
     64 
     65 /**
     66 * @return {number}
     67 */
     68 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.getNumSamples = function() { return this.raw().getWidth(); };
     69 
     70 /**
     71 * @return {tcuTexture.PixelBufferAccess}
     72 */
     73 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.toSinglesampleAccess = function() {
     74    DE_ASSERT(this.getNumSamples() == 1);
     75 
     76    return new tcuTexture.PixelBufferAccess({
     77                                  format: this.m_access.getFormat(),
     78                                  width: this.m_access.getHeight(),
     79                                  height: this.m_access.getDepth(),
     80                                  depth: 1,
     81                                  rowPitch: this.m_access.getSlicePitch(),
     82                                  slicePitch: this.m_access.getSlicePitch() * this.m_access.getDepth(),
     83                                  data: this.m_access.m_data,
     84                                  offset: this.m_access.m_offset});
     85 };
     86 
     87 /**
     88 * @param {tcuTexture.PixelBufferAccess} original
     89 * @return {rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess}
     90 */
     91 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.fromSinglesampleAccess = function(original) {
     92    return new rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess(
     93                new tcuTexture.PixelBufferAccess({
     94                                format: original.getFormat(),
     95                                width: 1,
     96                                height: original.getWidth(),
     97                                depth: original.getHeight(),
     98                                rowPitch: original.getFormat().getPixelSize(),
     99                                slicePitch: original.getRowPitch(),
    100                                data: original.m_data,
    101                                offset: original.m_offset}));
    102 };
    103 
    104 /**
    105 * @param {tcuTexture.PixelBufferAccess} multisampledAccess
    106 * @return {rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess}
    107 */
    108 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.fromMultisampleAccess = function(multisampledAccess) {
    109    return new rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess(multisampledAccess);
    110 };
    111 
    112 /**
    113 * @param {Array<number>} region
    114 * @return {rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess}
    115 */
    116 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.getSubregion = function(region) {
    117    var x = region[0];
    118    var y = region[1];
    119    var width = region[2];
    120    var height = region[3];
    121 
    122    return rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.fromMultisampleAccess(tcuTextureUtil.getSubregion(this.raw(), 0, x, y, this.getNumSamples(), width, height));
    123 };
    124 
    125 /**
    126 * @return {Array<number>} [x, y, width, height]
    127 */
    128 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.getBufferSize = function() {
    129    return [0, 0, this.raw().getHeight(), this.raw().getDepth()];
    130 };
    131 
    132 /**
    133 * @param {tcuTexture.PixelBufferAccess} dst
    134 */
    135 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.resolveMultisampleColorBuffer = function(dst) {
    136    var src = this;
    137    DE_ASSERT(dst.getWidth() == src.raw().getHeight());
    138    DE_ASSERT(dst.getHeight() == src.raw().getDepth());
    139 
    140    var numSamples = src.getNumSamples();
    141    var sum = [0, 0, 0, 0];
    142    for (var y = 0; y < dst.getHeight(); y++) {
    143        for (var x = 0; x < dst.getWidth(); x++) {
    144            sum[0] = 0;
    145            sum[1] = 0;
    146            sum[2] = 0;
    147            sum[3] = 0;
    148 
    149            for (var s = 0; s < src.raw().getWidth(); s++) {
    150                var pixel = src.raw().getPixel(s, x, y);
    151                sum[0] += pixel[0];
    152                sum[1] += pixel[1];
    153                sum[2] += pixel[2];
    154                sum[3] += pixel[3];
    155            }
    156 
    157            sum[0] /= numSamples;
    158            sum[1] /= numSamples;
    159            sum[2] /= numSamples;
    160            sum[3] /= numSamples;
    161 
    162            dst.setPixel(sum, x, y);
    163        }
    164    }
    165 };
    166 
    167 /**
    168 * @param {number} x
    169 * @param {number} y
    170 * @return {Array<number>}
    171 */
    172 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.resolveMultisamplePixel = function(x, y) {
    173    var sum = [0, 0, 0, 0];
    174    for (var s = 0; s < this.getNumSamples(); s++)
    175        sum = deMath.add(sum, this.raw().getPixel(s, x, y));
    176 
    177    for (var i = 0; i < sum.length; i++)
    178        sum[i] = sum[i] / this.getNumSamples();
    179 
    180    return sum;
    181 };
    182 
    183 /**
    184 * @param {Array<number>} color
    185 */
    186 rrMultisamplePixelBufferAccess.MultisamplePixelBufferAccess.prototype.clear = function(color) {
    187    this.raw().clear(color);
    188 };
    189 
    190 });