summaryrefslogtreecommitdiff
path: root/gfx/layers/Effects.h
blob: a0859a539ef4f49ff2c64f030b020c59dbc7550c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MOZILLA_LAYERS_EFFECTS_H
#define MOZILLA_LAYERS_EFFECTS_H

#include "mozilla/Assertions.h"         // for MOZ_ASSERT, etc
#include "mozilla/RefPtr.h"             // for RefPtr, already_AddRefed, etc
#include "mozilla/gfx/Matrix.h"         // for Matrix4x4
#include "mozilla/gfx/Point.h"          // for IntSize
#include "mozilla/gfx/Rect.h"           // for Rect
#include "mozilla/gfx/Types.h"          // for SamplingFilter, etc
#include "mozilla/layers/CompositorTypes.h"  // for EffectTypes, etc
#include "mozilla/layers/LayersTypes.h"
#include "mozilla/layers/TextureHost.h"  // for CompositingRenderTarget, etc
#include "mozilla/mozalloc.h"           // for operator delete, etc
#include "nscore.h"                     // for nsACString
#include "mozilla/EnumeratedArray.h"

namespace mozilla {
namespace layers {

/**
 * Effects and effect chains are used by the compositor API (see Compositor.h).
 * An effect chain represents a rendering method, for example some shader and
 * the data required for that shader to run. An effect is some component of the
 * chain and its data.
 *
 * An effect chain consists of a primary effect - how the 'texture' memory should
 * be interpreted (RGBA, BGRX, YCBCR, etc.) - and any number of secondary effects
 * - any way in which rendering can be changed, e.g., applying a mask layer.
 *
 * During the rendering process, an effect chain is created by the layer being
 * rendered and the primary effect is added by the compositable host. Secondary
 * effects may be added by the layer or compositable. The effect chain is passed
 * to the compositor by the compositable host as a parameter to DrawQuad.
 */

struct Effect
{
  NS_INLINE_DECL_REFCOUNTING(Effect)

  explicit Effect(EffectTypes aType) : mType(aType) {}

  EffectTypes mType;

  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix) = 0;

protected:
  virtual ~Effect() {}
};

// Render from a texture
struct TexturedEffect : public Effect
{
  TexturedEffect(EffectTypes aType,
                 TextureSource *aTexture,
                 bool aPremultiplied,
                 gfx::SamplingFilter aSamplingFilter)
     : Effect(aType)
     , mTextureCoords(0, 0, 1.0f, 1.0f)
     , mTexture(aTexture)
     , mPremultiplied(aPremultiplied)
     , mSamplingFilter(aSamplingFilter)
  {}

  virtual const char* Name() = 0;
  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);

  gfx::Rect mTextureCoords;
  TextureSource* mTexture;
  bool mPremultiplied;
  gfx::SamplingFilter mSamplingFilter;
  LayerRenderState mState;
};

// Support an alpha mask.
struct EffectMask : public Effect
{
  EffectMask(TextureSource *aMaskTexture,
             gfx::IntSize aSize,
             const gfx::Matrix4x4 &aMaskTransform)
    : Effect(EffectTypes::MASK)
    , mMaskTexture(aMaskTexture)
    , mSize(aSize)
    , mMaskTransform(aMaskTransform)
  {}

  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);

  TextureSource* mMaskTexture;
  gfx::IntSize mSize;
  gfx::Matrix4x4 mMaskTransform;
};

struct EffectBlendMode : public Effect
{
  explicit EffectBlendMode(gfx::CompositionOp aBlendMode)
    : Effect(EffectTypes::BLEND_MODE)
    , mBlendMode(aBlendMode)
  { }

  virtual const char* Name() { return "EffectBlendMode"; }
  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);

  gfx::CompositionOp mBlendMode;
};

// Render to a render target rather than the screen.
struct EffectRenderTarget : public TexturedEffect
{
  explicit EffectRenderTarget(CompositingRenderTarget *aRenderTarget)
    : TexturedEffect(EffectTypes::RENDER_TARGET, aRenderTarget, true,
                     gfx::SamplingFilter::LINEAR)
    , mRenderTarget(aRenderTarget)
  {}

  virtual const char* Name() { return "EffectRenderTarget"; }
  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);

  RefPtr<CompositingRenderTarget> mRenderTarget;

protected:
  EffectRenderTarget(EffectTypes aType, CompositingRenderTarget *aRenderTarget)
    : TexturedEffect(aType, aRenderTarget, true, gfx::SamplingFilter::LINEAR)
    , mRenderTarget(aRenderTarget)
  {}

};

// Render to a render target rather than the screen.
struct EffectColorMatrix : public Effect
{
  explicit EffectColorMatrix(gfx::Matrix5x4 aMatrix)
    : Effect(EffectTypes::COLOR_MATRIX)
    , mColorMatrix(aMatrix)
  {}

  virtual const char* Name() { return "EffectColorMatrix"; }
  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);
  const gfx::Matrix5x4 mColorMatrix;
};


struct EffectRGB : public TexturedEffect
{
  EffectRGB(TextureSource *aTexture,
            bool aPremultiplied,
            gfx::SamplingFilter aSamplingFilter,
            bool aFlipped = false)
    : TexturedEffect(EffectTypes::RGB, aTexture, aPremultiplied, aSamplingFilter)
  {}

  virtual const char* Name() { return "EffectRGB"; }
};

struct EffectYCbCr : public TexturedEffect
{
  EffectYCbCr(TextureSource *aSource, YUVColorSpace aYUVColorSpace, gfx::SamplingFilter aSamplingFilter)
    : TexturedEffect(EffectTypes::YCBCR, aSource, false, aSamplingFilter)
    , mYUVColorSpace(aYUVColorSpace)
  {}

  virtual const char* Name() { return "EffectYCbCr"; }

  YUVColorSpace mYUVColorSpace;
};

struct EffectNV12 : public TexturedEffect
{
  EffectNV12(TextureSource *aSource, gfx::SamplingFilter aSamplingFilter)
    : TexturedEffect(EffectTypes::NV12, aSource, false, aSamplingFilter)
  {}

  virtual const char* Name() { return "EffectNV12"; }
};

struct EffectComponentAlpha : public TexturedEffect
{
  EffectComponentAlpha(TextureSource *aOnBlack,
                       TextureSource *aOnWhite,
                       gfx::SamplingFilter aSamplingFilter)
    : TexturedEffect(EffectTypes::COMPONENT_ALPHA, nullptr, false, aSamplingFilter)
    , mOnBlack(aOnBlack)
    , mOnWhite(aOnWhite)
  {}

  virtual const char* Name() { return "EffectComponentAlpha"; }

  TextureSource* mOnBlack;
  TextureSource* mOnWhite;
};

struct EffectSolidColor : public Effect
{
  explicit EffectSolidColor(const gfx::Color &aColor)
    : Effect(EffectTypes::SOLID_COLOR)
    , mColor(aColor)
  {}

  virtual void PrintInfo(std::stringstream& aStream, const char* aPrefix);

  gfx::Color mColor;
};

struct EffectChain
{
  EffectChain() : mLayerRef(nullptr) {}
  explicit EffectChain(void* aLayerRef) : mLayerRef(aLayerRef) {}

  RefPtr<Effect> mPrimaryEffect;
  EnumeratedArray<EffectTypes, EffectTypes::MAX_SECONDARY, RefPtr<Effect>>
    mSecondaryEffects;
  void* mLayerRef; //!< For LayerScope logging
};

/**
 * Create a Textured effect corresponding to aFormat and using
 * aSource as the (first) texture source.
 *
 * Note that aFormat can be different form aSource->GetFormat if, we are
 * creating an effect that takes several texture sources (like with YCBCR
 * where aFormat would be FOMRAT_YCBCR and each texture source would be
 * a one-channel A8 texture)
 */
inline already_AddRefed<TexturedEffect>
CreateTexturedEffect(gfx::SurfaceFormat aFormat,
                     TextureSource* aSource,
                     const gfx::SamplingFilter aSamplingFilter,
                     bool isAlphaPremultiplied,
                     const LayerRenderState &state = LayerRenderState())
{
  MOZ_ASSERT(aSource);
  RefPtr<TexturedEffect> result;
  switch (aFormat) {
  case gfx::SurfaceFormat::B8G8R8A8:
  case gfx::SurfaceFormat::B8G8R8X8:
  case gfx::SurfaceFormat::R8G8B8X8:
  case gfx::SurfaceFormat::R5G6B5_UINT16:
  case gfx::SurfaceFormat::R8G8B8A8:
    result = new EffectRGB(aSource, isAlphaPremultiplied, aSamplingFilter);
    break;
  case gfx::SurfaceFormat::NV12:
    result = new EffectNV12(aSource, aSamplingFilter);
    break;
  case gfx::SurfaceFormat::YUV:
    MOZ_ASSERT_UNREACHABLE("gfx::SurfaceFormat::YUV is invalid");
    break;
  default:
    NS_WARNING("unhandled program type");
    break;
  }

  result->mState = state;

  return result.forget();
}

inline already_AddRefed<TexturedEffect>
CreateTexturedEffect(TextureHost* aHost,
                     TextureSource* aSource,
                     const gfx::SamplingFilter aSamplingFilter,
                     bool isAlphaPremultiplied,
                     const LayerRenderState &state = LayerRenderState())
{
  MOZ_ASSERT(aHost);
  MOZ_ASSERT(aSource);

  RefPtr<TexturedEffect> result;
  if (aHost->GetReadFormat() == gfx::SurfaceFormat::YUV) {
    MOZ_ASSERT(aHost->GetYUVColorSpace() != YUVColorSpace::UNKNOWN);
    result = new EffectYCbCr(aSource, aHost->GetYUVColorSpace(), aSamplingFilter);
  } else {
    result = CreateTexturedEffect(aHost->GetReadFormat(),
                                  aSource,
                                  aSamplingFilter,
                                  isAlphaPremultiplied,
                                  state);
  }
  return result.forget();
}

/**
 * Create a textured effect based on aSource format and the presence of
 * aSourceOnWhite.
 *
 * aSourceOnWhite can be null.
 */
inline already_AddRefed<TexturedEffect>
CreateTexturedEffect(TextureSource* aSource,
                     TextureSource* aSourceOnWhite,
                     const gfx::SamplingFilter aSamplingFilter,
                     bool isAlphaPremultiplied,
                     const LayerRenderState &state = LayerRenderState())
{
  MOZ_ASSERT(aSource);
  if (aSourceOnWhite) {
    MOZ_ASSERT(aSource->GetFormat() == gfx::SurfaceFormat::R8G8B8X8 ||
               aSource->GetFormat() == gfx::SurfaceFormat::B8G8R8X8);
    MOZ_ASSERT(aSource->GetFormat() == aSourceOnWhite->GetFormat());
    return MakeAndAddRef<EffectComponentAlpha>(aSource, aSourceOnWhite,
                                               aSamplingFilter);
  }

  return CreateTexturedEffect(aSource->GetFormat(),
                              aSource,
                              aSamplingFilter,
                              isAlphaPremultiplied,
                              state);
}

/**
 * Create a textured effect based on aSource format.
 *
 * This version excudes the possibility of component alpha.
 */
inline already_AddRefed<TexturedEffect>
CreateTexturedEffect(TextureSource *aTexture,
                     const gfx::SamplingFilter aSamplingFilter,
                     const LayerRenderState &state = LayerRenderState())
{
  return CreateTexturedEffect(aTexture, nullptr, aSamplingFilter, true, state);
}


} // namespace layers
} // namespace mozilla

#endif