Backed out 2 changesets (bug 1931901) for causing build bustages in TelemetryScalar...
[gecko.git] / gfx / layers / opengl / OGLShaderConfig.h
blobf7c939aa98ac860dc96737478f5ae41f9e3db139
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/. */
7 #ifndef GFX_OGLSHADERCONFIG_H
8 #define GFX_OGLSHADERCONFIG_H
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"
22 namespace mozilla {
23 namespace layers {
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,
45 class KnownUniform {
46 public:
47 // this needs to be kept in sync with strings in 'AddUniforms'
48 enum KnownUniformName {
49 NotAKnownUniform = -1,
51 LayerTransform = 0,
52 LayerTransformInverse,
53 MaskTransform,
54 BackdropTransform,
55 LayerRects,
56 MatrixProj,
57 TextureTransform,
58 TextureRects,
59 RenderTargetOffset,
60 LayerOpacity,
61 Texture,
62 YTexture,
63 CbTexture,
64 CrTexture,
65 RenderColor,
66 TexCoordMultiplier,
67 CbCrTexCoordMultiplier,
68 SSEdges,
69 ViewportSize,
70 VisibleCenter,
71 YuvColorMatrix,
72 YuvOffsetVector,
74 KnownUniformCount
77 KnownUniform() {
78 mName = NotAKnownUniform;
79 mNameString = nullptr;
80 mLocation = -1;
81 memset(&mValue, 0, sizeof(mValue));
84 bool UpdateUniform(int32_t i1) {
85 if (mLocation == -1) return false;
86 if (mValue.i1 != i1) {
87 mValue.i1 = i1;
88 return true;
90 return false;
93 bool UpdateUniform(float f1) {
94 if (mLocation == -1) return false;
95 if (mValue.f1 != f1) {
96 mValue.f1 = f1;
97 return true;
99 return false;
102 bool UpdateUniform(float f1, float f2) {
103 if (mLocation == -1) return false;
104 if (mValue.f16v[0] != f1 || mValue.f16v[1] != f2) {
105 mValue.f16v[0] = f1;
106 mValue.f16v[1] = f2;
107 return true;
109 return false;
112 bool UpdateUniform(float f1, float f2, float f3, float f4) {
113 if (mLocation == -1) return false;
114 if (mValue.f16v[0] != f1 || mValue.f16v[1] != f2 || mValue.f16v[2] != f3 ||
115 mValue.f16v[3] != f4) {
116 mValue.f16v[0] = f1;
117 mValue.f16v[1] = f2;
118 mValue.f16v[2] = f3;
119 mValue.f16v[3] = f4;
120 return true;
122 return false;
125 bool UpdateUniform(int cnt, const float* fp) {
126 if (mLocation == -1) return false;
127 switch (cnt) {
128 case 1:
129 case 2:
130 case 3:
131 case 4:
132 case 9:
133 case 16:
134 if (memcmp(mValue.f16v, fp, sizeof(float) * cnt) != 0) {
135 memcpy(mValue.f16v, fp, sizeof(float) * cnt);
136 return true;
138 return false;
141 MOZ_ASSERT_UNREACHABLE("cnt must be 1 2 3 4 9 or 16");
142 return false;
145 bool UpdateArrayUniform(int cnt, const float* fp) {
146 if (mLocation == -1) return false;
147 if (cnt > 16) {
148 return false;
151 if (memcmp(mValue.f16v, fp, sizeof(float) * cnt) != 0) {
152 memcpy(mValue.f16v, fp, sizeof(float) * cnt);
153 return true;
155 return false;
158 bool UpdateArrayUniform(int cnt, const gfx::Point3D* points) {
159 if (mLocation == -1) return false;
160 if (cnt > 4) {
161 return false;
164 float fp[12];
165 float* d = fp;
166 for (int i = 0; i < cnt; i++) {
167 // Note: Do not want to make assumptions about .x, .y, .z member packing.
168 // If gfx::Point3D is updated to make this guarantee, SIMD optimizations
169 // may be possible
170 *d++ = points[i].x;
171 *d++ = points[i].y;
172 *d++ = points[i].z;
175 if (memcmp(mValue.f16v, fp, sizeof(float) * cnt * 3) != 0) {
176 memcpy(mValue.f16v, fp, sizeof(float) * cnt * 3);
177 return true;
179 return false;
182 KnownUniformName mName;
183 const char* mNameString;
184 int32_t mLocation;
186 union {
187 int i1;
188 float f1;
189 float f16v[16];
190 } mValue;
193 class ShaderConfigOGL {
194 public:
195 ShaderConfigOGL()
196 : mFeatures(0),
197 mMultiplier(1),
198 mCompositionOp(gfx::CompositionOp::OP_OVER) {}
200 void SetRenderColor(bool aEnabled);
201 void SetTextureTarget(GLenum aTarget);
202 void SetMaskTextureTarget(GLenum aTarget);
203 void SetRBSwap(bool aEnabled);
204 void SetNoAlpha(bool aEnabled);
205 void SetOpacity(bool aEnabled);
206 void SetYCbCr(bool aEnabled);
207 void SetNV12(bool aEnabled);
208 void SetComponentAlpha(bool aEnabled);
209 void SetColorMatrix(bool aEnabled);
210 void SetBlur(bool aEnabled);
211 void SetMask(bool aEnabled);
212 void SetDEAA(bool aEnabled);
213 void SetCompositionOp(gfx::CompositionOp aOp);
214 void SetNoPremultipliedAlpha();
215 void SetDynamicGeometry(bool aEnabled);
216 void SetColorMultiplier(uint32_t aMultiplier);
218 bool operator<(const ShaderConfigOGL& other) const {
219 return mFeatures < other.mFeatures ||
220 (mFeatures == other.mFeatures &&
221 (int)mCompositionOp < (int)other.mCompositionOp) ||
222 (mFeatures == other.mFeatures &&
223 (int)mCompositionOp == (int)other.mCompositionOp &&
224 mMultiplier < other.mMultiplier);
227 public:
228 void SetFeature(int aBitmask, bool aState) {
229 if (aState)
230 mFeatures |= aBitmask;
231 else
232 mFeatures &= (~aBitmask);
235 int mFeatures;
236 uint32_t mMultiplier;
237 gfx::CompositionOp mCompositionOp;
240 static inline ShaderConfigOGL ShaderConfigFromTargetAndFormat(
241 GLenum aTarget, gfx::SurfaceFormat aFormat) {
242 ShaderConfigOGL config;
243 config.SetTextureTarget(aTarget);
244 config.SetRBSwap(aFormat == gfx::SurfaceFormat::B8G8R8A8 ||
245 aFormat == gfx::SurfaceFormat::B8G8R8X8);
246 config.SetNoAlpha(aFormat == gfx::SurfaceFormat::B8G8R8X8 ||
247 aFormat == gfx::SurfaceFormat::R8G8B8X8 ||
248 aFormat == gfx::SurfaceFormat::R5G6B5_UINT16);
249 return config;
252 } // namespace layers
253 } // namespace mozilla
255 #endif // GFX_OGLSHADERCONFIG_H