blob: e90ff8b10dee80d3cd3f3d56abd83d553ff7dae3 [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"
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"
Adlai Holler93439d92021-01-26 09:20:39 -050020#include "src/core/SkSpan.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkStringUtils.h"
22#include "src/core/SkTLazy.h"
23#include "src/gpu/GrAppliedClip.h"
Robert Phillips787fd9d2021-03-22 14:48:09 -040024#include "src/gpu/GrGeometryProcessor.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;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040033class GrRenderTargetProxy;
sugoi@google.com12b4e272012-12-06 20:13:11 +000034
Greg Danielf41b2bd2019-08-22 16:19:24 -040035class GrOpsTask : public GrRenderTask {
Brian Salomon54d212e2017-03-21 14:22:38 -040036private:
Greg Daniel524e28b2019-11-01 11:48:53 -040037 using DstProxyView = GrXferProcessor::DstProxyView;
Brian Salomon54d212e2017-03-21 14:22:38 -040038
bsalomon@google.comf6601872012-08-28 21:11:35 +000039public:
Herb Derby0b1228d2021-04-05 18:38:35 -040040 // Manage the arenas life time by maintaining are reference to it.
41 GrOpsTask(GrDrawingManager*, GrSurfaceProxyView, GrAuditTrail*, sk_sp<GrArenas>);
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 */
Adlai Hollerd71b7b02020-06-08 15:55:00 -040051 void endFlush(GrDrawingManager*) 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
Adlai Holler93439d92021-01-26 09:20:39 -050056 * of onExecute() 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
Michael Ludwigfcdd0612019-11-25 08:34:31 -050061 void addSampledTexture(GrSurfaceProxy* proxy) {
62 // This function takes a GrSurfaceProxy because all subsequent uses of the proxy do not
63 // require the specifics of GrTextureProxy, so this avoids a number of unnecessary virtual
64 // asTextureProxy() calls. However, sampling the proxy implicitly requires that the proxy
65 // be a texture. Eventually, when proxies are a unified type with flags, this can just
66 // assert that capability.
67 SkASSERT(proxy->asTextureProxy());
Greg Danielb20d7e52019-09-03 13:54:39 -040068 fSampledProxies.push_back(proxy);
69 }
70
Adlai Hollerabe45182020-11-17 09:22:13 -050071 void addOp(GrDrawingManager*, GrOp::Owner, GrTextureResolveManager, const GrCaps&);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040072
Adlai Hollerabe45182020-11-17 09:22:13 -050073 void addDrawOp(GrDrawingManager*, GrOp::Owner, const GrProcessorSet::Analysis&,
74 GrAppliedClip&&, const DstProxyView&, GrTextureResolveManager, const GrCaps&);
robertphillips9199a9f2016-07-13 07:48:43 -070075
Robert Phillips380b90c2017-08-30 07:41:07 -040076 void discard();
77
Brian Salomon590f5672020-12-16 11:44:47 -050078 enum class CanDiscardPreviousOps : bool {
79 kYes = true,
80 kNo = false
81 };
82
83 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
84 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
85 // Returns true if the clear can be converted into a load op (barring device caps).
86 bool resetForFullscreenClear(CanDiscardPreviousOps);
87
88 // Must only be called if native color buffer clearing is enabled.
89 void setColorLoadOp(GrLoadOp op, std::array<float, 4> color = {0, 0, 0, 0});
90
Adlai Holler93439d92021-01-26 09:20:39 -050091 // Merge as many opsTasks as possible from the head of 'tasks'. They should all be
92 // renderPass compatible. Return the number of tasks merged into 'this'.
93 int mergeFrom(SkSpan<const sk_sp<GrRenderTask>> tasks);
94
Robert Phillips44e2c5f2020-04-14 13:00:04 -040095#ifdef SK_DEBUG
Robert Phillips44e2c5f2020-04-14 13:00:04 -040096 int numClips() const override { return fNumClips; }
97 void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const override;
98#endif
Robert Phillipsc84c0302017-05-08 15:35:11 -040099
Robert Phillips438d9862019-11-14 12:46:05 -0500100#if GR_TEST_UTILS
Robert Phillips047d5bb2021-01-08 13:39:19 -0500101 void dump(const SkString& label,
102 SkString indent,
103 bool printDependencies,
104 bool close) const override;
John Stiles1e0136e2020-08-12 18:44:00 -0400105 const char* name() const final { return "Ops"; }
Robert Phillips438d9862019-11-14 12:46:05 -0500106 int numOpChains() const { return fOpChains.count(); }
107 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
108#endif
109
Brian Salomon13540482018-07-09 10:31:47 -0400110private:
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600111 bool isNoOp() const {
Chris Daltona43e2d22019-08-30 01:14:33 -0600112 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
113 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600114 //
115 // TODO: We should also consider stencil load/store here. We get away with it for now
116 // because we never discard stencil buffers.
Chris Daltona43e2d22019-08-30 01:14:33 -0600117 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600118 }
119
Greg Danielf41b2bd2019-08-22 16:19:24 -0400120 void deleteOps();
Robert Phillips9313aa72019-04-09 18:41:27 -0400121
Chris Dalton674f77a2019-09-30 20:49:39 -0600122 enum class StencilContent {
123 kDontCare,
124 kUserBitsCleared, // User bits: cleared
125 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
126 kPreserved
127 };
128
129 // Lets the caller specify what the content of the stencil buffer should be at the beginning
130 // of the render pass.
131 //
132 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
133 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
134 // works because renderTargetContexts are required to leave the user bits in a cleared state
135 // once finished.)
136 //
137 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
138 void setInitialStencilContent(StencilContent initialContent) {
139 fInitialStencilContent = initialContent;
140 }
141
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500142 // If a surfaceDrawContext splits its opsTask, it uses this method to guarantee stencil values
Chris Dalton674f77a2019-09-30 20:49:39 -0600143 // get preserved across its split tasks.
144 void setMustPreserveStencil() { fMustPreserveStencil = true; }
145
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.
Chris Daltonf8d75c62021-04-02 11:24:58 -0600173 bool prependChain(OpChain*, const GrCaps&, SkArenaAlloc* opsTaskArena, 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.
Chris Daltonf8d75c62021-04-02 11:24:58 -0600178 GrOp::Owner appendOp(GrOp::Owner op, GrProcessorSet::Analysis, const DstProxyView*,
179 const GrAppliedClip*, const GrCaps&, SkArenaAlloc* opsTaskArena,
180 GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500181
Greg Daniel15ecdf92019-08-30 15:35:23 -0400182 bool shouldExecute() const {
Adlai Holler19fd5142021-03-08 10:19:30 -0700183 return SkToBool(this->head());
Greg Daniel15ecdf92019-08-30 15:35:23 -0400184 }
185
Brian Salomon588cec72018-11-14 13:56:37 -0500186 private:
187 class List {
188 public:
189 List() = default;
Herb Derbyc76d4092020-10-07 16:46:15 -0400190 List(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500191 List(List&&);
192 List& operator=(List&& that);
193
194 bool empty() const { return !SkToBool(fHead); }
195 GrOp* head() const { return fHead.get(); }
196 GrOp* tail() const { return fTail; }
197
Herb Derbyc76d4092020-10-07 16:46:15 -0400198 GrOp::Owner popHead();
199 GrOp::Owner removeOp(GrOp* op);
200 void pushHead(GrOp::Owner op);
201 void pushTail(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500202
203 void validate() const;
204
205 private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400206 GrOp::Owner fHead{nullptr};
207 GrOp* fTail{nullptr};
Brian Salomon588cec72018-11-14 13:56:37 -0500208 };
209
210 void validate() const;
211
Greg Daniel524e28b2019-11-01 11:48:53 -0400212 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxyView&, const GrAppliedClip*,
Chris Daltonf8d75c62021-04-02 11:24:58 -0600213 const SkRect& bounds, const GrCaps&, SkArenaAlloc* opsTaskArena,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500214 GrAuditTrail*);
Chris Daltonf8d75c62021-04-02 11:24:58 -0600215 static List DoConcat(List, List, const GrCaps&, SkArenaAlloc* opsTaskArena, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500216
217 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700218 GrProcessorSet::Analysis fProcessorAnalysis;
Greg Daniel524e28b2019-11-01 11:48:53 -0400219 DstProxyView fDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500220 GrAppliedClip* fAppliedClip;
221 SkRect fBounds;
Brian Salomon54d212e2017-03-21 14:22:38 -0400222 };
223
Brian Salomond63638b2021-03-05 14:00:07 -0500224 void onCanSkip() override;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400225
226 bool onIsUsed(GrSurfaceProxy*) const override;
227
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400228 void gatherProxyIntervals(GrResourceAllocator*) const override;
229
Herb Derbyc76d4092020-10-07 16:46:15 -0400230 void recordOp(GrOp::Owner, GrProcessorSet::Analysis, GrAppliedClip*,
Adlai Hollerabe45182020-11-17 09:22:13 -0500231 const DstProxyView*, const GrCaps&);
Brian Salomon2790c522016-12-09 16:32:23 -0500232
Robert Phillipsee683652017-04-26 11:53:10 -0400233 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700234
Chris Dalton16a33c62019-09-24 22:19:17 -0600235 ExpectedOutcome onMakeClosed(const GrCaps& caps, SkIRect* targetUpdateBounds) override;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600236
Adlai Holler9c3b6df2021-02-08 11:31:57 -0500237 // Remove all ops, proxies, etc. Used in the merging algorithm when tasks can be skipped.
238 void reset();
239
Robert Phillips089b7c92020-08-12 11:57:07 -0400240 friend class OpsTaskTestingAccess;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600241
Greg Danielf41b2bd2019-08-22 16:19:24 -0400242 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
243 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
244 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
245 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
Brian Salomoneebe7352020-12-09 16:37:04 -0500246 friend class GrSurfaceDrawContext;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400247
Chris Daltonf8d75c62021-04-02 11:24:58 -0600248 GrAuditTrail* fAuditTrail;
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400249
Chris Dalton2517ce32021-04-13 00:21:15 -0600250 bool fUsesMSAASurface;
Brian Salomon982127b2021-01-21 10:43:35 -0500251 GrSwizzle fTargetSwizzle;
252 GrSurfaceOrigin fTargetOrigin;
253
Greg Danielf41b2bd2019-08-22 16:19:24 -0400254 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
Brian Salomon07bc9a22020-12-02 13:37:16 -0500255 std::array<float, 4> fLoadClearColor = {0, 0, 0, 0};
Chris Dalton674f77a2019-09-30 20:49:39 -0600256 StencilContent fInitialStencilContent = StencilContent::kDontCare;
257 bool fMustPreserveStencil = false;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400258
Brian Salomon3b8486a2020-04-21 12:43:26 -0400259 uint32_t fLastClipStackGenID = SK_InvalidUniqueID;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400260 SkIRect fLastDevClipBounds;
John Stiles9e8f4842020-07-09 11:30:05 -0400261 int fLastClipNumAnalyticElements;
csmartdalton7cdda992016-11-01 07:03:03 -0700262
Greg Danield358cbe2020-09-11 09:33:54 -0400263 GrXferBarrierFlags fRenderPassXferBarriers = GrXferBarrierFlags::kNone;
Greg Daniel9a18b082020-08-14 14:03:50 -0400264
Greg Danielf41b2bd2019-08-22 16:19:24 -0400265 // For ops/opsTask we have mean: 5 stdDev: 28
John Stiles48025a02020-07-27 10:33:50 -0400266 SkSTArray<25, OpChain> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400267
Herb Derby220dd692021-04-06 19:53:54 +0000268 // MDB TODO: 4096 for the first allocation may be huge overkill. Gather statistics to determine
269 // the correct size.
270 SkSTArray<1, std::unique_ptr<SkArenaAlloc>> fAllocators;
Herb Derby0b1228d2021-04-05 18:38:35 -0400271 sk_sp<GrArenas> fArenas;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400272 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400273
Greg Danielb20d7e52019-09-03 13:54:39 -0400274 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
275 // causing slow downs.
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500276 SkTArray<GrSurfaceProxy*, true> fSampledProxies;
Chris Dalton16a33c62019-09-24 22:19:17 -0600277
278 SkRect fTotalBounds = SkRect::MakeEmpty();
Greg Daniel94ed83f2019-09-27 13:05:43 -0400279 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
joshualitt6db519c2014-10-29 08:48:18 -0700280};
281
reed@google.comac10a2d2010-12-22 21:39:39 +0000282#endif