blob: 4d9202fe3e309595cdbc6124c8f336ed33793644 [file] [log] [blame]
tomhudson@google.com168e6342012-04-18 17:49:20 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.com8ea78d82012-10-24 20:11:30 +00008#ifndef GrEffect_DEFINED
9#define GrEffect_DEFINED
tomhudson@google.com168e6342012-04-18 17:49:20 +000010
bsalomon@google.com371e1052013-01-11 21:08:55 +000011#include "GrColor.h"
bsalomon@google.com6f261be2012-10-24 19:07:10 +000012#include "GrEffectUnitTest.h"
bsalomon@google.com371e1052013-01-11 21:08:55 +000013#include "GrNoncopyable.h"
14#include "GrRefCnt.h"
bsalomon@google.com34cccde2013-01-04 18:34:30 +000015#include "GrTexture.h"
bsalomon@google.com047696c2012-09-11 13:29:29 +000016#include "GrTextureAccess.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000017
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000018class GrBackendEffectFactory;
tomhudson@google.com168e6342012-04-18 17:49:20 +000019class GrContext;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000020class GrEffect;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000021class SkString;
22
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000023/**
24 * A Wrapper class for GrEffect. Its ref-count will track owners that may use effects to enqueue
25 * new draw operations separately from ownership within a deferred drawing queue. When the
26 * GrEffectRef ref count reaches zero the scratch GrResources owned by the effect can be recycled
27 * in service of later draws. However, the deferred draw queue may still own direct references to
28 * the underlying GrEffect.
29 */
30class GrEffectRef : public SkRefCnt {
31public:
32 SK_DECLARE_INST_COUNT(GrEffectRef);
33
34 GrEffect* get() { return fEffect; }
35 const GrEffect* get() const { return fEffect; }
36
bsalomon@google.com6340a412013-01-22 19:55:59 +000037 const GrEffect* operator-> () { return fEffect; }
38 const GrEffect* operator-> () const { return fEffect; }
39
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000040 void* operator new(size_t size);
41 void operator delete(void* target);
42
43private:
bsalomon@google.com64287c52013-01-16 15:25:55 +000044 friend class GrEffect; // to construct these
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000045
46 explicit GrEffectRef(GrEffect* effect);
47
48 virtual ~GrEffectRef();
49
50 GrEffect* fEffect;
51
52 typedef SkRefCnt INHERITED;
53};
54
bsalomon@google.com50db75c2013-01-11 13:54:30 +000055/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
56 Ganesh shading pipeline.
bsalomon@google.com289efe02012-05-21 20:57:59 +000057 Subclasses must have a function that produces a human-readable name:
58 static const char* Name();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000059 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000060
61 GrEffect subclass objects should be created by factory functions that return GrEffectRef.
62 There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static
63 member function of a GrEffect subclass.
bsalomon@google.com6340a412013-01-22 19:55:59 +000064
65 Because almost no code should ever handle a GrEffect outside of a GrEffectRef, we privately
66 inherit from GrRefCnt to help prevent accidental direct ref'ing/unref'ing of effects.
bsalomon@google.com289efe02012-05-21 20:57:59 +000067 */
bsalomon@google.com6340a412013-01-22 19:55:59 +000068class GrEffect : private GrRefCnt {
tomhudson@google.com168e6342012-04-18 17:49:20 +000069public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000070 SK_DECLARE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000071
bsalomon@google.coma469c282012-10-24 18:28:34 +000072 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000073
bsalomon@google.com371e1052013-01-11 21:08:55 +000074 /**
75 * Flags for getConstantColorComponents. They are defined so that the bit order reflects the
76 * GrColor shift order.
77 */
78 enum ValidComponentFlags {
79 kR_ValidComponentFlag = 1 << (GrColor_SHIFT_R / 8),
80 kG_ValidComponentFlag = 1 << (GrColor_SHIFT_G / 8),
81 kB_ValidComponentFlag = 1 << (GrColor_SHIFT_B / 8),
82 kA_ValidComponentFlag = 1 << (GrColor_SHIFT_A / 8),
83
84 kAll_ValidComponentFlags = (kR_ValidComponentFlag | kG_ValidComponentFlag |
85 kB_ValidComponentFlag | kA_ValidComponentFlag)
86 };
87
88 /**
89 * This function is used to perform optimizations. When called the color and validFlags params
90 * indicate whether the input components to this effect in the FS will have known values. The
91 * function updates both params to indicate known values of its output. A component of the color
92 * param only has meaning if the corresponding bit in validFlags is set.
93 */
94 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000095
bsalomon@google.com422e81a2012-10-25 14:11:03 +000096 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
97 identification. The factory should be an instance of templated class,
bsalomon@google.com396e61f2012-10-25 19:00:29 +000098 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +000099 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
100 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000101
102 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000103 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000104 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000105 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
106 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000107 }
108 ...
109 };
110 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000111 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +0000112
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000113 /** Returns true if this and other effect conservatively draw identically. It can only return
114 true when the two effects are of the same subclass (i.e. they return the same object from
115 from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000116
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000117 A return value of true from isEqual() should not be used to test whether the effects would
118 generate the same shader code. To test for identical code generation use the EffectKey
119 computed by the GrBackendEffectFactory:
120 effectA.getFactory().glEffectKey(effectA) == effectB.getFactory().glEffectKey(effectB).
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000121 */
bsalomon@google.com6340a412013-01-22 19:55:59 +0000122 bool isEqual(const GrEffectRef& other) const {
123 if (&this->getFactory() != &other->getFactory()) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000124 return false;
125 }
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000126 bool result = this->onIsEqual(*other.get());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000127#if GR_DEBUG
128 if (result) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000129 GrAssert(this->numTextures() == other->numTextures());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000130 for (int i = 0; i < this->numTextures(); ++i) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000131 GrAssert(*fTextureAccesses[i] == *other->fTextureAccesses[i]);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000132 }
133 }
134#endif
135 return result;
136 }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000137
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000138 /** Human-meaningful string to identify this effect; may be embedded
139 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000140 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +0000141
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000142 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000143
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000144 /** Returns the access pattern for the texture at index. index must be valid according to
145 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000146 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000147
148 /** Shortcut for textureAccess(index).texture(); */
149 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000150
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000151 /** Useful for effects that want to insert a texture matrix that is implied by the texture
152 dimensions */
153 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
154 GrAssert(NULL != texture);
155 SkMatrix mat;
156 mat.setIDiv(texture->width(), texture->height());
157 return mat;
158 }
159
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000160 void* operator new(size_t size);
161 void operator delete(void* target);
162
bsalomon@google.com6340a412013-01-22 19:55:59 +0000163 /** These use non-standard names because GrEffects should only be ref'ed an unref'ed deep in
164 the bowels. Rendering code should use GrEffectRef. */
165 void addRef() { this->ref(); }
166 void subRef() { this->unref(); }
167
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000168protected:
169 /**
170 * Subclasses call this from their constructor to register GrTextureAcceses. The effect subclass
171 * manages the lifetime of the accesses (this function only stores a pointer). This must only be
172 * called from the constructor because GrEffects are supposed to be immutable.
173 */
174 void addTextureAccess(const GrTextureAccess* textureAccess);
175
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000176 GrEffect() : fEffectRef(NULL) {};
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000177
bsalomon@google.com6340a412013-01-22 19:55:59 +0000178 /** This should be called by GrEffect subclass factories. See the comment on AutoEffectUnref for
179 an example factory function. */
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000180 static GrEffectRef* CreateEffectRef(GrEffect* effect) {
181 if (NULL == effect->fEffectRef) {
182 effect->fEffectRef = SkNEW_ARGS(GrEffectRef, (effect));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000183 } else {
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000184 effect->fEffectRef->ref();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000185 GrCrash("This function should only be called once per effect currently.");
186 }
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000187 return effect->fEffectRef;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000188 }
189
bsalomon@google.com6340a412013-01-22 19:55:59 +0000190 /** Helper used in subclass factory functions to unref the effect after it has been wrapped in a
191 GrEffectRef. E.g.:
192
193 class EffectSubclass : public GrEffect {
194 public:
195 GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) {
196 AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param2, ...)));
197 return CreateEffectRef(effect);
198 }
199 */
200 class AutoEffectUnref {
201 public:
202 AutoEffectUnref(GrEffect* effect) : fEffect(effect) { }
203 ~AutoEffectUnref() { fEffect->subRef(); }
204 operator GrEffect*() { return fEffect; }
205 private:
206 GrEffect* fEffect;
207 };
208
209 /** Helper for getting the GrEffect out of a GrEffectRef and down-casting to a GrEffect subclass
210 */
211 template <typename T>
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000212 static const T& CastEffect(const GrEffect& effectRef) {
213 return *static_cast<const T*>(&effectRef);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000214 }
215
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000216private:
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000217
218 /** Subclass implements this to support isEqual(). It will only be called if it is known that
bsalomon@google.com6340a412013-01-22 19:55:59 +0000219 the two effects are of the same subclass (i.e. they return the same object from
220 getFactory()).*/
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000221 virtual bool onIsEqual(const GrEffect& other) const = 0;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000222
bsalomon@google.com6340a412013-01-22 19:55:59 +0000223 void EffectRefDestroyed() { fEffectRef = NULL; }
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000224
bsalomon@google.com64287c52013-01-16 15:25:55 +0000225 friend class GrEffectRef; // to call GrEffectRef destroyed
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000226
227 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000228 GrEffectRef* fEffectRef;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000229
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000230 typedef GrRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000231};
232
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000233inline GrEffectRef::GrEffectRef(GrEffect* effect) {
234 GrAssert(NULL != effect);
235 effect->ref();
236 fEffect = effect;
237}
238
tomhudson@google.com168e6342012-04-18 17:49:20 +0000239#endif