Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrProcessor.h
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 "GrBufferAccess.h"
16 #include "SkMath.h"
17 #include "SkString.h"
18 #include "../private/SkAtomics.h"
19 
20 class GrContext;
21 class GrCoordTransform;
22 class GrInvariantOutput;
23 
29 public:
30  GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
31  SkASSERT(0 == fData->count() % sizeof(uint32_t));
32  }
33 
34  void add32(uint32_t v) {
35  ++fCount;
36  fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
37  }
38 
41  uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
42  SkASSERT(count > 0);
43  fCount += count;
44  return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
45  }
46 
47  size_t size() const { return sizeof(uint32_t) * fCount; }
48 
49 private:
50  SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
51  int fCount; // number of uint32_ts added to fData by the processor.
52 };
53 
60 class GrProcessor : public GrProgramElement {
61 public:
62  virtual ~GrProcessor();
63 
66  virtual const char* name() const = 0;
67 
68  // Human-readable dump of all information
69  virtual SkString dumpInfo() const {
70  SkString str;
71  str.appendf("Missing data");
72  return str;
73  }
74 
75  int numTextures() const { return fTextureAccesses.count(); }
76 
79  const GrTextureAccess& textureAccess(int index) const { return *fTextureAccesses[index]; }
80 
82  GrTexture* texture(int index) const { return this->textureAccess(index).getTexture(); }
83 
84  int numBuffers() const { return fBufferAccesses.count(); }
85 
88  const GrBufferAccess& bufferAccess(int index) const {
89  return *fBufferAccesses[index];
90  }
91 
96  kNone_RequiredFeatures = 0,
97  kFragmentPosition_RequiredFeature = 1 << 0,
98  kSampleLocations_RequiredFeature = 1 << 1
99  };
100 
101  GR_DECL_BITFIELD_OPS_FRIENDS(RequiredFeatures);
102 
103  RequiredFeatures requiredFeatures() const { return fRequiredFeatures; }
104 
105  void* operator new(size_t size);
106  void operator delete(void* target);
107 
108  void* operator new(size_t size, void* placement) {
109  return ::operator new(size, placement);
110  }
111  void operator delete(void* target, void* placement) {
112  ::operator delete(target, placement);
113  }
114 
118  template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
119 
120  uint32_t classID() const { SkASSERT(kIllegalProcessorClassID != fClassID); return fClassID; }
121 
122 protected:
123  GrProcessor() : fClassID(kIllegalProcessorClassID), fRequiredFeatures(kNone_RequiredFeatures) {}
124 
132  virtual void addTextureAccess(const GrTextureAccess* textureAccess);
133  virtual void addBufferAccess(const GrBufferAccess* bufferAccess);
134 
135  bool hasSameSamplers(const GrProcessor&) const;
136 
142  void setWillReadFragmentPosition() { fRequiredFeatures |= kFragmentPosition_RequiredFeature; }
143  void setWillUseSampleLocations() { fRequiredFeatures |= kSampleLocations_RequiredFeature; }
144 
145  void combineRequiredFeatures(const GrProcessor& other) {
146  fRequiredFeatures |= other.fRequiredFeatures;
147  }
148 
149  template <typename PROC_SUBCLASS> void initClassID() {
150  static uint32_t kClassID = GenClassID();
151  fClassID = kClassID;
152  }
153 
154  uint32_t fClassID;
155  SkSTArray<4, const GrTextureAccess*, true> fTextureAccesses;
156  SkSTArray<2, const GrBufferAccess*, true> fBufferAccesses;
157 
158 private:
159  static uint32_t GenClassID() {
160  // fCurrProcessorClassID has been initialized to kIllegalProcessorClassID. The
161  // atomic inc returns the old value not the incremented value. So we add
162  // 1 to the returned value.
163  uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrProcessorClassID)) + 1;
164  if (!id) {
165  SkFAIL("This should never wrap as it should only be called once for each GrProcessor "
166  "subclass.");
167  }
168  return id;
169  }
170 
171  enum {
172  kIllegalProcessorClassID = 0,
173  };
174  static int32_t gCurrProcessorClassID;
175 
176  RequiredFeatures fRequiredFeatures;
177 
178  typedef GrProgramElement INHERITED;
179 };
180 
181 GR_MAKE_BITFIELD_OPS(GrProcessor::RequiredFeatures);
182 
183 #endif
Used to represent a texture that is required by a GrProcessor.
Definition: GrTextureAccess.h:21
RequiredFeatures
Platform specific built-in features that a processor can request for the fragment shader...
Definition: GrProcessor.h:95
Used to represent a texel buffer that will be read in a GrProcessor.
Definition: GrBufferAccess.h:18
uint32_t *SK_WARN_UNUSED_RESULT add32n(int count)
Inserts count uint32_ts into the key.
Definition: GrProcessor.h:41
void setWillReadFragmentPosition()
If the prcoessor will generate code that uses platform specific built-in features, then it must call these methods from its constructor.
Definition: GrProcessor.h:142
Used by processors to build their keys.
Definition: GrProcessor.h:28
Base class for GrProcessor.
Definition: GrProgramElement.h:44
A class representing a linear transformation from one of the built-in coordinate sets (local or posit...
Definition: GrCoordTransform.h:43
GrTexture * texture(int index) const
Shortcut for textureAccess(index).texture();.
Definition: GrProcessor.h:82
Definition: GrInvariantOutput.h:57
Definition: GrContext.h:48
const GrBufferAccess & bufferAccess(int index) const
Returns the access pattern for the buffer at index.
Definition: GrProcessor.h:88
const GrTextureAccess & textureAccess(int index) const
Returns the access pattern for the texture at index.
Definition: GrProcessor.h:79
const T & cast() const
Helper for down-casting to a GrProcessor subclass.
Definition: GrProcessor.h:118
virtual const char * name() const =0
Human-meaningful string to identify this prcoessor; may be embedded in generated shader code...
Provides custom shader code to the Ganesh shading pipeline.
Definition: GrProcessor.h:60
virtual void addTextureAccess(const GrTextureAccess *textureAccess)
Subclasses call these from their constructor to register sampler sources.
Definition: GrTexture.h:19
Light weight class for managing strings.
Definition: SkString.h:121