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 MOZILLA_LAYERS_EFFECTS_H
8 #define MOZILLA_LAYERS_EFFECTS_H
10 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
11 #include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed, etc
12 #include "mozilla/gfx/Matrix.h" // for Matrix4x4
13 #include "mozilla/gfx/Point.h" // for IntSize
14 #include "mozilla/gfx/Rect.h" // for Rect
15 #include "mozilla/gfx/Types.h" // for SamplingFilter, etc
16 #include "mozilla/layers/CompositorTypes.h" // for EffectTypes, etc
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"
26 * Effects and effect chains are used by the compositor API (see Compositor.h).
27 * An effect chain represents a rendering method, for example some shader and
28 * the data required for that shader to run. An effect is some component of the
31 * An effect chain consists of a primary effect - how the 'texture' memory
32 * should be interpreted (RGBA, BGRX, YCBCR, etc.) - and any number of secondary
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 TexturedEffect
;
45 NS_INLINE_DECL_REFCOUNTING(Effect
)
47 explicit Effect(EffectTypes aType
) : mType(aType
) {}
51 virtual TexturedEffect
* AsTexturedEffect() { return nullptr; }
52 virtual void PrintInfo(std::stringstream
& aStream
, const char* aPrefix
) = 0;
55 virtual ~Effect() = default;
58 // Render from a texture
59 struct TexturedEffect
: public Effect
{
60 TexturedEffect(EffectTypes aType
, TextureSource
* aTexture
,
61 bool aPremultiplied
, gfx::SamplingFilter aSamplingFilter
)
63 mTextureCoords(0, 0, 1.0f
, 1.0f
),
65 mPremultiplied(aPremultiplied
),
66 mPremultipliedCopy(false),
67 mSamplingFilter(aSamplingFilter
) {}
69 TexturedEffect
* AsTexturedEffect() override
{ return this; }
70 virtual const char* Name() = 0;
71 void PrintInfo(std::stringstream
& aStream
, const char* aPrefix
) override
;
73 gfx::Rect mTextureCoords
;
74 TextureSource
* mTexture
;
76 bool mPremultipliedCopy
;
77 gfx::SamplingFilter mSamplingFilter
;
80 struct EffectRGB
: public TexturedEffect
{
81 EffectRGB(TextureSource
* aTexture
, bool aPremultiplied
,
82 gfx::SamplingFilter aSamplingFilter
, bool aFlipped
= false)
83 : TexturedEffect(EffectTypes::RGB
, aTexture
, aPremultiplied
,
86 const char* Name() override
{ return "EffectRGB"; }
89 struct EffectYCbCr
: public TexturedEffect
{
90 EffectYCbCr(TextureSource
* aSource
, gfx::YUVColorSpace aYUVColorSpace
,
91 gfx::ColorRange aColorRange
, gfx::ColorDepth aColorDepth
,
92 gfx::SamplingFilter aSamplingFilter
)
93 : TexturedEffect(EffectTypes::YCBCR
, aSource
, false, aSamplingFilter
),
94 mYUVColorSpace(aYUVColorSpace
),
95 mColorRange(aColorRange
),
96 mColorDepth(aColorDepth
) {}
98 const char* Name() override
{ return "EffectYCbCr"; }
100 gfx::YUVColorSpace mYUVColorSpace
;
101 gfx::ColorRange mColorRange
;
102 gfx::ColorDepth mColorDepth
;
105 struct EffectNV12
: public EffectYCbCr
{
106 EffectNV12(TextureSource
* aSource
, gfx::YUVColorSpace aYUVColorSpace
,
107 gfx::ColorRange aColorRange
, gfx::ColorDepth aColorDepth
,
108 gfx::SamplingFilter aSamplingFilter
)
109 : EffectYCbCr(aSource
, aYUVColorSpace
, aColorRange
, aColorDepth
,
111 mType
= EffectTypes::NV12
;
114 const char* Name() override
{ return "EffectNV12"; }
118 RefPtr
<Effect
> mPrimaryEffect
;
122 * Create a Textured effect corresponding to aFormat and using
123 * aSource as the (first) texture source.
125 * Note that aFormat can be different form aSource->GetFormat if, we are
126 * creating an effect that takes several texture sources (like with YCBCR
127 * where aFormat would be FORMAT_YCBCR and each texture source would be
128 * a one-channel A8 texture)
130 inline already_AddRefed
<TexturedEffect
> CreateTexturedEffect(
131 gfx::SurfaceFormat aFormat
, TextureSource
* aSource
,
132 const gfx::SamplingFilter aSamplingFilter
, bool isAlphaPremultiplied
) {
134 RefPtr
<TexturedEffect
> result
;
136 case gfx::SurfaceFormat::B8G8R8A8
:
137 case gfx::SurfaceFormat::B8G8R8X8
:
138 case gfx::SurfaceFormat::R8G8B8X8
:
139 case gfx::SurfaceFormat::R5G6B5_UINT16
:
140 case gfx::SurfaceFormat::R8G8B8A8
:
141 result
= new EffectRGB(aSource
, isAlphaPremultiplied
, aSamplingFilter
);
143 case gfx::SurfaceFormat::YUV
:
144 case gfx::SurfaceFormat::NV12
:
145 case gfx::SurfaceFormat::P010
:
146 case gfx::SurfaceFormat::P016
:
147 MOZ_ASSERT_UNREACHABLE(
148 "gfx::SurfaceFormat::YUV/NV12/P010/P016 is invalid");
151 NS_WARNING("unhandled program type");
155 return result
.forget();
158 inline already_AddRefed
<TexturedEffect
> CreateTexturedEffect(
159 TextureHost
* aHost
, TextureSource
* aSource
,
160 const gfx::SamplingFilter aSamplingFilter
, bool isAlphaPremultiplied
) {
164 RefPtr
<TexturedEffect
> result
;
166 switch (aHost
->GetReadFormat()) {
167 case gfx::SurfaceFormat::YUV
:
168 result
= new EffectYCbCr(aSource
, aHost
->GetYUVColorSpace(),
169 aHost
->GetColorRange(), aHost
->GetColorDepth(),
172 case gfx::SurfaceFormat::NV12
:
173 case gfx::SurfaceFormat::P010
:
174 case gfx::SurfaceFormat::P016
:
175 result
= new EffectNV12(aSource
, aHost
->GetYUVColorSpace(),
176 aHost
->GetColorRange(), aHost
->GetColorDepth(),
180 result
= CreateTexturedEffect(aHost
->GetReadFormat(), aSource
,
181 aSamplingFilter
, isAlphaPremultiplied
);
184 return result
.forget();
188 * Create a textured effect based on aSource format.
190 * This version excudes the possibility of component alpha.
192 inline already_AddRefed
<TexturedEffect
> CreateTexturedEffect(
193 TextureSource
* aTexture
, const gfx::SamplingFilter aSamplingFilter
) {
194 return CreateTexturedEffect(aTexture
->GetFormat(), aTexture
, aSamplingFilter
,
198 } // namespace layers
199 } // namespace mozilla