blob: ef752b5d0c29736bbab0493bb7b7a6c20c7d9508 [file] [log] [blame]
joshualitt4d8da812015-01-28 12:53:54 -08001/*
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 Salomon53e4c3c2016-12-21 11:38:53 -05008#ifndef GrOp_DEFINED
9#define GrOp_DEFINED
joshualitt4d8da812015-01-28 12:53:54 -080010
bungeman2c4bd072016-04-08 06:58:51 -070011#include "../private/SkAtomics.h"
Robert Phillips294870f2016-11-11 12:38:40 -050012#include "GrGpuResource.h"
joshualittdbe1e6f2015-07-16 08:12:45 -070013#include "GrNonAtomicRef.h"
Brian Salomon54d212e2017-03-21 14:22:38 -040014#include "GrXferProcessor.h"
bsalomon88cf17d2016-07-08 06:40:56 -070015#include "SkMatrix.h"
bsalomon16b99132015-08-13 14:55:50 -070016#include "SkRect.h"
bsalomon53469832015-08-18 09:20:09 -070017#include "SkString.h"
joshualitt4d8da812015-01-28 12:53:54 -080018
bungeman2c4bd072016-04-08 06:58:51 -070019#include <new>
20
bsalomon16b99132015-08-13 14:55:50 -070021class GrCaps;
egdaniel9cb63402016-06-23 08:37:05 -070022class GrGpuCommandBuffer;
Brian Salomon742e31d2016-12-07 17:06:19 -050023class GrOpFlushState;
joshualitt4d8da812015-01-28 12:53:54 -080024
bsalomonabd30f52015-08-13 13:34:48 -070025/**
Brian Salomon53e4c3c2016-12-21 11:38:53 -050026 * GrOp is the base class for all Ganesh deferred GPU operations. To facilitate reordering and to
27 * minimize draw calls, Ganesh does not generate geometry inline with draw calls. Instead, it
28 * captures the arguments to the draw and then generates the geometry when flushing. This gives GrOp
29 * subclasses complete freedom to decide how/when to combine in order to produce fewer draw calls
30 * and minimize state changes.
joshualitt4d8da812015-01-28 12:53:54 -080031 *
Brian Salomon25a88092016-12-01 09:36:50 -050032 * Ops of the same subclass may be merged using combineIfPossible. When two ops merge, one
33 * takes on the union of the data and the other is left empty. The merged op becomes responsible
34 * for drawing the data from both the original ops.
joshualitt4d8da812015-01-28 12:53:54 -080035 *
36 * If there are any possible optimizations which might require knowing more about the full state of
Brian Salomon25a88092016-12-01 09:36:50 -050037 * the draw, e.g. whether or not the GrOp is allowed to tweak alpha for coverage, then this
38 * information will be communicated to the GrOp prior to geometry generation.
bsalomondb4758c2015-11-23 11:14:20 -080039 *
Brian Salomon25a88092016-12-01 09:36:50 -050040 * The bounds of the op must contain all the vertices in device space *irrespective* of the clip.
bsalomondb4758c2015-11-23 11:14:20 -080041 * The bounds are used in determining which clip elements must be applied and thus the bounds cannot
42 * in turn depend upon the clip.
joshualitt4d8da812015-01-28 12:53:54 -080043 */
Brian Salomon25a88092016-12-01 09:36:50 -050044#define GR_OP_SPEW 0
45#if GR_OP_SPEW
46 #define GrOP_SPEW(code) code
47 #define GrOP_INFO(...) SkDebugf(__VA_ARGS__)
joshualittca1f07e2015-08-07 08:11:19 -070048#else
Brian Salomon25a88092016-12-01 09:36:50 -050049 #define GrOP_SPEW(code)
50 #define GrOP_INFO(...)
joshualittca1f07e2015-08-07 08:11:19 -070051#endif
joshualitt4d8da812015-01-28 12:53:54 -080052
reed1b55a962015-09-17 20:16:13 -070053// A helper macro to generate a class static id
Brian Salomon25a88092016-12-01 09:36:50 -050054#define DEFINE_OP_CLASS_ID \
reed1b55a962015-09-17 20:16:13 -070055 static uint32_t ClassID() { \
Brian Salomon25a88092016-12-01 09:36:50 -050056 static uint32_t kClassID = GenOpClassID(); \
reed1b55a962015-09-17 20:16:13 -070057 return kClassID; \
58 }
59
Brian Salomonf8334782017-01-03 09:42:58 -050060class GrOp : private SkNoncopyable {
joshualitt4d8da812015-01-28 12:53:54 -080061public:
Brian Salomon25a88092016-12-01 09:36:50 -050062 GrOp(uint32_t classID);
63 virtual ~GrOp();
joshualitt4d8da812015-01-28 12:53:54 -080064
65 virtual const char* name() const = 0;
joshualitt4d8da812015-01-28 12:53:54 -080066
Brian Salomon25a88092016-12-01 09:36:50 -050067 bool combineIfPossible(GrOp* that, const GrCaps& caps) {
joshualitt4d8da812015-01-28 12:53:54 -080068 if (this->classID() != that->classID()) {
69 return false;
70 }
71
bsalomoncb02b382015-08-12 11:14:50 -070072 return this->onCombineIfPossible(that, caps);
joshualitt4d8da812015-01-28 12:53:54 -080073 }
74
bsalomon88cf17d2016-07-08 06:40:56 -070075 const SkRect& bounds() const {
76 SkASSERT(kUninitialized_BoundsFlag != fBoundsFlags);
77 return fBounds;
78 }
79
Brian Salomon9e50f7b2017-03-06 12:02:34 -050080 void setClippedBounds(const SkRect& clippedBounds) {
81 fBounds = clippedBounds;
82 // The clipped bounds already incorporate any effect of the bounds flags.
83 fBoundsFlags = 0;
84 }
85
bsalomon88cf17d2016-07-08 06:40:56 -070086 bool hasAABloat() const {
87 SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag);
88 return SkToBool(fBoundsFlags & kAABloat_BoundsFlag);
89 }
90
91 bool hasZeroArea() const {
92 SkASSERT(fBoundsFlags != kUninitialized_BoundsFlag);
93 return SkToBool(fBoundsFlags & kZeroArea_BoundsFlag);
94 }
joshualitt99c7c072015-05-01 13:43:30 -070095
joshualitt4d8da812015-01-28 12:53:54 -080096 void* operator new(size_t size);
97 void operator delete(void* target);
98
99 void* operator new(size_t size, void* placement) {
100 return ::operator new(size, placement);
101 }
102 void operator delete(void* target, void* placement) {
103 ::operator delete(target, placement);
104 }
105
106 /**
Brian Salomon25a88092016-12-01 09:36:50 -0500107 * Helper for safely down-casting to a GrOp subclass
bsalomonabd30f52015-08-13 13:34:48 -0700108 */
reed1b55a962015-09-17 20:16:13 -0700109 template <typename T> const T& cast() const {
110 SkASSERT(T::ClassID() == this->classID());
111 return *static_cast<const T*>(this);
112 }
113
114 template <typename T> T* cast() {
115 SkASSERT(T::ClassID() == this->classID());
116 return static_cast<T*>(this);
117 }
joshualitt4d8da812015-01-28 12:53:54 -0800118
Brian Salomon25a88092016-12-01 09:36:50 -0500119 uint32_t classID() const { SkASSERT(kIllegalOpID != fClassID); return fClassID; }
joshualitt4d8da812015-01-28 12:53:54 -0800120
joshualitt08e65e72016-03-08 09:31:15 -0800121 // We lazily initialize the uniqueID because currently the only user is GrAuditTrail
halcanary9d524f22016-03-29 09:03:52 -0700122 uint32_t uniqueID() const {
Brian Salomon25a88092016-12-01 09:36:50 -0500123 if (kIllegalOpID == fUniqueID) {
124 fUniqueID = GenOpID();
joshualitt08e65e72016-03-08 09:31:15 -0800125 }
halcanary9d524f22016-03-29 09:03:52 -0700126 return fUniqueID;
joshualitt08e65e72016-03-08 09:31:15 -0800127 }
joshualittca1f07e2015-08-07 08:11:19 -0700128
Brian Salomonbde42852016-12-21 11:37:49 -0500129 /**
Brian Salomond543e0a2017-03-06 16:36:49 -0500130 * This is called to notify the op that it has been recorded into a GrOpList. Ops can use this
131 * to begin preparations for the flush of the op list. Note that the op still may either be
132 * combined into another op or have another op combined into it via combineIfPossible() after
133 * this call is made.
134 */
135 virtual void wasRecorded() {}
136
137 /**
Brian Salomonbde42852016-12-21 11:37:49 -0500138 * Called prior to executing. The op should perform any resource creation or data transfers
139 * necessary before execute() is called.
140 */
Brian Salomon742e31d2016-12-07 17:06:19 -0500141 void prepare(GrOpFlushState* state) { this->onPrepare(state); }
bsalomon53469832015-08-18 09:20:09 -0700142
Brian Salomon25a88092016-12-01 09:36:50 -0500143 /** Issues the op's commands to GrGpu. */
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500144 void execute(GrOpFlushState* state) { this->onExecute(state); }
bsalomon53469832015-08-18 09:20:09 -0700145
Brian Salomon25a88092016-12-01 09:36:50 -0500146 /** Used for spewing information about ops when debugging. */
robertphillips44fbc792016-06-29 06:56:12 -0700147 virtual SkString dumpInfo() const {
148 SkString string;
Brian Salomon25a88092016-12-01 09:36:50 -0500149 string.appendf("OpBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
robertphillips44fbc792016-06-29 06:56:12 -0700150 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
151 return string;
152 }
bsalomon53469832015-08-18 09:20:09 -0700153
joshualitt4d8da812015-01-28 12:53:54 -0800154protected:
bsalomon88cf17d2016-07-08 06:40:56 -0700155 /**
Brian Salomon25a88092016-12-01 09:36:50 -0500156 * Indicates that the op will produce geometry that extends beyond its bounds for the
bsalomon88cf17d2016-07-08 06:40:56 -0700157 * purpose of ensuring that the fragment shader runs on partially covered pixels for
158 * non-MSAA antialiasing.
159 */
160 enum class HasAABloat {
161 kYes,
162 kNo
163 };
164 /**
Brian Salomon25a88092016-12-01 09:36:50 -0500165 * Indicates that the geometry represented by the op has zero area (e.g. it is hairline or
166 * points).
bsalomon88cf17d2016-07-08 06:40:56 -0700167 */
168 enum class IsZeroArea {
169 kYes,
170 kNo
171 };
172 void setBounds(const SkRect& newBounds, HasAABloat aabloat, IsZeroArea zeroArea) {
173 fBounds = newBounds;
174 this->setBoundsFlags(aabloat, zeroArea);
175 }
176 void setTransformedBounds(const SkRect& srcBounds, const SkMatrix& m,
177 HasAABloat aabloat, IsZeroArea zeroArea) {
178 m.mapRect(&fBounds, srcBounds);
179 this->setBoundsFlags(aabloat, zeroArea);
180 }
joshualitt99c7c072015-05-01 13:43:30 -0700181
Brian Salomon25a88092016-12-01 09:36:50 -0500182 void joinBounds(const GrOp& that) {
bsalomon88cf17d2016-07-08 06:40:56 -0700183 if (that.hasAABloat()) {
184 fBoundsFlags |= kAABloat_BoundsFlag;
185 }
186 if (that.hasZeroArea()) {
187 fBoundsFlags |= kZeroArea_BoundsFlag;
188 }
189 return fBounds.joinPossiblyEmptyRect(that.fBounds);
190 }
191
Brian Salomon25a88092016-12-01 09:36:50 -0500192 void replaceBounds(const GrOp& that) {
bsalomon88cf17d2016-07-08 06:40:56 -0700193 fBounds = that.fBounds;
194 fBoundsFlags = that.fBoundsFlags;
joshualitt99c7c072015-05-01 13:43:30 -0700195 }
196
Brian Salomon25a88092016-12-01 09:36:50 -0500197 static uint32_t GenOpClassID() { return GenID(&gCurrOpClassID); }
reed1b55a962015-09-17 20:16:13 -0700198
bsalomonabd30f52015-08-13 13:34:48 -0700199private:
Brian Salomon25a88092016-12-01 09:36:50 -0500200 virtual bool onCombineIfPossible(GrOp*, const GrCaps& caps) = 0;
bsalomonabd30f52015-08-13 13:34:48 -0700201
Brian Salomon742e31d2016-12-07 17:06:19 -0500202 virtual void onPrepare(GrOpFlushState*) = 0;
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500203 virtual void onExecute(GrOpFlushState*) = 0;
bsalomon53469832015-08-18 09:20:09 -0700204
bsalomonabd30f52015-08-13 13:34:48 -0700205 static uint32_t GenID(int32_t* idCounter) {
reed1b55a962015-09-17 20:16:13 -0700206 // The atomic inc returns the old value not the incremented value. So we add
bsalomonabd30f52015-08-13 13:34:48 -0700207 // 1 to the returned value.
208 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(idCounter)) + 1;
209 if (!id) {
Brian Salomon25a88092016-12-01 09:36:50 -0500210 SkFAIL("This should never wrap as it should only be called once for each GrOp "
bsalomonabd30f52015-08-13 13:34:48 -0700211 "subclass.");
212 }
213 return id;
214 }
215
bsalomon88cf17d2016-07-08 06:40:56 -0700216 void setBoundsFlags(HasAABloat aabloat, IsZeroArea zeroArea) {
217 fBoundsFlags = 0;
218 fBoundsFlags |= (HasAABloat::kYes == aabloat) ? kAABloat_BoundsFlag : 0;
219 fBoundsFlags |= (IsZeroArea ::kYes == zeroArea) ? kZeroArea_BoundsFlag : 0;
220 }
221
bsalomonabd30f52015-08-13 13:34:48 -0700222 enum {
Brian Salomon25a88092016-12-01 09:36:50 -0500223 kIllegalOpID = 0,
bsalomonabd30f52015-08-13 13:34:48 -0700224 };
225
bsalomon88cf17d2016-07-08 06:40:56 -0700226 enum BoundsFlags {
227 kAABloat_BoundsFlag = 0x1,
228 kZeroArea_BoundsFlag = 0x2,
229 SkDEBUGCODE(kUninitialized_BoundsFlag = 0x4)
230 };
231
bsalomon88cf17d2016-07-08 06:40:56 -0700232 const uint16_t fClassID;
233 uint16_t fBoundsFlags;
234
Brian Salomon25a88092016-12-01 09:36:50 -0500235 static uint32_t GenOpID() { return GenID(&gCurrOpUniqueID); }
joshualitt08e65e72016-03-08 09:31:15 -0800236 mutable uint32_t fUniqueID;
bsalomon88cf17d2016-07-08 06:40:56 -0700237 SkRect fBounds;
238
Brian Salomon25a88092016-12-01 09:36:50 -0500239 static int32_t gCurrOpUniqueID;
240 static int32_t gCurrOpClassID;
bsalomonabd30f52015-08-13 13:34:48 -0700241};
242
joshualitt4d8da812015-01-28 12:53:54 -0800243#endif