Debian Codegraph @

src/gpu/ganesh/ops/GrOp.h

raw
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
8 #ifndef GrOp_DEFINED
9 #define GrOp_DEFINED
10
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkString.h"
14 #include "include/core/SkTypes.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/base/SkNoncopyable.h"
17 #include "include/private/base/SkTo.h"
18 #include "include/private/gpu/ganesh/GrTypesPriv.h"
19 #include "src/core/SkColorData.h"
20 #include "src/gpu/ganesh/GrSurfaceProxy.h"
21
22 #include <atomic>
23 #include <cstddef>
24 #include <cstdint>
25 #include <memory>
26 #include <new>
27 #include <utility>
28
29 class GrAppliedClip;
30 class GrCaps;
31 class GrDstProxyView;
32 class GrOpFlushState;
33 class GrPaint;
34 class GrRecordingContext;
35 class GrSurfaceProxyView;
36 class SkArenaAlloc;
37 enum class GrXferBarrierFlags;
38
39 /**
40 * GrOp is the base class for all Ganesh deferred GPU operations. To facilitate reordering and to
41 * minimize draw calls, Ganesh does not generate geometry inline with draw calls. Instead, it
42 * captures the arguments to the draw and then generates the geometry when flushing. This gives GrOp
43 * subclasses complete freedom to decide how/when to combine in order to produce fewer draw calls
44 * and minimize state changes.
45 *
46 * Ops of the same subclass may be merged or chained using combineIfPossible. When two ops merge,
47 * one takes on the union of the data and the other is left empty. The merged op becomes responsible
48 * for drawing the data from both the original ops. When ops are chained each op maintains its own
49 * data but they are linked in a list and the head op becomes responsible for executing the work for
50 * the chain.
51 *
52 * It is required that chainability is transitive. Moreover, if op A is able to merge with B then
53 * it must be the case that any op that can chain with A will either merge or chain with any op
54 * that can chain to B.
55 *
56 * The bounds of the op must contain all the vertices in device space *irrespective* of the clip.
57 * The bounds are used in determining which clip elements must be applied and thus the bounds cannot
58 * in turn depend upon the clip.
59 */
60 #define GR_OP_SPEW 0
61 #if GR_OP_SPEW
62 #define GrOP_SPEW(code) code
63 #define GrOP_INFO(...) SkDebugf(__VA_ARGS__)
64 #else
65 #define GrOP_SPEW(code)
66 #define GrOP_INFO(...)
67 #endif
68
69 // Print out op information at flush time
70 #define GR_FLUSH_TIME_OP_SPEW 0
71
72 // A helper macro to generate a class static id
73 #define DEFINE_OP_CLASS_ID \
74 static uint32_t ClassID() { \
75 static const uint32_t kClassID = GenOpClassID(); \
76 return kClassID; \
77 }
78
79 class GrOp : private SkNoncopyable {
80 public:
81 using Owner = std::unique_ptr<GrOp>;
82
83 template<typename Op, typename... Args>
84 static Owner Make(GrRecordingContext* context, Args&&... args) {
85 return Owner{new Op(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
93 template<typename Op, typename... Args>
94 static Owner MakeWithExtraMemory(
95 GrRecordingContext* context, size_t extraSize, Args&&... args) {
96 void* bytes = ::operator new(sizeof(Op) + extraSize);
97 return Owner{new (bytes) Op(std::forward<Args>(args)...)};
98 }
99
100 virtual ~GrOp() = default;
101
102 virtual const char* name() const = 0;
103
104 virtual void visitProxies(const GrVisitProxyFunc&) const {
105 // This default implementation assumes the op has no proxies
106 }
107
108 enum class CombineResult {
109 /**
110 * The op that combineIfPossible was called on now represents its own work plus that of
111 * the passed op. The passed op should be destroyed without being flushed. Currently it
112 * is not legal to merge an op passed to combineIfPossible() the passed op is already in a
113 * chain (though the op on which combineIfPossible() was called may be).
114 */
115 kMerged,
116 /**
117 * The caller *may* (but is not required) to chain these ops together. If they are chained
118 * then prepare() and execute() will be called on the head op but not the other ops in the
119 * chain. The head op will prepare and execute on behalf of all the ops in the chain.
120 */
121 kMayChain,
122 /**
123 * The ops cannot be combined.
124 */
125 kCannotCombine
126 };
127
128 // The arenas are the same as what was available when the op was created.
129 CombineResult combineIfPossible(GrOp* that, SkArenaAlloc* alloc, const GrCaps& caps);
130
131 const SkRect& bounds() const {
132 SkASSERT(kUninitialized_BoundsFlag != fBoundsFlags);
133 return fBounds;
134 }
135
136 void setClippedBounds(const SkRect& clippedBounds) {
137 fBounds = clippedBounds;
138 // The clipped bounds already incorporate any effect of the bounds flags.
139 fBoundsFlags = 0;
140 }
141
142 bool hasAABloat() const {
143 SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag);
144 return SkToBool(fBoundsFlags & kAABloat_BoundsFlag);
145 }
146
147 bool hasZeroArea() const {
148 SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag);
149 return SkToBool(fBoundsFlags & kZeroArea_BoundsFlag);
150 }
151
152 void operator delete(void* p) { ::operator delete(p); }
153
154 /**
155 * Helper for safely down-casting to a GrOp subclass
156 */
157 template <typename T> const T& cast() const {
158 SkASSERT(T::ClassID() == this->classID());
159 return *static_cast<const T*>(this);
160 }
161
162 template <typename T> T* cast() {
163 SkASSERT(T::ClassID() == this->classID());
164 return static_cast<T*>(this);
165 }
166
167 uint32_t classID() const { SkASSERT(kIllegalOpID != fClassID); return fClassID; }
168
169 // We lazily initialize the uniqueID because currently the only user is GrAuditTrail
170 uint32_t uniqueID() const {
171 if (kIllegalOpID == fUniqueID) {
172 fUniqueID = GenOpID();
173 }
174 return fUniqueID;
175 }
176
177 /**
178 * This can optionally be called before 'prepare' (but after sorting). Each op that overrides
179 * onPrePrepare must be prepared to handle both cases (when onPrePrepare has been called
180 * ahead of time and when it has not been called).
181 */
182 void prePrepare(GrRecordingContext* context, const GrSurfaceProxyView& dstView,
183 GrAppliedClip* clip, const GrDstProxyView& dstProxyView,
184 GrXferBarrierFlags renderPassXferBarriers, GrLoadOp colorLoadOp);
185
186 /**
187 * Called prior to executing. The op should perform any resource creation or data transfers
188 * necessary before execute() is called.
189 */
190 void prepare(GrOpFlushState* state);
191
192 /** Issues the op's commands to GrGpu. */
193 void execute(GrOpFlushState* state, const SkRect& chainBounds);
194
195 /** Used for spewing information about ops when debugging. */
196 #if defined(GPU_TEST_UTILS)
197 virtual SkString dumpInfo() const final {
198 return SkStringPrintf("%s\nOpBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]",
199 this->onDumpInfo().c_str(), fBounds.fLeft, fBounds.fTop,
200 fBounds.fRight, fBounds.fBottom);
201 }
202 #endif
203
204 /**
205 * A helper for iterating over an op chain in a range for loop that also downcasts to a GrOp
206 * subclass. E.g.:
207 * for (MyOpSubClass& op : ChainRange<MyOpSubClass>(this)) {
208 * // ...
209 * }
210 */
211 template <typename OpSubclass = GrOp> class ChainRange {
212 private:
213 class Iter {
214 public:
215 explicit Iter(const OpSubclass* head) : fCurr(head) {}
216 inline Iter& operator++() {
217 return *this = Iter(static_cast<const OpSubclass*>(fCurr->nextInChain()));
218 }
219 const OpSubclass& operator*() const { return *fCurr; }
220 bool operator!=(const Iter& that) const { return fCurr != that.fCurr; }
221
222 private:
223 const OpSubclass* fCurr;
224 };
225 const OpSubclass* fHead;
226
227 public:
228 explicit ChainRange(const OpSubclass* head) : fHead(head) {}
229 Iter begin() { return Iter(fHead); }
230 Iter end() { return Iter(nullptr); }
231 };
232
233 /**
234 * Concatenates two op chains. This op must be a tail and the passed op must be a head. The ops
235 * must be of the same subclass.
236 */
237 void chainConcat(GrOp::Owner);
238 /** Returns true if this is the head of a chain (including a length 1 chain). */
239 bool isChainHead() const { return !fPrevInChain; }
240 /** Returns true if this is the tail of a chain (including a length 1 chain). */
241 bool isChainTail() const { return !fNextInChain; }
242 /** The next op in the chain. */
243 GrOp* nextInChain() const { return fNextInChain.get(); }
244 /** The previous op in the chain. */
245 GrOp* prevInChain() const { return fPrevInChain; }
246 /**
247 * Cuts the chain after this op. The returned op is the op that was previously next in the
248 * chain or null if this was already a tail.
249 */
250 GrOp::Owner cutChain();
251 SkDEBUGCODE(void validateChain(GrOp* expectedTail = nullptr) const;)
252
253 #ifdef SK_DEBUG
254 virtual void validate() const {}
255 #endif
256
257 protected:
258 GrOp(uint32_t classID);
259
260 /**
261 * Indicates that the op will produce geometry that extends beyond its bounds for the
262 * purpose of ensuring that the fragment shader runs on partially covered pixels for
263 * non-MSAA antialiasing.
264 */
265 enum class HasAABloat : bool {
266 kNo = false,
267 kYes = true
268 };
269 /**
270 * Indicates that the geometry being drawn in a hairline stroke. A point that is drawn in device
271 * space is also considered a hairline.
272 */
273 enum class IsHairline : bool {
274 kNo = false,
275 kYes = true
276 };
277
278 void setBounds(const SkRect& newBounds, HasAABloat aabloat, IsHairline zeroArea) {
279 fBounds = newBounds;
280 this->setBoundsFlags(aabloat, zeroArea);
281 }
282 void setTransformedBounds(const SkRect& srcBounds, const SkMatrix& m,
283 HasAABloat aabloat, IsHairline zeroArea) {
284 m.mapRect(&fBounds, srcBounds);
285 this->setBoundsFlags(aabloat, zeroArea);
286 }
287 void makeFullScreen(GrSurfaceProxy* proxy) {
288 this->setBounds(proxy->getBoundsRect(), HasAABloat::kNo, IsHairline::kNo);
289 }
290
291 static uint32_t GenOpClassID() { return GenID(&gCurrOpClassID); }
292
293 private:
294 void joinBounds(const GrOp& that) {
295 if (that.hasAABloat()) {
296 fBoundsFlags |= kAABloat_BoundsFlag;
297 }
298 if (that.hasZeroArea()) {
299 fBoundsFlags |= kZeroArea_BoundsFlag;
300 }
301 return fBounds.joinPossiblyEmptyRect(that.fBounds);
302 }
303
304 virtual CombineResult onCombineIfPossible(GrOp*, SkArenaAlloc*, const GrCaps&) {
305 return CombineResult::kCannotCombine;
306 }
307
308 // TODO: the parameters to onPrePrepare mirror GrOpFlushState::OpArgs - fuse the two?
309 virtual void onPrePrepare(GrRecordingContext*,
310 const GrSurfaceProxyView& writeView,
311 GrAppliedClip*,
312 const GrDstProxyView&,
313 GrXferBarrierFlags renderPassXferBarriers,
314 GrLoadOp colorLoadOp) = 0;
315 virtual void onPrepare(GrOpFlushState*) = 0;
316 // If this op is chained then chainBounds is the union of the bounds of all ops in the chain.
317 // Otherwise, this op's bounds.
318 virtual void onExecute(GrOpFlushState*, const SkRect& chainBounds) = 0;
319 #if defined(GPU_TEST_UTILS)
320 virtual SkString onDumpInfo() const { return SkString(); }
321 #endif
322
323 static uint32_t GenID(std::atomic<uint32_t>* idCounter) {
324 uint32_t id = idCounter->fetch_add(1, std::memory_order_relaxed);
325 if (id == 0) {
326 SK_ABORT("This should never wrap as it should only be called once for each GrOp "
327 "subclass.");
328 }
329 return id;
330 }
331
332 void setBoundsFlags(HasAABloat aabloat, IsHairline zeroArea) {
333 fBoundsFlags = 0;
334 fBoundsFlags |= (HasAABloat::kYes == aabloat) ? kAABloat_BoundsFlag : 0;
335 fBoundsFlags |= (IsHairline ::kYes == zeroArea) ? kZeroArea_BoundsFlag : 0;
336 }
337
338 static constexpr uint16_t kIllegalOpID = 0;
339
340 enum BoundsFlags {
341 kAABloat_BoundsFlag = 0x1,
342 kZeroArea_BoundsFlag = 0x2,
343 SkDEBUGCODE(kUninitialized_BoundsFlag = 0x4)
344 };
345
346 Owner fNextInChain{nullptr};
347 GrOp* fPrevInChain = nullptr;
348 const uint16_t fClassID;
349 uint16_t fBoundsFlags;
350
351 static uint32_t GenOpID() { return GenID(&gCurrOpUniqueID); }
352 mutable uint32_t fUniqueID = SK_InvalidUniqueID;
353 SkRect fBounds;
354
355 static std::atomic<uint32_t> gCurrOpUniqueID;
356 static std::atomic<uint32_t> gCurrOpClassID;
357 };
358
359 #endif