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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkMatrix.h" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkString.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrRecordingContext.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 15 | #include "src/gpu/GrGpuResource.h" |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrMemoryPool.h" |
| 17 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrTracing.h" |
| 19 | #include "src/gpu/GrXferProcessor.h" |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 20 | #include <atomic> |
| 21 | #include <new> |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 22 | |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame] | 23 | class GrAppliedClip; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 24 | class GrCaps; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 25 | class GrOpFlushState; |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 26 | class GrOpsRenderPass; |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 27 | class GrPaint; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 28 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 29 | /** |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 30 | * GrOp is the base class for all Ganesh deferred GPU operations. To facilitate reordering and to |
| 31 | * minimize draw calls, Ganesh does not generate geometry inline with draw calls. Instead, it |
| 32 | * captures the arguments to the draw and then generates the geometry when flushing. This gives GrOp |
| 33 | * subclasses complete freedom to decide how/when to combine in order to produce fewer draw calls |
| 34 | * and minimize state changes. |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 35 | * |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 36 | * Ops of the same subclass may be merged or chained using combineIfPossible. When two ops merge, |
| 37 | * one takes on the union of the data and the other is left empty. The merged op becomes responsible |
| 38 | * for drawing the data from both the original ops. When ops are chained each op maintains its own |
| 39 | * data but they are linked in a list and the head op becomes responsible for executing the work for |
| 40 | * the chain. |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 41 | * |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 42 | * It is required that chainability is transitive. Moreover, if op A is able to merge with B then |
| 43 | * it must be the case that any op that can chain with A will either merge or chain with any op |
| 44 | * that can chain to B. |
| 45 | * |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 46 | * 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] | 47 | * The bounds are used in determining which clip elements must be applied and thus the bounds cannot |
| 48 | * in turn depend upon the clip. |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 49 | */ |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 50 | #define GR_OP_SPEW 0 |
| 51 | #if GR_OP_SPEW |
| 52 | #define GrOP_SPEW(code) code |
| 53 | #define GrOP_INFO(...) SkDebugf(__VA_ARGS__) |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 54 | #else |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 55 | #define GrOP_SPEW(code) |
| 56 | #define GrOP_INFO(...) |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 57 | #endif |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 58 | |
Robert Phillips | 2748391 | 2018-04-20 12:43:18 -0400 | [diff] [blame] | 59 | // Print out op information at flush time |
| 60 | #define GR_FLUSH_TIME_OP_SPEW 0 |
| 61 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 62 | // A helper macro to generate a class static id |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 63 | #define DEFINE_OP_CLASS_ID \ |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 64 | static uint32_t ClassID() { \ |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 65 | static uint32_t kClassID = GenOpClassID(); \ |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 66 | return kClassID; \ |
| 67 | } |
| 68 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 69 | class GrOp : private SkNoncopyable { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 70 | public: |
Herb Derby | c9a24c9 | 2020-12-01 16:59:40 -0500 | [diff] [blame^] | 71 | #if defined(GR_OP_ALLOCATE_USE_POOL) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 72 | struct DeleteFromPool { |
| 73 | DeleteFromPool() : fPool{nullptr} {} |
Herb Derby | 2acd43d | 2020-10-27 13:38:32 -0400 | [diff] [blame] | 74 | DeleteFromPool(GrMemoryPool* pool) : fPool{pool} {} |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 75 | void operator() (GrOp* op); |
Herb Derby | 2acd43d | 2020-10-27 13:38:32 -0400 | [diff] [blame] | 76 | GrMemoryPool* fPool; |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 77 | }; |
| 78 | using Owner = std::unique_ptr<GrOp, DeleteFromPool>; |
Herb Derby | c9a24c9 | 2020-12-01 16:59:40 -0500 | [diff] [blame^] | 79 | #else |
| 80 | using Owner = std::unique_ptr<GrOp>; |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | template<typename Op, typename... Args> |
| 84 | static Owner Make(GrRecordingContext* context, Args&&... args) { |
| 85 | return MakeWithExtraMemory<Op>(context, 0, std::forward<Args>(args)...); |
| 86 | } |
| 87 | |
| 88 | template<typename Op, typename... Args> |
| 89 | static Owner MakeWithProcessorSet( |
| 90 | GrRecordingContext* context, const SkPMColor4f& color, |
| 91 | GrPaint&& paint, Args&&... args); |
| 92 | |
Herb Derby | c9a24c9 | 2020-12-01 16:59:40 -0500 | [diff] [blame^] | 93 | #if defined(GR_OP_ALLOCATE_USE_POOL) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 94 | template<typename Op, typename... Args> |
| 95 | static Owner MakeWithExtraMemory( |
| 96 | GrRecordingContext* context, size_t extraSize, Args&&... args) { |
Herb Derby | 2acd43d | 2020-10-27 13:38:32 -0400 | [diff] [blame] | 97 | GrMemoryPool* pool = context->priv().opMemoryPool(); |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 98 | void* mem = pool->allocate(sizeof(Op) + extraSize); |
| 99 | GrOp* op = new (mem) Op(std::forward<Args>(args)...); |
| 100 | return Owner{op, pool}; |
| 101 | } |
Herb Derby | c9a24c9 | 2020-12-01 16:59:40 -0500 | [diff] [blame^] | 102 | #else |
| 103 | template<typename Op, typename... Args> |
| 104 | static Owner MakeWithExtraMemory( |
| 105 | GrRecordingContext* context, size_t extraSize, Args&&... args) { |
| 106 | void* bytes = ::operator new(sizeof(Op) + extraSize); |
| 107 | return Owner{new (bytes) Op(std::forward<Args>(args)...)}; |
| 108 | } |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 109 | #endif |
| 110 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 111 | virtual ~GrOp() = default; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 112 | |
| 113 | virtual const char* name() const = 0; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 114 | |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 115 | using VisitProxyFunc = std::function<void(GrSurfaceProxy*, GrMipmapped)>; |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 116 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 117 | virtual void visitProxies(const VisitProxyFunc&) const { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 118 | // This default implementation assumes the op has no proxies |
| 119 | } |
| 120 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 121 | enum class CombineResult { |
| 122 | /** |
| 123 | * The op that combineIfPossible was called on now represents its own work plus that of |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 124 | * the passed op. The passed op should be destroyed without being flushed. Currently it |
| 125 | * is not legal to merge an op passed to combineIfPossible() the passed op is already in a |
| 126 | * chain (though the op on which combineIfPossible() was called may be). |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 127 | */ |
| 128 | kMerged, |
| 129 | /** |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 130 | * The caller *may* (but is not required) to chain these ops together. If they are chained |
| 131 | * then prepare() and execute() will be called on the head op but not the other ops in the |
| 132 | * chain. The head op will prepare and execute on behalf of all the ops in the chain. |
| 133 | */ |
| 134 | kMayChain, |
| 135 | /** |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 136 | * The ops cannot be combined. |
| 137 | */ |
| 138 | kCannotCombine |
| 139 | }; |
| 140 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 141 | // The arenas are the same as what was available when the op was created. |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 142 | CombineResult combineIfPossible(GrOp* that, SkArenaAlloc* alloc, const GrCaps& caps); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 143 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 144 | const SkRect& bounds() const { |
| 145 | SkASSERT(kUninitialized_BoundsFlag != fBoundsFlags); |
| 146 | return fBounds; |
| 147 | } |
| 148 | |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 149 | void setClippedBounds(const SkRect& clippedBounds) { |
| 150 | fBounds = clippedBounds; |
| 151 | // The clipped bounds already incorporate any effect of the bounds flags. |
| 152 | fBoundsFlags = 0; |
| 153 | } |
| 154 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 155 | bool hasAABloat() const { |
| 156 | SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag); |
| 157 | return SkToBool(fBoundsFlags & kAABloat_BoundsFlag); |
| 158 | } |
| 159 | |
| 160 | bool hasZeroArea() const { |
| 161 | SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag); |
| 162 | return SkToBool(fBoundsFlags & kZeroArea_BoundsFlag); |
| 163 | } |
Herb Derby | c9a24c9 | 2020-12-01 16:59:40 -0500 | [diff] [blame^] | 164 | |
| 165 | #if defined(GR_OP_ALLOCATE_USE_POOL) |
| 166 | #if defined(SK_DEBUG) |
| 167 | // All GrOp-derived classes should be allocated in and deleted from a GrMemoryPool |
| 168 | void* operator new(size_t size); |
| 169 | void operator delete(void* target); |
| 170 | |
| 171 | void* operator new(size_t size, void* placement) { |
| 172 | return ::operator new(size, placement); |
| 173 | } |
| 174 | void operator delete(void* target, void* placement) { |
| 175 | ::operator delete(target, placement); |
| 176 | } |
| 177 | #endif |
| 178 | #else |
Herb Derby | d6cfe72 | 2020-10-05 15:50:47 -0400 | [diff] [blame] | 179 | // GrOps are allocated using ::operator new in the GrMemoryPool. Doing this style of memory |
| 180 | // allocation defeats the delete with size optimization. |
| 181 | void* operator new(size_t) { SK_ABORT("All GrOps are created by placement new."); } |
| 182 | void* operator new(size_t, void* p) { return p; } |
| 183 | void operator delete(void* p) { ::operator delete(p); } |
Herb Derby | d6cfe72 | 2020-10-05 15:50:47 -0400 | [diff] [blame] | 184 | #endif |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 187 | * Helper for safely down-casting to a GrOp subclass |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 188 | */ |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 189 | template <typename T> const T& cast() const { |
| 190 | SkASSERT(T::ClassID() == this->classID()); |
| 191 | return *static_cast<const T*>(this); |
| 192 | } |
| 193 | |
| 194 | template <typename T> T* cast() { |
| 195 | SkASSERT(T::ClassID() == this->classID()); |
| 196 | return static_cast<T*>(this); |
| 197 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 198 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 199 | uint32_t classID() const { SkASSERT(kIllegalOpID != fClassID); return fClassID; } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 200 | |
joshualitt | 08e65e7 | 2016-03-08 09:31:15 -0800 | [diff] [blame] | 201 | // We lazily initialize the uniqueID because currently the only user is GrAuditTrail |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 202 | uint32_t uniqueID() const { |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 203 | if (kIllegalOpID == fUniqueID) { |
| 204 | fUniqueID = GenOpID(); |
joshualitt | 08e65e7 | 2016-03-08 09:31:15 -0800 | [diff] [blame] | 205 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 206 | return fUniqueID; |
joshualitt | 08e65e7 | 2016-03-08 09:31:15 -0800 | [diff] [blame] | 207 | } |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 208 | |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 209 | /** |
Robert Phillips | 7327c9d | 2019-10-08 16:32:56 -0400 | [diff] [blame] | 210 | * This can optionally be called before 'prepare' (but after sorting). Each op that overrides |
| 211 | * onPrePrepare must be prepared to handle both cases (when onPrePrepare has been called |
| 212 | * ahead of time and when it has not been called). |
| 213 | */ |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 214 | void prePrepare(GrRecordingContext* context, const GrSurfaceProxyView& dstView, |
| 215 | GrAppliedClip* clip, const GrXferProcessor::DstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 216 | GrXferBarrierFlags renderPassXferBarriers, GrLoadOp colorLoadOp) { |
| 217 | this->onPrePrepare(context, dstView, clip, dstProxyView, renderPassXferBarriers, |
| 218 | colorLoadOp); |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame] | 219 | } |
Robert Phillips | 7327c9d | 2019-10-08 16:32:56 -0400 | [diff] [blame] | 220 | |
| 221 | /** |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 222 | * Called prior to executing. The op should perform any resource creation or data transfers |
| 223 | * necessary before execute() is called. |
| 224 | */ |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 225 | void prepare(GrOpFlushState* state) { this->onPrepare(state); } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 226 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 227 | /** Issues the op's commands to GrGpu. */ |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 228 | void execute(GrOpFlushState* state, const SkRect& chainBounds) { |
Brian Salomon | 5f39427 | 2019-07-02 14:07:49 -0400 | [diff] [blame] | 229 | TRACE_EVENT0("skia.gpu", name()); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 230 | this->onExecute(state, chainBounds); |
Xiao Yu | 9712601 | 2018-04-25 18:11:44 -0700 | [diff] [blame] | 231 | } |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 232 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 233 | /** Used for spewing information about ops when debugging. */ |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 234 | #if GR_TEST_UTILS |
John Stiles | 8dd1e22 | 2020-08-12 19:06:24 -0400 | [diff] [blame] | 235 | virtual SkString dumpInfo() const final { |
| 236 | return SkStringPrintf("%s\nOpBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]", |
| 237 | this->onDumpInfo().c_str(), fBounds.fLeft, fBounds.fTop, |
| 238 | fBounds.fRight, fBounds.fBottom); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 239 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 240 | #endif |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 241 | |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 242 | /** |
| 243 | * A helper for iterating over an op chain in a range for loop that also downcasts to a GrOp |
| 244 | * subclass. E.g.: |
| 245 | * for (MyOpSubClass& op : ChainRange<MyOpSubClass>(this)) { |
| 246 | * // ... |
| 247 | * } |
| 248 | */ |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 249 | template <typename OpSubclass = GrOp> class ChainRange { |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 250 | private: |
| 251 | class Iter { |
| 252 | public: |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 253 | explicit Iter(const OpSubclass* head) : fCurr(head) {} |
| 254 | inline Iter& operator++() { |
| 255 | return *this = Iter(static_cast<const OpSubclass*>(fCurr->nextInChain())); |
| 256 | } |
| 257 | const OpSubclass& operator*() const { return *fCurr; } |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 258 | bool operator!=(const Iter& that) const { return fCurr != that.fCurr; } |
| 259 | |
| 260 | private: |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 261 | const OpSubclass* fCurr; |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 262 | }; |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 263 | const OpSubclass* fHead; |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 264 | |
| 265 | public: |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 266 | explicit ChainRange(const OpSubclass* head) : fHead(head) {} |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 267 | Iter begin() { return Iter(fHead); } |
| 268 | Iter end() { return Iter(nullptr); } |
| 269 | }; |
| 270 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 271 | /** |
| 272 | * Concatenates two op chains. This op must be a tail and the passed op must be a head. The ops |
| 273 | * must be of the same subclass. |
| 274 | */ |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 275 | void chainConcat(GrOp::Owner); |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 276 | /** Returns true if this is the head of a chain (including a length 1 chain). */ |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 277 | bool isChainHead() const { return !fPrevInChain; } |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 278 | /** Returns true if this is the tail of a chain (including a length 1 chain). */ |
| 279 | bool isChainTail() const { return !fNextInChain; } |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 280 | /** The next op in the chain. */ |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 281 | GrOp* nextInChain() const { return fNextInChain.get(); } |
| 282 | /** The previous op in the chain. */ |
| 283 | GrOp* prevInChain() const { return fPrevInChain; } |
| 284 | /** |
| 285 | * Cuts the chain after this op. The returned op is the op that was previously next in the |
| 286 | * chain or null if this was already a tail. |
| 287 | */ |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 288 | GrOp::Owner cutChain(); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 289 | SkDEBUGCODE(void validateChain(GrOp* expectedTail = nullptr) const); |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 290 | |
Ethan Nicholas | 029b22c | 2018-10-18 16:49:56 -0400 | [diff] [blame] | 291 | #ifdef SK_DEBUG |
| 292 | virtual void validate() const {} |
| 293 | #endif |
| 294 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 295 | protected: |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 296 | GrOp(uint32_t classID); |
| 297 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 298 | /** |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 299 | * 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] | 300 | * purpose of ensuring that the fragment shader runs on partially covered pixels for |
| 301 | * non-MSAA antialiasing. |
| 302 | */ |
Chris Dalton | 3b51df1 | 2017-11-27 14:33:06 -0700 | [diff] [blame] | 303 | enum class HasAABloat : bool { |
| 304 | kNo = false, |
| 305 | kYes = true |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 306 | }; |
| 307 | /** |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 308 | * Indicates that the geometry being drawn in a hairline stroke. A point that is drawn in device |
| 309 | * space is also considered a hairline. |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 310 | */ |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 311 | enum class IsHairline : bool { |
Chris Dalton | 3b51df1 | 2017-11-27 14:33:06 -0700 | [diff] [blame] | 312 | kNo = false, |
| 313 | kYes = true |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 314 | }; |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 315 | |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 316 | void setBounds(const SkRect& newBounds, HasAABloat aabloat, IsHairline zeroArea) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 317 | fBounds = newBounds; |
| 318 | this->setBoundsFlags(aabloat, zeroArea); |
| 319 | } |
| 320 | void setTransformedBounds(const SkRect& srcBounds, const SkMatrix& m, |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 321 | HasAABloat aabloat, IsHairline zeroArea) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 322 | m.mapRect(&fBounds, srcBounds); |
| 323 | this->setBoundsFlags(aabloat, zeroArea); |
| 324 | } |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 325 | void makeFullScreen(GrSurfaceProxy* proxy) { |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 326 | this->setBounds(proxy->getBoundsRect(), HasAABloat::kNo, IsHairline::kNo); |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 327 | } |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 328 | |
Brian Salomon | b41417f | 2018-10-24 08:58:48 -0400 | [diff] [blame] | 329 | static uint32_t GenOpClassID() { return GenID(&gCurrOpClassID); } |
| 330 | |
| 331 | private: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 332 | void joinBounds(const GrOp& that) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 333 | if (that.hasAABloat()) { |
| 334 | fBoundsFlags |= kAABloat_BoundsFlag; |
| 335 | } |
| 336 | if (that.hasZeroArea()) { |
| 337 | fBoundsFlags |= kZeroArea_BoundsFlag; |
| 338 | } |
| 339 | return fBounds.joinPossiblyEmptyRect(that.fBounds); |
| 340 | } |
| 341 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 342 | virtual CombineResult onCombineIfPossible(GrOp*, SkArenaAlloc*, const GrCaps&) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 343 | return CombineResult::kCannotCombine; |
| 344 | } |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 345 | |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 346 | // TODO: the parameters to onPrePrepare mirror GrOpFlushState::OpArgs - fuse the two? |
| 347 | virtual void onPrePrepare(GrRecordingContext*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 348 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 349 | GrAppliedClip*, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 350 | const GrXferProcessor::DstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 351 | GrXferBarrierFlags renderPassXferBarriers, |
| 352 | GrLoadOp colorLoadOp) = 0; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 353 | virtual void onPrepare(GrOpFlushState*) = 0; |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 354 | // If this op is chained then chainBounds is the union of the bounds of all ops in the chain. |
| 355 | // Otherwise, this op's bounds. |
| 356 | virtual void onExecute(GrOpFlushState*, const SkRect& chainBounds) = 0; |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 357 | #if GR_TEST_UTILS |
| 358 | virtual SkString onDumpInfo() const { return SkString(); } |
| 359 | #endif |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 360 | |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 361 | static uint32_t GenID(std::atomic<uint32_t>* idCounter) { |
Adlai Holler | 4888cda | 2020-11-06 16:37:37 -0500 | [diff] [blame] | 362 | uint32_t id = idCounter->fetch_add(1, std::memory_order_relaxed); |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 363 | if (id == 0) { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 364 | SK_ABORT("This should never wrap as it should only be called once for each GrOp " |
Adlai Holler | 4888cda | 2020-11-06 16:37:37 -0500 | [diff] [blame] | 365 | "subclass."); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 366 | } |
| 367 | return id; |
| 368 | } |
| 369 | |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 370 | void setBoundsFlags(HasAABloat aabloat, IsHairline zeroArea) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 371 | fBoundsFlags = 0; |
| 372 | fBoundsFlags |= (HasAABloat::kYes == aabloat) ? kAABloat_BoundsFlag : 0; |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 373 | fBoundsFlags |= (IsHairline ::kYes == zeroArea) ? kZeroArea_BoundsFlag : 0; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 374 | } |
| 375 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 376 | enum { |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 377 | kIllegalOpID = 0, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 380 | enum BoundsFlags { |
| 381 | kAABloat_BoundsFlag = 0x1, |
| 382 | kZeroArea_BoundsFlag = 0x2, |
| 383 | SkDEBUGCODE(kUninitialized_BoundsFlag = 0x4) |
| 384 | }; |
| 385 | |
Adlai Holler | 4888cda | 2020-11-06 16:37:37 -0500 | [diff] [blame] | 386 | Owner fNextInChain{nullptr}; |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 387 | GrOp* fPrevInChain = nullptr; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 388 | const uint16_t fClassID; |
| 389 | uint16_t fBoundsFlags; |
| 390 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 391 | static uint32_t GenOpID() { return GenID(&gCurrOpUniqueID); } |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 392 | mutable uint32_t fUniqueID = SK_InvalidUniqueID; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 393 | SkRect fBounds; |
| 394 | |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 395 | static std::atomic<uint32_t> gCurrOpUniqueID; |
| 396 | static std::atomic<uint32_t> gCurrOpClassID; |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 397 | }; |
| 398 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 399 | #endif |