blob: 4f5cc08b99bfccbe32ac0c6a03500a5e29ca9936 [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
Robert Phillips3e87a8e2021-08-25 13:22:24 -04008#ifndef OpsTask_DEFINED
9#define OpsTask_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"
Robert Phillips06273bc2021-08-11 15:43:50 -040026#include "src/gpu/GrProcessorSet.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040027#include "src/gpu/GrRenderTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#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;
Robert Phillips3e87a8e2021-08-25 13:22:24 -040035class OpsTaskTestingAccess;
sugoi@google.com12b4e272012-12-06 20:13:11 +000036
Robert Phillips3e87a8e2021-08-25 13:22:24 -040037namespace skgpu::v1 {
38
39class SurfaceDrawContext;
40
41class OpsTask : public GrRenderTask {
bsalomon@google.comf6601872012-08-28 21:11:35 +000042public:
Herb Derby0b1228d2021-04-05 18:38:35 -040043 // Manage the arenas life time by maintaining are reference to it.
Robert Phillips3e87a8e2021-08-25 13:22:24 -040044 OpsTask(GrDrawingManager*, GrSurfaceProxyView, GrAuditTrail*, sk_sp<GrArenas>);
45 ~OpsTask() override;
bsalomona73239a2015-04-28 13:35:17 -070046
Robert Phillips3e87a8e2021-08-25 13:22:24 -040047 OpsTask* 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(); }
Chris Dalton5dfb3f42021-04-30 17:16:12 -060050 bool usesMSAASurface() const { return fUsesMSAASurface; }
Greg Daniel0b04b6b2021-06-24 19:19:00 -040051 GrXferBarrierFlags renderPassXferBarriers() const { return fRenderPassXferBarriers; }
Robert Phillipsf5442bb2017-04-17 14:18:34 -040052
reed@google.comac10a2d2010-12-22 21:39:39 +000053 /**
bsalomona73239a2015-04-28 13:35:17 -070054 * Empties the draw buffer of any queued up draws.
55 */
Adlai Hollerd71b7b02020-06-08 15:55:00 -040056 void endFlush(GrDrawingManager*) override;
Robert Phillipsf2361d22016-10-25 14:20:06 -040057
Robert Phillips29f38542019-10-16 09:20:25 -040058 void onPrePrepare(GrRecordingContext*) override;
bsalomona73239a2015-04-28 13:35:17 -070059 /**
bsalomondc438982016-08-31 11:53:49 -070060 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
Adlai Holler93439d92021-01-26 09:20:39 -050061 * of onExecute() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -070062 */
Brian Osman407b3422017-08-22 15:01:32 -040063 void onPrepare(GrOpFlushState* flushState) override;
64 bool onExecute(GrOpFlushState* flushState) override;
bsalomona73239a2015-04-28 13:35:17 -070065
Michael Ludwigfcdd0612019-11-25 08:34:31 -050066 void addSampledTexture(GrSurfaceProxy* proxy) {
67 // This function takes a GrSurfaceProxy because all subsequent uses of the proxy do not
68 // require the specifics of GrTextureProxy, so this avoids a number of unnecessary virtual
69 // asTextureProxy() calls. However, sampling the proxy implicitly requires that the proxy
70 // be a texture. Eventually, when proxies are a unified type with flags, this can just
71 // assert that capability.
72 SkASSERT(proxy->asTextureProxy());
Greg Danielb20d7e52019-09-03 13:54:39 -040073 fSampledProxies.push_back(proxy);
74 }
75
Adlai Hollerabe45182020-11-17 09:22:13 -050076 void addOp(GrDrawingManager*, GrOp::Owner, GrTextureResolveManager, const GrCaps&);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040077
Chris Daltonb4403a92021-05-27 14:59:27 -060078 void addDrawOp(GrDrawingManager*, GrOp::Owner, bool usesMSAA, const GrProcessorSet::Analysis&,
John Stiles52cb1d02021-06-02 11:58:05 -040079 GrAppliedClip&&, const GrDstProxyView&, GrTextureResolveManager, const GrCaps&);
robertphillips9199a9f2016-07-13 07:48:43 -070080
Robert Phillips380b90c2017-08-30 07:41:07 -040081 void discard();
82
Brian Salomon590f5672020-12-16 11:44:47 -050083 enum class CanDiscardPreviousOps : bool {
84 kYes = true,
85 kNo = false
86 };
87
88 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
89 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
90 // Returns true if the clear can be converted into a load op (barring device caps).
91 bool resetForFullscreenClear(CanDiscardPreviousOps);
92
93 // Must only be called if native color buffer clearing is enabled.
94 void setColorLoadOp(GrLoadOp op, std::array<float, 4> color = {0, 0, 0, 0});
95
Greg Daniel0b04b6b2021-06-24 19:19:00 -040096 // Returns whether the given opsTask can be appended at the end of this one.
Robert Phillips3e87a8e2021-08-25 13:22:24 -040097 bool canMerge(const OpsTask*) const;
Greg Daniel0b04b6b2021-06-24 19:19:00 -040098
Adlai Holler93439d92021-01-26 09:20:39 -050099 // Merge as many opsTasks as possible from the head of 'tasks'. They should all be
100 // renderPass compatible. Return the number of tasks merged into 'this'.
101 int mergeFrom(SkSpan<const sk_sp<GrRenderTask>> tasks);
102
Robert Phillips44e2c5f2020-04-14 13:00:04 -0400103#ifdef SK_DEBUG
Robert Phillips44e2c5f2020-04-14 13:00:04 -0400104 int numClips() const override { return fNumClips; }
Robert Phillips294723d2021-06-17 09:23:58 -0400105 void visitProxies_debugOnly(const GrVisitProxyFunc&) const override;
Robert Phillips44e2c5f2020-04-14 13:00:04 -0400106#endif
Robert Phillipsc84c0302017-05-08 15:35:11 -0400107
Robert Phillips438d9862019-11-14 12:46:05 -0500108#if GR_TEST_UTILS
Robert Phillips047d5bb2021-01-08 13:39:19 -0500109 void dump(const SkString& label,
110 SkString indent,
111 bool printDependencies,
112 bool close) const override;
John Stiles1e0136e2020-08-12 18:44:00 -0400113 const char* name() const final { return "Ops"; }
Robert Phillips438d9862019-11-14 12:46:05 -0500114 int numOpChains() const { return fOpChains.count(); }
115 const GrOp* getChain(int index) const { return fOpChains[index].head(); }
116#endif
117
Chris Dalton83420eb2021-06-23 18:47:09 -0600118protected:
Chris Dalton674f77a2019-09-30 20:49:39 -0600119 enum class StencilContent {
120 kDontCare,
121 kUserBitsCleared, // User bits: cleared
122 // Clip bit: don't care (Ganesh always pre-clears the clip bit.)
123 kPreserved
124 };
125
126 // Lets the caller specify what the content of the stencil buffer should be at the beginning
127 // of the render pass.
128 //
129 // When requesting kClear: Tilers will load the stencil buffer with a "clear" op; non-tilers
130 // will clear the stencil on first load, and then preserve it on subsequent loads. (Preserving
John Stiles0fbc6a32021-06-04 14:40:57 -0400131 // works because SurfaceDrawContexts are required to leave the user bits in a cleared state
Chris Dalton674f77a2019-09-30 20:49:39 -0600132 // once finished.)
133 //
134 // NOTE: initialContent must not be kClear if caps.performStencilClearsAsDraws() is true.
135 void setInitialStencilContent(StencilContent initialContent) {
136 fInitialStencilContent = initialContent;
137 }
138
Chris Dalton83420eb2021-06-23 18:47:09 -0600139 void recordOp(GrOp::Owner, bool usesMSAA, GrProcessorSet::Analysis, GrAppliedClip*,
140 const GrDstProxyView*, const GrCaps&);
141
142 ExpectedOutcome onMakeClosed(GrRecordingContext*, SkIRect* targetUpdateBounds) override;
143
144private:
Greg Daniel0b04b6b2021-06-24 19:19:00 -0400145 bool isColorNoOp() const {
Chris Dalton83420eb2021-06-23 18:47:09 -0600146 // TODO: GrLoadOp::kDiscard (i.e., storing a discard) should also be grounds for skipping
147 // execution. We currently don't because of Vulkan. See http://skbug.com/9373.
Chris Dalton83420eb2021-06-23 18:47:09 -0600148 return fOpChains.empty() && GrLoadOp::kLoad == fColorLoadOp;
149 }
150
151 void deleteOps();
152
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500153 // If a surfaceDrawContext splits its opsTask, it uses this method to guarantee stencil values
Chris Dalton674f77a2019-09-30 20:49:39 -0600154 // get preserved across its split tasks.
155 void setMustPreserveStencil() { fMustPreserveStencil = true; }
156
Greg Daniel0b04b6b2021-06-24 19:19:00 -0400157 // Prevents this opsTask from merging backward. This is used by DMSAA when a non-multisampled
158 // opsTask cannot be promoted to MSAA, or when we split a multisampled opsTask in order to
159 // resolve its texture.
160 void setCannotMergeBackward() { fCannotMergeBackward = true; }
161
Brian Salomon588cec72018-11-14 13:56:37 -0500162 class OpChain {
163 public:
John Stiles52cb1d02021-06-02 11:58:05 -0400164 OpChain(GrOp::Owner, GrProcessorSet::Analysis, GrAppliedClip*, const GrDstProxyView*);
Brian Salomon588cec72018-11-14 13:56:37 -0500165 ~OpChain() {
166 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
167 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400168 }
Chris Dalton8816b932017-11-29 16:48:25 -0700169
John Stiles48025a02020-07-27 10:33:50 -0400170 OpChain(const OpChain&) = delete;
171 OpChain& operator=(const OpChain&) = delete;
172 OpChain(OpChain&&) = default;
173 OpChain& operator=(OpChain&&) = default;
174
Robert Phillips294723d2021-06-17 09:23:58 -0400175 void visitProxies(const GrVisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400176
Brian Salomon588cec72018-11-14 13:56:37 -0500177 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400178
Brian Salomon588cec72018-11-14 13:56:37 -0500179 GrAppliedClip* appliedClip() const { return fAppliedClip; }
John Stiles52cb1d02021-06-02 11:58:05 -0400180 const GrDstProxyView& dstProxyView() const { return fDstProxyView; }
Brian Salomon588cec72018-11-14 13:56:37 -0500181 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700182
Herb Derbye32e1ab2020-10-27 10:29:46 -0400183 // Deletes all the ops in the chain.
184 void deleteOps();
Brian Salomon588cec72018-11-14 13:56:37 -0500185
186 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
187 // to merge ops between the chains. Upon success the passed chain is empty.
188 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
Chris Daltonf8d75c62021-04-02 11:24:58 -0600189 bool prependChain(OpChain*, const GrCaps&, SkArenaAlloc* opsTaskArena, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500190
191 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
192 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
193 // the same op type, have different clips or dst proxies.
John Stiles52cb1d02021-06-02 11:58:05 -0400194 GrOp::Owner appendOp(GrOp::Owner op, GrProcessorSet::Analysis, const GrDstProxyView*,
Chris Daltonf8d75c62021-04-02 11:24:58 -0600195 const GrAppliedClip*, const GrCaps&, SkArenaAlloc* opsTaskArena,
196 GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500197
Greg Daniel15ecdf92019-08-30 15:35:23 -0400198 bool shouldExecute() const {
Adlai Holler19fd5142021-03-08 10:19:30 -0700199 return SkToBool(this->head());
Greg Daniel15ecdf92019-08-30 15:35:23 -0400200 }
201
Brian Salomon588cec72018-11-14 13:56:37 -0500202 private:
203 class List {
204 public:
205 List() = default;
Herb Derbyc76d4092020-10-07 16:46:15 -0400206 List(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500207 List(List&&);
208 List& operator=(List&& that);
209
210 bool empty() const { return !SkToBool(fHead); }
211 GrOp* head() const { return fHead.get(); }
212 GrOp* tail() const { return fTail; }
213
Herb Derbyc76d4092020-10-07 16:46:15 -0400214 GrOp::Owner popHead();
215 GrOp::Owner removeOp(GrOp* op);
216 void pushHead(GrOp::Owner op);
217 void pushTail(GrOp::Owner);
Brian Salomon588cec72018-11-14 13:56:37 -0500218
219 void validate() const;
220
221 private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400222 GrOp::Owner fHead{nullptr};
223 GrOp* fTail{nullptr};
Brian Salomon588cec72018-11-14 13:56:37 -0500224 };
225
226 void validate() const;
227
John Stiles52cb1d02021-06-02 11:58:05 -0400228 bool tryConcat(List*, GrProcessorSet::Analysis, const GrDstProxyView&, const GrAppliedClip*,
Chris Daltonf8d75c62021-04-02 11:24:58 -0600229 const SkRect& bounds, const GrCaps&, SkArenaAlloc* opsTaskArena,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500230 GrAuditTrail*);
Chris Daltonf8d75c62021-04-02 11:24:58 -0600231 static List DoConcat(List, List, const GrCaps&, SkArenaAlloc* opsTaskArena, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500232
233 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700234 GrProcessorSet::Analysis fProcessorAnalysis;
John Stiles52cb1d02021-06-02 11:58:05 -0400235 GrDstProxyView fDstProxyView;
Brian Salomon588cec72018-11-14 13:56:37 -0500236 GrAppliedClip* fAppliedClip;
237 SkRect fBounds;
Brian Salomon54d212e2017-03-21 14:22:38 -0400238 };
239
Adlai Hollere9ea4142021-04-27 14:31:56 -0400240 void onMakeSkippable() override;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400241
242 bool onIsUsed(GrSurfaceProxy*) const override;
243
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400244 void gatherProxyIntervals(GrResourceAllocator*) const override;
245
Robert Phillipsee683652017-04-26 11:53:10 -0400246 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700247
Adlai Holler9c3b6df2021-02-08 11:31:57 -0500248 // Remove all ops, proxies, etc. Used in the merging algorithm when tasks can be skipped.
249 void reset();
250
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400251 friend class ::OpsTaskTestingAccess;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600252
Robert Phillips4dca8312021-07-28 15:13:20 -0400253 // The SDC and OpsTask have to work together to handle buffer clears. In most cases, buffer
Greg Danielf41b2bd2019-08-22 16:19:24 -0400254 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
Robert Phillips4dca8312021-07-28 15:13:20 -0400255 // cases, draw ops must be used, which makes the SDC the best place for those decisions. This,
256 // however, requires that the SDC be able to coordinate with the op list to achieve similar ends
257 friend class skgpu::v1::SurfaceDrawContext;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400258
Chris Daltonf8d75c62021-04-02 11:24:58 -0600259 GrAuditTrail* fAuditTrail;
Brian Salomonf1c9eae2020-05-01 15:00:34 -0400260
Chris Dalton2517ce32021-04-13 00:21:15 -0600261 bool fUsesMSAASurface;
Brian Salomon982127b2021-01-21 10:43:35 -0500262 GrSwizzle fTargetSwizzle;
263 GrSurfaceOrigin fTargetOrigin;
264
Greg Danielf41b2bd2019-08-22 16:19:24 -0400265 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
Brian Salomon07bc9a22020-12-02 13:37:16 -0500266 std::array<float, 4> fLoadClearColor = {0, 0, 0, 0};
Chris Dalton674f77a2019-09-30 20:49:39 -0600267 StencilContent fInitialStencilContent = StencilContent::kDontCare;
268 bool fMustPreserveStencil = false;
Greg Daniel0b04b6b2021-06-24 19:19:00 -0400269 bool fCannotMergeBackward = false;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400270
Brian Salomon3b8486a2020-04-21 12:43:26 -0400271 uint32_t fLastClipStackGenID = SK_InvalidUniqueID;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400272 SkIRect fLastDevClipBounds;
John Stiles9e8f4842020-07-09 11:30:05 -0400273 int fLastClipNumAnalyticElements;
csmartdalton7cdda992016-11-01 07:03:03 -0700274
Greg Danield358cbe2020-09-11 09:33:54 -0400275 GrXferBarrierFlags fRenderPassXferBarriers = GrXferBarrierFlags::kNone;
Greg Daniel9a18b082020-08-14 14:03:50 -0400276
Greg Danielf41b2bd2019-08-22 16:19:24 -0400277 // For ops/opsTask we have mean: 5 stdDev: 28
John Stiles48025a02020-07-27 10:33:50 -0400278 SkSTArray<25, OpChain> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400279
Herb Derby0b1228d2021-04-05 18:38:35 -0400280 sk_sp<GrArenas> fArenas;
Greg Danielf41b2bd2019-08-22 16:19:24 -0400281 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400282
Greg Danielb20d7e52019-09-03 13:54:39 -0400283 // TODO: We could look into this being a set if we find we're adding a lot of duplicates that is
284 // causing slow downs.
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500285 SkTArray<GrSurfaceProxy*, true> fSampledProxies;
Chris Dalton16a33c62019-09-24 22:19:17 -0600286
287 SkRect fTotalBounds = SkRect::MakeEmpty();
Greg Daniel94ed83f2019-09-27 13:05:43 -0400288 SkIRect fClippedContentBounds = SkIRect::MakeEmpty();
joshualitt6db519c2014-10-29 08:48:18 -0700289};
290
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400291} // namespace skgpu::v1
292
293#endif // OpsTask_DEFINED