1 /* 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 8 #ifndef GrProcessor_DEFINED 9 #define GrProcessor_DEFINED 10 11 #include "GrColor.h" 12 #include "GrProcessorUnitTest.h" 13 #include "GrProgramElement.h" 14 #include "GrTextureAccess.h" 15 #include "SkMath.h" 16 17 class GrContext; 18 class GrCoordTransform; 19 class GrInvariantOutput; 20 21 /** 22 * Used by processors to build their keys. It incorporates each per-processor key into a larger 23 * shader key. 24 */ 25 class GrProcessorKeyBuilder { 26 public: GrProcessorKeyBuilder(SkTArray<unsigned char,true> * data)27 GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) { 28 SkASSERT(0 == fData->count() % sizeof(uint32_t)); 29 } 30 add32(uint32_t v)31 void add32(uint32_t v) { 32 ++fCount; 33 fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v)); 34 } 35 36 /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next 37 add*() call. */ add32n(int count)38 uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) { 39 SkASSERT(count > 0); 40 fCount += count; 41 return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count)); 42 } 43 size()44 size_t size() const { return sizeof(uint32_t) * fCount; } 45 46 private: 47 SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key. 48 int fCount; // number of uint32_ts added to fData by the processor. 49 }; 50 51 /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be 52 immutable: after being constructed, their fields may not change. 53 54 Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an 55 processor must reach 0 before the thread terminates and the pool is destroyed. To create a 56 static processor use the helper macro GR_CREATE_STATIC_PROCESSOR declared below. 57 */ 58 class GrProcessor : public GrProgramElement { 59 public: 60 SK_DECLARE_INST_COUNT(GrProcessor) 61 62 virtual ~GrProcessor(); 63 64 /** Human-meaningful string to identify this prcoessor; may be embedded 65 in generated shader code. */ 66 virtual const char* name() const = 0; 67 numTextures()68 int numTextures() const { return fTextureAccesses.count(); } 69 70 /** Returns the access pattern for the texture at index. index must be valid according to 71 numTextures(). */ textureAccess(int index)72 const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; } 73 74 /** Shortcut for textureAccess(index).texture(); */ texture(int index)75 GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); } 76 77 /** Will this processor read the fragment position? */ willReadFragmentPosition()78 bool willReadFragmentPosition() const { return fWillReadFragmentPosition; } 79 80 void* operator new(size_t size); 81 void operator delete(void* target); 82 new(size_t size,void * placement)83 void* operator new(size_t size, void* placement) { 84 return ::operator new(size, placement); 85 } delete(void * target,void * placement)86 void operator delete(void* target, void* placement) { 87 ::operator delete(target, placement); 88 } 89 90 /** 91 * Helper for down-casting to a GrProcessor subclass 92 */ cast()93 template <typename T> const T& cast() const { return *static_cast<const T*>(this); } 94 classID()95 uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; } 96 97 protected: GrProcessor()98 GrProcessor() : fClassID(kIllegalProcessorClassID), fWillReadFragmentPosition(false) {} 99 100 /** 101 * Subclasses call this from their constructor to register GrTextureAccesses. The processor 102 * subclass manages the lifetime of the accesses (this function only stores a pointer). The 103 * GrTextureAccess is typically a member field of the GrProcessor subclass. This must only be 104 * called from the constructor because GrProcessors are immutable. 105 */ 106 void addTextureAccess(const GrTextureAccess* textureAccess); 107 108 bool hasSameTextureAccesses(const GrProcessor&) const; 109 110 /** 111 * If the prcoessor will generate a backend-specific processor that will read the fragment 112 * position in the FS then it must call this method from its constructor. Otherwise, the 113 * request to access the fragment position will be denied. 114 */ setWillReadFragmentPosition()115 void setWillReadFragmentPosition() { fWillReadFragmentPosition = true; } 116 initClassID()117 template <typename PROC_SUBCLASS> void initClassID() { 118 static uint32_t kClassID = GenClassID(); 119 fClassID = kClassID; 120 } 121 122 uint32_t fClassID; 123 124 private: GenClassID()125 static uint32_t GenClassID() { 126 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The 127 // atomic inc returns the old value not the incremented value. So we add 128 // 1 to the returned value. 129 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1; 130 if (!id) { 131 SkFAIL("This should never wrap as it should only be called once for each GrProcessor " 132 "subclass."); 133 } 134 return id; 135 } 136 137 enum { 138 kIllegalProcessorClassID = 0, 139 }; 140 static int32_t gCurrProcessorClassID; 141 142 SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses; 143 bool fWillReadFragmentPosition; 144 145 typedef GrProgramElement INHERITED; 146 }; 147 148 /** 149 * This creates a processor outside of the memory pool. The processor's destructor will be called 150 * at global destruction time. NAME will be the name of the created instance. 151 */ 152 #define GR_CREATE_STATIC_PROCESSOR(NAME, PROC_CLASS, ARGS) \ 153 static SkAlignedSStorage<sizeof(PROC_CLASS)> g_##NAME##_Storage; \ 154 static PROC_CLASS* NAME SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), PROC_CLASS, ARGS); \ 155 static SkAutoTDestroy<GrProcessor> NAME##_ad(NAME); 156 157 #endif 158