joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 8 | #ifndef GrOp_DEFINED |
| 9 | #define GrOp_DEFINED |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 10 | |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 11 | #include "../private/SkAtomics.h" |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 12 | #include "GrGpuResource.h" |
joshualitt | dbe1e6f | 2015-07-16 08:12:45 -0700 | [diff] [blame] | 13 | #include "GrNonAtomicRef.h" |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 14 | #include "GrXferProcessor.h" |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 15 | #include "SkMatrix.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 16 | #include "SkRect.h" |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 17 | #include "SkString.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 18 | |
bungeman | 2c4bd07 | 2016-04-08 06:58:51 -0700 | [diff] [blame] | 19 | #include <new> |
| 20 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 21 | class GrCaps; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 22 | class GrGpuCommandBuffer; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 23 | class GrOpFlushState; |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 24 | class GrRenderTargetOpList; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 25 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 26 | /** |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 27 | * GrOp is the base class for all Ganesh deferred GPU operations. To facilitate reordering and to |
| 28 | * minimize draw calls, Ganesh does not generate geometry inline with draw calls. Instead, it |
| 29 | * captures the arguments to the draw and then generates the geometry when flushing. This gives GrOp |
| 30 | * subclasses complete freedom to decide how/when to combine in order to produce fewer draw calls |
| 31 | * and minimize state changes. |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 32 | * |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 33 | * Ops of the same subclass may be merged using combineIfPossible. When two ops merge, one |
| 34 | * takes on the union of the data and the other is left empty. The merged op becomes responsible |
| 35 | * for drawing the data from both the original ops. |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 36 | * |
| 37 | * If there are any possible optimizations which might require knowing more about the full state of |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 38 | * the draw, e.g. whether or not the GrOp is allowed to tweak alpha for coverage, then this |
| 39 | * information will be communicated to the GrOp prior to geometry generation. |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 40 | * |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 41 | * The bounds of the op must contain all the vertices in device space *irrespective* of the clip. |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 42 | * The bounds are used in determining which clip elements must be applied and thus the bounds cannot |
| 43 | * in turn depend upon the clip. |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 44 | */ |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 45 | #define GR_OP_SPEW 0 |
| 46 | #if GR_OP_SPEW |
| 47 | #define GrOP_SPEW(code) code |
| 48 | #define GrOP_INFO(...) SkDebugf(__VA_ARGS__) |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 49 | #else |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 50 | #define GrOP_SPEW(code) |
| 51 | #define GrOP_INFO(...) |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 52 | #endif |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 53 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 54 | // A helper macro to generate a class static id |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 55 | #define DEFINE_OP_CLASS_ID \ |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 56 | static uint32_t ClassID() { \ |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 57 | static uint32_t kClassID = GenOpClassID(); \ |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 58 | return kClassID; \ |
| 59 | } |
| 60 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 61 | class GrOp : private SkNoncopyable { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 62 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 63 | GrOp(uint32_t classID); |
| 64 | virtual ~GrOp(); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 65 | |
| 66 | virtual const char* name() const = 0; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 67 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 68 | bool combineIfPossible(GrOp* that, const GrCaps& caps) { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 69 | if (this->classID() != that->classID()) { |
| 70 | return false; |
| 71 | } |
| 72 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 73 | return this->onCombineIfPossible(that, caps); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 74 | } |
| 75 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 76 | const SkRect& bounds() const { |
| 77 | SkASSERT(kUninitialized_BoundsFlag != fBoundsFlags); |
| 78 | return fBounds; |
| 79 | } |
| 80 | |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 81 | void setClippedBounds(const SkRect& clippedBounds) { |
| 82 | fBounds = clippedBounds; |
| 83 | // The clipped bounds already incorporate any effect of the bounds flags. |
| 84 | fBoundsFlags = 0; |
| 85 | } |
| 86 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 87 | bool hasAABloat() const { |
| 88 | SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag); |
| 89 | return SkToBool(fBoundsFlags & kAABloat_BoundsFlag); |
| 90 | } |
| 91 | |
| 92 | bool hasZeroArea() const { |
| 93 | SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag); |
| 94 | return SkToBool(fBoundsFlags & kZeroArea_BoundsFlag); |
| 95 | } |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 96 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 97 | void* operator new(size_t size); |
| 98 | void operator delete(void* target); |
| 99 | |
| 100 | void* operator new(size_t size, void* placement) { |
| 101 | return ::operator new(size, placement); |
| 102 | } |
| 103 | void operator delete(void* target, void* placement) { |
| 104 | ::operator delete(target, placement); |
| 105 | } |
| 106 | |
| 107 | /** |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 108 | * Helper for safely down-casting to a GrOp subclass |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 109 | */ |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 110 | template <typename T> const T& cast() const { |
| 111 | SkASSERT(T::ClassID() == this->classID()); |
| 112 | return *static_cast<const T*>(this); |
| 113 | } |
| 114 | |
| 115 | template <typename T> T* cast() { |
| 116 | SkASSERT(T::ClassID() == this->classID()); |
| 117 | return static_cast<T*>(this); |
| 118 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 119 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 120 | uint32_t classID() const { SkASSERT(kIllegalOpID != fClassID); return fClassID; } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 121 | |
joshualitt | 08e65e7 | 2016-03-08 09:31:15 -0800 | [diff] [blame] | 122 | // We lazily initialize the uniqueID because currently the only user is GrAuditTrail |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 123 | uint32_t uniqueID() const { |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 124 | if (kIllegalOpID == fUniqueID) { |
| 125 | fUniqueID = GenOpID(); |
joshualitt | 08e65e7 | 2016-03-08 09:31:15 -0800 | [diff] [blame] | 126 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 127 | return fUniqueID; |
joshualitt | 08e65e7 | 2016-03-08 09:31:15 -0800 | [diff] [blame] | 128 | } |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 129 | |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 130 | /** |
Brian Salomon | d543e0a | 2017-03-06 16:36:49 -0500 | [diff] [blame] | 131 | * This is called to notify the op that it has been recorded into a GrOpList. Ops can use this |
| 132 | * to begin preparations for the flush of the op list. Note that the op still may either be |
| 133 | * combined into another op or have another op combined into it via combineIfPossible() after |
| 134 | * this call is made. |
| 135 | */ |
Robert Phillips | e3302df | 2017-04-24 07:31:02 -0400 | [diff] [blame] | 136 | virtual void wasRecorded(GrRenderTargetOpList*) {} |
Brian Salomon | d543e0a | 2017-03-06 16:36:49 -0500 | [diff] [blame] | 137 | |
| 138 | /** |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 139 | * Called prior to executing. The op should perform any resource creation or data transfers |
| 140 | * necessary before execute() is called. |
| 141 | */ |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 142 | void prepare(GrOpFlushState* state) { this->onPrepare(state); } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 143 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 144 | /** Issues the op's commands to GrGpu. */ |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 145 | void execute(GrOpFlushState* state) { this->onExecute(state); } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 146 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 147 | /** Used for spewing information about ops when debugging. */ |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 148 | virtual SkString dumpInfo() const { |
| 149 | SkString string; |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 150 | string.appendf("OpBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 151 | fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom); |
| 152 | return string; |
| 153 | } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 154 | |
Robert Phillips | 178ce3e | 2017-04-13 09:15:47 -0400 | [diff] [blame] | 155 | virtual bool needsCommandBufferIsolation() const { return false; } |
| 156 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 157 | protected: |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 158 | /** |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 159 | * Indicates that the op will produce geometry that extends beyond its bounds for the |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 160 | * purpose of ensuring that the fragment shader runs on partially covered pixels for |
| 161 | * non-MSAA antialiasing. |
| 162 | */ |
| 163 | enum class HasAABloat { |
| 164 | kYes, |
| 165 | kNo |
| 166 | }; |
| 167 | /** |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 168 | * Indicates that the geometry represented by the op has zero area (e.g. it is hairline or |
| 169 | * points). |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 170 | */ |
| 171 | enum class IsZeroArea { |
| 172 | kYes, |
| 173 | kNo |
| 174 | }; |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 175 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 176 | void setBounds(const SkRect& newBounds, HasAABloat aabloat, IsZeroArea zeroArea) { |
| 177 | fBounds = newBounds; |
| 178 | this->setBoundsFlags(aabloat, zeroArea); |
| 179 | } |
| 180 | void setTransformedBounds(const SkRect& srcBounds, const SkMatrix& m, |
| 181 | HasAABloat aabloat, IsZeroArea zeroArea) { |
| 182 | m.mapRect(&fBounds, srcBounds); |
| 183 | this->setBoundsFlags(aabloat, zeroArea); |
| 184 | } |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 185 | void makeFullScreen(GrSurfaceProxy* proxy) { |
| 186 | this->setBounds(SkRect::MakeIWH(proxy->width(), proxy->height()), |
| 187 | HasAABloat::kNo, IsZeroArea::kNo); |
| 188 | } |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 189 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 190 | void joinBounds(const GrOp& that) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 191 | if (that.hasAABloat()) { |
| 192 | fBoundsFlags |= kAABloat_BoundsFlag; |
| 193 | } |
| 194 | if (that.hasZeroArea()) { |
| 195 | fBoundsFlags |= kZeroArea_BoundsFlag; |
| 196 | } |
| 197 | return fBounds.joinPossiblyEmptyRect(that.fBounds); |
| 198 | } |
| 199 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 200 | void replaceBounds(const GrOp& that) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 201 | fBounds = that.fBounds; |
| 202 | fBoundsFlags = that.fBoundsFlags; |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 205 | static uint32_t GenOpClassID() { return GenID(&gCurrOpClassID); } |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 206 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 207 | private: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 208 | virtual bool onCombineIfPossible(GrOp*, const GrCaps& caps) = 0; |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 209 | |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 210 | virtual void onPrepare(GrOpFlushState*) = 0; |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 211 | virtual void onExecute(GrOpFlushState*) = 0; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 212 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 213 | static uint32_t GenID(int32_t* idCounter) { |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 214 | // The atomic inc returns the old value not the incremented value. So we add |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 215 | // 1 to the returned value. |
| 216 | uint32_t id = static_cast<uint32_t>(sk_atomic_inc(idCounter)) + 1; |
| 217 | if (!id) { |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 218 | SkFAIL("This should never wrap as it should only be called once for each GrOp " |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 219 | "subclass."); |
| 220 | } |
| 221 | return id; |
| 222 | } |
| 223 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 224 | void setBoundsFlags(HasAABloat aabloat, IsZeroArea zeroArea) { |
| 225 | fBoundsFlags = 0; |
| 226 | fBoundsFlags |= (HasAABloat::kYes == aabloat) ? kAABloat_BoundsFlag : 0; |
| 227 | fBoundsFlags |= (IsZeroArea ::kYes == zeroArea) ? kZeroArea_BoundsFlag : 0; |
| 228 | } |
| 229 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 230 | enum { |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 231 | kIllegalOpID = 0, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 234 | enum BoundsFlags { |
| 235 | kAABloat_BoundsFlag = 0x1, |
| 236 | kZeroArea_BoundsFlag = 0x2, |
| 237 | SkDEBUGCODE(kUninitialized_BoundsFlag = 0x4) |
| 238 | }; |
| 239 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 240 | const uint16_t fClassID; |
| 241 | uint16_t fBoundsFlags; |
| 242 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 243 | static uint32_t GenOpID() { return GenID(&gCurrOpUniqueID); } |
joshualitt | 08e65e7 | 2016-03-08 09:31:15 -0800 | [diff] [blame] | 244 | mutable uint32_t fUniqueID; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 245 | SkRect fBounds; |
| 246 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 247 | static int32_t gCurrOpUniqueID; |
| 248 | static int32_t gCurrOpClassID; |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 251 | #endif |