tor-browser

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

OGLShaderConfig.h (6786B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef GFX_OGLSHADERCONFIG_H
      8 #define GFX_OGLSHADERCONFIG_H
      9 
     10 #include "gfxTypes.h"
     11 #include "ImageTypes.h"
     12 #include "mozilla/Assertions.h"  // for MOZ_ASSERT, etc
     13 #include "mozilla/RefPtr.h"      // for RefPtr
     14 #include "mozilla/gfx/Matrix.h"  // for Matrix4x4
     15 #include "mozilla/gfx/Rect.h"    // for Rect
     16 #include "mozilla/gfx/Types.h"
     17 #include "nsDebug.h"   // for NS_ASSERTION
     18 #include "nsPoint.h"   // for nsIntPoint
     19 #include "nsTArray.h"  // for nsTArray
     20 #include "mozilla/layers/CompositorTypes.h"
     21 
     22 namespace mozilla {
     23 namespace layers {
     24 
     25 enum ShaderFeatures {
     26  ENABLE_RENDER_COLOR = 0x01,
     27  ENABLE_TEXTURE_RECT = 0x02,
     28  ENABLE_TEXTURE_EXTERNAL = 0x04,
     29  ENABLE_TEXTURE_YCBCR = 0x08,
     30  ENABLE_TEXTURE_NV12 = 0x10,
     31  ENABLE_TEXTURE_COMPONENT_ALPHA = 0x20,
     32  ENABLE_TEXTURE_NO_ALPHA = 0x40,
     33  ENABLE_TEXTURE_RB_SWAP = 0x80,
     34  ENABLE_OPACITY = 0x100,
     35  ENABLE_BLUR = 0x200,
     36  ENABLE_COLOR_MATRIX = 0x400,
     37  ENABLE_MASK = 0x800,
     38  ENABLE_NO_PREMUL_ALPHA = 0x1000,
     39  ENABLE_DEAA = 0x2000,
     40  ENABLE_DYNAMIC_GEOMETRY = 0x4000,
     41  ENABLE_MASK_TEXTURE_RECT = 0x8000,
     42  ENABLE_TEXTURE_NV12_GA_SWITCH = 0x10000,
     43  ENABLE_ROUNDED_CLIP = 0x20000,
     44 };
     45 
     46 class KnownUniform {
     47 public:
     48  // this needs to be kept in sync with strings in 'AddUniforms'
     49  enum KnownUniformName {
     50    NotAKnownUniform = -1,
     51 
     52    LayerTransform = 0,
     53    LayerTransformInverse,
     54    MaskTransform,
     55    BackdropTransform,
     56    LayerRects,
     57    MatrixProj,
     58    TextureTransform,
     59    TextureRects,
     60    RenderTargetOffset,
     61    LayerOpacity,
     62    Texture,
     63    YTexture,
     64    CbTexture,
     65    CrTexture,
     66    RenderColor,
     67    TexCoordMultiplier,
     68    CbCrTexCoordMultiplier,
     69    SSEdges,
     70    ViewportSize,
     71    VisibleCenter,
     72    YuvColorMatrix,
     73    YuvOffsetVector,
     74    RoundedClipRect,
     75    RoundedClipRadii,
     76 
     77    KnownUniformCount
     78  };
     79 
     80  KnownUniform() {
     81    mName = NotAKnownUniform;
     82    mNameString = nullptr;
     83    mLocation = -1;
     84    memset(&mValue, 0, sizeof(mValue));
     85  }
     86 
     87  bool UpdateUniform(int32_t i1) {
     88    if (mLocation == -1) return false;
     89    if (mValue.i1 != i1) {
     90      mValue.i1 = i1;
     91      return true;
     92    }
     93    return false;
     94  }
     95 
     96  bool UpdateUniform(float f1) {
     97    if (mLocation == -1) return false;
     98    if (mValue.f1 != f1) {
     99      mValue.f1 = f1;
    100      return true;
    101    }
    102    return false;
    103  }
    104 
    105  bool UpdateUniform(float f1, float f2) {
    106    if (mLocation == -1) return false;
    107    if (mValue.f16v[0] != f1 || mValue.f16v[1] != f2) {
    108      mValue.f16v[0] = f1;
    109      mValue.f16v[1] = f2;
    110      return true;
    111    }
    112    return false;
    113  }
    114 
    115  bool UpdateUniform(float f1, float f2, float f3, float f4) {
    116    if (mLocation == -1) return false;
    117    if (mValue.f16v[0] != f1 || mValue.f16v[1] != f2 || mValue.f16v[2] != f3 ||
    118        mValue.f16v[3] != f4) {
    119      mValue.f16v[0] = f1;
    120      mValue.f16v[1] = f2;
    121      mValue.f16v[2] = f3;
    122      mValue.f16v[3] = f4;
    123      return true;
    124    }
    125    return false;
    126  }
    127 
    128  bool UpdateUniform(int cnt, const float* fp) {
    129    if (mLocation == -1) return false;
    130    switch (cnt) {
    131      case 1:
    132      case 2:
    133      case 3:
    134      case 4:
    135      case 9:
    136      case 16:
    137        if (memcmp(mValue.f16v, fp, sizeof(float) * cnt) != 0) {
    138          memcpy(mValue.f16v, fp, sizeof(float) * cnt);
    139          return true;
    140        }
    141        return false;
    142    }
    143 
    144    MOZ_ASSERT_UNREACHABLE("cnt must be 1 2 3 4 9 or 16");
    145    return false;
    146  }
    147 
    148  bool UpdateArrayUniform(int cnt, const float* fp) {
    149    if (mLocation == -1) return false;
    150    if (cnt > 16) {
    151      return false;
    152    }
    153 
    154    if (memcmp(mValue.f16v, fp, sizeof(float) * cnt) != 0) {
    155      memcpy(mValue.f16v, fp, sizeof(float) * cnt);
    156      return true;
    157    }
    158    return false;
    159  }
    160 
    161  bool UpdateArrayUniform(int cnt, const gfx::Point3D* points) {
    162    if (mLocation == -1) return false;
    163    if (cnt > 4) {
    164      return false;
    165    }
    166 
    167    float fp[12];
    168    float* d = fp;
    169    for (int i = 0; i < cnt; i++) {
    170      // Note: Do not want to make assumptions about .x, .y, .z member packing.
    171      // If gfx::Point3D is updated to make this guarantee, SIMD optimizations
    172      // may be possible
    173      *d++ = points[i].x;
    174      *d++ = points[i].y;
    175      *d++ = points[i].z;
    176    }
    177 
    178    if (memcmp(mValue.f16v, fp, sizeof(float) * cnt * 3) != 0) {
    179      memcpy(mValue.f16v, fp, sizeof(float) * cnt * 3);
    180      return true;
    181    }
    182    return false;
    183  }
    184 
    185  KnownUniformName mName;
    186  const char* mNameString;
    187  int32_t mLocation;
    188 
    189  union {
    190    int i1;
    191    float f1;
    192    float f16v[16];
    193  } mValue;
    194 };
    195 
    196 class ShaderConfigOGL {
    197 public:
    198  ShaderConfigOGL()
    199      : mFeatures(0),
    200        mMultiplier(1),
    201        mCompositionOp(gfx::CompositionOp::OP_OVER) {}
    202 
    203  void SetRenderColor(bool aEnabled);
    204  void SetTextureTarget(GLenum aTarget);
    205  void SetMaskTextureTarget(GLenum aTarget);
    206  void SetRBSwap(bool aEnabled);
    207  void SetNoAlpha(bool aEnabled);
    208  void SetOpacity(bool aEnabled);
    209  void SetYCbCr(bool aEnabled);
    210  void SetNV12(bool aEnabled);
    211  void SetComponentAlpha(bool aEnabled);
    212  void SetColorMatrix(bool aEnabled);
    213  void SetBlur(bool aEnabled);
    214  void SetMask(bool aEnabled);
    215  void SetDEAA(bool aEnabled);
    216  void SetCompositionOp(gfx::CompositionOp aOp);
    217  void SetNoPremultipliedAlpha();
    218  void SetDynamicGeometry(bool aEnabled);
    219  void SetColorMultiplier(uint32_t aMultiplier);
    220  void SetRoundedClip(bool aEnabled);
    221 
    222  bool operator<(const ShaderConfigOGL& other) const {
    223    return mFeatures < other.mFeatures ||
    224           (mFeatures == other.mFeatures &&
    225            (int)mCompositionOp < (int)other.mCompositionOp) ||
    226           (mFeatures == other.mFeatures &&
    227            (int)mCompositionOp == (int)other.mCompositionOp &&
    228            mMultiplier < other.mMultiplier);
    229  }
    230 
    231 public:
    232  void SetFeature(int aBitmask, bool aState) {
    233    if (aState)
    234      mFeatures |= aBitmask;
    235    else
    236      mFeatures &= (~aBitmask);
    237  }
    238 
    239  int mFeatures;
    240  uint32_t mMultiplier;
    241  gfx::CompositionOp mCompositionOp;
    242 };
    243 
    244 static inline ShaderConfigOGL ShaderConfigFromTargetAndFormat(
    245    GLenum aTarget, gfx::SurfaceFormat aFormat) {
    246  ShaderConfigOGL config;
    247  config.SetTextureTarget(aTarget);
    248  config.SetRBSwap(aFormat == gfx::SurfaceFormat::B8G8R8A8 ||
    249                   aFormat == gfx::SurfaceFormat::B8G8R8X8);
    250  config.SetNoAlpha(aFormat == gfx::SurfaceFormat::B8G8R8X8 ||
    251                    aFormat == gfx::SurfaceFormat::R8G8B8X8 ||
    252                    aFormat == gfx::SurfaceFormat::R5G6B5_UINT16);
    253  return config;
    254 }
    255 
    256 }  // namespace layers
    257 }  // namespace mozilla
    258 
    259 #endif  // GFX_OGLSHADERCONFIG_H