blob: b1ccc8237f3600e2f59e260dd34c7ea916e6ffee [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"
Mike Klein0ec1c572018-12-04 11:52:51 -050016#include <atomic>
Robert Phillipsc589b0b2017-04-17 07:53:07 -040017
Robert Phillipsc0138922017-03-08 11:50:55 -050018uint32_t GrOpList::CreateUniqueID() {
Mike Klein0ec1c572018-12-04 11:52:51 -050019 static std::atomic<uint32_t> nextID{1};
Robert Phillipsc0138922017-03-08 11:50:55 -050020 uint32_t id;
Robert Phillipsc0138922017-03-08 11:50:55 -050021 do {
Mike Klein0ec1c572018-12-04 11:52:51 -050022 id = nextID++;
Robert Phillipsc0138922017-03-08 11:50:55 -050023 } while (id == SK_InvalidUniqueID);
24 return id;
25}
26
Robert Phillipsc994a932018-06-19 13:09:54 -040027GrOpList::GrOpList(GrResourceProvider* resourceProvider, sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips5efd5ea2017-05-30 13:47:32 -040028 GrSurfaceProxy* surfaceProxy, GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -040029 : fOpMemoryPool(std::move(opMemoryPool))
30 , fAuditTrail(auditTrail)
31 , fUniqueID(CreateUniqueID())
32 , fFlags(0) {
33 SkASSERT(fOpMemoryPool);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040034 fTarget.setProxy(sk_ref_sp(surfaceProxy), kWrite_GrIOType);
Robert Phillips6cdc22c2017-05-11 16:29:14 -040035 fTarget.get()->setLastOpList(this);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040036
Robert Phillips4150eea2018-02-07 17:08:21 -050037 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
38 // MDB TODO: remove this! We are currently moving to having all the ops that target
39 // the RT as a dest (e.g., clear, etc.) rely on the opList's 'fTarget' pointer
40 // for the IO Ref. This works well but until they are all swapped over (and none
41 // are pre-emptively instantiating proxies themselves) we need to instantiate
42 // here so that the GrSurfaces are created in an order that preserves the GrSurface
43 // re-use assumptions.
44 fTarget.get()->instantiate(resourceProvider);
45 }
46
Robert Phillips5f567c72017-09-14 08:27:37 -040047 fTarget.markPendingIO();
Robert Phillipsf2361d22016-10-25 14:20:06 -040048}
49
50GrOpList::~GrOpList() {
Chris Daltona84cacf2017-10-04 10:30:29 -060051 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
52 // Ensure the target proxy doesn't keep hold of a dangling back pointer.
53 fTarget.get()->setLastOpList(nullptr);
54 }
Robert Phillipsf2361d22016-10-25 14:20:06 -040055}
56
Robert Phillips318c4192017-05-17 09:36:38 -040057bool GrOpList::instantiate(GrResourceProvider* resourceProvider) {
58 return SkToBool(fTarget.get()->instantiate(resourceProvider));
59}
60
Chris Daltona84cacf2017-10-04 10:30:29 -060061void GrOpList::endFlush() {
Robert Phillips6cdc22c2017-05-11 16:29:14 -040062 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
63 fTarget.get()->setLastOpList(nullptr);
64 }
65
Robert Phillips5efd5ea2017-05-30 13:47:32 -040066 fTarget.reset();
Brian Osman099fa0f2017-10-02 16:38:32 -040067 fDeferredProxies.reset();
Robert Phillips6cdc22c2017-05-11 16:29:14 -040068 fAuditTrail = nullptr;
69}
70
Brian Osman099fa0f2017-10-02 16:38:32 -040071void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) {
72 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Robert Phillips4150eea2018-02-07 17:08:21 -050073 if (resourceProvider->explicitlyAllocateGPUResources()) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040074 SkASSERT(fDeferredProxies[i]->isInstantiated());
Robert Phillips4150eea2018-02-07 17:08:21 -050075 } else {
76 fDeferredProxies[i]->instantiate(resourceProvider);
77 }
Brian Osman099fa0f2017-10-02 16:38:32 -040078 }
Brian Osman5d034742017-09-11 13:38:55 -040079}
80
Brian Osman407b3422017-08-22 15:01:32 -040081void GrOpList::prepare(GrOpFlushState* flushState) {
Brian Osman099fa0f2017-10-02 16:38:32 -040082 for (int i = 0; i < fDeferredProxies.count(); ++i) {
83 fDeferredProxies[i]->texPriv().scheduleUpload(flushState);
Brian Osman407b3422017-08-22 15:01:32 -040084 }
85
86 this->onPrepare(flushState);
87}
88
Robert Phillipsf2361d22016-10-25 14:20:06 -040089// Add a GrOpList-based dependency
90void GrOpList::addDependency(GrOpList* dependedOn) {
91 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
92
93 if (this->dependsOn(dependedOn)) {
94 return; // don't add duplicate dependencies
95 }
96
Robert Phillips73fd0d62017-09-15 11:48:08 +000097 fDependencies.push_back(dependedOn);
Robert Phillipse6d06182018-08-06 16:56:26 -040098 dependedOn->addDependent(this);
99
100 SkDEBUGCODE(this->validate());
Robert Phillipsf2361d22016-10-25 14:20:06 -0400101}
102
103// Convert from a GrSurface-based dependency to a GrOpList one
Robert Phillipsee683652017-04-26 11:53:10 -0400104void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400105 if (dependedOn->getLastOpList()) {
106 // If it is still receiving dependencies, this GrOpList shouldn't be closed
107 SkASSERT(!this->isClosed());
108
109 GrOpList* opList = dependedOn->getLastOpList();
110 if (opList == this) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400111 // self-read - presumably for dst reads. We can't make it closed in the self-read case.
Robert Phillipsf2361d22016-10-25 14:20:06 -0400112 } else {
113 this->addDependency(opList);
114
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400115 // We are closing 'opList' here bc the current contents of it are what 'this' opList
116 // depends on. We need a break in 'opList' so that the usage of that state has a
117 // chance to execute.
Robert Phillipsee683652017-04-26 11:53:10 -0400118 opList->makeClosed(caps);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400119 }
120 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400121
122 if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) {
123 if (textureProxy->texPriv().isDeferred()) {
124 fDeferredProxies.push_back(textureProxy);
125 }
126 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400127}
128
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400129bool GrOpList::dependsOn(const GrOpList* dependedOn) const {
130 for (int i = 0; i < fDependencies.count(); ++i) {
131 if (fDependencies[i] == dependedOn) {
132 return true;
133 }
134 }
135
136 return false;
137}
138
Robert Phillipse6d06182018-08-06 16:56:26 -0400139
140void GrOpList::addDependent(GrOpList* dependent) {
141 fDependents.push_back(dependent);
142}
143
144#ifdef SK_DEBUG
145bool GrOpList::isDependedent(const GrOpList* dependent) const {
146 for (int i = 0; i < fDependents.count(); ++i) {
147 if (fDependents[i] == dependent) {
148 return true;
149 }
150 }
151
152 return false;
153}
154
155void GrOpList::validate() const {
156 // TODO: check for loops and duplicates
157
158 for (int i = 0; i < fDependencies.count(); ++i) {
159 SkASSERT(fDependencies[i]->isDependedent(this));
160 }
161}
162#endif
163
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400164bool GrOpList::isInstantiated() const { return fTarget.get()->isInstantiated(); }
Robert Phillips09dfc472017-09-13 15:25:47 -0400165
Robert Phillips46acf9d2018-10-09 09:31:40 -0400166void GrOpList::closeThoseWhoDependOnMe(const GrCaps& caps) {
167 for (int i = 0; i < fDependents.count(); ++i) {
168 if (!fDependents[i]->isClosed()) {
169 fDependents[i]->makeClosed(caps);
170 }
171 }
172}
173
Robert Phillips01a91282018-07-26 08:03:04 -0400174bool GrOpList::isFullyInstantiated() const {
175 if (!this->isInstantiated()) {
176 return false;
177 }
178
179 GrSurfaceProxy* proxy = fTarget.get();
180 bool needsStencil = proxy->asRenderTargetProxy()
181 ? proxy->asRenderTargetProxy()->needsStencil()
182 : false;
183
184 if (needsStencil) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400185 GrRenderTarget* rt = proxy->peekRenderTarget();
Robert Phillips01a91282018-07-26 08:03:04 -0400186
187 if (!rt->renderTargetPriv().getStencilAttachment()) {
188 return false;
189 }
190 }
191
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400192 GrSurface* surface = proxy->peekSurface();
193 if (surface->wasDestroyed()) {
194 return false;
195 }
196
Robert Phillips01a91282018-07-26 08:03:04 -0400197 return true;
198}
199
Robert Phillips4150eea2018-02-07 17:08:21 -0500200#ifdef SK_DEBUG
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400201static const char* op_to_name(GrLoadOp op) {
202 return GrLoadOp::kLoad == op ? "load" : GrLoadOp::kClear == op ? "clear" : "discard";
203}
204
Robert Phillips27483912018-04-20 12:43:18 -0400205void GrOpList::dump(bool printDependencies) const {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400206 SkDebugf("--------------------------------------------------------------\n");
Robert Phillipsba5c4392018-07-25 12:37:14 -0400207 SkDebugf("opListID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID,
208 fTarget.get() ? fTarget.get()->uniqueID().asUInt() : -1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400209 fTarget.get() && fTarget.get()->peekSurface()
210 ? fTarget.get()->peekSurface()->uniqueID().asUInt()
211 : -1);
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400212 SkDebugf("ColorLoadOp: %s %x StencilLoadOp: %s\n",
213 op_to_name(fColorLoadOp),
Brian Osman9a9baae2018-11-05 15:06:26 -0500214 GrLoadOp::kClear == fColorLoadOp ? fLoadClearColor.toBytes_RGBA() : 0x0,
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400215 op_to_name(fStencilLoadOp));
Robert Phillips27483912018-04-20 12:43:18 -0400216
217 if (printDependencies) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400218 SkDebugf("I rely On (%d): ", fDependencies.count());
Robert Phillips27483912018-04-20 12:43:18 -0400219 for (int i = 0; i < fDependencies.count(); ++i) {
220 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
221 }
222 SkDebugf("\n");
Robert Phillipse6d06182018-08-06 16:56:26 -0400223
224 SkDebugf("(%d) Rely On Me: ", fDependents.count());
225 for (int i = 0; i < fDependents.count(); ++i) {
226 SkDebugf("%d, ", fDependents[i]->fUniqueID);
227 }
228 SkDebugf("\n");
Robert Phillipsf2361d22016-10-25 14:20:06 -0400229 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400230}
231#endif