blob: d497f56f416a6590bf47b0a22414eb135d355b9a [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"
Greg Danielf41b2bd2019-08-22 16:19:24 -040015#include "include/private/SkColorData.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkTArray.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040017#include "include/private/SkTDArray.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040018#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkClipStack.h"
20#include "src/core/SkStringUtils.h"
21#include "src/core/SkTLazy.h"
22#include "src/gpu/GrAppliedClip.h"
23#include "src/gpu/GrPathRendering.h"
24#include "src/gpu/GrPrimitiveProcessor.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040025#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/ops/GrDrawOp.h"
27#include "src/gpu/ops/GrOp.h"
Scroggo97c88c22011-05-11 14:05:25 +000028
joshualitt086cee12016-01-12 06:45:24 -080029class GrAuditTrail;
Greg Danielf21bf9e2019-08-22 20:12:20 +000030class GrCaps;
Greg Danielf41b2bd2019-08-22 16:19:24 -040031class GrClearOp;
32class GrGpuBuffer;
33class GrOpMemoryPool;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040034class GrRenderTargetProxy;
Michael Ludwigd0840ec2019-12-12 09:48:38 -050035class SkArenaAlloc;
sugoi@google.com12b4e272012-12-06 20:13:11 +000036
Greg Danielf41b2bd2019-08-22 16:19:24 -040037class GrOpsTask : public GrRenderTask {
Brian Salomon54d212e2017-03-21 14:22:38 -040038private:
Greg Daniel524e28b2019-11-01 11:48:53 -040039 using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon54d212e2017-03-21 14:22:38 -040040
bsalomon@google.comf6601872012-08-28 21:11:35 +000041public:
Michael Ludwigcb10e4d2019-12-12 13:59:20 -050042 // The GrOpMemoryPool must outlive the GrOpsTask, either by preserving the context that owns
43 // the pool, or by moving the pool to the DDL that takes over the GrOpsTask.
Michael Ludwigd0840ec2019-12-12 09:48:38 -050044 GrOpsTask(GrOpMemoryPool*, SkArenaAlloc*, GrSurfaceProxyView, GrAuditTrail*);
Greg Danielf41b2bd2019-08-22 16:19:24 -040045 ~GrOpsTask() override;
bsalomona73239a2015-04-28 13:35:17 -070046
Greg Danielf41b2bd2019-08-22 16:19:24 -040047 GrOpsTask* asOpsTask() override { return this; }
reed@google.comac10a2d2010-12-22 21:39:39 +000048
Brian Salomon588cec72018-11-14 13:56:37 -050049 bool isEmpty() const { return fOpChains.empty(); }
Robert Phillipsf5442bb2017-04-17 14:18:34 -040050
reed@google.comac10a2d2010-12-22 21:39:39 +000051 /**
bsalomona73239a2015-04-28 13:35:17 -070052 * Empties the draw buffer of any queued up draws.
53 */
Chris Daltona84cacf2017-10-04 10:30:29 -060054 void endFlush() override;
Robert Phillipsf2361d22016-10-25 14:20:06 -040055
Robert Phillips29f38542019-10-16 09:20:25 -040056 void onPrePrepare(GrRecordingContext*) override;
bsalomona73239a2015-04-28 13:35:17 -070057 /**
bsalomondc438982016-08-31 11:53:49 -070058 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
Brian Salomon1e41f4a2016-12-07 15:05:04 -050059 * of executeOps() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -070060 */
Brian Osman407b3422017-08-22 15:01:32 -040061 void onPrepare(GrOpFlushState* flushState) override;
62 bool onExecute(GrOpFlushState* flushState) override;
bsalomona73239a2015-04-28 13:35:17 -070063
Michael Ludwigfcdd0612019-11-25 08:34:31 -050064 void addSampledTexture(GrSurfaceProxy* proxy) {
65 // This function takes a GrSurfaceProxy because all subsequent uses of the proxy do not
66 // require the specifics of GrTextureProxy, so this avoids a number of unnecessary virtual
67 // asTextureProxy() calls. However, sampling the proxy implicitly requires that the proxy
68 // be a texture. Eventually, when proxies are a unified type with flags, this can just
69 // assert that capability.
70 SkASSERT(proxy->asTextureProxy());
Greg Danielb20d7e52019-09-03 13:54:39 -040071 fSampledProxies.push_back(proxy);
72 }
73
Chris Dalton08755122019-08-05 16:13:47 -060074 void addOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
75 const GrCaps& caps) {
76 auto addDependency = [ textureResolveManager, &caps, this ] (
Michael Ludwigfcdd0612019-11-25 08:34:31 -050077 GrSurfaceProxy* p, GrMipMapped mipmapped) {
Chris Dalton08755122019-08-05 16:13:47 -060078 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040079 };
80
81 op->visitProxies(addDependency);
82
Chris Dalton945ee652019-01-23 09:10:36 -070083 this->recordOp(std::move(op), GrProcessorSet::EmptySetAnalysis(), nullptr, nullptr, caps);
Brian Salomon54d212e2017-03-21 14:22:38 -040084 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040085
Chris Dalton08755122019-08-05 16:13:47 -060086 void addWaitOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
87 const GrCaps& caps) {
88 fHasWaitOp = true;
89 this->addOp(std::move(op), textureResolveManager, caps);
Greg Danielcb324152019-02-25 11:36:53 -050090 }
91
Chris Dalton945ee652019-01-23 09:10:36 -070092 void addDrawOp(std::unique_ptr<GrDrawOp> op, const GrProcessorSet::Analysis& processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -040093 GrAppliedClip&& clip, const DstProxyView& dstProxyView,
Chris Dalton08755122019-08-05 16:13:47 -060094 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
95 auto addDependency = [ textureResolveManager, &caps, this ] (
Michael Ludwigfcdd0612019-11-25 08:34:31 -050096 GrSurfaceProxy* p, GrMipMapped mipmapped) {
Greg Danielb20d7e52019-09-03 13:54:39 -040097 this->addSampledTexture(p);
Chris Dalton08755122019-08-05 16:13:47 -060098 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040099 };
100
101 op->visitProxies(addDependency);
102 clip.visitProxies(addDependency);
Greg Daniel524e28b2019-11-01 11:48:53 -0400103 if (dstProxyView.proxy()) {
104 this->addSampledTexture(dstProxyView.proxy());
105 addDependency(dstProxyView.proxy(), GrMipMapped::kNo);
Robert Phillips2739ab02018-07-27 07:35:55 -0400106 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400107
Chris Dalton945ee652019-01-23 09:10:36 -0700108 this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
Greg Daniel524e28b2019-11-01 11:48:53 -0400109 &dstProxyView, caps);
Brian Salomon69868af2016-12-22 15:42:51 -0500110 }
robertphillips9199a9f2016-07-13 07:48:43 -0700111
Robert Phillips380b90c2017-08-30 07:41:07 -0400112 void discard();
113
Robert Phillips27483912018-04-20 12:43:18 -0400114 SkDEBUGCODE(void dump(bool printDependencies) const override;)
Robert Phillipsc84c0302017-05-08 15:35:11 -0400115 SkDEBUGCODE(int numClips() const override { return fNumClips; })
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500116 SkDEBUGCODE(void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const override;)
Robert Phillipsc84c0302017-05-08 15:35:11 -0400117
Robert Phillips438d9862019-11-14 12:46:05 -0500118#if GR_TEST_UTILS
119 int numOpChains() const { return fOpChains.count(); }
120 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
121#endif
122
Brian Salomon13540482018-07-09 10:31:47 -0400123private:
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600124 bool isNoOp() const {
Chris Daltona43e2d22019-08-30 01:14:33 -0600125 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
126 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600127 //
128 // TODO: We should also consider stencil load/store here. We get away with it for now
129 // because we never discard stencil buffers.
Chris Daltona43e2d22019-08-30 01:14:33 -0600130 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600131 }
132
Greg Danielf41b2bd2019-08-22 16:19:24 -0400133 void deleteOps();
Robert Phillips9313aa72019-04-09 18:41:27 -0400134
Chris Dalton674f77a2019-09-30 20:49:39 -0600135 enum class StencilContent {
136 kDontCare,
137 kUserBitsCleared, // User bits: cleared
138 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
139 kPreserved
140 };
141
142 // Lets the caller specify what the content of the stencil buffer should be at the beginning
143 // of the render pass.
144 //
145 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
146 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
147 // works because renderTargetContexts are required to leave the user bits in a cleared state
148 // once finished.)
149 //
150 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
151 void setInitialStencilContent(StencilContent initialContent) {
152 fInitialStencilContent = initialContent;
153 }
154
155 // If a renderTargetContext splits its opsTask, it uses this method to guarantee stencil values
156 // get preserved across its split tasks.
157 void setMustPreserveStencil() { fMustPreserveStencil = true; }
158
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500159 // Must only be called if native color buffer clearing is enabled.
160 void setColorLoadOp(GrLoadOp op, const SkPMColor4f& color);
161 // Sets the clear color to transparent black
162 void setColorLoadOp(GrLoadOp op) {
163 static const SkPMColor4f kDefaultClearColor = {0.f, 0.f, 0.f, 0.f};
164 this->setColorLoadOp(op, kDefaultClearColor);
165 }
166
Chris Dalton6b982802019-06-27 13:53:46 -0600167 enum class CanDiscardPreviousOps : bool {
168 kYes = true,
169 kNo = false
170 };
171
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500172 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
173 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
174 // Returns true if the clear can be converted into a load op (barring device caps).
Chris Dalton6b982802019-06-27 13:53:46 -0600175 bool resetForFullscreenClear(CanDiscardPreviousOps);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500176
Brian Salomon588cec72018-11-14 13:56:37 -0500177 class OpChain {
178 public:
179 OpChain(const OpChain&) = delete;
180 OpChain& operator=(const OpChain&) = delete;
Greg Daniel524e28b2019-11-01 11:48:53 -0400181 OpChain(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
182 const DstProxyView*);
Brian Salomon588cec72018-11-14 13:56:37 -0500183
184 ~OpChain() {
185 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
186 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400187 }
Chris Dalton8816b932017-11-29 16:48:25 -0700188
Chris Dalton1706cbf2019-05-21 19:35:29 -0600189 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400190
Brian Salomon588cec72018-11-14 13:56:37 -0500191 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400192
Brian Salomon588cec72018-11-14 13:56:37 -0500193 GrAppliedClip* appliedClip() const { return fAppliedClip; }
Greg Daniel524e28b2019-11-01 11:48:53 -0400194 const DstProxyView& dstProxyView() const { return fDstProxyView; }
Brian Salomon588cec72018-11-14 13:56:37 -0500195 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700196
Brian Salomon588cec72018-11-14 13:56:37 -0500197 // Deletes all the ops in the chain via the pool.
198 void deleteOps(GrOpMemoryPool* pool);
199
200 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
201 // to merge ops between the chains. Upon success the passed chain is empty.
202 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500203 bool prependChain(OpChain*, const GrCaps&, GrOpMemoryPool*, SkArenaAlloc*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500204
205 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
206 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
207 // the same op type, have different clips or dst proxies.
Chris Dalton945ee652019-01-23 09:10:36 -0700208 std::unique_ptr<GrOp> appendOp(std::unique_ptr<GrOp> op, GrProcessorSet::Analysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400209 const DstProxyView*, const GrAppliedClip*, const GrCaps&,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500210 GrOpMemoryPool*, SkArenaAlloc*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500211
Greg Daniel15ecdf92019-08-30 15:35:23 -0400212 void setSkipExecuteFlag() { fSkipExecute = true; }
213 bool shouldExecute() const {
214 return SkToBool(this->head()) && !fSkipExecute;
215 }
216
Brian Salomon588cec72018-11-14 13:56:37 -0500217 private:
218 class List {
219 public:
220 List() = default;
221 List(std::unique_ptr<GrOp>);
222 List(List&&);
223 List& operator=(List&& that);
224
225 bool empty() const { return !SkToBool(fHead); }
226 GrOp* head() const { return fHead.get(); }
227 GrOp* tail() const { return fTail; }
228
229 std::unique_ptr<GrOp> popHead();
230 std::unique_ptr<GrOp> removeOp(GrOp* op);
231 void pushHead(std::unique_ptr<GrOp> op);
232 void pushTail(std::unique_ptr<GrOp>);
233
234 void validate() const;
235
236 private:
237 std::unique_ptr<GrOp> fHead;
238 GrOp* fTail = nullptr;
239 };
240
241 void validate() const;
242
Greg Daniel524e28b2019-11-01 11:48:53 -0400243 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxyView&, const GrAppliedClip*,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500244 const SkRect& bounds, const GrCaps&, GrOpMemoryPool*, SkArenaAlloc*,
245 GrAuditTrail*);
246 static List DoConcat(List, List, const GrCaps&, GrOpMemoryPool*, SkArenaAlloc*,
247 GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500248
249 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700250 GrProcessorSet::Analysis fProcessorAnalysis;
Greg Daniel524e28b2019-11-01 11:48:53 -0400251 DstProxyView fDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500252 GrAppliedClip* fAppliedClip;
253 SkRect fBounds;
Greg Daniel15ecdf92019-08-30 15:35:23 -0400254
255 // We set this flag to true if any of the ops' proxies fail to instantiate so that we know
256 // not to try and draw the op.
257 bool fSkipExecute = false;
Brian Salomon54d212e2017-03-21 14:22:38 -0400258 };
259
Greg Danielf41b2bd2019-08-22 16:19:24 -0400260
261 bool onIsUsed(GrSurfaceProxy*) const override;
262
Chris Dalton6b498102019-08-01 14:14:52 -0600263 void handleInternalAllocationFailure() override;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500264
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400265 void gatherProxyIntervals(GrResourceAllocator*) const override;
266
Greg Daniel524e28b2019-11-01 11:48:53 -0400267 void recordOp(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
268 const DstProxyView*, const GrCaps& caps);
Brian Salomon2790c522016-12-09 16:32:23 -0500269
Robert Phillipsee683652017-04-26 11:53:10 -0400270 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700271
Chris Dalton16a33c62019-09-24 22:19:17 -0600272 ExpectedOutcome onMakeClosed(const GrCaps& caps, SkIRect* targetUpdateBounds) override;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600273
Greg Danielf41b2bd2019-08-22 16:19:24 -0400274 friend class GrRenderTargetContextPriv; // for stencil clip state. TODO: this is invasive
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600275
Greg Danielf41b2bd2019-08-22 16:19:24 -0400276 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
277 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
278 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
279 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
280 friend class GrRenderTargetContext;
281
282 // This is a backpointer to the GrOpMemoryPool that holds the memory for this GrOpsTask's ops.
Michael Ludwigcb10e4d2019-12-12 13:59:20 -0500283 // In the DDL case, the GrOpMemoryPool must have been detached from the original recording
284 // context and moved into the owning DDL.
285 GrOpMemoryPool* fOpMemoryPool;
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500286 SkArenaAlloc* fRecordTimeAllocator;
Michael Ludwigcb10e4d2019-12-12 13:59:20 -0500287 GrAuditTrail* fAuditTrail;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400288
289 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
290 SkPMColor4f fLoadClearColor = SK_PMColor4fTRANSPARENT;
Chris Dalton674f77a2019-09-30 20:49:39 -0600291 StencilContent fInitialStencilContent = StencilContent::kDontCare;
292 bool fMustPreserveStencil = false;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400293
294 uint32_t fLastClipStackGenID;
295 SkIRect fLastDevClipBounds;
296 int fLastClipNumAnalyticFPs;
csmartdalton7cdda992016-11-01 07:03:03 -0700297
Greg Danielcb324152019-02-25 11:36:53 -0500298 // We must track if we have a wait op so that we don't delete the op when we have a full clear.
299 bool fHasWaitOp = false;;
300
Greg Danielf41b2bd2019-08-22 16:19:24 -0400301 // For ops/opsTask we have mean: 5 stdDev: 28
302 SkSTArray<25, OpChain, true> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400303
Robert Phillipsf6d7eb12017-04-26 14:55:34 -0400304 // MDB TODO: 4096 for the first allocation of the clip space will be huge overkill.
305 // Gather statistics to determine the correct size.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400306 SkArenaAlloc fClipAllocator{4096};
307 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400308
Greg Danielb20d7e52019-09-03 13:54:39 -0400309 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
310 // causing slow downs.
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500311 SkTArray<GrSurfaceProxy*, true> fSampledProxies;
Chris Dalton16a33c62019-09-24 22:19:17 -0600312
313 SkRect fTotalBounds = SkRect::MakeEmpty();
Greg Daniel94ed83f2019-09-27 13:05:43 -0400314 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
joshualitt6db519c2014-10-29 08:48:18 -0700315};
316
reed@google.comac10a2d2010-12-22 21:39:39 +0000317#endif