Bumping manifests a=b2g-bump
[gecko.git] / gfx / layers / Effects.h
blob8a09b987798be4c1cadb425403efc40965f3c51f
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_LAYERS_EFFECTS_H
7 #define MOZILLA_LAYERS_EFFECTS_H
9 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
10 #include "mozilla/RefPtr.h" // for RefPtr, TemporaryRef, etc
11 #include "mozilla/gfx/Matrix.h" // for Matrix4x4
12 #include "mozilla/gfx/Point.h" // for IntSize
13 #include "mozilla/gfx/Rect.h" // for Rect
14 #include "mozilla/gfx/Types.h" // for Filter, etc
15 #include "mozilla/layers/CompositorTypes.h" // for EffectTypes, etc
16 #include "mozilla/layers/LayersTypes.h"
17 #include "mozilla/layers/TextureHost.h" // for CompositingRenderTarget, etc
18 #include "mozilla/mozalloc.h" // for operator delete, etc
19 #include "nscore.h" // for nsACString
20 #include "mozilla/EnumeratedArray.h"
21 #include "gfxVR.h"
23 namespace mozilla {
24 namespace layers {
26 /**
27 * Effects and effect chains are used by the compositor API (see Compositor.h).
28 * An effect chain represents a rendering method, for example some shader and
29 * the data required for that shader to run. An effect is some component of the
30 * chain and its data.
32 * An effect chain consists of a primary effect - how the 'texture' memory should
33 * be interpreted (RGBA, BGRX, YCBCR, etc.) - and any number of secondary effects
34 * - any way in which rendering can be changed, e.g., applying a mask layer.
36 * During the rendering process, an effect chain is created by the layer being
37 * rendered and the primary effect is added by the compositable host. Secondary
38 * effects may be added by the layer or compositable. The effect chain is passed
39 * to the compositor by the compositable host as a parameter to DrawQuad.
42 struct Effect
44 NS_INLINE_DECL_REFCOUNTING(Effect)
46 explicit Effect(EffectTypes aType) : mType(aType) {}
48 EffectTypes mType;
50 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix) = 0;
52 protected:
53 virtual ~Effect() {}
56 // Render from a texture
57 struct TexturedEffect : public Effect
59 TexturedEffect(EffectTypes aType,
60 TextureSource *aTexture,
61 bool aPremultiplied,
62 gfx::Filter aFilter)
63 : Effect(aType)
64 , mTextureCoords(0, 0, 1.0f, 1.0f)
65 , mTexture(aTexture)
66 , mPremultiplied(aPremultiplied)
67 , mFilter(aFilter)
70 virtual const char* Name() = 0;
71 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
73 gfx::Rect mTextureCoords;
74 TextureSource* mTexture;
75 bool mPremultiplied;
76 gfx::Filter mFilter;
79 // Support an alpha mask.
80 struct EffectMask : public Effect
82 EffectMask(TextureSource *aMaskTexture,
83 gfx::IntSize aSize,
84 const gfx::Matrix4x4 &aMaskTransform)
85 : Effect(EffectTypes::MASK)
86 , mMaskTexture(aMaskTexture)
87 , mIs3D(false)
88 , mSize(aSize)
89 , mMaskTransform(aMaskTransform)
92 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
94 TextureSource* mMaskTexture;
95 bool mIs3D;
96 gfx::IntSize mSize;
97 gfx::Matrix4x4 mMaskTransform;
100 struct EffectVRDistortion : public Effect
102 EffectVRDistortion(gfx::VRHMDInfo* aHMD,
103 CompositingRenderTarget* aRenderTarget)
104 : Effect(EffectTypes::VR_DISTORTION)
105 , mHMD(aHMD)
106 , mRenderTarget(aRenderTarget)
107 , mTexture(aRenderTarget)
110 EffectVRDistortion(gfx::VRHMDInfo* aHMD,
111 TextureSource* aTexture)
112 : Effect(EffectTypes::VR_DISTORTION)
113 , mHMD(aHMD)
114 , mRenderTarget(nullptr)
115 , mTexture(aTexture)
118 virtual const char* Name() { return "EffectVRDistortion"; }
119 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
121 nsRefPtr<gfx::VRHMDInfo> mHMD;
122 RefPtr<CompositingRenderTarget> mRenderTarget;
123 TextureSource* mTexture;
125 // The viewport for each eye in the source and
126 // destination textures.
127 gfx::IntRect mViewports[2];
130 struct EffectBlendMode : public Effect
132 explicit EffectBlendMode(gfx::CompositionOp aBlendMode)
133 : Effect(EffectTypes::BLEND_MODE)
134 , mBlendMode(aBlendMode)
137 virtual const char* Name() { return "EffectBlendMode"; }
138 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
140 gfx::CompositionOp mBlendMode;
143 // Render to a render target rather than the screen.
144 struct EffectRenderTarget : public TexturedEffect
146 explicit EffectRenderTarget(CompositingRenderTarget *aRenderTarget)
147 : TexturedEffect(EffectTypes::RENDER_TARGET, aRenderTarget, true, gfx::Filter::LINEAR)
148 , mRenderTarget(aRenderTarget)
151 virtual const char* Name() { return "EffectRenderTarget"; }
152 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
154 RefPtr<CompositingRenderTarget> mRenderTarget;
156 protected:
157 EffectRenderTarget(EffectTypes aType, CompositingRenderTarget *aRenderTarget)
158 : TexturedEffect(aType, aRenderTarget, true, gfx::Filter::LINEAR)
159 , mRenderTarget(aRenderTarget)
164 // Render to a render target rather than the screen.
165 struct EffectColorMatrix : public Effect
167 explicit EffectColorMatrix(gfx::Matrix5x4 aMatrix)
168 : Effect(EffectTypes::COLOR_MATRIX)
169 , mColorMatrix(aMatrix)
172 virtual const char* Name() { return "EffectColorMatrix"; }
173 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
174 const gfx::Matrix5x4 mColorMatrix;
178 struct EffectRGB : public TexturedEffect
180 EffectRGB(TextureSource *aTexture,
181 bool aPremultiplied,
182 gfx::Filter aFilter,
183 bool aFlipped = false)
184 : TexturedEffect(EffectTypes::RGB, aTexture, aPremultiplied, aFilter)
187 virtual const char* Name() { return "EffectRGB"; }
190 struct EffectYCbCr : public TexturedEffect
192 EffectYCbCr(TextureSource *aSource, gfx::Filter aFilter)
193 : TexturedEffect(EffectTypes::YCBCR, aSource, false, aFilter)
196 virtual const char* Name() { return "EffectYCbCr"; }
199 struct EffectComponentAlpha : public TexturedEffect
201 EffectComponentAlpha(TextureSource *aOnBlack,
202 TextureSource *aOnWhite,
203 gfx::Filter aFilter)
204 : TexturedEffect(EffectTypes::COMPONENT_ALPHA, nullptr, false, aFilter)
205 , mOnBlack(aOnBlack)
206 , mOnWhite(aOnWhite)
209 virtual const char* Name() { return "EffectComponentAlpha"; }
211 TextureSource* mOnBlack;
212 TextureSource* mOnWhite;
215 struct EffectSolidColor : public Effect
217 explicit EffectSolidColor(const gfx::Color &aColor)
218 : Effect(EffectTypes::SOLID_COLOR)
219 , mColor(aColor)
222 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
224 gfx::Color mColor;
227 struct EffectChain
229 EffectChain() : mLayerRef(nullptr) {}
230 explicit EffectChain(void* aLayerRef) : mLayerRef(aLayerRef) {}
232 RefPtr<Effect> mPrimaryEffect;
233 EnumeratedArray<EffectTypes, EffectTypes::MAX_SECONDARY, RefPtr<Effect>>
234 mSecondaryEffects;
235 void* mLayerRef; //!< For LayerScope logging
239 * Create a Textured effect corresponding to aFormat and using
240 * aSource as the (first) texture source.
242 * Note that aFormat can be different form aSource->GetFormat if, we are
243 * creating an effect that takes several texture sources (like with YCBCR
244 * where aFormat would be FOMRAT_YCBCR and each texture source would be
245 * a one-channel A8 texture)
247 inline TemporaryRef<TexturedEffect>
248 CreateTexturedEffect(gfx::SurfaceFormat aFormat,
249 TextureSource* aSource,
250 const gfx::Filter& aFilter,
251 bool isAlphaPremultiplied)
253 MOZ_ASSERT(aSource);
254 RefPtr<TexturedEffect> result;
255 switch (aFormat) {
256 case gfx::SurfaceFormat::B8G8R8A8:
257 case gfx::SurfaceFormat::B8G8R8X8:
258 case gfx::SurfaceFormat::R8G8B8X8:
259 case gfx::SurfaceFormat::R5G6B5:
260 case gfx::SurfaceFormat::R8G8B8A8:
261 result = new EffectRGB(aSource, isAlphaPremultiplied, aFilter);
262 break;
263 case gfx::SurfaceFormat::YUV:
264 result = new EffectYCbCr(aSource, aFilter);
265 break;
266 default:
267 NS_WARNING("unhandled program type");
268 break;
271 return result;
275 * Create a textured effect based on aSource format and the presence of
276 * aSourceOnWhite.
278 * aSourceOnWhite can be null.
280 inline TemporaryRef<TexturedEffect>
281 CreateTexturedEffect(TextureSource* aSource,
282 TextureSource* aSourceOnWhite,
283 const gfx::Filter& aFilter,
284 bool isAlphaPremultiplied)
286 MOZ_ASSERT(aSource);
287 if (aSourceOnWhite) {
288 MOZ_ASSERT(aSource->GetFormat() == gfx::SurfaceFormat::R8G8B8X8 ||
289 aSourceOnWhite->GetFormat() == gfx::SurfaceFormat::B8G8R8X8);
290 return new EffectComponentAlpha(aSource, aSourceOnWhite, aFilter);
293 return CreateTexturedEffect(aSource->GetFormat(),
294 aSource,
295 aFilter,
296 isAlphaPremultiplied);
300 * Create a textured effect based on aSource format.
302 * This version excudes the possibility of component alpha.
304 inline TemporaryRef<TexturedEffect>
305 CreateTexturedEffect(TextureSource *aTexture,
306 const gfx::Filter& aFilter)
308 return CreateTexturedEffect(aTexture, nullptr, aFilter, true);
312 } // namespace layers
313 } // namespace mozilla
315 #endif