Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / gfx / layers / Effects.h
blob694b7c1240d323bf16abaf278702544e472d09b2
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"
22 namespace mozilla {
23 namespace layers {
25 /**
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
29 * chain and its data.
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
33 * 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 TexturedEffect;
44 struct Effect {
45 NS_INLINE_DECL_REFCOUNTING(Effect)
47 explicit Effect(EffectTypes aType) : mType(aType) {}
49 EffectTypes mType;
51 virtual TexturedEffect* AsTexturedEffect() { return nullptr; }
52 virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix) = 0;
54 protected:
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)
62 : Effect(aType),
63 mTextureCoords(0, 0, 1.0f, 1.0f),
64 mTexture(aTexture),
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;
75 bool mPremultiplied;
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,
84 aSamplingFilter) {}
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,
110 aSamplingFilter) {
111 mType = EffectTypes::NV12;
114 const char* Name() override { return "EffectNV12"; }
117 struct EffectChain {
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) {
133 MOZ_ASSERT(aSource);
134 RefPtr<TexturedEffect> result;
135 switch (aFormat) {
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);
142 break;
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");
149 break;
150 default:
151 NS_WARNING("unhandled program type");
152 break;
155 return result.forget();
158 inline already_AddRefed<TexturedEffect> CreateTexturedEffect(
159 TextureHost* aHost, TextureSource* aSource,
160 const gfx::SamplingFilter aSamplingFilter, bool isAlphaPremultiplied) {
161 MOZ_ASSERT(aHost);
162 MOZ_ASSERT(aSource);
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(),
170 aSamplingFilter);
171 break;
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(),
177 aSamplingFilter);
178 break;
179 default:
180 result = CreateTexturedEffect(aHost->GetReadFormat(), aSource,
181 aSamplingFilter, isAlphaPremultiplied);
182 break;
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,
195 true);
198 } // namespace layers
199 } // namespace mozilla
201 #endif