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 | #include "src/gpu/GrTextureResolveRenderTask.h" |
14 | |
15 | uint32_t GrRenderTask::CreateUniqueID() { |
16 | static std::atomic<uint32_t> nextID{1}; |
17 | uint32_t id; |
18 | do { |
19 | id = nextID++; |
20 | } while (id == SK_InvalidUniqueID); |
21 | return id; |
22 | } |
23 | |
24 | GrRenderTask::GrRenderTask() |
25 | : fUniqueID(CreateUniqueID()) |
26 | , fFlags(0) { |
27 | } |
28 | |
29 | GrRenderTask::GrRenderTask(GrSurfaceProxyView targetView) |
30 | : fTargetView(std::move(targetView)) |
31 | , fUniqueID(CreateUniqueID()) |
32 | , fFlags(0) { |
33 | } |
34 | |
35 | GrRenderTask::~GrRenderTask() { |
36 | GrSurfaceProxy* proxy = fTargetView.proxy(); |
37 | if (proxy && this == proxy->getLastRenderTask()) { |
38 | // Ensure the target proxy doesn't keep hold of a dangling back pointer. |
39 | proxy->setLastRenderTask(nullptr); |
40 | } |
41 | } |
42 | |
43 | #ifdef SK_DEBUG |
44 | bool GrRenderTask::deferredProxiesAreInstantiated() const { |
45 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
46 | if (!fDeferredProxies[i]->isInstantiated()) { |
47 | return false; |
48 | } |
49 | } |
50 | |
51 | return true; |
52 | } |
53 | #endif |
54 | |
55 | void GrRenderTask::makeClosed(const GrCaps& caps) { |
56 | if (this->isClosed()) { |
57 | return; |
58 | } |
59 | |
60 | SkIRect targetUpdateBounds; |
61 | if (ExpectedOutcome::kTargetDirty == this->onMakeClosed(caps, &targetUpdateBounds)) { |
62 | GrSurfaceProxy* proxy = fTargetView.proxy(); |
63 | if (proxy->requiresManualMSAAResolve()) { |
64 | SkASSERT(fTargetView.asRenderTargetProxy()); |
65 | fTargetView.asRenderTargetProxy()->markMSAADirty(targetUpdateBounds, |
66 | fTargetView.origin()); |
67 | } |
68 | GrTextureProxy* textureProxy = fTargetView.asTextureProxy(); |
69 | if (textureProxy && GrMipMapped::kYes == textureProxy->mipMapped()) { |
70 | textureProxy->markMipMapsDirty(); |
71 | } |
72 | } |
73 | |
74 | if (fTextureResolveTask) { |
75 | this->addDependency(fTextureResolveTask); |
76 | fTextureResolveTask->makeClosed(caps); |
77 | fTextureResolveTask = nullptr; |
78 | } |
79 | |
80 | this->setFlag(kClosed_Flag); |
81 | } |
82 | |
83 | void GrRenderTask::prepare(GrOpFlushState* flushState) { |
84 | for (int i = 0; i < fDeferredProxies.count(); ++i) { |
85 | fDeferredProxies[i]->texPriv().scheduleUpload(flushState); |
86 | } |
87 | |
88 | this->onPrepare(flushState); |
89 | } |
90 | |
91 | // Add a GrRenderTask-based dependency |
92 | void GrRenderTask::addDependency(GrRenderTask* dependedOn) { |
93 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
94 | SkASSERT(!this->dependsOn(dependedOn)); // caller should weed out duplicates |
95 | |
96 | fDependencies.push_back(dependedOn); |
97 | dependedOn->addDependent(this); |
98 | |
99 | SkDEBUGCODE(this->validate()); |
100 | } |
101 | |
102 | void GrRenderTask::addDependenciesFromOtherTask(GrRenderTask* otherTask) { |
103 | SkASSERT(otherTask); |
104 | for (GrRenderTask* task : otherTask->fDependencies) { |
105 | // The task should not be adding a dependency to itself. |
106 | SkASSERT(task != this); |
107 | if (!this->dependsOn(task)) { |
108 | this->addDependency(task); |
109 | } |
110 | } |
111 | } |
112 | |
113 | // Convert from a GrSurface-based dependency to a GrRenderTask one |
114 | void GrRenderTask::addDependency(GrSurfaceProxy* dependedOn, GrMipMapped mipMapped, |
115 | GrTextureResolveManager textureResolveManager, |
116 | const GrCaps& caps) { |
117 | // If it is still receiving dependencies, this GrRenderTask shouldn't be closed |
118 | SkASSERT(!this->isClosed()); |
119 | |
120 | GrRenderTask* dependedOnTask = dependedOn->getLastRenderTask(); |
121 | |
122 | if (dependedOnTask == this) { |
123 | // self-read - presumably for dst reads. We don't need to do anything in this case. The |
124 | // XferProcessor will detect what is happening and insert a texture barrier. |
125 | SkASSERT(GrMipMapped::kNo == mipMapped); |
126 | // We should never attempt a self-read on a surface that has a separate MSAA renderbuffer. |
127 | SkASSERT(!dependedOn->requiresManualMSAAResolve()); |
128 | SkASSERT(!dependedOn->asTextureProxy() || |
129 | !dependedOn->asTextureProxy()->texPriv().isDeferred()); |
130 | return; |
131 | } |
132 | |
133 | if (dependedOnTask) { |
134 | if (this->dependsOn(dependedOnTask) || fTextureResolveTask == dependedOnTask) { |
135 | return; // don't add duplicate dependencies |
136 | } |
137 | |
138 | // We are closing 'dependedOnTask' here bc the current contents of it are what 'this' |
139 | // renderTask depends on. We need a break in 'dependedOnTask' so that the usage of |
140 | // that state has a chance to execute. |
141 | dependedOnTask->makeClosed(caps); |
142 | } |
143 | |
144 | auto resolveFlags = GrSurfaceProxy::ResolveFlags::kNone; |
145 | |
146 | if (dependedOn->requiresManualMSAAResolve()) { |
147 | auto* renderTargetProxy = dependedOn->asRenderTargetProxy(); |
148 | SkASSERT(renderTargetProxy); |
149 | if (renderTargetProxy->isMSAADirty()) { |
150 | resolveFlags |= GrSurfaceProxy::ResolveFlags::kMSAA; |
151 | } |
152 | } |
153 | |
154 | GrTextureProxy* textureProxy = dependedOn->asTextureProxy(); |
155 | if (GrMipMapped::kYes == mipMapped) { |
156 | SkASSERT(textureProxy); |
157 | if (GrMipMapped::kYes != textureProxy->mipMapped()) { |
158 | // There are some cases where we might be given a non-mipmapped texture with a mipmap |
159 | // filter. See skbug.com/7094. |
160 | mipMapped = GrMipMapped::kNo; |
161 | } else if (textureProxy->mipMapsAreDirty()) { |
162 | resolveFlags |= GrSurfaceProxy::ResolveFlags::kMipMaps; |
163 | } |
164 | } |
165 | |
166 | // Does this proxy have msaa to resolve and/or mipmaps to regenerate? |
167 | if (GrSurfaceProxy::ResolveFlags::kNone != resolveFlags) { |
168 | if (!fTextureResolveTask) { |
169 | fTextureResolveTask = textureResolveManager.newTextureResolveRenderTask(caps); |
170 | } |
171 | fTextureResolveTask->addProxy(sk_ref_sp(dependedOn), resolveFlags, caps); |
172 | |
173 | // addProxy() should have closed the texture proxy's previous task. |
174 | SkASSERT(!dependedOnTask || dependedOnTask->isClosed()); |
175 | SkASSERT(dependedOn->getLastRenderTask() == fTextureResolveTask); |
176 | |
177 | #ifdef SK_DEBUG |
178 | // addProxy() should have called addDependency (in this instance, recursively) on |
179 | // fTextureResolveTask. |
180 | if (dependedOnTask) { |
181 | SkASSERT(fTextureResolveTask->dependsOn(dependedOnTask)); |
182 | } |
183 | if (textureProxy && textureProxy->texPriv().isDeferred()) { |
184 | SkASSERT(fTextureResolveTask->fDeferredProxies.back() == textureProxy); |
185 | } |
186 | |
187 | // The GrTextureResolveRenderTask factory should have also marked the proxy clean, set the |
188 | // last renderTask on the textureProxy to textureResolveTask, and closed textureResolveTask. |
189 | if (GrRenderTargetProxy* renderTargetProxy = dependedOn->asRenderTargetProxy()) { |
190 | SkASSERT(!renderTargetProxy->isMSAADirty()); |
191 | } |
192 | if (textureProxy) { |
193 | SkASSERT(!textureProxy->mipMapsAreDirty()); |
194 | } |
195 | SkASSERT(dependedOn->getLastRenderTask() == fTextureResolveTask); |
196 | #endif |
197 | return; |
198 | } |
199 | |
200 | if (textureProxy && textureProxy->texPriv().isDeferred()) { |
201 | fDeferredProxies.push_back(textureProxy); |
202 | } |
203 | |
204 | if (dependedOnTask) { |
205 | this->addDependency(dependedOnTask); |
206 | } |
207 | } |
208 | |
209 | bool GrRenderTask::dependsOn(const GrRenderTask* dependedOn) const { |
210 | for (int i = 0; i < fDependencies.count(); ++i) { |
211 | if (fDependencies[i] == dependedOn) { |
212 | return true; |
213 | } |
214 | } |
215 | |
216 | return false; |
217 | } |
218 | |
219 | |
220 | void GrRenderTask::addDependent(GrRenderTask* dependent) { |
221 | fDependents.push_back(dependent); |
222 | } |
223 | |
224 | #ifdef SK_DEBUG |
225 | bool GrRenderTask::isDependedent(const GrRenderTask* dependent) const { |
226 | for (int i = 0; i < fDependents.count(); ++i) { |
227 | if (fDependents[i] == dependent) { |
228 | return true; |
229 | } |
230 | } |
231 | |
232 | return false; |
233 | } |
234 | |
235 | void GrRenderTask::validate() const { |
236 | // TODO: check for loops and duplicates |
237 | |
238 | for (int i = 0; i < fDependencies.count(); ++i) { |
239 | SkASSERT(fDependencies[i]->isDependedent(this)); |
240 | } |
241 | } |
242 | #endif |
243 | |
244 | void GrRenderTask::closeThoseWhoDependOnMe(const GrCaps& caps) { |
245 | for (int i = 0; i < fDependents.count(); ++i) { |
246 | if (!fDependents[i]->isClosed()) { |
247 | fDependents[i]->makeClosed(caps); |
248 | } |
249 | } |
250 | } |
251 | |
252 | bool GrRenderTask::isInstantiated() const { |
253 | // Some renderTasks (e.g. GrTransferFromRenderTask) don't have a target. |
254 | GrSurfaceProxy* proxy = fTargetView.proxy(); |
255 | if (!proxy) { |
256 | return true; |
257 | } |
258 | |
259 | if (!proxy->isInstantiated()) { |
260 | return false; |
261 | } |
262 | |
263 | GrSurface* surface = proxy->peekSurface(); |
264 | if (surface->wasDestroyed()) { |
265 | return false; |
266 | } |
267 | |
268 | return true; |
269 | } |
270 | |
271 | #ifdef SK_DEBUG |
272 | void GrRenderTask::dump(bool printDependencies) const { |
273 | SkDebugf("--------------------------------------------------------------\n" ); |
274 | GrSurfaceProxy* proxy = fTargetView.proxy(); |
275 | SkDebugf("renderTaskID: %d - proxyID: %d - surfaceID: %d\n" , fUniqueID, |
276 | proxy ? proxy->uniqueID().asUInt() : -1, |
277 | proxy && proxy->peekSurface() |
278 | ? proxy->peekSurface()->uniqueID().asUInt() |
279 | : -1); |
280 | |
281 | if (printDependencies) { |
282 | SkDebugf("I rely On (%d): " , fDependencies.count()); |
283 | for (int i = 0; i < fDependencies.count(); ++i) { |
284 | SkDebugf("%d, " , fDependencies[i]->fUniqueID); |
285 | } |
286 | SkDebugf("\n" ); |
287 | |
288 | SkDebugf("(%d) Rely On Me: " , fDependents.count()); |
289 | for (int i = 0; i < fDependents.count(); ++i) { |
290 | SkDebugf("%d, " , fDependents[i]->fUniqueID); |
291 | } |
292 | SkDebugf("\n" ); |
293 | } |
294 | } |
295 | #endif |
296 | |