2012-04-18 17:49:20 +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.
|
|
|
|
*/
|
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
#ifndef GrProcessor_DEFINED
|
|
|
|
#define GrProcessor_DEFINED
|
2012-04-18 17:49:20 +00:00
|
|
|
|
2013-01-11 21:08:55 +00:00
|
|
|
#include "GrColor.h"
|
2014-09-23 16:50:21 +00:00
|
|
|
#include "GrProcessorUnitTest.h"
|
2014-09-04 20:12:37 +00:00
|
|
|
#include "GrProgramElement.h"
|
2012-09-11 13:29:29 +00:00
|
|
|
#include "GrTextureAccess.h"
|
2014-10-15 20:49:02 +00:00
|
|
|
#include "SkMath.h"
|
2012-04-20 18:35:38 +00:00
|
|
|
|
2012-04-18 17:49:20 +00:00
|
|
|
class GrContext;
|
2013-10-02 13:04:56 +00:00
|
|
|
class GrCoordTransform;
|
2014-11-12 16:35:25 +00:00
|
|
|
class GrInvariantOutput;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
/**
|
|
|
|
* Used by processors to build their keys. It incorporates each per-processor key into a larger
|
|
|
|
* shader key.
|
|
|
|
*/
|
|
|
|
class GrProcessorKeyBuilder {
|
|
|
|
public:
|
|
|
|
GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
|
|
|
|
SkASSERT(0 == fData->count() % sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void add32(uint32_t v) {
|
|
|
|
++fCount;
|
|
|
|
fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
|
|
|
|
add*() call. */
|
|
|
|
uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
|
|
|
|
SkASSERT(count > 0);
|
|
|
|
fCount += count;
|
|
|
|
return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const { return sizeof(uint32_t) * fCount; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
|
|
|
|
int fCount; // number of uint32_ts added to fData by the processor.
|
|
|
|
};
|
|
|
|
|
2014-10-15 18:05:26 +00:00
|
|
|
/** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
|
|
|
|
immutable: after being constructed, their fields may not change.
|
2013-01-16 15:16:18 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
|
2014-10-15 18:05:26 +00:00
|
|
|
processor must reach 0 before the thread terminates and the pool is destroyed. To create a
|
|
|
|
static processor use the helper macro GR_CREATE_STATIC_PROCESSOR declared below.
|
|
|
|
*/
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrProcessor : public GrProgramElement {
|
2012-04-18 17:49:20 +00:00
|
|
|
public:
|
2014-09-23 16:50:21 +00:00
|
|
|
virtual ~GrProcessor();
|
2012-04-18 17:49:20 +00:00
|
|
|
|
2014-10-15 18:05:26 +00:00
|
|
|
/** Human-meaningful string to identify this prcoessor; may be embedded
|
2012-08-02 15:15:16 +00:00
|
|
|
in generated shader code. */
|
2014-12-04 19:35:33 +00:00
|
|
|
virtual const char* name() const = 0;
|
2012-05-21 20:57:59 +00:00
|
|
|
|
2013-01-11 13:54:30 +00:00
|
|
|
int numTextures() const { return fTextureAccesses.count(); }
|
2012-04-18 17:49:20 +00:00
|
|
|
|
2012-09-11 15:45:20 +00:00
|
|
|
/** Returns the access pattern for the texture at index. index must be valid according to
|
|
|
|
numTextures(). */
|
2013-01-11 13:54:30 +00:00
|
|
|
const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
|
2012-09-11 15:45:20 +00:00
|
|
|
|
|
|
|
/** Shortcut for textureAccess(index).texture(); */
|
|
|
|
GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
|
2012-08-02 15:15:16 +00:00
|
|
|
|
2014-10-15 18:05:26 +00:00
|
|
|
/** Will this processor read the fragment position? */
|
2013-05-09 14:55:46 +00:00
|
|
|
bool willReadFragmentPosition() const { return fWillReadFragmentPosition; }
|
2013-03-12 12:26:08 +00:00
|
|
|
|
2012-07-24 21:36:16 +00:00
|
|
|
void* operator new(size_t size);
|
|
|
|
void operator delete(void* target);
|
|
|
|
|
2013-04-23 15:37:27 +00:00
|
|
|
void* operator new(size_t size, void* placement) {
|
|
|
|
return ::operator new(size, placement);
|
|
|
|
}
|
|
|
|
void operator delete(void* target, void* placement) {
|
|
|
|
::operator delete(target, placement);
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:21:41 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* Helper for down-casting to a GrProcessor subclass
|
2014-09-16 15:21:41 +00:00
|
|
|
*/
|
|
|
|
template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
|
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
|
|
|
|
|
2013-01-11 13:54:30 +00:00
|
|
|
protected:
|
2014-12-04 19:35:33 +00:00
|
|
|
GrProcessor() : fClassID(kIllegalProcessorClassID), fWillReadFragmentPosition(false) {}
|
2014-10-16 16:18:09 +00:00
|
|
|
|
2013-01-11 13:54:30 +00:00
|
|
|
/**
|
2014-10-15 18:05:26 +00:00
|
|
|
* Subclasses call this from their constructor to register GrTextureAccesses. The processor
|
2013-09-06 15:31:06 +00:00
|
|
|
* subclass manages the lifetime of the accesses (this function only stores a pointer). The
|
2014-09-23 16:50:21 +00:00
|
|
|
* GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be
|
|
|
|
* called from the constructor because GrProcessors are immutable.
|
2013-01-11 13:54:30 +00:00
|
|
|
*/
|
|
|
|
void addTextureAccess(const GrTextureAccess* textureAccess);
|
|
|
|
|
2014-10-16 16:18:09 +00:00
|
|
|
bool hasSameTextureAccesses(const GrProcessor&) const;
|
2013-05-09 14:55:46 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-15 18:05:26 +00:00
|
|
|
* If the prcoessor will generate a backend-specific processor that will read the fragment
|
|
|
|
* position in the FS then it must call this method from its constructor. Otherwise, the
|
|
|
|
* request to access the fragment position will be denied.
|
2013-05-09 14:55:46 +00:00
|
|
|
*/
|
|
|
|
void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; }
|
2013-03-29 19:22:36 +00:00
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
template <typename PROC_SUBCLASS> void initClassID() {
|
|
|
|
static uint32_t kClassID = GenClassID();
|
|
|
|
fClassID = kClassID;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t fClassID;
|
|
|
|
|
2014-10-15 15:19:04 +00:00
|
|
|
private:
|
2014-12-04 19:35:33 +00:00
|
|
|
static uint32_t GenClassID() {
|
|
|
|
// fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
|
|
|
|
// atomic inc returns the old value not the incremented value. So we add
|
|
|
|
// 1 to the returned value.
|
|
|
|
uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
|
|
|
|
if (!id) {
|
|
|
|
SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
|
|
|
|
"subclass.");
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kIllegalProcessorClassID = 0,
|
|
|
|
};
|
|
|
|
static int32_t gCurrProcessorClassID;
|
2013-01-16 15:16:18 +00:00
|
|
|
|
2013-03-12 12:26:08 +00:00
|
|
|
SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
|
2013-05-09 14:55:46 +00:00
|
|
|
bool fWillReadFragmentPosition;
|
2013-01-16 15:16:18 +00:00
|
|
|
|
2014-09-04 20:12:37 +00:00
|
|
|
typedef GrProgramElement INHERITED;
|
2012-04-18 17:49:20 +00:00
|
|
|
};
|
|
|
|
|
2014-10-15 18:05:26 +00:00
|
|
|
/**
|
|
|
|
* This creates a processor outside of the memory pool. The processor's destructor will be called
|
|
|
|
* at global destruction time. NAME will be the name of the created instance.
|
|
|
|
*/
|
|
|
|
#define GR_CREATE_STATIC_PROCESSOR(NAME, PROC_CLASS, ARGS) \
|
|
|
|
static SkAlignedSStorage<sizeof(PROC_CLASS)> g_##NAME##_Storage; \
|
|
|
|
static PROC_CLASS* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), PROC_CLASS, ARGS); \
|
|
|
|
static SkAutoTDestroy<GrProcessor> NAME##_ad(NAME);
|
|
|
|
|
2012-04-18 17:49:20 +00:00
|
|
|
#endif
|