2012-05-18 19:54:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2012-10-25 19:00:29 +00:00
|
|
|
#ifndef GrBackendEffectFactory_DEFINED
|
|
|
|
#define GrBackendEffectFactory_DEFINED
|
2012-05-18 19:54:48 +00:00
|
|
|
|
|
|
|
#include "GrTypes.h"
|
2012-05-18 20:06:45 +00:00
|
|
|
#include "SkTemplates.h"
|
2014-05-28 20:02:17 +00:00
|
|
|
#include "SkThread.h"
|
2013-09-18 13:00:55 +00:00
|
|
|
#include "SkTypes.h"
|
2012-05-18 19:54:48 +00:00
|
|
|
|
2012-10-29 19:51:22 +00:00
|
|
|
/** Given a GrEffect of a particular type, creates the corresponding graphics-backend-specific
|
|
|
|
effect object. Also tracks equivalence of shaders generated via a key. Each factory instance
|
|
|
|
is assigned a generation ID at construction. The ID of the return of GrEffect::getFactory()
|
|
|
|
is used as a type identifier. Thus a GrEffect subclass must return a singleton from
|
|
|
|
getFactory(). GrEffect subclasses should use the derived class GrTBackendEffectFactory that is
|
|
|
|
templated on the GrEffect subclass as their factory object. It requires that the GrEffect
|
|
|
|
subclass has a nested class (or typedef) GLEffect which is its GL implementation and a subclass
|
|
|
|
of GrGLEffect.
|
2012-05-18 19:54:48 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-22 19:55:59 +00:00
|
|
|
class GrEffectRef;
|
2012-10-25 13:22:00 +00:00
|
|
|
class GrGLEffect;
|
2012-08-02 15:15:16 +00:00
|
|
|
class GrGLCaps;
|
2013-03-20 19:19:53 +00:00
|
|
|
class GrDrawEffect;
|
2012-05-18 19:54:48 +00:00
|
|
|
|
2014-04-07 19:34:38 +00:00
|
|
|
class GrBackendEffectFactory : SkNoncopyable {
|
2012-05-18 19:54:48 +00:00
|
|
|
public:
|
2012-10-25 21:42:05 +00:00
|
|
|
typedef uint32_t EffectKey;
|
2012-05-18 19:54:48 +00:00
|
|
|
enum {
|
2012-11-05 16:36:02 +00:00
|
|
|
kNoEffectKey = 0,
|
2013-10-02 13:04:56 +00:00
|
|
|
kEffectKeyBits = 10,
|
2012-10-25 21:42:05 +00:00
|
|
|
/**
|
2013-10-02 13:04:56 +00:00
|
|
|
* The framework automatically includes coord transforms and texture accesses in their
|
|
|
|
* effect's EffectKey, so effects don't need to account for them in GenKey().
|
2012-10-25 21:42:05 +00:00
|
|
|
*/
|
2013-09-30 19:57:15 +00:00
|
|
|
kTextureKeyBits = 4,
|
2013-10-02 13:04:56 +00:00
|
|
|
kTransformKeyBits = 6,
|
|
|
|
kAttribKeyBits = 6,
|
|
|
|
kClassIDBits = 6
|
2012-05-18 19:54:48 +00:00
|
|
|
};
|
|
|
|
|
2013-03-20 19:19:53 +00:00
|
|
|
virtual EffectKey glEffectKey(const GrDrawEffect&, const GrGLCaps&) const = 0;
|
|
|
|
virtual GrGLEffect* createGLInstance(const GrDrawEffect&) const = 0;
|
2012-05-18 19:54:48 +00:00
|
|
|
|
2012-10-25 19:00:29 +00:00
|
|
|
bool operator ==(const GrBackendEffectFactory& b) const {
|
2012-10-25 12:47:42 +00:00
|
|
|
return fEffectClassID == b.fEffectClassID;
|
2012-05-18 19:54:48 +00:00
|
|
|
}
|
2012-10-25 19:00:29 +00:00
|
|
|
bool operator !=(const GrBackendEffectFactory& b) const {
|
2012-05-18 19:54:48 +00:00
|
|
|
return !(*this == b);
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:57:59 +00:00
|
|
|
virtual const char* name() const = 0;
|
|
|
|
|
2013-10-02 13:04:56 +00:00
|
|
|
static EffectKey GetTransformKey(EffectKey key) {
|
|
|
|
return key >> (kEffectKeyBits + kTextureKeyBits) & ((1U << kTransformKeyBits) - 1);
|
|
|
|
}
|
|
|
|
|
2012-05-18 19:54:48 +00:00
|
|
|
protected:
|
|
|
|
enum {
|
2012-10-25 12:47:42 +00:00
|
|
|
kIllegalEffectClassID = 0,
|
2012-05-18 19:54:48 +00:00
|
|
|
};
|
|
|
|
|
2012-10-25 19:00:29 +00:00
|
|
|
GrBackendEffectFactory() {
|
2012-10-25 12:47:42 +00:00
|
|
|
fEffectClassID = kIllegalEffectClassID;
|
2012-05-18 19:54:48 +00:00
|
|
|
}
|
2013-01-04 15:29:06 +00:00
|
|
|
virtual ~GrBackendEffectFactory() {}
|
2012-05-18 19:54:48 +00:00
|
|
|
|
2012-10-25 21:42:05 +00:00
|
|
|
static EffectKey GenID() {
|
2013-09-06 23:13:05 +00:00
|
|
|
SkDEBUGCODE(static const int32_t kClassIDBits = 8 * sizeof(EffectKey) -
|
2013-03-26 19:35:06 +00:00
|
|
|
kTextureKeyBits - kEffectKeyBits - kAttribKeyBits);
|
2012-10-25 12:47:42 +00:00
|
|
|
// fCurrEffectClassID has been initialized to kIllegalEffectClassID. The
|
2012-05-18 19:54:48 +00:00
|
|
|
// atomic inc returns the old value not the incremented value. So we add
|
|
|
|
// 1 to the returned value.
|
2012-10-25 12:47:42 +00:00
|
|
|
int32_t id = sk_atomic_inc(&fCurrEffectClassID) + 1;
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(id < (1 << kClassIDBits));
|
2012-11-02 20:45:37 +00:00
|
|
|
return static_cast<EffectKey>(id);
|
2012-05-18 19:54:48 +00:00
|
|
|
}
|
|
|
|
|
2012-10-25 21:42:05 +00:00
|
|
|
EffectKey fEffectClassID;
|
2012-05-18 19:54:48 +00:00
|
|
|
|
|
|
|
private:
|
2012-10-25 12:47:42 +00:00
|
|
|
static int32_t fCurrEffectClassID;
|
2012-05-18 19:54:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|