1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/layers/Effects.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,243 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 +* This Source Code Form is subject to the terms of the Mozilla Public 1.6 +* License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef MOZILLA_LAYERS_EFFECTS_H 1.10 +#define MOZILLA_LAYERS_EFFECTS_H 1.11 + 1.12 +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc 1.13 +#include "mozilla/RefPtr.h" // for RefPtr, TemporaryRef, etc 1.14 +#include "mozilla/gfx/Matrix.h" // for Matrix4x4 1.15 +#include "mozilla/gfx/Point.h" // for IntSize 1.16 +#include "mozilla/gfx/Rect.h" // for Rect 1.17 +#include "mozilla/gfx/Types.h" // for Filter, etc 1.18 +#include "mozilla/layers/CompositorTypes.h" // for EffectTypes, etc 1.19 +#include "mozilla/layers/LayersTypes.h" 1.20 +#include "mozilla/layers/TextureHost.h" // for CompositingRenderTarget, etc 1.21 +#include "mozilla/mozalloc.h" // for operator delete, etc 1.22 +#include "nscore.h" // for nsACString 1.23 + 1.24 +namespace mozilla { 1.25 +namespace layers { 1.26 + 1.27 +/** 1.28 + * Effects and effect chains are used by the compositor API (see Compositor.h). 1.29 + * An effect chain represents a rendering method, for example some shader and 1.30 + * the data required for that shader to run. An effect is some component of the 1.31 + * chain and its data. 1.32 + * 1.33 + * An effect chain consists of a primary effect - how the 'texture' memory should 1.34 + * be interpreted (RGBA, BGRX, YCBCR, etc.) - and any number of secondary effects 1.35 + * - any way in which rendering can be changed, e.g., applying a mask layer. 1.36 + * 1.37 + * During the rendering process, an effect chain is created by the layer being 1.38 + * rendered and the primary effect is added by the compositable host. Secondary 1.39 + * effects may be added by the layer or compositable. The effect chain is passed 1.40 + * to the compositor by the compositable host as a parameter to DrawQuad. 1.41 + */ 1.42 + 1.43 +struct Effect 1.44 +{ 1.45 + NS_INLINE_DECL_REFCOUNTING(Effect) 1.46 + 1.47 + Effect(EffectTypes aType) : mType(aType) {} 1.48 + 1.49 + EffectTypes mType; 1.50 + 1.51 + virtual void PrintInfo(nsACString& aTo, const char* aPrefix) = 0; 1.52 + 1.53 +protected: 1.54 + virtual ~Effect() {} 1.55 +}; 1.56 + 1.57 +// Render from a texture 1.58 +struct TexturedEffect : public Effect 1.59 +{ 1.60 + TexturedEffect(EffectTypes aType, 1.61 + TextureSource *aTexture, 1.62 + bool aPremultiplied, 1.63 + gfx::Filter aFilter) 1.64 + : Effect(aType) 1.65 + , mTextureCoords(0, 0, 1.0f, 1.0f) 1.66 + , mTexture(aTexture) 1.67 + , mPremultiplied(aPremultiplied) 1.68 + , mFilter(aFilter) 1.69 + {} 1.70 + 1.71 + virtual const char* Name() = 0; 1.72 + virtual void PrintInfo(nsACString& aTo, const char* aPrefix); 1.73 + 1.74 + gfx::Rect mTextureCoords; 1.75 + TextureSource* mTexture; 1.76 + bool mPremultiplied; 1.77 + gfx::Filter mFilter;; 1.78 +}; 1.79 + 1.80 +// Support an alpha mask. 1.81 +struct EffectMask : public Effect 1.82 +{ 1.83 + EffectMask(TextureSource *aMaskTexture, 1.84 + gfx::IntSize aSize, 1.85 + const gfx::Matrix4x4 &aMaskTransform) 1.86 + : Effect(EFFECT_MASK) 1.87 + , mMaskTexture(aMaskTexture) 1.88 + , mIs3D(false) 1.89 + , mSize(aSize) 1.90 + , mMaskTransform(aMaskTransform) 1.91 + {} 1.92 + 1.93 + virtual void PrintInfo(nsACString& aTo, const char* aPrefix); 1.94 + 1.95 + TextureSource* mMaskTexture; 1.96 + bool mIs3D; 1.97 + gfx::IntSize mSize; 1.98 + gfx::Matrix4x4 mMaskTransform; 1.99 +}; 1.100 + 1.101 +// Render to a render target rather than the screen. 1.102 +struct EffectRenderTarget : public TexturedEffect 1.103 +{ 1.104 + EffectRenderTarget(CompositingRenderTarget *aRenderTarget) 1.105 + : TexturedEffect(EFFECT_RENDER_TARGET, aRenderTarget, true, gfx::Filter::LINEAR) 1.106 + , mRenderTarget(aRenderTarget) 1.107 + {} 1.108 + 1.109 + virtual const char* Name() { return "EffectRenderTarget"; } 1.110 + virtual void PrintInfo(nsACString& aTo, const char* aPrefix); 1.111 + 1.112 + RefPtr<CompositingRenderTarget> mRenderTarget; 1.113 +}; 1.114 + 1.115 +struct EffectRGB : public TexturedEffect 1.116 +{ 1.117 + EffectRGB(TextureSource *aTexture, 1.118 + bool aPremultiplied, 1.119 + gfx::Filter aFilter, 1.120 + bool aFlipped = false) 1.121 + : TexturedEffect(EFFECT_RGB, aTexture, aPremultiplied, aFilter) 1.122 + {} 1.123 + 1.124 + virtual const char* Name() { return "EffectRGB"; } 1.125 +}; 1.126 + 1.127 +struct EffectYCbCr : public TexturedEffect 1.128 +{ 1.129 + EffectYCbCr(TextureSource *aSource, gfx::Filter aFilter) 1.130 + : TexturedEffect(EFFECT_YCBCR, aSource, false, aFilter) 1.131 + {} 1.132 + 1.133 + virtual const char* Name() { return "EffectYCbCr"; } 1.134 +}; 1.135 + 1.136 +struct EffectComponentAlpha : public TexturedEffect 1.137 +{ 1.138 + EffectComponentAlpha(TextureSource *aOnBlack, 1.139 + TextureSource *aOnWhite, 1.140 + gfx::Filter aFilter) 1.141 + : TexturedEffect(EFFECT_COMPONENT_ALPHA, nullptr, false, aFilter) 1.142 + , mOnBlack(aOnBlack) 1.143 + , mOnWhite(aOnWhite) 1.144 + {} 1.145 + 1.146 + virtual const char* Name() { return "EffectComponentAlpha"; } 1.147 + 1.148 + TextureSource* mOnBlack; 1.149 + TextureSource* mOnWhite; 1.150 +}; 1.151 + 1.152 +struct EffectSolidColor : public Effect 1.153 +{ 1.154 + EffectSolidColor(const gfx::Color &aColor) 1.155 + : Effect(EFFECT_SOLID_COLOR) 1.156 + , mColor(aColor) 1.157 + {} 1.158 + 1.159 + virtual void PrintInfo(nsACString& aTo, const char* aPrefix); 1.160 + 1.161 + gfx::Color mColor; 1.162 +}; 1.163 + 1.164 +struct EffectChain 1.165 +{ 1.166 + EffectChain() : mLayerRef(nullptr) {} 1.167 + explicit EffectChain(void* aLayerRef) : mLayerRef(aLayerRef) {} 1.168 + 1.169 + RefPtr<Effect> mPrimaryEffect; 1.170 + RefPtr<Effect> mSecondaryEffects[EFFECT_MAX_SECONDARY]; 1.171 + void* mLayerRef; //!< For LayerScope logging 1.172 +}; 1.173 + 1.174 +/** 1.175 + * Create a Textured effect corresponding to aFormat and using 1.176 + * aSource as the (first) texture source. 1.177 + * 1.178 + * Note that aFormat can be different form aSource->GetFormat if, we are 1.179 + * creating an effect that takes several texture sources (like with YCBCR 1.180 + * where aFormat would be FOMRAT_YCBCR and each texture source would be 1.181 + * a one-channel A8 texture) 1.182 + */ 1.183 +inline TemporaryRef<TexturedEffect> 1.184 +CreateTexturedEffect(gfx::SurfaceFormat aFormat, 1.185 + TextureSource* aSource, 1.186 + const gfx::Filter& aFilter) 1.187 +{ 1.188 + MOZ_ASSERT(aSource); 1.189 + RefPtr<TexturedEffect> result; 1.190 + switch (aFormat) { 1.191 + case gfx::SurfaceFormat::B8G8R8A8: 1.192 + case gfx::SurfaceFormat::B8G8R8X8: 1.193 + case gfx::SurfaceFormat::R8G8B8X8: 1.194 + case gfx::SurfaceFormat::R5G6B5: 1.195 + case gfx::SurfaceFormat::R8G8B8A8: 1.196 + result = new EffectRGB(aSource, true, aFilter); 1.197 + break; 1.198 + case gfx::SurfaceFormat::YUV: 1.199 + result = new EffectYCbCr(aSource, aFilter); 1.200 + break; 1.201 + default: 1.202 + NS_WARNING("unhandled program type"); 1.203 + break; 1.204 + } 1.205 + 1.206 + return result; 1.207 +} 1.208 + 1.209 +/** 1.210 + * Create a textured effect based on aSource format and the presence of 1.211 + * aSourceOnWhite. 1.212 + * 1.213 + * aSourceOnWhite can be null. 1.214 + */ 1.215 +inline TemporaryRef<TexturedEffect> 1.216 +CreateTexturedEffect(TextureSource* aSource, 1.217 + TextureSource* aSourceOnWhite, 1.218 + const gfx::Filter& aFilter) 1.219 +{ 1.220 + MOZ_ASSERT(aSource); 1.221 + if (aSourceOnWhite) { 1.222 + MOZ_ASSERT(aSource->GetFormat() == gfx::SurfaceFormat::R8G8B8X8 || 1.223 + aSourceOnWhite->GetFormat() == gfx::SurfaceFormat::B8G8R8X8); 1.224 + return new EffectComponentAlpha(aSource, aSourceOnWhite, aFilter); 1.225 + } 1.226 + 1.227 + return CreateTexturedEffect(aSource->GetFormat(), aSource, aFilter); 1.228 +} 1.229 + 1.230 +/** 1.231 + * Create a textured effect based on aSource format. 1.232 + * 1.233 + * This version excudes the possibility of component alpha. 1.234 + */ 1.235 +inline TemporaryRef<TexturedEffect> 1.236 +CreateTexturedEffect(TextureSource *aTexture, 1.237 + const gfx::Filter& aFilter) 1.238 +{ 1.239 + return CreateTexturedEffect(aTexture, nullptr, aFilter); 1.240 +} 1.241 + 1.242 + 1.243 +} // namespace layers 1.244 +} // namespace mozilla 1.245 + 1.246 +#endif