blob: a89b22c0b0ea06f6fe453fbe3dc6bc37d49dd2d8 [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:
Robert Phillipsbb581ce2017-05-29 15:05:15 -040038 using DstProxy = GrXferProcessor::DstProxy;
Brian Salomon54d212e2017-03-21 14:22:38 -040039
bsalomon@google.comf6601872012-08-28 21:11:35 +000040public:
Greg Danielf41b2bd2019-08-22 16:19:24 -040041 GrOpsTask(sk_sp<GrOpMemoryPool>, sk_sp<GrRenderTargetProxy>, GrAuditTrail*);
42 ~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
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
Brian Salomon1e41f4a2016-12-07 15:05:04 -050055 * of executeOps() 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
Chris Dalton08755122019-08-05 16:13:47 -060060 void addOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
61 const GrCaps& caps) {
62 auto addDependency = [ textureResolveManager, &caps, this ] (
63 GrSurfaceProxy* p, GrMipMapped mipmapped) {
64 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040065 };
66
67 op->visitProxies(addDependency);
68
Chris Dalton945ee652019-01-23 09:10:36 -070069 this->recordOp(std::move(op), GrProcessorSet::EmptySetAnalysis(), nullptr, nullptr, caps);
Brian Salomon54d212e2017-03-21 14:22:38 -040070 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040071
Chris Dalton08755122019-08-05 16:13:47 -060072 void addWaitOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
73 const GrCaps& caps) {
74 fHasWaitOp = true;
75 this->addOp(std::move(op), textureResolveManager, caps);
Greg Danielcb324152019-02-25 11:36:53 -050076 }
77
Chris Dalton945ee652019-01-23 09:10:36 -070078 void addDrawOp(std::unique_ptr<GrDrawOp> op, const GrProcessorSet::Analysis& processorAnalysis,
Chris Dalton08755122019-08-05 16:13:47 -060079 GrAppliedClip&& clip, const DstProxy& dstProxy,
80 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
81 auto addDependency = [ textureResolveManager, &caps, this ] (
82 GrSurfaceProxy* p, GrMipMapped mipmapped) {
83 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040084 };
85
86 op->visitProxies(addDependency);
87 clip.visitProxies(addDependency);
Robert Phillips2739ab02018-07-27 07:35:55 -040088 if (dstProxy.proxy()) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060089 addDependency(dstProxy.proxy(), GrMipMapped::kNo);
Robert Phillips2739ab02018-07-27 07:35:55 -040090 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040091
Chris Dalton945ee652019-01-23 09:10:36 -070092 this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
93 &dstProxy, caps);
Brian Salomon69868af2016-12-22 15:42:51 -050094 }
robertphillips9199a9f2016-07-13 07:48:43 -070095
Robert Phillips380b90c2017-08-30 07:41:07 -040096 void discard();
97
Robert Phillips27483912018-04-20 12:43:18 -040098 SkDEBUGCODE(void dump(bool printDependencies) const override;)
Robert Phillipsc84c0302017-05-08 15:35:11 -040099 SkDEBUGCODE(int numClips() const override { return fNumClips; })
Chris Dalton706a6ff2017-11-29 22:01:06 -0700100 SkDEBUGCODE(void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const;)
Robert Phillipsc84c0302017-05-08 15:35:11 -0400101
Brian Salomon13540482018-07-09 10:31:47 -0400102private:
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600103 bool isNoOp() const {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400104 // TODO: GrLoadOp::kDiscard -> [empty OpsTask] -> GrStoreOp::kStore should also be a no-op.
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600105 // We don't count it as a no-op right now because of Vulkan. There are real cases where we
106 // store a discard, and if we skip that render pass, then the next time we load the render
107 // target, Vulkan detects loading of uninitialized memory and complains. If we don't skip
108 // storing the discard, then we trick Vulkan and it doesn't notice us doing anything wrong.
109 // We should definitely address this issue properly.
110 //
111 // TODO: We should also consider stencil load/store here. We get away with it for now
112 // because we never discard stencil buffers.
113 return fOpChains.empty() && GrLoadOp::kClear != fColorLoadOp &&
114 GrLoadOp::kDiscard != fColorLoadOp;
115 }
116
Greg Danielf41b2bd2019-08-22 16:19:24 -0400117 void deleteOps();
Robert Phillips9313aa72019-04-09 18:41:27 -0400118
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000119 // Must only be called if native stencil buffer clearing is enabled
Chris Dalton6b982802019-06-27 13:53:46 -0600120 void setStencilLoadOp(GrLoadOp op) { fStencilLoadOp = op; }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500121 // Must only be called if native color buffer clearing is enabled.
122 void setColorLoadOp(GrLoadOp op, const SkPMColor4f& color);
123 // Sets the clear color to transparent black
124 void setColorLoadOp(GrLoadOp op) {
125 static const SkPMColor4f kDefaultClearColor = {0.f, 0.f, 0.f, 0.f};
126 this->setColorLoadOp(op, kDefaultClearColor);
127 }
128
Chris Dalton6b982802019-06-27 13:53:46 -0600129 enum class CanDiscardPreviousOps : bool {
130 kYes = true,
131 kNo = false
132 };
133
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500134 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
135 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
136 // Returns true if the clear can be converted into a load op (barring device caps).
Chris Dalton6b982802019-06-27 13:53:46 -0600137 bool resetForFullscreenClear(CanDiscardPreviousOps);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500138
Brian Salomon588cec72018-11-14 13:56:37 -0500139 class OpChain {
140 public:
141 OpChain(const OpChain&) = delete;
142 OpChain& operator=(const OpChain&) = delete;
Chris Dalton945ee652019-01-23 09:10:36 -0700143 OpChain(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*, const DstProxy*);
Brian Salomon588cec72018-11-14 13:56:37 -0500144
145 ~OpChain() {
146 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
147 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400148 }
Chris Dalton8816b932017-11-29 16:48:25 -0700149
Chris Dalton1706cbf2019-05-21 19:35:29 -0600150 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400151
Brian Salomon588cec72018-11-14 13:56:37 -0500152 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400153
Brian Salomon588cec72018-11-14 13:56:37 -0500154 GrAppliedClip* appliedClip() const { return fAppliedClip; }
155 const DstProxy& dstProxy() const { return fDstProxy; }
156 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700157
Brian Salomon588cec72018-11-14 13:56:37 -0500158 // Deletes all the ops in the chain via the pool.
159 void deleteOps(GrOpMemoryPool* pool);
160
161 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
162 // to merge ops between the chains. Upon success the passed chain is empty.
163 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
164 bool prependChain(OpChain*, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
165
166 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
167 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
168 // the same op type, have different clips or dst proxies.
Chris Dalton945ee652019-01-23 09:10:36 -0700169 std::unique_ptr<GrOp> appendOp(std::unique_ptr<GrOp> op, GrProcessorSet::Analysis,
170 const DstProxy*, const GrAppliedClip*, const GrCaps&,
171 GrOpMemoryPool*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500172
173 private:
174 class List {
175 public:
176 List() = default;
177 List(std::unique_ptr<GrOp>);
178 List(List&&);
179 List& operator=(List&& that);
180
181 bool empty() const { return !SkToBool(fHead); }
182 GrOp* head() const { return fHead.get(); }
183 GrOp* tail() const { return fTail; }
184
185 std::unique_ptr<GrOp> popHead();
186 std::unique_ptr<GrOp> removeOp(GrOp* op);
187 void pushHead(std::unique_ptr<GrOp> op);
188 void pushTail(std::unique_ptr<GrOp>);
189
190 void validate() const;
191
192 private:
193 std::unique_ptr<GrOp> fHead;
194 GrOp* fTail = nullptr;
195 };
196
197 void validate() const;
198
Chris Dalton945ee652019-01-23 09:10:36 -0700199 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxy&, const GrAppliedClip*,
200 const SkRect& bounds, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700201 static List DoConcat(List, List, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500202
203 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700204 GrProcessorSet::Analysis fProcessorAnalysis;
Brian Salomon588cec72018-11-14 13:56:37 -0500205 DstProxy fDstProxy;
206 GrAppliedClip* fAppliedClip;
207 SkRect fBounds;
Brian Salomon54d212e2017-03-21 14:22:38 -0400208 };
209
Greg Danielf41b2bd2019-08-22 16:19:24 -0400210
211 bool onIsUsed(GrSurfaceProxy*) const override;
212
Chris Dalton6b498102019-08-01 14:14:52 -0600213 void handleInternalAllocationFailure() override;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500214
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400215 void gatherProxyIntervals(GrResourceAllocator*) const override;
216
Chris Dalton945ee652019-01-23 09:10:36 -0700217 void recordOp(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*, const DstProxy*,
218 const GrCaps& caps);
Brian Salomon2790c522016-12-09 16:32:23 -0500219
Robert Phillipsee683652017-04-26 11:53:10 -0400220 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700221
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600222 ExpectedOutcome onMakeClosed(const GrCaps& caps) override {
223 this->forwardCombine(caps);
224 return (this->isNoOp()) ? ExpectedOutcome::kTargetUnchanged : ExpectedOutcome::kTargetDirty;
225 }
226
Greg Danielf41b2bd2019-08-22 16:19:24 -0400227 friend class GrRenderTargetContextPriv; // for stencil clip state. TODO: this is invasive
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600228
Greg Danielf41b2bd2019-08-22 16:19:24 -0400229 // The RTC and OpsTask have to work together to handle buffer clears. In most cases, buffer
230 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
231 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
232 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
233 friend class GrRenderTargetContext;
234
235 // This is a backpointer to the GrOpMemoryPool that holds the memory for this GrOpsTask's ops.
236 // In the DDL case, these back pointers keep the DDL's GrOpMemoryPool alive as long as its
237 // constituent GrOpsTask survives.
238 sk_sp<GrOpMemoryPool> fOpMemoryPool;
239 GrAuditTrail* fAuditTrail;
240
241 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
242 SkPMColor4f fLoadClearColor = SK_PMColor4fTRANSPARENT;
243 GrLoadOp fStencilLoadOp = GrLoadOp::kLoad;
244
245 uint32_t fLastClipStackGenID;
246 SkIRect fLastDevClipBounds;
247 int fLastClipNumAnalyticFPs;
csmartdalton7cdda992016-11-01 07:03:03 -0700248
Greg Danielcb324152019-02-25 11:36:53 -0500249 // We must track if we have a wait op so that we don't delete the op when we have a full clear.
250 bool fHasWaitOp = false;;
251
Greg Danielf41b2bd2019-08-22 16:19:24 -0400252 // For ops/opsTask we have mean: 5 stdDev: 28
253 SkSTArray<25, OpChain, true> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400254
Robert Phillipsf6d7eb12017-04-26 14:55:34 -0400255 // MDB TODO: 4096 for the first allocation of the clip space will be huge overkill.
256 // Gather statistics to determine the correct size.
Greg Danielf41b2bd2019-08-22 16:19:24 -0400257 SkArenaAlloc fClipAllocator{4096};
258 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400259
joshualitt6db519c2014-10-29 08:48:18 -0700260};
261
reed@google.comac10a2d2010-12-22 21:39:39 +0000262#endif