blob: c77e69f8a809973a7231fc9b5446a01806800b13 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
Greg Danielf41b2bd2019-08-22 16:19:24 -04002 * Copyright 2019 Google Inc.
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Greg Danielf41b2bd2019-08-22 16:19:24 -04008#ifndef GrOpsTask_DEFINED
9#define GrOpsTask_DEFINED
reed@google.comac10a2d2010-12-22 21:39:39 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkMatrix.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040012#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkStrokeRec.h"
14#include "include/core/SkTypes.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040015#include "include/gpu/GrRecordingContext.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040016#include "include/private/SkColorData.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/private/SkTArray.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040018#include "include/private/SkTDArray.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040019#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkClipStack.h"
21#include "src/core/SkStringUtils.h"
22#include "src/core/SkTLazy.h"
23#include "src/gpu/GrAppliedClip.h"
24#include "src/gpu/GrPathRendering.h"
25#include "src/gpu/GrPrimitiveProcessor.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040026#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/ops/GrDrawOp.h"
28#include "src/gpu/ops/GrOp.h"
Scroggo97c88c22011-05-11 14:05:25 +000029
joshualitt086cee12016-01-12 06:45:24 -080030class GrAuditTrail;
Greg Danielf21bf9e2019-08-22 20:12:20 +000031class GrCaps;
Greg Danielf41b2bd2019-08-22 16:19:24 -040032class GrClearOp;
33class GrGpuBuffer;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040034class GrRenderTargetProxy;
sugoi@google.com12b4e272012-12-06 20:13:11 +000035
Greg Danielf41b2bd2019-08-22 16:19:24 -040036class GrOpsTask : public GrRenderTask {
Brian Salomon54d212e2017-03-21 14:22:38 -040037private:
Greg Daniel524e28b2019-11-01 11:48:53 -040038 using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon54d212e2017-03-21 14:22:38 -040039
bsalomon@google.comf6601872012-08-28 21:11:35 +000040public:
Michael Ludwig28b0c5d2019-12-19 14:51:00 -050041 // The Arenas must outlive the GrOpsTask, either by preserving the context that owns
Michael Ludwigcb10e4d2019-12-12 13:59:20 -050042 // the pool, or by moving the pool to the DDL that takes over the GrOpsTask.
Adlai Hollerd71b7b02020-06-08 15:55:00 -040043 GrOpsTask(GrDrawingManager*, GrRecordingContext::Arenas, GrSurfaceProxyView, GrAuditTrail*);
Greg Danielf41b2bd2019-08-22 16:19:24 -040044 ~GrOpsTask() override;
bsalomona73239a2015-04-28 13:35:17 -070045
Greg Danielf41b2bd2019-08-22 16:19:24 -040046 GrOpsTask* asOpsTask() override { return this; }
reed@google.comac10a2d2010-12-22 21:39:39 +000047
Brian Salomon588cec72018-11-14 13:56:37 -050048 bool isEmpty() const { return fOpChains.empty(); }
Robert Phillipsf5442bb2017-04-17 14:18:34 -040049
reed@google.comac10a2d2010-12-22 21:39:39 +000050 /**
bsalomona73239a2015-04-28 13:35:17 -070051 * Empties the draw buffer of any queued up draws.
52 */
Adlai Hollerd71b7b02020-06-08 15:55:00 -040053 void endFlush(GrDrawingManager*) override;
Robert Phillipsf2361d22016-10-25 14:20:06 -040054
Robert Phillips29f38542019-10-16 09:20:25 -040055 void onPrePrepare(GrRecordingContext*) override;
bsalomona73239a2015-04-28 13:35:17 -070056 /**
bsalomondc438982016-08-31 11:53:49 -070057 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
Brian Salomon1e41f4a2016-12-07 15:05:04 -050058 * of executeOps() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -070059 */
Brian Osman407b3422017-08-22 15:01:32 -040060 void onPrepare(GrOpFlushState* flushState) override;
61 bool onExecute(GrOpFlushState* flushState) override;
bsalomona73239a2015-04-28 13:35:17 -070062
Michael Ludwigfcdd0612019-11-25 08:34:31 -050063 void addSampledTexture(GrSurfaceProxy* proxy) {
64 // This function takes a GrSurfaceProxy because all subsequent uses of the proxy do not
65 // require the specifics of GrTextureProxy, so this avoids a number of unnecessary virtual
66 // asTextureProxy() calls. However, sampling the proxy implicitly requires that the proxy
67 // be a texture. Eventually, when proxies are a unified type with flags, this can just
68 // assert that capability.
69 SkASSERT(proxy->asTextureProxy());
Greg Danielb20d7e52019-09-03 13:54:39 -040070 fSampledProxies.push_back(proxy);
71 }
72
Adlai Hollerabe45182020-11-17 09:22:13 -050073 void addOp(GrDrawingManager*, GrOp::Owner, GrTextureResolveManager, const GrCaps&);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040074
Adlai Hollerabe45182020-11-17 09:22:13 -050075 void addDrawOp(GrDrawingManager*, GrOp::Owner, const GrProcessorSet::Analysis&,
76 GrAppliedClip&&, const DstProxyView&, GrTextureResolveManager, const GrCaps&);
robertphillips9199a9f2016-07-13 07:48:43 -070077
Robert Phillips380b90c2017-08-30 07:41:07 -040078 void discard();
79
Robert Phillips44e2c5f2020-04-14 13:00:04 -040080#ifdef SK_DEBUG
Robert Phillips44e2c5f2020-04-14 13:00:04 -040081 int numClips() const override { return fNumClips; }
82 void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const override;
83#endif
Robert Phillipsc84c0302017-05-08 15:35:11 -040084
Robert Phillips438d9862019-11-14 12:46:05 -050085#if GR_TEST_UTILS
John Stiles1e0136e2020-08-12 18:44:00 -040086 void dump(bool printDependencies) const override;
87 const char* name() const final { return "Ops"; }
Robert Phillips438d9862019-11-14 12:46:05 -050088 int numOpChains() const { return fOpChains.count(); }
89 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
90#endif
91
Brian Salomon13540482018-07-09 10:31:47 -040092private:
Chris Daltonaa3cbb82019-08-21 00:01:21 -060093 bool isNoOp() const {
Chris Daltona43e2d22019-08-30 01:14:33 -060094 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
95 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
Chris Daltonaa3cbb82019-08-21 00:01:21 -060096 //
97 // TODO: We should also consider stencil load/store here. We get away with it for now
98 // because we never discard stencil buffers.
Chris Daltona43e2d22019-08-30 01:14:33 -060099 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600100 }
101
Greg Danielf41b2bd2019-08-22 16:19:24 -0400102 void deleteOps();
Robert Phillips9313aa72019-04-09 18:41:27 -0400103
Chris Dalton674f77a2019-09-30 20:49:39 -0600104 enum class StencilContent {
105 kDontCare,
106 kUserBitsCleared, // User bits: cleared
107 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
108 kPreserved
109 };
110
111 // Lets the caller specify what the content of the stencil buffer should be at the beginning
112 // of the render pass.
113 //
114 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
115 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
116 // works because renderTargetContexts are required to leave the user bits in a cleared state
117 // once finished.)
118 //
119 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
120 void setInitialStencilContent(StencilContent initialContent) {
121 fInitialStencilContent = initialContent;
122 }
123
124 // If a renderTargetContext splits its opsTask, it uses this method to guarantee stencil values
125 // get preserved across its split tasks.
126 void setMustPreserveStencil() { fMustPreserveStencil = true; }
127
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500128 // Must only be called if native color buffer clearing is enabled.
129 void setColorLoadOp(GrLoadOp op, const SkPMColor4f& color);
130 // Sets the clear color to transparent black
131 void setColorLoadOp(GrLoadOp op) {
132 static const SkPMColor4f kDefaultClearColor = {0.f, 0.f, 0.f, 0.f};
133 this->setColorLoadOp(op, kDefaultClearColor);
134 }
135
Chris Dalton6b982802019-06-27 13:53:46 -0600136 enum class CanDiscardPreviousOps : bool {
137 kYes = true,
138 kNo = false
139 };
140
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500141 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
142 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
143 // Returns true if the clear can be converted into a load op (barring device caps).
Chris Dalton6b982802019-06-27 13:53:46 -0600144 bool resetForFullscreenClear(CanDiscardPreviousOps);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500145
Brian Salomon588cec72018-11-14 13:56:37 -0500146 class OpChain {
147 public:
Herb Derbyc76d4092020-10-07 16:46:15 -0400148 OpChain(GrOp::Owner, GrProcessorSet::Analysis, GrAppliedClip*, const DstProxyView*);
Brian Salomon588cec72018-11-14 13:56:37 -0500149 ~OpChain() {
150 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
151 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400152 }
Chris Dalton8816b932017-11-29 16:48:25 -0700153
John Stiles48025a02020-07-27 10:33:50 -0400154 OpChain(const OpChain&) = delete;
155 OpChain& operator=(const OpChain&) = delete;
156 OpChain(OpChain&&) = default;
157 OpChain& operator=(OpChain&&) = default;
158
Chris Dalton1706cbf2019-05-21 19:35:29 -0600159 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400160
Brian Salomon588cec72018-11-14 13:56:37 -0500161 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400162
Brian Salomon588cec72018-11-14 13:56:37 -0500163 GrAppliedClip* appliedClip() const { return fAppliedClip; }
Greg Daniel524e28b2019-11-01 11:48:53 -0400164 const DstProxyView& dstProxyView() const { return fDstProxyView; }
Brian Salomon588cec72018-11-14 13:56:37 -0500165 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700166
Herb Derbye32e1ab2020-10-27 10:29:46 -0400167 // Deletes all the ops in the chain.
168 void deleteOps();
Brian Salomon588cec72018-11-14 13:56:37 -0500169
170 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
171 // to merge ops between the chains. Upon success the passed chain is empty.
172 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500173 bool prependChain(OpChain*, const GrCaps&, GrRecordingContext::Arenas*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500174
175 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
176 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
177 // the same op type, have different clips or dst proxies.
Herb Derbyc76d4092020-10-07 16:46:15 -0400178 GrOp::Owner appendOp(GrOp::Owner op, GrProcessorSet::Analysis,
179 const DstProxyView*, const GrAppliedClip*, const GrCaps&,
180 GrRecordingContext::Arenas*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500181
Greg Daniel15ecdf92019-08-30 15:35:23 -0400182 void setSkipExecuteFlag() { fSkipExecute = true; }
183 bool shouldExecute() const {
184 return SkToBool(this->head()) && !fSkipExecute;
185 }
186
Brian Salomon588cec72018-11-14 13:56:37 -0500187 private:
188 class List {
189 public:
190 List() = default;
Herb Derbyc76d4092020-10-07 16:46:15 -0400191 List(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500192 List(List&&);
193 List& operator=(List&& that);
194
195 bool empty() const { return !SkToBool(fHead); }
196 GrOp* head() const { return fHead.get(); }
197 GrOp* tail() const { return fTail; }
198
Herb Derbyc76d4092020-10-07 16:46:15 -0400199 GrOp::Owner popHead();
200 GrOp::Owner removeOp(GrOp* op);
201 void pushHead(GrOp::Owner op);
202 void pushTail(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500203
204 void validate() const;
205
206 private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400207 GrOp::Owner fHead{nullptr};
208 GrOp* fTail{nullptr};
Brian Salomon588cec72018-11-14 13:56:37 -0500209 };
210
211 void validate() const;
212
Greg Daniel524e28b2019-11-01 11:48:53 -0400213 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxyView&, const GrAppliedClip*,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500214 const SkRect& bounds, const GrCaps&, GrRecordingContext::Arenas*,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500215 GrAuditTrail*);
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500216 static List DoConcat(List, List, const GrCaps&, GrRecordingContext::Arenas*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500217
218 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700219 GrProcessorSet::Analysis fProcessorAnalysis;
Greg Daniel524e28b2019-11-01 11:48:53 -0400220 DstProxyView fDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500221 GrAppliedClip* fAppliedClip;
222 SkRect fBounds;
Greg Daniel15ecdf92019-08-30 15:35:23 -0400223
224 // We set this flag to true if any of the ops' proxies fail to instantiate so that we know
225 // not to try and draw the op.
226 bool fSkipExecute = false;
Brian Salomon54d212e2017-03-21 14:22:38 -0400227 };
228
Greg Danielf41b2bd2019-08-22 16:19:24 -0400229
230 bool onIsUsed(GrSurfaceProxy*) const override;
231
Chris Dalton6b498102019-08-01 14:14:52 -0600232 void handleInternalAllocationFailure() override;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500233
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400234 void gatherProxyIntervals(GrResourceAllocator*) const override;
235
Herb Derbyc76d4092020-10-07 16:46:15 -0400236 void recordOp(GrOp::Owner, GrProcessorSet::Analysis, GrAppliedClip*,
Adlai Hollerabe45182020-11-17 09:22:13 -0500237 const DstProxyView*, const GrCaps&);
Brian Salomon2790c522016-12-09 16:32:23 -0500238
Robert Phillipsee683652017-04-26 11:53:10 -0400239 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700240
Chris Dalton16a33c62019-09-24 22:19:17 -0600241 ExpectedOutcome onMakeClosed(const GrCaps& caps, SkIRect* targetUpdateBounds) override;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600242
Robert Phillips089b7c92020-08-12 11:57:07 -0400243 friend class OpsTaskTestingAccess;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600244
Greg Danielf41b2bd2019-08-22 16:19:24 -0400245 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
246 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
247 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
248 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
249 friend class GrRenderTargetContext;
250
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500251 // This is a backpointer to the Arenas that holds the memory for this GrOpsTask's ops. In the
252 // DDL case, the Arenas must have been detached from the original recording context and moved
253 // into the owning DDL.
254 GrRecordingContext::Arenas fArenas;
255 GrAuditTrail* fAuditTrail;
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400256
Greg Danielf41b2bd2019-08-22 16:19:24 -0400257 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
258 SkPMColor4f fLoadClearColor = SK_PMColor4fTRANSPARENT;
Chris Dalton674f77a2019-09-30 20:49:39 -0600259 StencilContent fInitialStencilContent = StencilContent::kDontCare;
260 bool fMustPreserveStencil = false;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400261
Brian Salomon3b8486a2020-04-21 12:43:26 -0400262 uint32_t fLastClipStackGenID = SK_InvalidUniqueID;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400263 SkIRect fLastDevClipBounds;
John Stiles9e8f4842020-07-09 11:30:05 -0400264 int fLastClipNumAnalyticElements;
csmartdalton7cdda992016-11-01 07:03:03 -0700265
Greg Danield358cbe2020-09-11 09:33:54 -0400266 GrXferBarrierFlags fRenderPassXferBarriers = GrXferBarrierFlags::kNone;
Greg Daniel9a18b082020-08-14 14:03:50 -0400267
Greg Danielf41b2bd2019-08-22 16:19:24 -0400268 // For ops/opsTask we have mean: 5 stdDev: 28
John Stiles48025a02020-07-27 10:33:50 -0400269 SkSTArray<25, OpChain> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400270
Robert Phillipsf6d7eb12017-04-26 14:55:34 -0400271 // MDB TODO: 4096 for the first allocation of the clip space will be huge overkill.
272 // Gather statistics to determine the correct size.
Herb Derby6e2c56f2020-08-01 16:26:04 -0400273 SkArenaAllocWithReset fClipAllocator{4096};
Greg Danielf41b2bd2019-08-22 16:19:24 -0400274 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400275
Greg Danielb20d7e52019-09-03 13:54:39 -0400276 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
277 // causing slow downs.
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500278 SkTArray<GrSurfaceProxy*, true> fSampledProxies;
Chris Dalton16a33c62019-09-24 22:19:17 -0600279
280 SkRect fTotalBounds = SkRect::MakeEmpty();
Greg Daniel94ed83f2019-09-27 13:05:43 -0400281 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
joshualitt6db519c2014-10-29 08:48:18 -0700282};
283
reed@google.comac10a2d2010-12-22 21:39:39 +0000284#endif