blob: bcc751d9e868eb75ad50251136810e22369dd15a [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"
Michael Ludwig28b0c5d2019-12-19 14:51:00 -050015#include "include/private/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
Brian Salomon198393b2020-04-20 15:38:21 -040036/** Observer is notified when a GrOpsTask is closed. */
37class GrOpsTaskClosedObserver {
38public:
39 virtual ~GrOpsTaskClosedObserver() = 0;
40 virtual void wasClosed(const GrOpsTask& task) = 0;
41};
42
Greg Danielf41b2bd2019-08-22 16:19:24 -040043class GrOpsTask : public GrRenderTask {
Brian Salomon54d212e2017-03-21 14:22:38 -040044private:
Greg Daniel524e28b2019-11-01 11:48:53 -040045 using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon54d212e2017-03-21 14:22:38 -040046
bsalomon@google.comf6601872012-08-28 21:11:35 +000047public:
Michael Ludwig28b0c5d2019-12-19 14:51:00 -050048 // The Arenas must outlive the GrOpsTask, either by preserving the context that owns
Michael Ludwigcb10e4d2019-12-12 13:59:20 -050049 // the pool, or by moving the pool to the DDL that takes over the GrOpsTask.
Michael Ludwig28b0c5d2019-12-19 14:51:00 -050050 GrOpsTask(GrRecordingContext::Arenas, GrSurfaceProxyView, GrAuditTrail*);
Greg Danielf41b2bd2019-08-22 16:19:24 -040051 ~GrOpsTask() override;
bsalomona73239a2015-04-28 13:35:17 -070052
Greg Danielf41b2bd2019-08-22 16:19:24 -040053 GrOpsTask* asOpsTask() override { return this; }
reed@google.comac10a2d2010-12-22 21:39:39 +000054
Brian Salomon198393b2020-04-20 15:38:21 -040055 /** Each OpsTask supports a single observer at a time. */
56 void setClosedObserver(GrOpsTaskClosedObserver* observer) { fClosedObserver = observer; }
57
Brian Salomon588cec72018-11-14 13:56:37 -050058 bool isEmpty() const { return fOpChains.empty(); }
Robert Phillipsf5442bb2017-04-17 14:18:34 -040059
reed@google.comac10a2d2010-12-22 21:39:39 +000060 /**
bsalomona73239a2015-04-28 13:35:17 -070061 * Empties the draw buffer of any queued up draws.
62 */
Chris Daltona84cacf2017-10-04 10:30:29 -060063 void endFlush() override;
Robert Phillipsf2361d22016-10-25 14:20:06 -040064
Robert Phillips29f38542019-10-16 09:20:25 -040065 void onPrePrepare(GrRecordingContext*) override;
bsalomona73239a2015-04-28 13:35:17 -070066 /**
bsalomondc438982016-08-31 11:53:49 -070067 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
Brian Salomon1e41f4a2016-12-07 15:05:04 -050068 * of executeOps() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -070069 */
Brian Osman407b3422017-08-22 15:01:32 -040070 void onPrepare(GrOpFlushState* flushState) override;
71 bool onExecute(GrOpFlushState* flushState) override;
bsalomona73239a2015-04-28 13:35:17 -070072
Michael Ludwigfcdd0612019-11-25 08:34:31 -050073 void addSampledTexture(GrSurfaceProxy* proxy) {
74 // This function takes a GrSurfaceProxy because all subsequent uses of the proxy do not
75 // require the specifics of GrTextureProxy, so this avoids a number of unnecessary virtual
76 // asTextureProxy() calls. However, sampling the proxy implicitly requires that the proxy
77 // be a texture. Eventually, when proxies are a unified type with flags, this can just
78 // assert that capability.
79 SkASSERT(proxy->asTextureProxy());
Greg Danielb20d7e52019-09-03 13:54:39 -040080 fSampledProxies.push_back(proxy);
81 }
82
Chris Dalton08755122019-08-05 16:13:47 -060083 void addOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
84 const GrCaps& caps) {
85 auto addDependency = [ textureResolveManager, &caps, this ] (
Michael Ludwigfcdd0612019-11-25 08:34:31 -050086 GrSurfaceProxy* p, GrMipMapped mipmapped) {
Chris Dalton08755122019-08-05 16:13:47 -060087 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040088 };
89
90 op->visitProxies(addDependency);
91
Chris Dalton945ee652019-01-23 09:10:36 -070092 this->recordOp(std::move(op), GrProcessorSet::EmptySetAnalysis(), nullptr, nullptr, caps);
Brian Salomon54d212e2017-03-21 14:22:38 -040093 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040094
Chris Dalton08755122019-08-05 16:13:47 -060095 void addWaitOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
96 const GrCaps& caps) {
97 fHasWaitOp = true;
98 this->addOp(std::move(op), textureResolveManager, caps);
Greg Danielcb324152019-02-25 11:36:53 -050099 }
100
Chris Dalton945ee652019-01-23 09:10:36 -0700101 void addDrawOp(std::unique_ptr<GrDrawOp> op, const GrProcessorSet::Analysis& processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400102 GrAppliedClip&& clip, const DstProxyView& dstProxyView,
Chris Dalton08755122019-08-05 16:13:47 -0600103 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
104 auto addDependency = [ textureResolveManager, &caps, this ] (
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500105 GrSurfaceProxy* p, GrMipMapped mipmapped) {
Greg Danielb20d7e52019-09-03 13:54:39 -0400106 this->addSampledTexture(p);
Chris Dalton08755122019-08-05 16:13:47 -0600107 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400108 };
109
110 op->visitProxies(addDependency);
111 clip.visitProxies(addDependency);
Greg Daniel524e28b2019-11-01 11:48:53 -0400112 if (dstProxyView.proxy()) {
113 this->addSampledTexture(dstProxyView.proxy());
114 addDependency(dstProxyView.proxy(), GrMipMapped::kNo);
Robert Phillips2739ab02018-07-27 07:35:55 -0400115 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400116
Chris Dalton945ee652019-01-23 09:10:36 -0700117 this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
Greg Daniel524e28b2019-11-01 11:48:53 -0400118 &dstProxyView, caps);
Brian Salomon69868af2016-12-22 15:42:51 -0500119 }
robertphillips9199a9f2016-07-13 07:48:43 -0700120
Robert Phillips380b90c2017-08-30 07:41:07 -0400121 void discard();
122
Robert Phillips44e2c5f2020-04-14 13:00:04 -0400123#ifdef SK_DEBUG
124 void dump(bool printDependencies) const override;
125 const char* name() const final { return "Ops"; }
126 int numClips() const override { return fNumClips; }
127 void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const override;
128#endif
Robert Phillipsc84c0302017-05-08 15:35:11 -0400129
Robert Phillips438d9862019-11-14 12:46:05 -0500130#if GR_TEST_UTILS
131 int numOpChains() const { return fOpChains.count(); }
132 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
133#endif
134
Brian Salomon13540482018-07-09 10:31:47 -0400135private:
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600136 bool isNoOp() const {
Chris Daltona43e2d22019-08-30 01:14:33 -0600137 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
138 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600139 //
140 // TODO: We should also consider stencil load/store here. We get away with it for now
141 // because we never discard stencil buffers.
Chris Daltona43e2d22019-08-30 01:14:33 -0600142 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600143 }
144
Greg Danielf41b2bd2019-08-22 16:19:24 -0400145 void deleteOps();
Robert Phillips9313aa72019-04-09 18:41:27 -0400146
Chris Dalton674f77a2019-09-30 20:49:39 -0600147 enum class StencilContent {
148 kDontCare,
149 kUserBitsCleared, // User bits: cleared
150 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
151 kPreserved
152 };
153
154 // Lets the caller specify what the content of the stencil buffer should be at the beginning
155 // of the render pass.
156 //
157 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
158 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
159 // works because renderTargetContexts are required to leave the user bits in a cleared state
160 // once finished.)
161 //
162 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
163 void setInitialStencilContent(StencilContent initialContent) {
164 fInitialStencilContent = initialContent;
165 }
166
167 // If a renderTargetContext splits its opsTask, it uses this method to guarantee stencil values
168 // get preserved across its split tasks.
169 void setMustPreserveStencil() { fMustPreserveStencil = true; }
170
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500171 // Must only be called if native color buffer clearing is enabled.
172 void setColorLoadOp(GrLoadOp op, const SkPMColor4f& color);
173 // Sets the clear color to transparent black
174 void setColorLoadOp(GrLoadOp op) {
175 static const SkPMColor4f kDefaultClearColor = {0.f, 0.f, 0.f, 0.f};
176 this->setColorLoadOp(op, kDefaultClearColor);
177 }
178
Chris Dalton6b982802019-06-27 13:53:46 -0600179 enum class CanDiscardPreviousOps : bool {
180 kYes = true,
181 kNo = false
182 };
183
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500184 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
185 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
186 // Returns true if the clear can be converted into a load op (barring device caps).
Chris Dalton6b982802019-06-27 13:53:46 -0600187 bool resetForFullscreenClear(CanDiscardPreviousOps);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500188
Brian Salomon588cec72018-11-14 13:56:37 -0500189 class OpChain {
190 public:
191 OpChain(const OpChain&) = delete;
192 OpChain& operator=(const OpChain&) = delete;
Greg Daniel524e28b2019-11-01 11:48:53 -0400193 OpChain(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
194 const DstProxyView*);
Brian Salomon588cec72018-11-14 13:56:37 -0500195
196 ~OpChain() {
197 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
198 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400199 }
Chris Dalton8816b932017-11-29 16:48:25 -0700200
Chris Dalton1706cbf2019-05-21 19:35:29 -0600201 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400202
Brian Salomon588cec72018-11-14 13:56:37 -0500203 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400204
Brian Salomon588cec72018-11-14 13:56:37 -0500205 GrAppliedClip* appliedClip() const { return fAppliedClip; }
Greg Daniel524e28b2019-11-01 11:48:53 -0400206 const DstProxyView& dstProxyView() const { return fDstProxyView; }
Brian Salomon588cec72018-11-14 13:56:37 -0500207 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700208
Brian Salomon588cec72018-11-14 13:56:37 -0500209 // Deletes all the ops in the chain via the pool.
210 void deleteOps(GrOpMemoryPool* pool);
211
212 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
213 // to merge ops between the chains. Upon success the passed chain is empty.
214 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500215 bool prependChain(OpChain*, const GrCaps&, GrRecordingContext::Arenas*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500216
217 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
218 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
219 // the same op type, have different clips or dst proxies.
Chris Dalton945ee652019-01-23 09:10:36 -0700220 std::unique_ptr<GrOp> appendOp(std::unique_ptr<GrOp> op, GrProcessorSet::Analysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400221 const DstProxyView*, const GrAppliedClip*, const GrCaps&,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500222 GrRecordingContext::Arenas*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500223
Greg Daniel15ecdf92019-08-30 15:35:23 -0400224 void setSkipExecuteFlag() { fSkipExecute = true; }
225 bool shouldExecute() const {
226 return SkToBool(this->head()) && !fSkipExecute;
227 }
228
Brian Salomon588cec72018-11-14 13:56:37 -0500229 private:
230 class List {
231 public:
232 List() = default;
233 List(std::unique_ptr<GrOp>);
234 List(List&&);
235 List& operator=(List&& that);
236
237 bool empty() const { return !SkToBool(fHead); }
238 GrOp* head() const { return fHead.get(); }
239 GrOp* tail() const { return fTail; }
240
241 std::unique_ptr<GrOp> popHead();
242 std::unique_ptr<GrOp> removeOp(GrOp* op);
243 void pushHead(std::unique_ptr<GrOp> op);
244 void pushTail(std::unique_ptr<GrOp>);
245
246 void validate() const;
247
248 private:
249 std::unique_ptr<GrOp> fHead;
250 GrOp* fTail = nullptr;
251 };
252
253 void validate() const;
254
Greg Daniel524e28b2019-11-01 11:48:53 -0400255 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxyView&, const GrAppliedClip*,
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500256 const SkRect& bounds, const GrCaps&, GrRecordingContext::Arenas*,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500257 GrAuditTrail*);
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500258 static List DoConcat(List, List, const GrCaps&, GrRecordingContext::Arenas*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500259
260 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700261 GrProcessorSet::Analysis fProcessorAnalysis;
Greg Daniel524e28b2019-11-01 11:48:53 -0400262 DstProxyView fDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500263 GrAppliedClip* fAppliedClip;
264 SkRect fBounds;
Greg Daniel15ecdf92019-08-30 15:35:23 -0400265
266 // We set this flag to true if any of the ops' proxies fail to instantiate so that we know
267 // not to try and draw the op.
268 bool fSkipExecute = false;
Brian Salomon54d212e2017-03-21 14:22:38 -0400269 };
270
Greg Danielf41b2bd2019-08-22 16:19:24 -0400271
272 bool onIsUsed(GrSurfaceProxy*) const override;
273
Chris Dalton6b498102019-08-01 14:14:52 -0600274 void handleInternalAllocationFailure() override;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500275
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400276 void gatherProxyIntervals(GrResourceAllocator*) const override;
277
Greg Daniel524e28b2019-11-01 11:48:53 -0400278 void recordOp(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
279 const DstProxyView*, const GrCaps& caps);
Brian Salomon2790c522016-12-09 16:32:23 -0500280
Robert Phillipsee683652017-04-26 11:53:10 -0400281 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700282
Chris Dalton16a33c62019-09-24 22:19:17 -0600283 ExpectedOutcome onMakeClosed(const GrCaps& caps, SkIRect* targetUpdateBounds) override;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600284
Greg Danielf41b2bd2019-08-22 16:19:24 -0400285 friend class GrRenderTargetContextPriv; // for stencil clip state. TODO: this is invasive
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600286
Greg Danielf41b2bd2019-08-22 16:19:24 -0400287 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
288 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
289 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
290 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
291 friend class GrRenderTargetContext;
292
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500293 // This is a backpointer to the Arenas that holds the memory for this GrOpsTask's ops. In the
294 // DDL case, the Arenas must have been detached from the original recording context and moved
295 // into the owning DDL.
296 GrRecordingContext::Arenas fArenas;
297 GrAuditTrail* fAuditTrail;
Brian Salomon198393b2020-04-20 15:38:21 -0400298 GrOpsTaskClosedObserver* fClosedObserver = nullptr;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400299
300 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
301 SkPMColor4f fLoadClearColor = SK_PMColor4fTRANSPARENT;
Chris Dalton674f77a2019-09-30 20:49:39 -0600302 StencilContent fInitialStencilContent = StencilContent::kDontCare;
303 bool fMustPreserveStencil = false;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400304
Brian Salomon198393b2020-04-20 15:38:21 -0400305 uint32_t fLastClipStackGenID = SK_InvalidUniqueID;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400306 SkIRect fLastDevClipBounds;
307 int fLastClipNumAnalyticFPs;
csmartdalton7cdda992016-11-01 07:03:03 -0700308
Greg Danielcb324152019-02-25 11:36:53 -0500309 // We must track if we have a wait op so that we don't delete the op when we have a full clear.
Brian Salomon198393b2020-04-20 15:38:21 -0400310 bool fHasWaitOp = false;
Greg Danielcb324152019-02-25 11:36:53 -0500311
Greg Danielf41b2bd2019-08-22 16:19:24 -0400312 // For ops/opsTask we have mean: 5 stdDev: 28
313 SkSTArray<25, OpChain, true> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400314
Robert Phillipsf6d7eb12017-04-26 14:55:34 -0400315 // MDB TODO: 4096 for the first allocation of the clip space will be huge overkill.
316 // Gather statistics to determine the correct size.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400317 SkArenaAlloc fClipAllocator{4096};
318 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400319
Greg Danielb20d7e52019-09-03 13:54:39 -0400320 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
321 // causing slow downs.
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500322 SkTArray<GrSurfaceProxy*, true> fSampledProxies;
Chris Dalton16a33c62019-09-24 22:19:17 -0600323
324 SkRect fTotalBounds = SkRect::MakeEmpty();
Greg Daniel94ed83f2019-09-27 13:05:43 -0400325 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
joshualitt6db519c2014-10-29 08:48:18 -0700326};
327
reed@google.comac10a2d2010-12-22 21:39:39 +0000328#endif