OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrBatch_DEFINED | 8 #ifndef GrBatch_DEFINED |
9 #define GrBatch_DEFINED | 9 #define GrBatch_DEFINED |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 */ | 34 */ |
35 #define GR_BATCH_SPEW 0 | 35 #define GR_BATCH_SPEW 0 |
36 #if GR_BATCH_SPEW | 36 #if GR_BATCH_SPEW |
37 #define GrBATCH_INFO(...) SkDebugf(__VA_ARGS__) | 37 #define GrBATCH_INFO(...) SkDebugf(__VA_ARGS__) |
38 #define GrBATCH_SPEW(code) code | 38 #define GrBATCH_SPEW(code) code |
39 #else | 39 #else |
40 #define GrBATCH_SPEW(code) | 40 #define GrBATCH_SPEW(code) |
41 #define GrBATCH_INFO(...) | 41 #define GrBATCH_INFO(...) |
42 #endif | 42 #endif |
43 | 43 |
| 44 // A helper macro to generate a class static id |
| 45 #define DEFINE_BATCH_CLASS_ID \ |
| 46 static uint32_t ClassID() { \ |
| 47 static uint32_t kClassID = GenBatchClassID(); \ |
| 48 return kClassID; \ |
| 49 } |
| 50 |
44 class GrBatch : public GrNonAtomicRef { | 51 class GrBatch : public GrNonAtomicRef { |
45 public: | 52 public: |
46 GrBatch(); | 53 GrBatch(uint32_t classID); |
47 ~GrBatch() override; | 54 ~GrBatch() override; |
48 | 55 |
49 virtual const char* name() const = 0; | 56 virtual const char* name() const = 0; |
50 | 57 |
51 bool combineIfPossible(GrBatch* that, const GrCaps& caps) { | 58 bool combineIfPossible(GrBatch* that, const GrCaps& caps) { |
52 if (this->classID() != that->classID()) { | 59 if (this->classID() != that->classID()) { |
53 return false; | 60 return false; |
54 } | 61 } |
55 | 62 |
56 return this->onCombineIfPossible(that, caps); | 63 return this->onCombineIfPossible(that, caps); |
57 } | 64 } |
58 | 65 |
59 const SkRect& bounds() const { return fBounds; } | 66 const SkRect& bounds() const { return fBounds; } |
60 | 67 |
61 void* operator new(size_t size); | 68 void* operator new(size_t size); |
62 void operator delete(void* target); | 69 void operator delete(void* target); |
63 | 70 |
64 void* operator new(size_t size, void* placement) { | 71 void* operator new(size_t size, void* placement) { |
65 return ::operator new(size, placement); | 72 return ::operator new(size, placement); |
66 } | 73 } |
67 void operator delete(void* target, void* placement) { | 74 void operator delete(void* target, void* placement) { |
68 ::operator delete(target, placement); | 75 ::operator delete(target, placement); |
69 } | 76 } |
70 | 77 |
71 /** | 78 /** |
72 * Helper for down-casting to a GrBatch subclass | 79 * Helper for safely down-casting to a GrBatch subclass |
73 */ | 80 */ |
74 template <typename T> const T& cast() const { return *static_cast<const T*>(
this); } | 81 template <typename T> const T& cast() const { |
75 template <typename T> T* cast() { return static_cast<T*>(this); } | 82 SkASSERT(T::ClassID() == this->classID()); |
| 83 return *static_cast<const T*>(this); |
| 84 } |
| 85 |
| 86 template <typename T> T* cast() { |
| 87 SkASSERT(T::ClassID() == this->classID()); |
| 88 return static_cast<T*>(this); |
| 89 } |
76 | 90 |
77 uint32_t classID() const { SkASSERT(kIllegalBatchID != fClassID); return fCl
assID; } | 91 uint32_t classID() const { SkASSERT(kIllegalBatchID != fClassID); return fCl
assID; } |
78 | 92 |
79 #if GR_BATCH_SPEW | 93 #if GR_BATCH_SPEW |
80 uint32_t uniqueID() const { return fUniqueID; } | 94 uint32_t uniqueID() const { return fUniqueID; } |
81 #endif | 95 #endif |
82 SkDEBUGCODE(bool isUsed() const { return fUsed; }) | 96 SkDEBUGCODE(bool isUsed() const { return fUsed; }) |
83 | 97 |
84 /** Called prior to drawing. The batch should perform any resource creation
necessary to | 98 /** Called prior to drawing. The batch should perform any resource creation
necessary to |
85 to quickly issue its draw when draw is called. */ | 99 to quickly issue its draw when draw is called. */ |
86 void prepare(GrBatchFlushState* state) { this->onPrepare(state); } | 100 void prepare(GrBatchFlushState* state) { this->onPrepare(state); } |
87 | 101 |
88 /** Issues the batches commands to GrGpu. */ | 102 /** Issues the batches commands to GrGpu. */ |
89 void draw(GrBatchFlushState* state) { this->onDraw(state); } | 103 void draw(GrBatchFlushState* state) { this->onDraw(state); } |
90 | 104 |
91 /** Used to block batching across render target changes. Remove this once we
store | 105 /** Used to block batching across render target changes. Remove this once we
store |
92 GrBatches for different RTs in different targets. */ | 106 GrBatches for different RTs in different targets. */ |
93 virtual uint32_t renderTargetUniqueID() const = 0; | 107 virtual uint32_t renderTargetUniqueID() const = 0; |
94 | 108 |
95 /** Used for spewing information about batches when debugging. */ | 109 /** Used for spewing information about batches when debugging. */ |
96 virtual SkString dumpInfo() const = 0; | 110 virtual SkString dumpInfo() const = 0; |
97 | 111 |
98 protected: | 112 protected: |
99 template <typename PROC_SUBCLASS> void initClassID() { | |
100 static uint32_t kClassID = GenID(&gCurrBatchClassID); | |
101 fClassID = kClassID; | |
102 } | |
103 | |
104 // NOTE, compute some bounds, even if extremely conservative. Do *NOT* setL
argest on the bounds | 113 // NOTE, compute some bounds, even if extremely conservative. Do *NOT* setL
argest on the bounds |
105 // rect because we outset it for dst copy textures | 114 // rect because we outset it for dst copy textures |
106 void setBounds(const SkRect& newBounds) { fBounds = newBounds; } | 115 void setBounds(const SkRect& newBounds) { fBounds = newBounds; } |
107 | 116 |
108 void joinBounds(const SkRect& otherBounds) { | 117 void joinBounds(const SkRect& otherBounds) { |
109 return fBounds.joinPossiblyEmptyRect(otherBounds); | 118 return fBounds.joinPossiblyEmptyRect(otherBounds); |
110 } | 119 } |
111 | 120 |
| 121 static uint32_t GenBatchClassID() { return GenID(&gCurrBatchClassID); } |
| 122 |
112 SkRect fBounds; | 123 SkRect fBounds; |
113 | 124 |
114 private: | 125 private: |
115 virtual bool onCombineIfPossible(GrBatch*, const GrCaps& caps) = 0; | 126 virtual bool onCombineIfPossible(GrBatch*, const GrCaps& caps) = 0; |
116 | 127 |
117 virtual void onPrepare(GrBatchFlushState*) = 0; | 128 virtual void onPrepare(GrBatchFlushState*) = 0; |
118 virtual void onDraw(GrBatchFlushState*) = 0; | 129 virtual void onDraw(GrBatchFlushState*) = 0; |
119 | 130 |
120 static uint32_t GenID(int32_t* idCounter) { | 131 static uint32_t GenID(int32_t* idCounter) { |
121 // fCurrProcessorClassID has been initialized to kIllegalProcessorClassI
D. The | 132 // The atomic inc returns the old value not the incremented value. So we
add |
122 // atomic inc returns the old value not the incremented value. So we add | |
123 // 1 to the returned value. | 133 // 1 to the returned value. |
124 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(idCounter)) + 1; | 134 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(idCounter)) + 1; |
125 if (!id) { | 135 if (!id) { |
126 SkFAIL("This should never wrap as it should only be called once for
each GrBatch " | 136 SkFAIL("This should never wrap as it should only be called once for
each GrBatch " |
127 "subclass."); | 137 "subclass."); |
128 } | 138 } |
129 return id; | 139 return id; |
130 } | 140 } |
131 | 141 |
132 enum { | 142 enum { |
133 kIllegalBatchID = 0, | 143 kIllegalBatchID = 0, |
134 }; | 144 }; |
135 | 145 |
136 uint32_t fClassID; | |
137 SkDEBUGCODE(bool fUsed;) | 146 SkDEBUGCODE(bool fUsed;) |
| 147 const uint32_t fClassID; |
138 #if GR_BATCH_SPEW | 148 #if GR_BATCH_SPEW |
139 uint32_t fUniqueID; | 149 static uint32_t GenBatchID() { return GenID(&gCurrBatchUniqueID); } |
140 static int32_t gCurrBatchUniqueID; | 150 const uint32_t fUniqueID; |
| 151 static int32_t gCurrBatchUniqueID; |
141 #endif | 152 #endif |
142 static int32_t gCurrBatchClassID; | 153 static int32_t gCurrBatchClassID; |
143 typedef GrNonAtomicRef INHERITED; | 154 typedef GrNonAtomicRef INHERITED; |
144 }; | 155 }; |
145 | 156 |
146 #endif | 157 #endif |
OLD | NEW |