blob: de4b2a14fbc091f2f358b3a9897c1a035923c5f9 [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"
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000017#include "GrTypesPriv.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000018
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000019class GrBackendEffectFactory;
tomhudson@google.com168e6342012-04-18 17:49:20 +000020class GrContext;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000021class GrEffect;
twiz@google.coma5e65ec2012-08-02 15:15:16 +000022class SkString;
23
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000024/**
25 * A Wrapper class for GrEffect. Its ref-count will track owners that may use effects to enqueue
26 * new draw operations separately from ownership within a deferred drawing queue. When the
27 * GrEffectRef ref count reaches zero the scratch GrResources owned by the effect can be recycled
28 * in service of later draws. However, the deferred draw queue may still own direct references to
29 * the underlying GrEffect.
30 */
31class GrEffectRef : public SkRefCnt {
32public:
33 SK_DECLARE_INST_COUNT(GrEffectRef);
34
35 GrEffect* get() { return fEffect; }
36 const GrEffect* get() const { return fEffect; }
37
bsalomon@google.com6340a412013-01-22 19:55:59 +000038 const GrEffect* operator-> () { return fEffect; }
39 const GrEffect* operator-> () const { return fEffect; }
40
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000041 void* operator new(size_t size);
42 void operator delete(void* target);
43
44private:
bsalomon@google.com64287c52013-01-16 15:25:55 +000045 friend class GrEffect; // to construct these
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000046
47 explicit GrEffectRef(GrEffect* effect);
48
49 virtual ~GrEffectRef();
50
51 GrEffect* fEffect;
52
53 typedef SkRefCnt INHERITED;
54};
55
bsalomon@google.com50db75c2013-01-11 13:54:30 +000056/** Provides custom vertex shader, fragment shader, uniform data for a particular stage of the
57 Ganesh shading pipeline.
bsalomon@google.com289efe02012-05-21 20:57:59 +000058 Subclasses must have a function that produces a human-readable name:
59 static const char* Name();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000060 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000061
62 GrEffect subclass objects should be created by factory functions that return GrEffectRef.
63 There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static
64 member function of a GrEffect subclass.
bsalomon@google.com6340a412013-01-22 19:55:59 +000065
66 Because almost no code should ever handle a GrEffect outside of a GrEffectRef, we privately
67 inherit from GrRefCnt to help prevent accidental direct ref'ing/unref'ing of effects.
bsalomon@google.com289efe02012-05-21 20:57:59 +000068 */
bsalomon@google.com6340a412013-01-22 19:55:59 +000069class GrEffect : private GrRefCnt {
tomhudson@google.com168e6342012-04-18 17:49:20 +000070public:
bsalomon@google.coma469c282012-10-24 18:28:34 +000071 SK_DECLARE_INST_COUNT(GrEffect)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000072
bsalomon@google.comc7818882013-03-20 19:19:53 +000073 /**
74 * The types of vertex coordinates available to an effect in the vertex shader. Effects can
75 * require their own vertex attribute but these coordinates are made available by the framework
76 * in all programs. kCustom_CoordsType is provided to signify that an alternative set of coords
77 * is used (usually an explicit vertex attribute) but its meaning is determined by the effect
78 * subclass.
79 */
80 enum CoordsType {
81 kLocal_CoordsType,
82 kPosition_CoordsType,
83
84 kCustom_CoordsType,
85 };
86
bsalomon@google.coma469c282012-10-24 18:28:34 +000087 virtual ~GrEffect();
tomhudson@google.com168e6342012-04-18 17:49:20 +000088
bsalomon@google.com371e1052013-01-11 21:08:55 +000089 /**
bsalomon@google.com371e1052013-01-11 21:08:55 +000090 * This function is used to perform optimizations. When called the color and validFlags params
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000091 * indicate whether the input components to this effect in the FS will have known values.
92 * validFlags is a bitfield of GrColorComponentFlags. The function updates both params to
93 * indicate known values of its output. A component of the color param only has meaning if the
94 * corresponding bit in validFlags is set.
bsalomon@google.com371e1052013-01-11 21:08:55 +000095 */
96 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const = 0;
tomhudson@google.com168e6342012-04-18 17:49:20 +000097
bsalomon@google.com422e81a2012-10-25 14:11:03 +000098 /** This object, besides creating back-end-specific helper objects, is used for run-time-type-
99 identification. The factory should be an instance of templated class,
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000100 GrTBackendEffectFactory. It is templated on the subclass of GrEffect. The subclass must have
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000101 a nested type (or typedef) named GLEffect which will be the subclass of GrGLEffect created
102 by the factory.
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000103
104 Example:
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000105 class MyCustomEffect : public GrEffect {
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000106 ...
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000107 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
108 return GrTBackendEffectFactory<MyCustomEffect>::getInstance();
bsalomon@google.comae4f96a2012-05-18 19:54:48 +0000109 }
110 ...
111 };
112 */
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000113 virtual const GrBackendEffectFactory& getFactory() const = 0;
tomhudson@google.comb88bbd22012-05-01 12:48:07 +0000114
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000115 /** Returns true if this and other effect conservatively draw identically. It can only return
116 true when the two effects are of the same subclass (i.e. they return the same object from
117 from getFactory()).
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000118
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000119 A return value of true from isEqual() should not be used to test whether the effects would
120 generate the same shader code. To test for identical code generation use the EffectKey
121 computed by the GrBackendEffectFactory:
122 effectA.getFactory().glEffectKey(effectA) == effectB.getFactory().glEffectKey(effectB).
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000123 */
bsalomon@google.com6340a412013-01-22 19:55:59 +0000124 bool isEqual(const GrEffectRef& other) const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000125 return this->isEqual(*other.get());
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000126 }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000127
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000128 /** Human-meaningful string to identify this effect; may be embedded
129 in generated shader code. */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000130 const char* name() const;
bsalomon@google.com289efe02012-05-21 20:57:59 +0000131
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000132 int numTextures() const { return fTextureAccesses.count(); }
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000133
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000134 /** Returns the access pattern for the texture at index. index must be valid according to
135 numTextures(). */
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000136 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000137
138 /** Shortcut for textureAccess(index).texture(); */
139 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000140
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000141
142 int numVertexAttribs() const { return fVertexAttribTypes.count(); }
143
144 GrSLType vertexAttribType(int index) const { return fVertexAttribTypes[index]; }
145
146 static const int kMaxVertexAttribs = 2;
147
148
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000149 /** Useful for effects that want to insert a texture matrix that is implied by the texture
150 dimensions */
151 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
152 GrAssert(NULL != texture);
153 SkMatrix mat;
154 mat.setIDiv(texture->width(), texture->height());
155 return mat;
156 }
157
tomhudson@google.comdcba4c22012-07-24 21:36:16 +0000158 void* operator new(size_t size);
159 void operator delete(void* target);
160
bsalomon@google.com838f6e12013-01-23 21:37:01 +0000161 /** These functions are used when recording effects into a deferred drawing queue. The inc call
162 keeps the effect alive outside of GrEffectRef while allowing any resources owned by the
163 effect to be returned to the cache for reuse. The dec call must balance the inc call. */
164 void incDeferredRefCounts() const {
165 this->ref();
166 int count = fTextureAccesses.count();
167 for (int t = 0; t < count; ++t) {
168 fTextureAccesses[t]->getTexture()->incDeferredRefCount();
169 }
170 }
171 void decDeferredRefCounts() const {
172 int count = fTextureAccesses.count();
173 for (int t = 0; t < count; ++t) {
174 fTextureAccesses[t]->getTexture()->decDeferredRefCount();
175 }
176 this->unref();
177 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000178
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000179protected:
180 /**
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000181 * Subclasses call this from their constructor to register GrTextureAccesses. The effect
182 * subclass manages the lifetime of the accesses (this function only stores a pointer). This
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000183 * must only be called from the constructor because GrEffects are immutable.
bsalomon@google.com50db75c2013-01-11 13:54:30 +0000184 */
185 void addTextureAccess(const GrTextureAccess* textureAccess);
186
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000187 /**
skia.committer@gmail.com91274b92013-03-13 07:01:04 +0000188 * Subclasses call this from their constructor to register vertex attributes (at most
189 * kMaxVertexAttribs). This must only be called from the constructor because GrEffects are
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000190 * immutable.
191 */
192 void addVertexAttrib(GrSLType type);
193
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000194 GrEffect() : fEffectRef(NULL) {};
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000195
bsalomon@google.com6340a412013-01-22 19:55:59 +0000196 /** This should be called by GrEffect subclass factories. See the comment on AutoEffectUnref for
197 an example factory function. */
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000198 static GrEffectRef* CreateEffectRef(GrEffect* effect) {
199 if (NULL == effect->fEffectRef) {
200 effect->fEffectRef = SkNEW_ARGS(GrEffectRef, (effect));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000201 } else {
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000202 effect->fEffectRef->ref();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000203 }
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000204 return effect->fEffectRef;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000205 }
206
bsalomon@google.comca432082013-01-23 19:53:46 +0000207 static const GrEffectRef* CreateEffectRef(const GrEffect* effect) {
208 return CreateEffectRef(const_cast<GrEffect*>(effect));
209 }
210
bsalomon@google.com6340a412013-01-22 19:55:59 +0000211 /** Helper used in subclass factory functions to unref the effect after it has been wrapped in a
212 GrEffectRef. E.g.:
213
214 class EffectSubclass : public GrEffect {
215 public:
216 GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) {
217 AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param2, ...)));
218 return CreateEffectRef(effect);
219 }
220 */
221 class AutoEffectUnref {
222 public:
223 AutoEffectUnref(GrEffect* effect) : fEffect(effect) { }
bsalomon@google.com838f6e12013-01-23 21:37:01 +0000224 ~AutoEffectUnref() { fEffect->unref(); }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000225 operator GrEffect*() { return fEffect; }
226 private:
227 GrEffect* fEffect;
228 };
229
230 /** Helper for getting the GrEffect out of a GrEffectRef and down-casting to a GrEffect subclass
231 */
232 template <typename T>
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000233 static const T& CastEffect(const GrEffect& effectRef) {
234 return *static_cast<const T*>(&effectRef);
bsalomon@google.com6340a412013-01-22 19:55:59 +0000235 }
236
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000237private:
bsalomon@google.comca432082013-01-23 19:53:46 +0000238 bool isEqual(const GrEffect& other) const {
239 if (&this->getFactory() != &other.getFactory()) {
240 return false;
241 }
242 bool result = this->onIsEqual(other);
243#if GR_DEBUG
244 if (result) {
245 GrAssert(this->numTextures() == other.numTextures());
246 for (int i = 0; i < this->numTextures(); ++i) {
247 GrAssert(*fTextureAccesses[i] == *other.fTextureAccesses[i]);
248 }
249 }
250#endif
251 return result;
252 }
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000253
254 /** Subclass implements this to support isEqual(). It will only be called if it is known that
bsalomon@google.com6340a412013-01-22 19:55:59 +0000255 the two effects are of the same subclass (i.e. they return the same object from
256 getFactory()).*/
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000257 virtual bool onIsEqual(const GrEffect& other) const = 0;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000258
bsalomon@google.com6340a412013-01-22 19:55:59 +0000259 void EffectRefDestroyed() { fEffectRef = NULL; }
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000260
bsalomon@google.com838f6e12013-01-23 21:37:01 +0000261 friend class GrEffectRef; // to call EffectRefDestroyed()
bsalomon@google.comca432082013-01-23 19:53:46 +0000262 friend class GrEffectStage; // to rewrap GrEffect in GrEffectRef when restoring an effect-stage
bsalomon@google.com838f6e12013-01-23 21:37:01 +0000263 // from deferred state, to call isEqual on naked GrEffects, and
264 // to inc/dec deferred ref counts.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000265
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000266 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
267 SkSTArray<kMaxVertexAttribs, GrSLType, true> fVertexAttribTypes;
268 GrEffectRef* fEffectRef;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000269
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000270 typedef GrRefCnt INHERITED;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000271};
272
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000273inline GrEffectRef::GrEffectRef(GrEffect* effect) {
274 GrAssert(NULL != effect);
275 effect->ref();
276 fEffect = effect;
277}
278
tomhudson@google.com168e6342012-04-18 17:49:20 +0000279#endif