summaryrefslogtreecommitdiffstats
path: root/gfx/layers/opengl/OGLShaderConfig.h
blob: f7c939aa98ac860dc96737478f5ae41f9e3db139 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef GFX_OGLSHADERCONFIG_H
#define GFX_OGLSHADERCONFIG_H

#include "gfxTypes.h"
#include "ImageTypes.h"
#include "mozilla/Assertions.h"  // for MOZ_ASSERT, etc
#include "mozilla/RefPtr.h"      // for RefPtr
#include "mozilla/gfx/Matrix.h"  // for Matrix4x4
#include "mozilla/gfx/Rect.h"    // for Rect
#include "mozilla/gfx/Types.h"
#include "nsDebug.h"   // for NS_ASSERTION
#include "nsPoint.h"   // for nsIntPoint
#include "nsTArray.h"  // for nsTArray
#include "mozilla/layers/CompositorTypes.h"

namespace mozilla {
namespace layers {

enum ShaderFeatures {
  ENABLE_RENDER_COLOR = 0x01,
  ENABLE_TEXTURE_RECT = 0x02,
  ENABLE_TEXTURE_EXTERNAL = 0x04,
  ENABLE_TEXTURE_YCBCR = 0x08,
  ENABLE_TEXTURE_NV12 = 0x10,
  ENABLE_TEXTURE_COMPONENT_ALPHA = 0x20,
  ENABLE_TEXTURE_NO_ALPHA = 0x40,
  ENABLE_TEXTURE_RB_SWAP = 0x80,
  ENABLE_OPACITY = 0x100,
  ENABLE_BLUR = 0x200,
  ENABLE_COLOR_MATRIX = 0x400,
  ENABLE_MASK = 0x800,
  ENABLE_NO_PREMUL_ALPHA = 0x1000,
  ENABLE_DEAA = 0x2000,
  ENABLE_DYNAMIC_GEOMETRY = 0x4000,
  ENABLE_MASK_TEXTURE_RECT = 0x8000,
  ENABLE_TEXTURE_NV12_GA_SWITCH = 0x10000,
};

class KnownUniform {
 public:
  // this needs to be kept in sync with strings in 'AddUniforms'
  enum KnownUniformName {
    NotAKnownUniform = -1,

    LayerTransform = 0,
    LayerTransformInverse,
    MaskTransform,
    BackdropTransform,
    LayerRects,
    MatrixProj,
    TextureTransform,
    TextureRects,
    RenderTargetOffset,
    LayerOpacity,
    Texture,
    YTexture,
    CbTexture,
    CrTexture,
    RenderColor,
    TexCoordMultiplier,
    CbCrTexCoordMultiplier,
    SSEdges,
    ViewportSize,
    VisibleCenter,
    YuvColorMatrix,
    YuvOffsetVector,

    KnownUniformCount
  };

  KnownUniform() {
    mName = NotAKnownUniform;
    mNameString = nullptr;
    mLocation = -1;
    memset(&mValue, 0, sizeof(mValue));
  }

  bool UpdateUniform(int32_t i1) {
    if (mLocation == -1) return false;
    if (mValue.i1 != i1) {
      mValue.i1 = i1;
      return true;
    }
    return false;
  }

  bool UpdateUniform(float f1) {
    if (mLocation == -1) return false;
    if (mValue.f1 != f1) {
      mValue.f1 = f1;
      return true;
    }
    return false;
  }

  bool UpdateUniform(float f1, float f2) {
    if (mLocation == -1) return false;
    if (mValue.f16v[0] != f1 || mValue.f16v[1] != f2) {
      mValue.f16v[0] = f1;
      mValue.f16v[1] = f2;
      return true;
    }
    return false;
  }

  bool UpdateUniform(float f1, float f2, float f3, float f4) {
    if (mLocation == -1) return false;
    if (mValue.f16v[0] != f1 || mValue.f16v[1] != f2 || mValue.f16v[2] != f3 ||
        mValue.f16v[3] != f4) {
      mValue.f16v[0] = f1;
      mValue.f16v[1] = f2;
      mValue.f16v[2] = f3;
      mValue.f16v[3] = f4;
      return true;
    }
    return false;
  }

  bool UpdateUniform(int cnt, const float* fp) {
    if (mLocation == -1) return false;
    switch (cnt) {
      case 1:
      case 2:
      case 3:
      case 4:
      case 9:
      case 16:
        if (memcmp(mValue.f16v, fp, sizeof(float) * cnt) != 0) {
          memcpy(mValue.f16v, fp, sizeof(float) * cnt);
          return true;
        }
        return false;
    }

    MOZ_ASSERT_UNREACHABLE("cnt must be 1 2 3 4 9 or 16");
    return false;
  }

  bool UpdateArrayUniform(int cnt, const float* fp) {
    if (mLocation == -1) return false;
    if (cnt > 16) {
      return false;
    }

    if (memcmp(mValue.f16v, fp, sizeof(float) * cnt) != 0) {
      memcpy(mValue.f16v, fp, sizeof(float) * cnt);
      return true;
    }
    return false;
  }

  bool UpdateArrayUniform(int cnt, const gfx::Point3D* points) {
    if (mLocation == -1) return false;
    if (cnt > 4) {
      return false;
    }

    float fp[12];
    float* d = fp;
    for (int i = 0; i < cnt; i++) {
      // Note: Do not want to make assumptions about .x, .y, .z member packing.
      // If gfx::Point3D is updated to make this guarantee, SIMD optimizations
      // may be possible
      *d++ = points[i].x;
      *d++ = points[i].y;
      *d++ = points[i].z;
    }

    if (memcmp(mValue.f16v, fp, sizeof(float) * cnt * 3) != 0) {
      memcpy(mValue.f16v, fp, sizeof(float) * cnt * 3);
      return true;
    }
    return false;
  }

  KnownUniformName mName;
  const char* mNameString;
  int32_t mLocation;

  union {
    int i1;
    float f1;
    float f16v[16];
  } mValue;
};

class ShaderConfigOGL {
 public:
  ShaderConfigOGL()
      : mFeatures(0),
        mMultiplier(1),
        mCompositionOp(gfx::CompositionOp::OP_OVER) {}

  void SetRenderColor(bool aEnabled);
  void SetTextureTarget(GLenum aTarget);
  void SetMaskTextureTarget(GLenum aTarget);
  void SetRBSwap(bool aEnabled);
  void SetNoAlpha(bool aEnabled);
  void SetOpacity(bool aEnabled);
  void SetYCbCr(bool aEnabled);
  void SetNV12(bool aEnabled);
  void SetComponentAlpha(bool aEnabled);
  void SetColorMatrix(bool aEnabled);
  void SetBlur(bool aEnabled);
  void SetMask(bool aEnabled);
  void SetDEAA(bool aEnabled);
  void SetCompositionOp(gfx::CompositionOp aOp);
  void SetNoPremultipliedAlpha();
  void SetDynamicGeometry(bool aEnabled);
  void SetColorMultiplier(uint32_t aMultiplier);

  bool operator<(const ShaderConfigOGL& other) const {
    return mFeatures < other.mFeatures ||
           (mFeatures == other.mFeatures &&
            (int)mCompositionOp < (int)other.mCompositionOp) ||
           (mFeatures == other.mFeatures &&
            (int)mCompositionOp == (int)other.mCompositionOp &&
            mMultiplier < other.mMultiplier);
  }

 public:
  void SetFeature(int aBitmask, bool aState) {
    if (aState)
      mFeatures |= aBitmask;
    else
      mFeatures &= (~aBitmask);
  }

  int mFeatures;
  uint32_t mMultiplier;
  gfx::CompositionOp mCompositionOp;
};

static inline ShaderConfigOGL ShaderConfigFromTargetAndFormat(
    GLenum aTarget, gfx::SurfaceFormat aFormat) {
  ShaderConfigOGL config;
  config.SetTextureTarget(aTarget);
  config.SetRBSwap(aFormat == gfx::SurfaceFormat::B8G8R8A8 ||
                   aFormat == gfx::SurfaceFormat::B8G8R8X8);
  config.SetNoAlpha(aFormat == gfx::SurfaceFormat::B8G8R8X8 ||
                    aFormat == gfx::SurfaceFormat::R8G8B8X8 ||
                    aFormat == gfx::SurfaceFormat::R5G6B5_UINT16);
  return config;
}

}  // namespace layers
}  // namespace mozilla

#endif  // GFX_OGLSHADERCONFIG_H