blob: 5cee97d1a829a9d8692077fcaa692e74d5bcf179 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/GrOpList.h"
Robert Phillips5efd5ea2017-05-30 13:47:32 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
11#include "include/private/GrSurfaceProxy.h"
12#include "src/gpu/GrDeferredProxyUploader.h"
13#include "src/gpu/GrMemoryPool.h"
14#include "src/gpu/GrRenderTargetPriv.h"
15#include "src/gpu/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 Phillips12c46292019-04-23 07:36:17 -040027GrOpList::GrOpList(sk_sp<GrOpMemoryPool> opMemoryPool,
28 sk_sp<GrSurfaceProxy> surfaceProxy,
29 GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -040030 : fOpMemoryPool(std::move(opMemoryPool))
31 , fAuditTrail(auditTrail)
32 , fUniqueID(CreateUniqueID())
33 , fFlags(0) {
34 SkASSERT(fOpMemoryPool);
Robert Phillips831a2932019-04-12 17:18:39 -040035 fTarget.setProxy(std::move(surfaceProxy), kWrite_GrIOType);
Robert Phillips6cdc22c2017-05-11 16:29:14 -040036 fTarget.get()->setLastOpList(this);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040037
Robert Phillips5f567c72017-09-14 08:27:37 -040038 fTarget.markPendingIO();
Robert Phillipsf2361d22016-10-25 14:20:06 -040039}
40
41GrOpList::~GrOpList() {
Chris Daltona84cacf2017-10-04 10:30:29 -060042 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
43 // Ensure the target proxy doesn't keep hold of a dangling back pointer.
44 fTarget.get()->setLastOpList(nullptr);
45 }
Robert Phillipsf2361d22016-10-25 14:20:06 -040046}
47
Robert Phillips7eeb74f2019-03-29 07:26:46 -040048// TODO: this can go away when explicit allocation has stuck
Robert Phillips318c4192017-05-17 09:36:38 -040049bool GrOpList::instantiate(GrResourceProvider* resourceProvider) {
Robert Phillips12c46292019-04-23 07:36:17 -040050 SkASSERT(fTarget.get()->isInstantiated());
51 return true;
Robert Phillips318c4192017-05-17 09:36:38 -040052}
53
Chris Daltona84cacf2017-10-04 10:30:29 -060054void GrOpList::endFlush() {
Robert Phillips6cdc22c2017-05-11 16:29:14 -040055 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
56 fTarget.get()->setLastOpList(nullptr);
57 }
58
Robert Phillips5efd5ea2017-05-30 13:47:32 -040059 fTarget.reset();
Brian Osman099fa0f2017-10-02 16:38:32 -040060 fDeferredProxies.reset();
Robert Phillips6cdc22c2017-05-11 16:29:14 -040061 fAuditTrail = nullptr;
62}
63
Brian Osman099fa0f2017-10-02 16:38:32 -040064void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) {
65 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Robert Phillips12c46292019-04-23 07:36:17 -040066 SkASSERT(fDeferredProxies[i]->isInstantiated());
Brian Osman099fa0f2017-10-02 16:38:32 -040067 }
Brian Osman5d034742017-09-11 13:38:55 -040068}
69
Brian Osman407b3422017-08-22 15:01:32 -040070void GrOpList::prepare(GrOpFlushState* flushState) {
Brian Osman099fa0f2017-10-02 16:38:32 -040071 for (int i = 0; i < fDeferredProxies.count(); ++i) {
72 fDeferredProxies[i]->texPriv().scheduleUpload(flushState);
Brian Osman407b3422017-08-22 15:01:32 -040073 }
74
75 this->onPrepare(flushState);
76}
77
Robert Phillipsf2361d22016-10-25 14:20:06 -040078// Add a GrOpList-based dependency
79void GrOpList::addDependency(GrOpList* dependedOn) {
80 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
81
82 if (this->dependsOn(dependedOn)) {
83 return; // don't add duplicate dependencies
84 }
85
Robert Phillips73fd0d62017-09-15 11:48:08 +000086 fDependencies.push_back(dependedOn);
Robert Phillipse6d06182018-08-06 16:56:26 -040087 dependedOn->addDependent(this);
88
89 SkDEBUGCODE(this->validate());
Robert Phillipsf2361d22016-10-25 14:20:06 -040090}
91
92// Convert from a GrSurface-based dependency to a GrOpList one
Robert Phillipsee683652017-04-26 11:53:10 -040093void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) {
Robert Phillipsf2361d22016-10-25 14:20:06 -040094 if (dependedOn->getLastOpList()) {
95 // If it is still receiving dependencies, this GrOpList shouldn't be closed
96 SkASSERT(!this->isClosed());
97
98 GrOpList* opList = dependedOn->getLastOpList();
99 if (opList == this) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400100 // self-read - presumably for dst reads. We can't make it closed in the self-read case.
Robert Phillipsf2361d22016-10-25 14:20:06 -0400101 } else {
102 this->addDependency(opList);
103
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400104 // We are closing 'opList' here bc the current contents of it are what 'this' opList
105 // depends on. We need a break in 'opList' so that the usage of that state has a
106 // chance to execute.
Robert Phillipsee683652017-04-26 11:53:10 -0400107 opList->makeClosed(caps);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400108 }
109 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400110
111 if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) {
112 if (textureProxy->texPriv().isDeferred()) {
113 fDeferredProxies.push_back(textureProxy);
114 }
115 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400116}
117
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400118bool GrOpList::dependsOn(const GrOpList* dependedOn) const {
119 for (int i = 0; i < fDependencies.count(); ++i) {
120 if (fDependencies[i] == dependedOn) {
121 return true;
122 }
123 }
124
125 return false;
126}
127
Robert Phillipse6d06182018-08-06 16:56:26 -0400128
129void GrOpList::addDependent(GrOpList* dependent) {
130 fDependents.push_back(dependent);
131}
132
133#ifdef SK_DEBUG
134bool GrOpList::isDependedent(const GrOpList* dependent) const {
135 for (int i = 0; i < fDependents.count(); ++i) {
136 if (fDependents[i] == dependent) {
137 return true;
138 }
139 }
140
141 return false;
142}
143
144void GrOpList::validate() const {
145 // TODO: check for loops and duplicates
146
147 for (int i = 0; i < fDependencies.count(); ++i) {
148 SkASSERT(fDependencies[i]->isDependedent(this));
149 }
150}
151#endif
152
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400153bool GrOpList::isInstantiated() const { return fTarget.get()->isInstantiated(); }
Robert Phillips09dfc472017-09-13 15:25:47 -0400154
Robert Phillips46acf9d2018-10-09 09:31:40 -0400155void GrOpList::closeThoseWhoDependOnMe(const GrCaps& caps) {
156 for (int i = 0; i < fDependents.count(); ++i) {
157 if (!fDependents[i]->isClosed()) {
158 fDependents[i]->makeClosed(caps);
159 }
160 }
161}
162
Robert Phillips01a91282018-07-26 08:03:04 -0400163bool GrOpList::isFullyInstantiated() const {
164 if (!this->isInstantiated()) {
165 return false;
166 }
167
168 GrSurfaceProxy* proxy = fTarget.get();
169 bool needsStencil = proxy->asRenderTargetProxy()
170 ? proxy->asRenderTargetProxy()->needsStencil()
171 : false;
172
173 if (needsStencil) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400174 GrRenderTarget* rt = proxy->peekRenderTarget();
Robert Phillips01a91282018-07-26 08:03:04 -0400175
176 if (!rt->renderTargetPriv().getStencilAttachment()) {
177 return false;
178 }
179 }
180
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400181 GrSurface* surface = proxy->peekSurface();
182 if (surface->wasDestroyed()) {
183 return false;
184 }
185
Robert Phillips01a91282018-07-26 08:03:04 -0400186 return true;
187}
188
Robert Phillips4150eea2018-02-07 17:08:21 -0500189#ifdef SK_DEBUG
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400190
Robert Phillips27483912018-04-20 12:43:18 -0400191void GrOpList::dump(bool printDependencies) const {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400192 SkDebugf("--------------------------------------------------------------\n");
Robert Phillipsba5c4392018-07-25 12:37:14 -0400193 SkDebugf("opListID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID,
194 fTarget.get() ? fTarget.get()->uniqueID().asUInt() : -1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400195 fTarget.get() && fTarget.get()->peekSurface()
196 ? fTarget.get()->peekSurface()->uniqueID().asUInt()
197 : -1);
Chris Dalton7b2c8552019-05-13 15:49:33 -0600198 SkDebugf("ColorLoadOp: %s %x\n",
199 GrLoadOpName(fColorLoadOp),
200 GrLoadOp::kClear == fColorLoadOp ? fLoadClearColor.toBytes_RGBA() : 0x0);
Robert Phillips27483912018-04-20 12:43:18 -0400201
202 if (printDependencies) {
Robert Phillipsce3c28f2018-07-18 13:52:40 -0400203 SkDebugf("I rely On (%d): ", fDependencies.count());
Robert Phillips27483912018-04-20 12:43:18 -0400204 for (int i = 0; i < fDependencies.count(); ++i) {
205 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
206 }
207 SkDebugf("\n");
Robert Phillipse6d06182018-08-06 16:56:26 -0400208
209 SkDebugf("(%d) Rely On Me: ", fDependents.count());
210 for (int i = 0; i < fDependents.count(); ++i) {
211 SkDebugf("%d, ", fDependents[i]->fUniqueID);
212 }
213 SkDebugf("\n");
Robert Phillipsf2361d22016-10-25 14:20:06 -0400214 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400215}
216#endif