blob: 39a58a6f3ec89132122fe9cd31850e2d66a12038 [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"
Brian Osmana5842bc2021-05-11 13:41:46 -040013#include "include/core/SkSpan.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkStrokeRec.h"
15#include "include/core/SkTypes.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040016#include "include/gpu/GrRecordingContext.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"
John Stiles87960de2021-06-03 16:44:53 -040024#include "src/gpu/GrDstProxyView.h"
Robert Phillips787fd9d2021-03-22 14:48:09 -040025#include "src/gpu/GrGeometryProcessor.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 {
bsalomon@google.comf6601872012-08-28 21:11:35 +000037public:
Herb Derby0b1228d2021-04-05 18:38:35 -040038 // Manage the arenas life time by maintaining are reference to it.
39 GrOpsTask(GrDrawingManager*, GrSurfaceProxyView, GrAuditTrail*, sk_sp<GrArenas>);
Greg Danielf41b2bd2019-08-22 16:19:24 -040040 ~GrOpsTask() override;
bsalomona73239a2015-04-28 13:35:17 -070041
Greg Danielf41b2bd2019-08-22 16:19:24 -040042 GrOpsTask* asOpsTask() override { return this; }
reed@google.comac10a2d2010-12-22 21:39:39 +000043
Brian Salomon588cec72018-11-14 13:56:37 -050044 bool isEmpty() const { return fOpChains.empty(); }
Chris Dalton5dfb3f42021-04-30 17:16:12 -060045 bool usesMSAASurface() const { return fUsesMSAASurface; }
Robert Phillipsf5442bb2017-04-17 14:18:34 -040046
reed@google.comac10a2d2010-12-22 21:39:39 +000047 /**
bsalomona73239a2015-04-28 13:35:17 -070048 * Empties the draw buffer of any queued up draws.
49 */
Adlai Hollerd71b7b02020-06-08 15:55:00 -040050 void endFlush(GrDrawingManager*) override;
Robert Phillipsf2361d22016-10-25 14:20:06 -040051
Robert Phillips29f38542019-10-16 09:20:25 -040052 void onPrePrepare(GrRecordingContext*) override;
bsalomona73239a2015-04-28 13:35:17 -070053 /**
bsalomondc438982016-08-31 11:53:49 -070054 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
Adlai Holler93439d92021-01-26 09:20:39 -050055 * of onExecute() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -070056 */
Brian Osman407b3422017-08-22 15:01:32 -040057 void onPrepare(GrOpFlushState* flushState) override;
58 bool onExecute(GrOpFlushState* flushState) override;
bsalomona73239a2015-04-28 13:35:17 -070059
Michael Ludwigfcdd0612019-11-25 08:34:31 -050060 void addSampledTexture(GrSurfaceProxy* proxy) {
61 // This function takes a GrSurfaceProxy because all subsequent uses of the proxy do not
62 // require the specifics of GrTextureProxy, so this avoids a number of unnecessary virtual
63 // asTextureProxy() calls. However, sampling the proxy implicitly requires that the proxy
64 // be a texture. Eventually, when proxies are a unified type with flags, this can just
65 // assert that capability.
66 SkASSERT(proxy->asTextureProxy());
Greg Danielb20d7e52019-09-03 13:54:39 -040067 fSampledProxies.push_back(proxy);
68 }
69
Adlai Hollerabe45182020-11-17 09:22:13 -050070 void addOp(GrDrawingManager*, GrOp::Owner, GrTextureResolveManager, const GrCaps&);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040071
Chris Daltonb4403a92021-05-27 14:59:27 -060072 void addDrawOp(GrDrawingManager*, GrOp::Owner, bool usesMSAA, const GrProcessorSet::Analysis&,
John Stiles52cb1d02021-06-02 11:58:05 -040073 GrAppliedClip&&, const GrDstProxyView&, GrTextureResolveManager, const GrCaps&);
robertphillips9199a9f2016-07-13 07:48:43 -070074
Robert Phillips380b90c2017-08-30 07:41:07 -040075 void discard();
76
Brian Salomon590f5672020-12-16 11:44:47 -050077 enum class CanDiscardPreviousOps : bool {
78 kYes = true,
79 kNo = false
80 };
81
82 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
83 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
84 // Returns true if the clear can be converted into a load op (barring device caps).
85 bool resetForFullscreenClear(CanDiscardPreviousOps);
86
87 // Must only be called if native color buffer clearing is enabled.
88 void setColorLoadOp(GrLoadOp op, std::array<float, 4> color = {0, 0, 0, 0});
89
Adlai Holler93439d92021-01-26 09:20:39 -050090 // Merge as many opsTasks as possible from the head of 'tasks'. They should all be
91 // renderPass compatible. Return the number of tasks merged into 'this'.
92 int mergeFrom(SkSpan<const sk_sp<GrRenderTask>> tasks);
93
Robert Phillips44e2c5f2020-04-14 13:00:04 -040094#ifdef SK_DEBUG
Robert Phillips44e2c5f2020-04-14 13:00:04 -040095 int numClips() const override { return fNumClips; }
Robert Phillips294723d2021-06-17 09:23:58 -040096 void visitProxies_debugOnly(const GrVisitProxyFunc&) const override;
Robert Phillips44e2c5f2020-04-14 13:00:04 -040097#endif
Robert Phillipsc84c0302017-05-08 15:35:11 -040098
Robert Phillips438d9862019-11-14 12:46:05 -050099#if GR_TEST_UTILS
Robert Phillips047d5bb2021-01-08 13:39:19 -0500100 void dump(const SkString& label,
101 SkString indent,
102 bool printDependencies,
103 bool close) const override;
John Stiles1e0136e2020-08-12 18:44:00 -0400104 const char* name() const final { return "Ops"; }
Robert Phillips438d9862019-11-14 12:46:05 -0500105 int numOpChains() const { return fOpChains.count(); }
106 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
107#endif
108
Brian Salomon13540482018-07-09 10:31:47 -0400109private:
Greg Daniel9a2d3d12021-06-23 22:36:06 +0000110 bool isNoOp() const {
Chris Daltona43e2d22019-08-30 01:14:33 -0600111 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
112 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
Greg Daniel9a2d3d12021-06-23 22:36:06 +0000113 //
114 // TODO: We should also consider stencil load/store here. We get away with it for now
115 // because we never discard stencil buffers.
Chris Daltona43e2d22019-08-30 01:14:33 -0600116 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600117 }
118
Greg Danielf41b2bd2019-08-22 16:19:24 -0400119 void deleteOps();
Robert Phillips9313aa72019-04-09 18:41:27 -0400120
Chris Dalton674f77a2019-09-30 20:49:39 -0600121 enum class StencilContent {
122 kDontCare,
123 kUserBitsCleared, // User bits: cleared
124 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
125 kPreserved
126 };
127
128 // Lets the caller specify what the content of the stencil buffer should be at the beginning
129 // of the render pass.
130 //
131 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
132 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
John Stiles0fbc6a32021-06-04 14:40:57 -0400133 // works because SurfaceDrawContexts are required to leave the user bits in a cleared state
Chris Dalton674f77a2019-09-30 20:49:39 -0600134 // once finished.)
135 //
136 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
137 void setInitialStencilContent(StencilContent initialContent) {
138 fInitialStencilContent = initialContent;
139 }
140
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500141 // If a surfaceDrawContext splits its opsTask, it uses this method to guarantee stencil values
Chris Dalton674f77a2019-09-30 20:49:39 -0600142 // get preserved across its split tasks.
143 void setMustPreserveStencil() { fMustPreserveStencil = true; }
144
Brian Salomon588cec72018-11-14 13:56:37 -0500145 class OpChain {
146 public:
John Stiles52cb1d02021-06-02 11:58:05 -0400147 OpChain(GrOp::Owner, GrProcessorSet::Analysis, GrAppliedClip*, const GrDstProxyView*);
Brian Salomon588cec72018-11-14 13:56:37 -0500148 ~OpChain() {
149 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
150 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400151 }
Chris Dalton8816b932017-11-29 16:48:25 -0700152
John Stiles48025a02020-07-27 10:33:50 -0400153 OpChain(const OpChain&) = delete;
154 OpChain& operator=(const OpChain&) = delete;
155 OpChain(OpChain&&) = default;
156 OpChain& operator=(OpChain&&) = default;
157
Robert Phillips294723d2021-06-17 09:23:58 -0400158 void visitProxies(const GrVisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400159
Brian Salomon588cec72018-11-14 13:56:37 -0500160 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400161
Brian Salomon588cec72018-11-14 13:56:37 -0500162 GrAppliedClip* appliedClip() const { return fAppliedClip; }
John Stiles52cb1d02021-06-02 11:58:05 -0400163 const GrDstProxyView& dstProxyView() const { return fDstProxyView; }
Brian Salomon588cec72018-11-14 13:56:37 -0500164 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700165
Herb Derbye32e1ab2020-10-27 10:29:46 -0400166 // Deletes all the ops in the chain.
167 void deleteOps();
Brian Salomon588cec72018-11-14 13:56:37 -0500168
169 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
170 // to merge ops between the chains. Upon success the passed chain is empty.
171 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
Chris Daltonf8d75c62021-04-02 11:24:58 -0600172 bool prependChain(OpChain*, const GrCaps&, SkArenaAlloc* opsTaskArena, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500173
174 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
175 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
176 // the same op type, have different clips or dst proxies.
John Stiles52cb1d02021-06-02 11:58:05 -0400177 GrOp::Owner appendOp(GrOp::Owner op, GrProcessorSet::Analysis, const GrDstProxyView*,
Chris Daltonf8d75c62021-04-02 11:24:58 -0600178 const GrAppliedClip*, const GrCaps&, SkArenaAlloc* opsTaskArena,
179 GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500180
Greg Daniel15ecdf92019-08-30 15:35:23 -0400181 bool shouldExecute() const {
Adlai Holler19fd5142021-03-08 10:19:30 -0700182 return SkToBool(this->head());
Greg Daniel15ecdf92019-08-30 15:35:23 -0400183 }
184
Brian Salomon588cec72018-11-14 13:56:37 -0500185 private:
186 class List {
187 public:
188 List() = default;
Herb Derbyc76d4092020-10-07 16:46:15 -0400189 List(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500190 List(List&&);
191 List& operator=(List&& that);
192
193 bool empty() const { return !SkToBool(fHead); }
194 GrOp* head() const { return fHead.get(); }
195 GrOp* tail() const { return fTail; }
196
Herb Derbyc76d4092020-10-07 16:46:15 -0400197 GrOp::Owner popHead();
198 GrOp::Owner removeOp(GrOp* op);
199 void pushHead(GrOp::Owner op);
200 void pushTail(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500201
202 void validate() const;
203
204 private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400205 GrOp::Owner fHead{nullptr};
206 GrOp* fTail{nullptr};
Brian Salomon588cec72018-11-14 13:56:37 -0500207 };
208
209 void validate() const;
210
John Stiles52cb1d02021-06-02 11:58:05 -0400211 bool tryConcat(List*, GrProcessorSet::Analysis, const GrDstProxyView&, const GrAppliedClip*,
Chris Daltonf8d75c62021-04-02 11:24:58 -0600212 const SkRect& bounds, const GrCaps&, SkArenaAlloc* opsTaskArena,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500213 GrAuditTrail*);
Chris Daltonf8d75c62021-04-02 11:24:58 -0600214 static List DoConcat(List, List, const GrCaps&, SkArenaAlloc* opsTaskArena, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500215
216 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700217 GrProcessorSet::Analysis fProcessorAnalysis;
John Stiles52cb1d02021-06-02 11:58:05 -0400218 GrDstProxyView fDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500219 GrAppliedClip* fAppliedClip;
220 SkRect fBounds;
Brian Salomon54d212e2017-03-21 14:22:38 -0400221 };
222
Adlai Hollere9ea4142021-04-27 14:31:56 -0400223 void onMakeSkippable() override;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400224
225 bool onIsUsed(GrSurfaceProxy*) const override;
226
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400227 void gatherProxyIntervals(GrResourceAllocator*) const override;
228
Herb Derbyc76d4092020-10-07 16:46:15 -0400229 void recordOp(GrOp::Owner, GrProcessorSet::Analysis, GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400230 const GrDstProxyView*, const GrCaps&);
Brian Salomon2790c522016-12-09 16:32:23 -0500231
Robert Phillipsee683652017-04-26 11:53:10 -0400232 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700233
Chris Daltonaa938ce2021-06-23 18:13:59 -0600234 ExpectedOutcome onMakeClosed(GrRecordingContext*, SkIRect* targetUpdateBounds) override;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600235
Adlai Holler9c3b6df2021-02-08 11:31:57 -0500236 // Remove all ops, proxies, etc. Used in the merging algorithm when tasks can be skipped.
237 void reset();
238
Robert Phillips089b7c92020-08-12 11:57:07 -0400239 friend class OpsTaskTestingAccess;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600240
Greg Danielf41b2bd2019-08-22 16:19:24 -0400241 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
242 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
243 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
244 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
Brian Salomoneebe7352020-12-09 16:37:04 -0500245 friend class GrSurfaceDrawContext;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400246
Chris Daltonf8d75c62021-04-02 11:24:58 -0600247 GrAuditTrail* fAuditTrail;
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400248
Chris Dalton2517ce32021-04-13 00:21:15 -0600249 bool fUsesMSAASurface;
Brian Salomon982127b2021-01-21 10:43:35 -0500250 GrSwizzle fTargetSwizzle;
251 GrSurfaceOrigin fTargetOrigin;
252
Greg Danielf41b2bd2019-08-22 16:19:24 -0400253 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
Brian Salomon07bc9a22020-12-02 13:37:16 -0500254 std::array<float, 4> fLoadClearColor = {0, 0, 0, 0};
Chris Dalton674f77a2019-09-30 20:49:39 -0600255 StencilContent fInitialStencilContent = StencilContent::kDontCare;
256 bool fMustPreserveStencil = false;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400257
Brian Salomon3b8486a2020-04-21 12:43:26 -0400258 uint32_t fLastClipStackGenID = SK_InvalidUniqueID;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400259 SkIRect fLastDevClipBounds;
John Stiles9e8f4842020-07-09 11:30:05 -0400260 int fLastClipNumAnalyticElements;
csmartdalton7cdda992016-11-01 07:03:03 -0700261
Greg Danield358cbe2020-09-11 09:33:54 -0400262 GrXferBarrierFlags fRenderPassXferBarriers = GrXferBarrierFlags::kNone;
Greg Daniel9a18b082020-08-14 14:03:50 -0400263
Greg Danielf41b2bd2019-08-22 16:19:24 -0400264 // For ops/opsTask we have mean: 5 stdDev: 28
John Stiles48025a02020-07-27 10:33:50 -0400265 SkSTArray<25, OpChain> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400266
Herb Derby0b1228d2021-04-05 18:38:35 -0400267 sk_sp<GrArenas> fArenas;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400268 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400269
Greg Danielb20d7e52019-09-03 13:54:39 -0400270 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
271 // causing slow downs.
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500272 SkTArray<GrSurfaceProxy*, true> fSampledProxies;
Chris Dalton16a33c62019-09-24 22:19:17 -0600273
274 SkRect fTotalBounds = SkRect::MakeEmpty();
Greg Daniel94ed83f2019-09-27 13:05:43 -0400275 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
joshualitt6db519c2014-10-29 08:48:18 -0700276};
277
reed@google.comac10a2d2010-12-22 21:39:39 +0000278#endif