1 /*
2 * Copyright 2019 Google LLC
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 "src/gpu/GrRenderTask.h"
9
10 #include "src/gpu/GrRenderTargetPriv.h"
11 #include "src/gpu/GrStencilAttachment.h"
12 #include "src/gpu/GrTextureProxyPriv.h"
13
CreateUniqueID()14 uint32_t GrRenderTask::CreateUniqueID() {
15 static std::atomic<uint32_t> nextID{1};
16 uint32_t id;
17 do {
18 id = nextID++;
19 } while (id == SK_InvalidUniqueID);
20 return id;
21 }
22
GrRenderTask(sk_sp<GrSurfaceProxy> target)23 GrRenderTask::GrRenderTask(sk_sp<GrSurfaceProxy> target)
24 : fTarget(std::move(target))
25 , fUniqueID(CreateUniqueID())
26 , fFlags(0) {
27 }
28
~GrRenderTask()29 GrRenderTask::~GrRenderTask() {
30 if (fTarget && this == fTarget->getLastRenderTask()) {
31 // Ensure the target proxy doesn't keep hold of a dangling back pointer.
32 fTarget->setLastRenderTask(nullptr);
33 }
34 }
35
36 #ifdef SK_DEBUG
deferredProxiesAreInstantiated() const37 bool GrRenderTask::deferredProxiesAreInstantiated() const {
38 for (int i = 0; i < fDeferredProxies.count(); ++i) {
39 if (!fDeferredProxies[i]->isInstantiated()) {
40 return false;
41 }
42 }
43
44 return true;
45 }
46 #endif
47
prepare(GrOpFlushState * flushState)48 void GrRenderTask::prepare(GrOpFlushState* flushState) {
49 for (int i = 0; i < fDeferredProxies.count(); ++i) {
50 fDeferredProxies[i]->texPriv().scheduleUpload(flushState);
51 }
52
53 this->onPrepare(flushState);
54 }
55
56 // Add a GrRenderTask-based dependency
addDependency(GrRenderTask * dependedOn)57 void GrRenderTask::addDependency(GrRenderTask* dependedOn) {
58 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
59
60 if (this->dependsOn(dependedOn)) {
61 return; // don't add duplicate dependencies
62 }
63
64 fDependencies.push_back(dependedOn);
65 dependedOn->addDependent(this);
66
67 SkDEBUGCODE(this->validate());
68 }
69
70 // Convert from a GrSurface-based dependency to a GrRenderTask one
addDependency(GrSurfaceProxy * dependedOn,GrMipMapped mipMapped,GrTextureResolveManager textureResolveManager,const GrCaps & caps)71 void GrRenderTask::addDependency(GrSurfaceProxy* dependedOn, GrMipMapped mipMapped,
72 GrTextureResolveManager textureResolveManager,
73 const GrCaps& caps) {
74 GrRenderTask* dependedOnTask = dependedOn->getLastRenderTask();
75 GrTextureProxy* textureProxy = dependedOn->asTextureProxy();
76
77 if (GrMipMapped::kYes == mipMapped) {
78 SkASSERT(textureProxy);
79 if (GrMipMapped::kYes != textureProxy->mipMapped()) {
80 // There are some cases where we might be given a non-mipmapped texture with a mipmap
81 // filter. See skbug.com/7094.
82 mipMapped = GrMipMapped::kNo;
83 }
84 }
85
86 // Does this proxy have mipmaps that need to be regenerated?
87 if (GrMipMapped::kYes == mipMapped && textureProxy->mipMapsAreDirty()) {
88 // We only read our own target during dst reads, and we shouldn't use mipmaps in that case.
89 SkASSERT(dependedOnTask != this);
90
91 // Create an opList that resolves the texture's mipmap data.
92 GrRenderTask* textureResolveTask = textureResolveManager.newTextureResolveRenderTask(
93 sk_ref_sp(textureProxy), GrTextureResolveFlags::kMipMaps, caps);
94
95 // The GrTextureResolveRenderTask factory should have called addDependency (in this
96 // instance, recursively) on the textureProxy.
97 SkASSERT(!dependedOnTask || textureResolveTask->dependsOn(dependedOnTask));
98 SkASSERT(!textureProxy->texPriv().isDeferred() ||
99 textureResolveTask->fDeferredProxies.back() == textureProxy);
100
101 // The GrTextureResolveRenderTask factory should have also marked the mipmaps clean and set
102 // the last opList on the textureProxy to textureResolveTask.
103 SkASSERT(!textureProxy->mipMapsAreDirty());
104 SkASSERT(textureProxy->getLastRenderTask() == textureResolveTask);
105
106 // Fall through and add textureResolveTask as a dependency of "this".
107 dependedOnTask = textureResolveTask;
108 } else if (textureProxy && textureProxy->texPriv().isDeferred()) {
109 fDeferredProxies.push_back(textureProxy);
110 }
111
112 if (dependedOnTask) {
113 // If it is still receiving dependencies, this GrRenderTask shouldn't be closed
114 SkASSERT(!this->isClosed());
115
116 if (dependedOnTask == this) {
117 // self-read - presumably for dst reads. We can't make it closed in the self-read case.
118 } else {
119 this->addDependency(dependedOnTask);
120
121 // We are closing 'dependedOnTask' here bc the current contents of it are what 'this'
122 // dependedOnTask depends on. We need a break in 'dependedOnTask' so that the usage of
123 // that state has a chance to execute.
124 dependedOnTask->makeClosed(caps);
125 }
126 }
127 }
128
dependsOn(const GrRenderTask * dependedOn) const129 bool GrRenderTask::dependsOn(const GrRenderTask* 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
139
addDependent(GrRenderTask * dependent)140 void GrRenderTask::addDependent(GrRenderTask* dependent) {
141 fDependents.push_back(dependent);
142 }
143
144 #ifdef SK_DEBUG
isDependedent(const GrRenderTask * dependent) const145 bool GrRenderTask::isDependedent(const GrRenderTask* 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
validate() const155 void GrRenderTask::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
closeThoseWhoDependOnMe(const GrCaps & caps)164 void GrRenderTask::closeThoseWhoDependOnMe(const GrCaps& caps) {
165 for (int i = 0; i < fDependents.count(); ++i) {
166 if (!fDependents[i]->isClosed()) {
167 fDependents[i]->makeClosed(caps);
168 }
169 }
170 }
171
isInstantiated() const172 bool GrRenderTask::isInstantiated() const {
173 if (!fTarget->isInstantiated()) {
174 return false;
175 }
176
177 int minStencilSampleCount = (fTarget->asRenderTargetProxy())
178 ? fTarget->asRenderTargetProxy()->numStencilSamples()
179 : 0;
180
181 if (minStencilSampleCount) {
182 GrRenderTarget* rt = fTarget->peekRenderTarget();
183 SkASSERT(rt);
184
185 GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
186 if (!stencil) {
187 return false;
188 }
189 SkASSERT(stencil->numSamples() >= minStencilSampleCount);
190 }
191
192 GrSurface* surface = fTarget->peekSurface();
193 if (surface->wasDestroyed()) {
194 return false;
195 }
196
197 return true;
198 }
199
200 #ifdef SK_DEBUG
dump(bool printDependencies) const201 void GrRenderTask::dump(bool printDependencies) const {
202 SkDebugf("--------------------------------------------------------------\n");
203 SkDebugf("renderTaskID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID,
204 fTarget ? fTarget->uniqueID().asUInt() : -1,
205 fTarget && fTarget->peekSurface()
206 ? fTarget->peekSurface()->uniqueID().asUInt()
207 : -1);
208
209 if (printDependencies) {
210 SkDebugf("I rely On (%d): ", fDependencies.count());
211 for (int i = 0; i < fDependencies.count(); ++i) {
212 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
213 }
214 SkDebugf("\n");
215
216 SkDebugf("(%d) Rely On Me: ", fDependents.count());
217 for (int i = 0; i < fDependents.count(); ++i) {
218 SkDebugf("%d, ", fDependents[i]->fUniqueID);
219 }
220 SkDebugf("\n");
221 }
222 }
223 #endif
224