blob: 4c20a240e29ad049b392e9b31c597ccb29d460f2 [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 Phillips7eeb74f2019-03-29 07:26:46 -040057// TODO: this can go away when explicit allocation has stuck
Robert Phillips318c4192017-05-17 09:36:38 -040058bool GrOpList::instantiate(GrResourceProvider* resourceProvider) {
Robert Phillips7eeb74f2019-03-29 07:26:46 -040059 if (resourceProvider->explicitlyAllocateGPUResources()) {
60 SkASSERT(fTarget.get()->isInstantiated());
61 return true;
62 } else {
63 return SkToBool(fTarget.get()->instantiate(resourceProvider));
64 }
Robert Phillips318c4192017-05-17 09:36:38 -040065}
66
Chris Daltona84cacf2017-10-04 10:30:29 -060067void GrOpList::endFlush() {
Robert Phillips6cdc22c2017-05-11 16:29:14 -040068 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
69 fTarget.get()->setLastOpList(nullptr);
70 }
71
Robert Phillips5efd5ea2017-05-30 13:47:32 -040072 fTarget.reset();
Brian Osman099fa0f2017-10-02 16:38:32 -040073 fDeferredProxies.reset();
Robert Phillips6cdc22c2017-05-11 16:29:14 -040074 fAuditTrail = nullptr;
75}
76
Brian Osman099fa0f2017-10-02 16:38:32 -040077void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) {
78 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Robert Phillips4150eea2018-02-07 17:08:21 -050079 if (resourceProvider->explicitlyAllocateGPUResources()) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040080 SkASSERT(fDeferredProxies[i]->isInstantiated());
Robert Phillips4150eea2018-02-07 17:08:21 -050081 } else {
82 fDeferredProxies[i]->instantiate(resourceProvider);
83 }
Brian Osman099fa0f2017-10-02 16:38:32 -040084 }
Brian Osman5d034742017-09-11 13:38:55 -040085}
86
Brian Osman407b3422017-08-22 15:01:32 -040087void GrOpList::prepare(GrOpFlushState* flushState) {
Brian Osman099fa0f2017-10-02 16:38:32 -040088 for (int i = 0; i < fDeferredProxies.count(); ++i) {
89 fDeferredProxies[i]->texPriv().scheduleUpload(flushState);
Brian Osman407b3422017-08-22 15:01:32 -040090 }
91
92 this->onPrepare(flushState);
93}
94
Robert Phillipsf2361d22016-10-25 14:20:06 -040095// Add a GrOpList-based dependency
96void GrOpList::addDependency(GrOpList* dependedOn) {
97 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
98
99 if (this->dependsOn(dependedOn)) {
100 return; // don't add duplicate dependencies
101 }
102
Robert Phillips73fd0d62017-09-15 11:48:08 +0000103 fDependencies.push_back(dependedOn);
Robert Phillipse6d06182018-08-06 16:56:26 -0400104 dependedOn->addDependent(this);
105
106 SkDEBUGCODE(this->validate());
Robert Phillipsf2361d22016-10-25 14:20:06 -0400107}
108
109// Convert from a GrSurface-based dependency to a GrOpList one
Robert Phillipsee683652017-04-26 11:53:10 -0400110void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400111 if (dependedOn->getLastOpList()) {
112 // If it is still receiving dependencies, this GrOpList shouldn't be closed
113 SkASSERT(!this->isClosed());
114
115 GrOpList* opList = dependedOn->getLastOpList();
116 if (opList == this) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400117 // self-read - presumably for dst reads. We can't make it closed in the self-read case.
Robert Phillipsf2361d22016-10-25 14:20:06 -0400118 } else {
119 this->addDependency(opList);
120
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400121 // We are closing 'opList' here bc the current contents of it are what 'this' opList
122 // depends on. We need a break in 'opList' so that the usage of that state has a
123 // chance to execute.
Robert Phillipsee683652017-04-26 11:53:10 -0400124 opList->makeClosed(caps);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400125 }
126 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400127
128 if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) {
129 if (textureProxy->texPriv().isDeferred()) {
130 fDeferredProxies.push_back(textureProxy);
131 }
132 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400133}
134
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400135bool GrOpList::dependsOn(const GrOpList* dependedOn) const {
136 for (int i = 0; i < fDependencies.count(); ++i) {
137 if (fDependencies[i] == dependedOn) {
138 return true;
139 }
140 }
141
142 return false;
143}
144
Robert Phillipse6d06182018-08-06 16:56:26 -0400145
146void GrOpList::addDependent(GrOpList* dependent) {
147 fDependents.push_back(dependent);
148}
149
150#ifdef SK_DEBUG
151bool GrOpList::isDependedent(const GrOpList* dependent) const {
152 for (int i = 0; i < fDependents.count(); ++i) {
153 if (fDependents[i] == dependent) {
154 return true;
155 }
156 }
157
158 return false;
159}
160
161void GrOpList::validate() const {
162 // TODO: check for loops and duplicates
163
164 for (int i = 0; i < fDependencies.count(); ++i) {
165 SkASSERT(fDependencies[i]->isDependedent(this));
166 }
167}
168#endif
169
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400170bool GrOpList::isInstantiated() const { return fTarget.get()->isInstantiated(); }
Robert Phillips09dfc472017-09-13 15:25:47 -0400171
Robert Phillips46acf9d2018-10-09 09:31:40 -0400172void GrOpList::closeThoseWhoDependOnMe(const GrCaps& caps) {
173 for (int i = 0; i < fDependents.count(); ++i) {
174 if (!fDependents[i]->isClosed()) {
175 fDependents[i]->makeClosed(caps);
176 }
177 }
178}
179
Robert Phillips01a91282018-07-26 08:03:04 -0400180bool GrOpList::isFullyInstantiated() const {
181 if (!this->isInstantiated()) {
182 return false;
183 }
184
185 GrSurfaceProxy* proxy = fTarget.get();
186 bool needsStencil = proxy->asRenderTargetProxy()
187 ? proxy->asRenderTargetProxy()->needsStencil()
188 : false;
189
190 if (needsStencil) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400191 GrRenderTarget* rt = proxy->peekRenderTarget();
Robert Phillips01a91282018-07-26 08:03:04 -0400192
193 if (!rt->renderTargetPriv().getStencilAttachment()) {
194 return false;
195 }
196 }
197
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400198 GrSurface* surface = proxy->peekSurface();
199 if (surface->wasDestroyed()) {
200 return false;
201 }
202
Robert Phillips01a91282018-07-26 08:03:04 -0400203 return true;
204}
205
Robert Phillips4150eea2018-02-07 17:08:21 -0500206#ifdef SK_DEBUG
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400207static const char* op_to_name(GrLoadOp op) {
208 return GrLoadOp::kLoad == op ? "load" : GrLoadOp::kClear == op ? "clear" : "discard";
209}
210
Robert Phillips27483912018-04-20 12:43:18 -0400211void GrOpList::dump(bool printDependencies) const {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400212 SkDebugf("--------------------------------------------------------------\n");
Robert Phillipsba5c4392018-07-25 12:37:14 -0400213 SkDebugf("opListID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID,
214 fTarget.get() ? fTarget.get()->uniqueID().asUInt() : -1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400215 fTarget.get() && fTarget.get()->peekSurface()
216 ? fTarget.get()->peekSurface()->uniqueID().asUInt()
217 : -1);
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400218 SkDebugf("ColorLoadOp: %s %x StencilLoadOp: %s\n",
219 op_to_name(fColorLoadOp),
Brian Osman9a9baae2018-11-05 15:06:26 -0500220 GrLoadOp::kClear == fColorLoadOp ? fLoadClearColor.toBytes_RGBA() : 0x0,
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400221 op_to_name(fStencilLoadOp));
Robert Phillips27483912018-04-20 12:43:18 -0400222
223 if (printDependencies) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400224 SkDebugf("I rely On (%d): ", fDependencies.count());
Robert Phillips27483912018-04-20 12:43:18 -0400225 for (int i = 0; i < fDependencies.count(); ++i) {
226 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
227 }
228 SkDebugf("\n");
Robert Phillipse6d06182018-08-06 16:56:26 -0400229
230 SkDebugf("(%d) Rely On Me: ", fDependents.count());
231 for (int i = 0; i < fDependents.count(); ++i) {
232 SkDebugf("%d, ", fDependents[i]->fUniqueID);
233 }
234 SkDebugf("\n");
Robert Phillipsf2361d22016-10-25 14:20:06 -0400235 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400236}
237#endif