blob: fd6950909c34e3a9b4f1363a3a04a496287690d1 [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;
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:
Greg Daniel16f5c652019-10-29 11:26:01 -040041 GrOpsTask(sk_sp<GrOpMemoryPool>, GrSurfaceProxyView, GrAuditTrail*);
Greg Danielf41b2bd2019-08-22 16:19:24 -040042 ~GrOpsTask() override;
bsalomona73239a2015-04-28 13:35:17 -070043
Greg Danielf41b2bd2019-08-22 16:19:24 -040044 GrOpsTask* asOpsTask() override { return this; }
reed@google.comac10a2d2010-12-22 21:39:39 +000045
Brian Salomon588cec72018-11-14 13:56:37 -050046 bool isEmpty() const { return fOpChains.empty(); }
Robert Phillipsf5442bb2017-04-17 14:18:34 -040047
reed@google.comac10a2d2010-12-22 21:39:39 +000048 /**
bsalomona73239a2015-04-28 13:35:17 -070049 * Empties the draw buffer of any queued up draws.
50 */
Chris Daltona84cacf2017-10-04 10:30:29 -060051 void endFlush() override;
Robert Phillipsf2361d22016-10-25 14:20:06 -040052
Robert Phillips29f38542019-10-16 09:20:25 -040053 void onPrePrepare(GrRecordingContext*) override;
bsalomona73239a2015-04-28 13:35:17 -070054 /**
bsalomondc438982016-08-31 11:53:49 -070055 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
Brian Salomon1e41f4a2016-12-07 15:05:04 -050056 * of executeOps() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -070057 */
Brian Osman407b3422017-08-22 15:01:32 -040058 void onPrepare(GrOpFlushState* flushState) override;
59 bool onExecute(GrOpFlushState* flushState) override;
bsalomona73239a2015-04-28 13:35:17 -070060
Greg Danielb20d7e52019-09-03 13:54:39 -040061 void addSampledTexture(GrTextureProxy* proxy) {
62 fSampledProxies.push_back(proxy);
63 }
64
Chris Dalton08755122019-08-05 16:13:47 -060065 void addOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
66 const GrCaps& caps) {
67 auto addDependency = [ textureResolveManager, &caps, this ] (
Greg Danieldcf9ca12019-08-27 14:30:21 -040068 GrTextureProxy* p, GrMipMapped mipmapped) {
Chris Dalton08755122019-08-05 16:13:47 -060069 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040070 };
71
72 op->visitProxies(addDependency);
73
Chris Dalton945ee652019-01-23 09:10:36 -070074 this->recordOp(std::move(op), GrProcessorSet::EmptySetAnalysis(), nullptr, nullptr, caps);
Brian Salomon54d212e2017-03-21 14:22:38 -040075 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040076
Chris Dalton08755122019-08-05 16:13:47 -060077 void addWaitOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
78 const GrCaps& caps) {
79 fHasWaitOp = true;
80 this->addOp(std::move(op), textureResolveManager, caps);
Greg Danielcb324152019-02-25 11:36:53 -050081 }
82
Chris Dalton945ee652019-01-23 09:10:36 -070083 void addDrawOp(std::unique_ptr<GrDrawOp> op, const GrProcessorSet::Analysis& processorAnalysis,
Greg Daniel524e28b2019-11-01 11:48:53 -040084 GrAppliedClip&& clip, const DstProxyView& dstProxyView,
Chris Dalton08755122019-08-05 16:13:47 -060085 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
86 auto addDependency = [ textureResolveManager, &caps, this ] (
Greg Danieldcf9ca12019-08-27 14:30:21 -040087 GrTextureProxy* p, GrMipMapped mipmapped) {
Greg Danielb20d7e52019-09-03 13:54:39 -040088 this->addSampledTexture(p);
Chris Dalton08755122019-08-05 16:13:47 -060089 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040090 };
91
92 op->visitProxies(addDependency);
93 clip.visitProxies(addDependency);
Greg Daniel524e28b2019-11-01 11:48:53 -040094 if (dstProxyView.proxy()) {
95 this->addSampledTexture(dstProxyView.proxy());
96 addDependency(dstProxyView.proxy(), GrMipMapped::kNo);
Robert Phillips2739ab02018-07-27 07:35:55 -040097 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040098
Chris Dalton945ee652019-01-23 09:10:36 -070099 this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
Greg Daniel524e28b2019-11-01 11:48:53 -0400100 &dstProxyView, caps);
Brian Salomon69868af2016-12-22 15:42:51 -0500101 }
robertphillips9199a9f2016-07-13 07:48:43 -0700102
Robert Phillips380b90c2017-08-30 07:41:07 -0400103 void discard();
104
Robert Phillips27483912018-04-20 12:43:18 -0400105 SkDEBUGCODE(void dump(bool printDependencies) const override;)
Robert Phillipsc84c0302017-05-08 15:35:11 -0400106 SkDEBUGCODE(int numClips() const override { return fNumClips; })
Greg Danieldcf9ca12019-08-27 14:30:21 -0400107 SkDEBUGCODE(void visitProxies_debugOnly(const VisitSurfaceProxyFunc&) const override;)
Robert Phillipsc84c0302017-05-08 15:35:11 -0400108
Robert Phillips438d9862019-11-14 12:46:05 -0500109#if GR_TEST_UTILS
110 int numOpChains() const { return fOpChains.count(); }
111 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
112#endif
113
Brian Salomon13540482018-07-09 10:31:47 -0400114private:
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600115 bool isNoOp() const {
Chris Daltona43e2d22019-08-30 01:14:33 -0600116 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
117 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600118 //
119 // TODO: We should also consider stencil load/store here. We get away with it for now
120 // because we never discard stencil buffers.
Chris Daltona43e2d22019-08-30 01:14:33 -0600121 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600122 }
123
Greg Danielf41b2bd2019-08-22 16:19:24 -0400124 void deleteOps();
Robert Phillips9313aa72019-04-09 18:41:27 -0400125
Chris Dalton674f77a2019-09-30 20:49:39 -0600126 enum class StencilContent {
127 kDontCare,
128 kUserBitsCleared, // User bits: cleared
129 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
130 kPreserved
131 };
132
133 // Lets the caller specify what the content of the stencil buffer should be at the beginning
134 // of the render pass.
135 //
136 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
137 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
138 // works because renderTargetContexts are required to leave the user bits in a cleared state
139 // once finished.)
140 //
141 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
142 void setInitialStencilContent(StencilContent initialContent) {
143 fInitialStencilContent = initialContent;
144 }
145
146 // If a renderTargetContext splits its opsTask, it uses this method to guarantee stencil values
147 // get preserved across its split tasks.
148 void setMustPreserveStencil() { fMustPreserveStencil = true; }
149
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500150 // Must only be called if native color buffer clearing is enabled.
151 void setColorLoadOp(GrLoadOp op, const SkPMColor4f& color);
152 // Sets the clear color to transparent black
153 void setColorLoadOp(GrLoadOp op) {
154 static const SkPMColor4f kDefaultClearColor = {0.f, 0.f, 0.f, 0.f};
155 this->setColorLoadOp(op, kDefaultClearColor);
156 }
157
Chris Dalton6b982802019-06-27 13:53:46 -0600158 enum class CanDiscardPreviousOps : bool {
159 kYes = true,
160 kNo = false
161 };
162
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500163 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
164 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
165 // Returns true if the clear can be converted into a load op (barring device caps).
Chris Dalton6b982802019-06-27 13:53:46 -0600166 bool resetForFullscreenClear(CanDiscardPreviousOps);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500167
Brian Salomon588cec72018-11-14 13:56:37 -0500168 class OpChain {
169 public:
170 OpChain(const OpChain&) = delete;
171 OpChain& operator=(const OpChain&) = delete;
Greg Daniel524e28b2019-11-01 11:48:53 -0400172 OpChain(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
173 const DstProxyView*);
Brian Salomon588cec72018-11-14 13:56:37 -0500174
175 ~OpChain() {
176 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
177 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400178 }
Chris Dalton8816b932017-11-29 16:48:25 -0700179
Chris Dalton1706cbf2019-05-21 19:35:29 -0600180 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400181
Brian Salomon588cec72018-11-14 13:56:37 -0500182 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400183
Brian Salomon588cec72018-11-14 13:56:37 -0500184 GrAppliedClip* appliedClip() const { return fAppliedClip; }
Greg Daniel524e28b2019-11-01 11:48:53 -0400185 const DstProxyView& dstProxyView() const { return fDstProxyView; }
Brian Salomon588cec72018-11-14 13:56:37 -0500186 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700187
Brian Salomon588cec72018-11-14 13:56:37 -0500188 // Deletes all the ops in the chain via the pool.
189 void deleteOps(GrOpMemoryPool* pool);
190
191 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
192 // to merge ops between the chains. Upon success the passed chain is empty.
193 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
194 bool prependChain(OpChain*, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
195
196 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
197 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
198 // the same op type, have different clips or dst proxies.
Chris Dalton945ee652019-01-23 09:10:36 -0700199 std::unique_ptr<GrOp> appendOp(std::unique_ptr<GrOp> op, GrProcessorSet::Analysis,
Greg Daniel524e28b2019-11-01 11:48:53 -0400200 const DstProxyView*, const GrAppliedClip*, const GrCaps&,
Chris Dalton945ee652019-01-23 09:10:36 -0700201 GrOpMemoryPool*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500202
Greg Daniel15ecdf92019-08-30 15:35:23 -0400203 void setSkipExecuteFlag() { fSkipExecute = true; }
204 bool shouldExecute() const {
205 return SkToBool(this->head()) && !fSkipExecute;
206 }
207
Brian Salomon588cec72018-11-14 13:56:37 -0500208 private:
209 class List {
210 public:
211 List() = default;
212 List(std::unique_ptr<GrOp>);
213 List(List&&);
214 List& operator=(List&& that);
215
216 bool empty() const { return !SkToBool(fHead); }
217 GrOp* head() const { return fHead.get(); }
218 GrOp* tail() const { return fTail; }
219
220 std::unique_ptr<GrOp> popHead();
221 std::unique_ptr<GrOp> removeOp(GrOp* op);
222 void pushHead(std::unique_ptr<GrOp> op);
223 void pushTail(std::unique_ptr<GrOp>);
224
225 void validate() const;
226
227 private:
228 std::unique_ptr<GrOp> fHead;
229 GrOp* fTail = nullptr;
230 };
231
232 void validate() const;
233
Greg Daniel524e28b2019-11-01 11:48:53 -0400234 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxyView&, const GrAppliedClip*,
Chris Dalton945ee652019-01-23 09:10:36 -0700235 const SkRect& bounds, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700236 static List DoConcat(List, List, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500237
238 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700239 GrProcessorSet::Analysis fProcessorAnalysis;
Greg Daniel524e28b2019-11-01 11:48:53 -0400240 DstProxyView fDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500241 GrAppliedClip* fAppliedClip;
242 SkRect fBounds;
Greg Daniel15ecdf92019-08-30 15:35:23 -0400243
244 // We set this flag to true if any of the ops' proxies fail to instantiate so that we know
245 // not to try and draw the op.
246 bool fSkipExecute = false;
Brian Salomon54d212e2017-03-21 14:22:38 -0400247 };
248
Greg Danielf41b2bd2019-08-22 16:19:24 -0400249
250 bool onIsUsed(GrSurfaceProxy*) const override;
251
Chris Dalton6b498102019-08-01 14:14:52 -0600252 void handleInternalAllocationFailure() override;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500253
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400254 void gatherProxyIntervals(GrResourceAllocator*) const override;
255
Greg Daniel524e28b2019-11-01 11:48:53 -0400256 void recordOp(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*,
257 const DstProxyView*, const GrCaps& caps);
Brian Salomon2790c522016-12-09 16:32:23 -0500258
Robert Phillipsee683652017-04-26 11:53:10 -0400259 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700260
Chris Dalton16a33c62019-09-24 22:19:17 -0600261 ExpectedOutcome onMakeClosed(const GrCaps& caps, SkIRect* targetUpdateBounds) override;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600262
Greg Danielf41b2bd2019-08-22 16:19:24 -0400263 friend class GrRenderTargetContextPriv; // for stencil clip state. TODO: this is invasive
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600264
Greg Danielf41b2bd2019-08-22 16:19:24 -0400265 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
266 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
267 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
268 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
269 friend class GrRenderTargetContext;
270
271 // This is a backpointer to the GrOpMemoryPool that holds the memory for this GrOpsTask's ops.
272 // In the DDL case, these back pointers keep the DDL's GrOpMemoryPool alive as long as its
273 // constituent GrOpsTask survives.
274 sk_sp<GrOpMemoryPool> fOpMemoryPool;
275 GrAuditTrail* fAuditTrail;
276
277 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
278 SkPMColor4f fLoadClearColor = SK_PMColor4fTRANSPARENT;
Chris Dalton674f77a2019-09-30 20:49:39 -0600279 StencilContent fInitialStencilContent = StencilContent::kDontCare;
280 bool fMustPreserveStencil = false;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400281
282 uint32_t fLastClipStackGenID;
283 SkIRect fLastDevClipBounds;
284 int fLastClipNumAnalyticFPs;
csmartdalton7cdda992016-11-01 07:03:03 -0700285
Greg Danielcb324152019-02-25 11:36:53 -0500286 // We must track if we have a wait op so that we don't delete the op when we have a full clear.
287 bool fHasWaitOp = false;;
288
Greg Danielf41b2bd2019-08-22 16:19:24 -0400289 // For ops/opsTask we have mean: 5 stdDev: 28
290 SkSTArray<25, OpChain, true> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400291
Robert Phillipsf6d7eb12017-04-26 14:55:34 -0400292 // MDB TODO: 4096 for the first allocation of the clip space will be huge overkill.
293 // Gather statistics to determine the correct size.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400294 SkArenaAlloc fClipAllocator{4096};
295 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400296
Greg Danielb20d7e52019-09-03 13:54:39 -0400297 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
298 // causing slow downs.
299 SkTArray<GrTextureProxy*, true> fSampledProxies;
Chris Dalton16a33c62019-09-24 22:19:17 -0600300
301 SkRect fTotalBounds = SkRect::MakeEmpty();
Greg Daniel94ed83f2019-09-27 13:05:43 -0400302 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
joshualitt6db519c2014-10-29 08:48:18 -0700303};
304
reed@google.comac10a2d2010-12-22 21:39:39 +0000305#endif