blob: fc675e33aaf1f850a9eab429e50ddc79cbb6b1dc [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
Greg Danielf21bf9e2019-08-22 20:12:20 +00002 * Copyright 2010 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 Danielf21bf9e2019-08-22 20:12:20 +00008#ifndef GrRenderTargetOpList_DEFINED
9#define GrRenderTargetOpList_DEFINED
reed@google.comac10a2d2010-12-22 21:39:39 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkMatrix.h"
12#include "include/core/SkStrokeRec.h"
13#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTArray.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040015#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkClipStack.h"
17#include "src/core/SkStringUtils.h"
18#include "src/core/SkTLazy.h"
19#include "src/gpu/GrAppliedClip.h"
Greg Danielf21bf9e2019-08-22 20:12:20 +000020#include "src/gpu/GrOpList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrPathRendering.h"
22#include "src/gpu/GrPrimitiveProcessor.h"
23#include "src/gpu/ops/GrDrawOp.h"
24#include "src/gpu/ops/GrOp.h"
Scroggo97c88c22011-05-11 14:05:25 +000025
joshualitt086cee12016-01-12 06:45:24 -080026class GrAuditTrail;
Greg Daniel2a595412019-08-22 14:51:48 -040027class GrClearOp;
Greg Danielf21bf9e2019-08-22 20:12:20 +000028class GrCaps;
Robert Phillipsc7635fa2016-10-28 13:25:24 -040029class GrRenderTargetProxy;
sugoi@google.com12b4e272012-12-06 20:13:11 +000030
Greg Danielf21bf9e2019-08-22 20:12:20 +000031class GrRenderTargetOpList final : public GrOpList {
Brian Salomon54d212e2017-03-21 14:22:38 -040032private:
Robert Phillipsbb581ce2017-05-29 15:05:15 -040033 using DstProxy = GrXferProcessor::DstProxy;
Brian Salomon54d212e2017-03-21 14:22:38 -040034
bsalomon@google.comf6601872012-08-28 21:11:35 +000035public:
Greg Danielf21bf9e2019-08-22 20:12:20 +000036 GrRenderTargetOpList(sk_sp<GrOpMemoryPool>, sk_sp<GrRenderTargetProxy>, GrAuditTrail*);
bsalomona73239a2015-04-28 13:35:17 -070037
Greg Danielf21bf9e2019-08-22 20:12:20 +000038 ~GrRenderTargetOpList() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000039
Brian Salomon588cec72018-11-14 13:56:37 -050040 bool isEmpty() const { return fOpChains.empty(); }
Robert Phillipsf5442bb2017-04-17 14:18:34 -040041
reed@google.comac10a2d2010-12-22 21:39:39 +000042 /**
bsalomona73239a2015-04-28 13:35:17 -070043 * Empties the draw buffer of any queued up draws.
44 */
Chris Daltona84cacf2017-10-04 10:30:29 -060045 void endFlush() override;
Robert Phillipsf2361d22016-10-25 14:20:06 -040046
bsalomona73239a2015-04-28 13:35:17 -070047 /**
bsalomondc438982016-08-31 11:53:49 -070048 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
Brian Salomon1e41f4a2016-12-07 15:05:04 -050049 * of executeOps() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -070050 */
Brian Osman407b3422017-08-22 15:01:32 -040051 void onPrepare(GrOpFlushState* flushState) override;
52 bool onExecute(GrOpFlushState* flushState) override;
bsalomona73239a2015-04-28 13:35:17 -070053
Chris Dalton08755122019-08-05 16:13:47 -060054 void addOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
55 const GrCaps& caps) {
56 auto addDependency = [ textureResolveManager, &caps, this ] (
57 GrSurfaceProxy* p, GrMipMapped mipmapped) {
58 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040059 };
60
61 op->visitProxies(addDependency);
62
Chris Dalton945ee652019-01-23 09:10:36 -070063 this->recordOp(std::move(op), GrProcessorSet::EmptySetAnalysis(), nullptr, nullptr, caps);
Brian Salomon54d212e2017-03-21 14:22:38 -040064 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040065
Chris Dalton08755122019-08-05 16:13:47 -060066 void addWaitOp(std::unique_ptr<GrOp> op, GrTextureResolveManager textureResolveManager,
67 const GrCaps& caps) {
68 fHasWaitOp = true;
69 this->addOp(std::move(op), textureResolveManager, caps);
Greg Danielcb324152019-02-25 11:36:53 -050070 }
71
Chris Dalton945ee652019-01-23 09:10:36 -070072 void addDrawOp(std::unique_ptr<GrDrawOp> op, const GrProcessorSet::Analysis& processorAnalysis,
Chris Dalton08755122019-08-05 16:13:47 -060073 GrAppliedClip&& clip, const DstProxy& dstProxy,
74 GrTextureResolveManager textureResolveManager, const GrCaps& caps) {
75 auto addDependency = [ textureResolveManager, &caps, this ] (
76 GrSurfaceProxy* p, GrMipMapped mipmapped) {
77 this->addDependency(p, mipmapped, textureResolveManager, caps);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040078 };
79
80 op->visitProxies(addDependency);
81 clip.visitProxies(addDependency);
Robert Phillips2739ab02018-07-27 07:35:55 -040082 if (dstProxy.proxy()) {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -060083 addDependency(dstProxy.proxy(), GrMipMapped::kNo);
Robert Phillips2739ab02018-07-27 07:35:55 -040084 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040085
Chris Dalton945ee652019-01-23 09:10:36 -070086 this->recordOp(std::move(op), processorAnalysis, clip.doesClip() ? &clip : nullptr,
87 &dstProxy, caps);
Brian Salomon69868af2016-12-22 15:42:51 -050088 }
robertphillips9199a9f2016-07-13 07:48:43 -070089
Robert Phillips380b90c2017-08-30 07:41:07 -040090 void discard();
91
Greg Danielf21bf9e2019-08-22 20:12:20 +000092 GrRenderTargetOpList* asRenderTargetOpList() override { return this; }
93
Robert Phillips27483912018-04-20 12:43:18 -040094 SkDEBUGCODE(void dump(bool printDependencies) const override;)
Robert Phillipsc84c0302017-05-08 15:35:11 -040095 SkDEBUGCODE(int numClips() const override { return fNumClips; })
Chris Dalton706a6ff2017-11-29 22:01:06 -070096 SkDEBUGCODE(void visitProxies_debugOnly(const GrOp::VisitProxyFunc&) const;)
Robert Phillipsc84c0302017-05-08 15:35:11 -040097
Brian Salomon13540482018-07-09 10:31:47 -040098private:
Greg Danielf21bf9e2019-08-22 20:12:20 +000099 friend class GrRenderTargetContextPriv; // for stencil clip state. TODO: this is invasive
100
101 // The RTC and RTOpList have to work together to handle buffer clears. In most cases, buffer
102 // clearing can be done natively, in which case the op list's load ops are sufficient. In other
103 // cases, draw ops must be used, which makes the RTC the best place for those decisions. This,
104 // however, requires that the RTC be able to coordinate with the op list to achieve similar ends
105 friend class GrRenderTargetContext;
106
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600107 bool isNoOp() const {
Greg Danielf21bf9e2019-08-22 20:12:20 +0000108 // TODO: GrLoadOp::kDiscard -> [empty opList] -> GrStoreOp::kStore should also be a no-op.
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600109 // We don't count it as a no-op right now because of Vulkan. There are real cases where we
110 // store a discard, and if we skip that render pass, then the next time we load the render
111 // target, Vulkan detects loading of uninitialized memory and complains. If we don't skip
112 // storing the discard, then we trick Vulkan and it doesn't notice us doing anything wrong.
113 // We should definitely address this issue properly.
114 //
115 // TODO: We should also consider stencil load/store here. We get away with it for now
116 // because we never discard stencil buffers.
117 return fOpChains.empty() && GrLoadOp::kClear != fColorLoadOp &&
118 GrLoadOp::kDiscard != fColorLoadOp;
119 }
120
Greg Danielf21bf9e2019-08-22 20:12:20 +0000121 bool onIsUsed(GrSurfaceProxy*) const override;
Robert Phillips9313aa72019-04-09 18:41:27 -0400122
Michael Ludwig6e17f1d2019-05-15 14:00:20 +0000123 // Must only be called if native stencil buffer clearing is enabled
Chris Dalton6b982802019-06-27 13:53:46 -0600124 void setStencilLoadOp(GrLoadOp op) { fStencilLoadOp = op; }
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500125 // Must only be called if native color buffer clearing is enabled.
126 void setColorLoadOp(GrLoadOp op, const SkPMColor4f& color);
127 // Sets the clear color to transparent black
128 void setColorLoadOp(GrLoadOp op) {
129 static const SkPMColor4f kDefaultClearColor = {0.f, 0.f, 0.f, 0.f};
130 this->setColorLoadOp(op, kDefaultClearColor);
131 }
132
Chris Dalton6b982802019-06-27 13:53:46 -0600133 enum class CanDiscardPreviousOps : bool {
134 kYes = true,
135 kNo = false
136 };
137
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500138 // Perform book-keeping for a fullscreen clear, regardless of how the clear is implemented later
139 // (i.e. setColorLoadOp(), adding a ClearOp, or adding a GrFillRectOp that covers the device).
140 // Returns true if the clear can be converted into a load op (barring device caps).
Chris Dalton6b982802019-06-27 13:53:46 -0600141 bool resetForFullscreenClear(CanDiscardPreviousOps);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500142
Greg Danielf21bf9e2019-08-22 20:12:20 +0000143 void deleteOps();
144
Brian Salomon588cec72018-11-14 13:56:37 -0500145 class OpChain {
146 public:
147 OpChain(const OpChain&) = delete;
148 OpChain& operator=(const OpChain&) = delete;
Chris Dalton945ee652019-01-23 09:10:36 -0700149 OpChain(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*, const DstProxy*);
Brian Salomon588cec72018-11-14 13:56:37 -0500150
151 ~OpChain() {
152 // The ops are stored in a GrMemoryPool and must be explicitly deleted via the pool.
153 SkASSERT(fList.empty());
Brian Salomon54d212e2017-03-21 14:22:38 -0400154 }
Chris Dalton8816b932017-11-29 16:48:25 -0700155
Chris Dalton1706cbf2019-05-21 19:35:29 -0600156 void visitProxies(const GrOp::VisitProxyFunc&) const;
Robert Phillipsc994a932018-06-19 13:09:54 -0400157
Brian Salomon588cec72018-11-14 13:56:37 -0500158 GrOp* head() const { return fList.head(); }
Robert Phillips7c525e62018-06-12 10:11:12 -0400159
Brian Salomon588cec72018-11-14 13:56:37 -0500160 GrAppliedClip* appliedClip() const { return fAppliedClip; }
161 const DstProxy& dstProxy() const { return fDstProxy; }
162 const SkRect& bounds() const { return fBounds; }
Chris Dalton8816b932017-11-29 16:48:25 -0700163
Brian Salomon588cec72018-11-14 13:56:37 -0500164 // Deletes all the ops in the chain via the pool.
165 void deleteOps(GrOpMemoryPool* pool);
166
167 // Attempts to move the ops from the passed chain to this chain at the head. Also attempts
168 // to merge ops between the chains. Upon success the passed chain is empty.
169 // Fails when the chains aren't of the same op type, have different clips or dst proxies.
170 bool prependChain(OpChain*, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
171
172 // Attempts to add 'op' to this chain either by merging or adding to the tail. Returns
173 // 'op' to the caller upon failure, otherwise null. Fails when the op and chain aren't of
174 // the same op type, have different clips or dst proxies.
Chris Dalton945ee652019-01-23 09:10:36 -0700175 std::unique_ptr<GrOp> appendOp(std::unique_ptr<GrOp> op, GrProcessorSet::Analysis,
176 const DstProxy*, const GrAppliedClip*, const GrCaps&,
177 GrOpMemoryPool*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500178
179 private:
180 class List {
181 public:
182 List() = default;
183 List(std::unique_ptr<GrOp>);
184 List(List&&);
185 List& operator=(List&& that);
186
187 bool empty() const { return !SkToBool(fHead); }
188 GrOp* head() const { return fHead.get(); }
189 GrOp* tail() const { return fTail; }
190
191 std::unique_ptr<GrOp> popHead();
192 std::unique_ptr<GrOp> removeOp(GrOp* op);
193 void pushHead(std::unique_ptr<GrOp> op);
194 void pushTail(std::unique_ptr<GrOp>);
195
196 void validate() const;
197
198 private:
199 std::unique_ptr<GrOp> fHead;
200 GrOp* fTail = nullptr;
201 };
202
203 void validate() const;
204
Chris Dalton945ee652019-01-23 09:10:36 -0700205 bool tryConcat(List*, GrProcessorSet::Analysis, const DstProxy&, const GrAppliedClip*,
206 const SkRect& bounds, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
Chris Dalton6f6ae6a2019-01-18 12:10:36 -0700207 static List DoConcat(List, List, const GrCaps&, GrOpMemoryPool*, GrAuditTrail*);
Brian Salomon588cec72018-11-14 13:56:37 -0500208
209 List fList;
Chris Dalton945ee652019-01-23 09:10:36 -0700210 GrProcessorSet::Analysis fProcessorAnalysis;
Brian Salomon588cec72018-11-14 13:56:37 -0500211 DstProxy fDstProxy;
212 GrAppliedClip* fAppliedClip;
213 SkRect fBounds;
Brian Salomon54d212e2017-03-21 14:22:38 -0400214 };
215
Chris Dalton6b498102019-08-01 14:14:52 -0600216 void handleInternalAllocationFailure() override;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500217
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400218 void gatherProxyIntervals(GrResourceAllocator*) const override;
219
Chris Dalton945ee652019-01-23 09:10:36 -0700220 void recordOp(std::unique_ptr<GrOp>, GrProcessorSet::Analysis, GrAppliedClip*, const DstProxy*,
221 const GrCaps& caps);
Brian Salomon2790c522016-12-09 16:32:23 -0500222
Robert Phillipsee683652017-04-26 11:53:10 -0400223 void forwardCombine(const GrCaps&);
bsalomonad792c12015-09-10 11:10:50 -0700224
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600225 ExpectedOutcome onMakeClosed(const GrCaps& caps) override {
226 this->forwardCombine(caps);
227 return (this->isNoOp()) ? ExpectedOutcome::kTargetUnchanged : ExpectedOutcome::kTargetDirty;
228 }
229
Greg Danielf21bf9e2019-08-22 20:12:20 +0000230 GrLoadOp fColorLoadOp = GrLoadOp::kLoad;
231 SkPMColor4f fLoadClearColor = SK_PMColor4fTRANSPARENT;
232 GrLoadOp fStencilLoadOp = GrLoadOp::kLoad;
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600233
Greg Danielf21bf9e2019-08-22 20:12:20 +0000234 uint32_t fLastClipStackGenID;
235 SkIRect fLastDevClipBounds;
236 int fLastClipNumAnalyticFPs;
csmartdalton7cdda992016-11-01 07:03:03 -0700237
Greg Danielcb324152019-02-25 11:36:53 -0500238 // We must track if we have a wait op so that we don't delete the op when we have a full clear.
239 bool fHasWaitOp = false;;
240
Greg Danielf21bf9e2019-08-22 20:12:20 +0000241 // For ops/opList we have mean: 5 stdDev: 28
242 SkSTArray<25, OpChain, true> fOpChains;
Brian Salomon54d212e2017-03-21 14:22:38 -0400243
Robert Phillipsf6d7eb12017-04-26 14:55:34 -0400244 // MDB TODO: 4096 for the first allocation of the clip space will be huge overkill.
245 // Gather statistics to determine the correct size.
Greg Danielf21bf9e2019-08-22 20:12:20 +0000246 SkArenaAlloc fClipAllocator{4096};
247 SkDEBUGCODE(int fNumClips;)
Brian Salomon54d212e2017-03-21 14:22:38 -0400248
Greg Danielf21bf9e2019-08-22 20:12:20 +0000249 typedef GrOpList INHERITED;
joshualitt6db519c2014-10-29 08:48:18 -0700250};
251
reed@google.comac10a2d2010-12-22 21:39:39 +0000252#endif