blob: 9a9a67131ca20bdd8494a0e317101d198b7d0a6f [file] [log] [blame]
Robert Phillipsf2361d22016-10-25 14:20:06 -04001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrOpList.h"
Robert Phillips5efd5ea2017-05-30 13:47:32 -04009
10#include "GrContext.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040011#include "GrDeferredProxyUploader.h"
Robert Phillipsc994a932018-06-19 13:09:54 -040012#include "GrMemoryPool.h"
Robert Phillips01a91282018-07-26 08:03:04 -040013#include "GrRenderTargetPriv.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040014#include "GrSurfaceProxy.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040015#include "GrTextureProxyPriv.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040016
Robert Phillipsc589b0b2017-04-17 07:53:07 -040017#include "SkAtomics.h"
18
Robert Phillipsc0138922017-03-08 11:50:55 -050019uint32_t GrOpList::CreateUniqueID() {
20 static int32_t gUniqueID = SK_InvalidUniqueID;
21 uint32_t id;
22 // Loop in case our global wraps around, as we never want to return a 0.
23 do {
24 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
25 } while (id == SK_InvalidUniqueID);
26 return id;
27}
28
Robert Phillipsc994a932018-06-19 13:09:54 -040029GrOpList::GrOpList(GrResourceProvider* resourceProvider, sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips5efd5ea2017-05-30 13:47:32 -040030 GrSurfaceProxy* surfaceProxy, GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -040031 : fOpMemoryPool(std::move(opMemoryPool))
32 , fAuditTrail(auditTrail)
33 , fUniqueID(CreateUniqueID())
34 , fFlags(0) {
35 SkASSERT(fOpMemoryPool);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040036 fTarget.setProxy(sk_ref_sp(surfaceProxy), kWrite_GrIOType);
Robert Phillips6cdc22c2017-05-11 16:29:14 -040037 fTarget.get()->setLastOpList(this);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040038
Robert Phillips4150eea2018-02-07 17:08:21 -050039 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
40 // MDB TODO: remove this! We are currently moving to having all the ops that target
41 // the RT as a dest (e.g., clear, etc.) rely on the opList's 'fTarget' pointer
42 // for the IO Ref. This works well but until they are all swapped over (and none
43 // are pre-emptively instantiating proxies themselves) we need to instantiate
44 // here so that the GrSurfaces are created in an order that preserves the GrSurface
45 // re-use assumptions.
46 fTarget.get()->instantiate(resourceProvider);
47 }
48
Robert Phillips5f567c72017-09-14 08:27:37 -040049 fTarget.markPendingIO();
Robert Phillipsf2361d22016-10-25 14:20:06 -040050}
51
52GrOpList::~GrOpList() {
Chris Daltona84cacf2017-10-04 10:30:29 -060053 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
54 // Ensure the target proxy doesn't keep hold of a dangling back pointer.
55 fTarget.get()->setLastOpList(nullptr);
56 }
Robert Phillipsf2361d22016-10-25 14:20:06 -040057}
58
Robert Phillips318c4192017-05-17 09:36:38 -040059bool GrOpList::instantiate(GrResourceProvider* resourceProvider) {
60 return SkToBool(fTarget.get()->instantiate(resourceProvider));
61}
62
Chris Daltona84cacf2017-10-04 10:30:29 -060063void GrOpList::endFlush() {
Robert Phillips6cdc22c2017-05-11 16:29:14 -040064 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
65 fTarget.get()->setLastOpList(nullptr);
66 }
67
Robert Phillips5efd5ea2017-05-30 13:47:32 -040068 fTarget.reset();
Brian Osman099fa0f2017-10-02 16:38:32 -040069 fDeferredProxies.reset();
Robert Phillips6cdc22c2017-05-11 16:29:14 -040070 fAuditTrail = nullptr;
71}
72
Brian Osman099fa0f2017-10-02 16:38:32 -040073void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) {
74 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Robert Phillips4150eea2018-02-07 17:08:21 -050075 if (resourceProvider->explicitlyAllocateGPUResources()) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040076 SkASSERT(fDeferredProxies[i]->isInstantiated());
Robert Phillips4150eea2018-02-07 17:08:21 -050077 } else {
78 fDeferredProxies[i]->instantiate(resourceProvider);
79 }
Brian Osman099fa0f2017-10-02 16:38:32 -040080 }
Brian Osman5d034742017-09-11 13:38:55 -040081}
82
Brian Osman407b3422017-08-22 15:01:32 -040083void GrOpList::prepare(GrOpFlushState* flushState) {
Brian Osman099fa0f2017-10-02 16:38:32 -040084 for (int i = 0; i < fDeferredProxies.count(); ++i) {
85 fDeferredProxies[i]->texPriv().scheduleUpload(flushState);
Brian Osman407b3422017-08-22 15:01:32 -040086 }
87
88 this->onPrepare(flushState);
89}
90
Robert Phillipsf2361d22016-10-25 14:20:06 -040091// Add a GrOpList-based dependency
92void GrOpList::addDependency(GrOpList* dependedOn) {
93 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
94
95 if (this->dependsOn(dependedOn)) {
96 return; // don't add duplicate dependencies
97 }
98
Robert Phillips73fd0d62017-09-15 11:48:08 +000099 fDependencies.push_back(dependedOn);
Robert Phillipse6d06182018-08-06 16:56:26 -0400100 dependedOn->addDependent(this);
101
102 SkDEBUGCODE(this->validate());
Robert Phillipsf2361d22016-10-25 14:20:06 -0400103}
104
105// Convert from a GrSurface-based dependency to a GrOpList one
Robert Phillipsee683652017-04-26 11:53:10 -0400106void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400107 if (dependedOn->getLastOpList()) {
108 // If it is still receiving dependencies, this GrOpList shouldn't be closed
109 SkASSERT(!this->isClosed());
110
111 GrOpList* opList = dependedOn->getLastOpList();
112 if (opList == this) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400113 // self-read - presumably for dst reads. We can't make it closed in the self-read case.
Robert Phillipsf2361d22016-10-25 14:20:06 -0400114 } else {
115 this->addDependency(opList);
116
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400117 // We are closing 'opList' here bc the current contents of it are what 'this' opList
118 // depends on. We need a break in 'opList' so that the usage of that state has a
119 // chance to execute.
Robert Phillipsee683652017-04-26 11:53:10 -0400120 opList->makeClosed(caps);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400121 }
122 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400123
124 if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) {
125 if (textureProxy->texPriv().isDeferred()) {
126 fDeferredProxies.push_back(textureProxy);
127 }
128 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400129}
130
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400131bool GrOpList::dependsOn(const GrOpList* dependedOn) const {
132 for (int i = 0; i < fDependencies.count(); ++i) {
133 if (fDependencies[i] == dependedOn) {
134 return true;
135 }
136 }
137
138 return false;
139}
140
Robert Phillipse6d06182018-08-06 16:56:26 -0400141
142void GrOpList::addDependent(GrOpList* dependent) {
143 fDependents.push_back(dependent);
144}
145
146#ifdef SK_DEBUG
147bool GrOpList::isDependedent(const GrOpList* dependent) const {
148 for (int i = 0; i < fDependents.count(); ++i) {
149 if (fDependents[i] == dependent) {
150 return true;
151 }
152 }
153
154 return false;
155}
156
157void GrOpList::validate() const {
158 // TODO: check for loops and duplicates
159
160 for (int i = 0; i < fDependencies.count(); ++i) {
161 SkASSERT(fDependencies[i]->isDependedent(this));
162 }
163}
164#endif
165
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400166bool GrOpList::isInstantiated() const { return fTarget.get()->isInstantiated(); }
Robert Phillips09dfc472017-09-13 15:25:47 -0400167
Robert Phillips01a91282018-07-26 08:03:04 -0400168bool GrOpList::isFullyInstantiated() const {
169 if (!this->isInstantiated()) {
170 return false;
171 }
172
173 GrSurfaceProxy* proxy = fTarget.get();
174 bool needsStencil = proxy->asRenderTargetProxy()
175 ? proxy->asRenderTargetProxy()->needsStencil()
176 : false;
177
178 if (needsStencil) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400179 GrRenderTarget* rt = proxy->peekRenderTarget();
Robert Phillips01a91282018-07-26 08:03:04 -0400180
181 if (!rt->renderTargetPriv().getStencilAttachment()) {
182 return false;
183 }
184 }
185
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400186 GrSurface* surface = proxy->peekSurface();
187 if (surface->wasDestroyed()) {
188 return false;
189 }
190
Robert Phillips01a91282018-07-26 08:03:04 -0400191 return true;
192}
193
Robert Phillips4150eea2018-02-07 17:08:21 -0500194#ifdef SK_DEBUG
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400195static const char* op_to_name(GrLoadOp op) {
196 return GrLoadOp::kLoad == op ? "load" : GrLoadOp::kClear == op ? "clear" : "discard";
197}
198
Robert Phillips27483912018-04-20 12:43:18 -0400199void GrOpList::dump(bool printDependencies) const {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400200 SkDebugf("--------------------------------------------------------------\n");
Robert Phillipsba5c4392018-07-25 12:37:14 -0400201 SkDebugf("opListID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID,
202 fTarget.get() ? fTarget.get()->uniqueID().asUInt() : -1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400203 fTarget.get() && fTarget.get()->peekSurface()
204 ? fTarget.get()->peekSurface()->uniqueID().asUInt()
205 : -1);
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400206 SkDebugf("ColorLoadOp: %s %x StencilLoadOp: %s\n",
207 op_to_name(fColorLoadOp),
208 GrLoadOp::kClear == fColorLoadOp ? fLoadClearColor : 0x0,
209 op_to_name(fStencilLoadOp));
Robert Phillips27483912018-04-20 12:43:18 -0400210
211 if (printDependencies) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400212 SkDebugf("I rely On (%d): ", fDependencies.count());
Robert Phillips27483912018-04-20 12:43:18 -0400213 for (int i = 0; i < fDependencies.count(); ++i) {
214 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
215 }
216 SkDebugf("\n");
Robert Phillipse6d06182018-08-06 16:56:26 -0400217
218 SkDebugf("(%d) Rely On Me: ", fDependents.count());
219 for (int i = 0; i < fDependents.count(); ++i) {
220 SkDebugf("%d, ", fDependents[i]->fUniqueID);
221 }
222 SkDebugf("\n");
Robert Phillipsf2361d22016-10-25 14:20:06 -0400223 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400224}
225#endif