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