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"
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
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.
44 NS_INLINE_DECL_REFCOUNTING(Effect
)
46 explicit Effect(EffectTypes aType
) : mType(aType
) {}
50 virtual void PrintInfo(std::stringstream
& aStream
, const char* aPrefix
) = 0;
56 // Render from a texture
57 struct TexturedEffect
: public Effect
59 TexturedEffect(EffectTypes aType
,
60 TextureSource
*aTexture
,
64 , mTextureCoords(0, 0, 1.0f
, 1.0f
)
66 , mPremultiplied(aPremultiplied
)
70 virtual const char* Name() = 0;
71 virtual void PrintInfo(std::stringstream
& aStream
, const char* aPrefix
);
73 gfx::Rect mTextureCoords
;
74 TextureSource
* mTexture
;
79 // Support an alpha mask.
80 struct EffectMask
: public Effect
82 EffectMask(TextureSource
*aMaskTexture
,
84 const gfx::Matrix4x4
&aMaskTransform
)
85 : Effect(EffectTypes::MASK
)
86 , mMaskTexture(aMaskTexture
)
89 , mMaskTransform(aMaskTransform
)
92 virtual void PrintInfo(std::stringstream
& aStream
, const char* aPrefix
);
94 TextureSource
* mMaskTexture
;
97 gfx::Matrix4x4 mMaskTransform
;
100 struct EffectVRDistortion
: public Effect
102 EffectVRDistortion(gfx::VRHMDInfo
* aHMD
,
103 CompositingRenderTarget
* aRenderTarget
)
104 : Effect(EffectTypes::VR_DISTORTION
)
106 , mRenderTarget(aRenderTarget
)
107 , mTexture(aRenderTarget
)
110 EffectVRDistortion(gfx::VRHMDInfo
* aHMD
,
111 TextureSource
* aTexture
)
112 : Effect(EffectTypes::VR_DISTORTION
)
114 , mRenderTarget(nullptr)
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
;
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
,
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
,
204 : TexturedEffect(EffectTypes::COMPONENT_ALPHA
, nullptr, false, aFilter
)
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
)
222 virtual void PrintInfo(std::stringstream
& aStream
, const char* aPrefix
);
229 EffectChain() : mLayerRef(nullptr) {}
230 explicit EffectChain(void* aLayerRef
) : mLayerRef(aLayerRef
) {}
232 RefPtr
<Effect
> mPrimaryEffect
;
233 EnumeratedArray
<EffectTypes
, EffectTypes::MAX_SECONDARY
, RefPtr
<Effect
>>
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
)
254 RefPtr
<TexturedEffect
> result
;
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
);
263 case gfx::SurfaceFormat::YUV
:
264 result
= new EffectYCbCr(aSource
, aFilter
);
267 NS_WARNING("unhandled program type");
275 * Create a textured effect based on aSource format and the presence of
278 * aSourceOnWhite can be null.
280 inline TemporaryRef
<TexturedEffect
>
281 CreateTexturedEffect(TextureSource
* aSource
,
282 TextureSource
* aSourceOnWhite
,
283 const gfx::Filter
& aFilter
,
284 bool isAlphaPremultiplied
)
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(),
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