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