blob: b1a5a8b05d39d4b5f0058cf2eb2b5c913beb7011 [file] [log] [blame]
Chris Dalton6b498102019-08-01 14:14:52 -06001/*
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"
Chris Daltone2a903e2019-09-18 13:41:50 -060013#include "src/gpu/GrTextureResolveRenderTask.h"
Chris Dalton6b498102019-08-01 14:14:52 -060014
15uint32_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
Greg Daniel16f5c652019-10-29 11:26:01 -040024GrRenderTask::GrRenderTask()
25 : fUniqueID(CreateUniqueID())
26 , fFlags(0) {
27}
28
29GrRenderTask::GrRenderTask(GrSurfaceProxyView targetView)
30 : fTargetView(std::move(targetView))
Chris Dalton6b498102019-08-01 14:14:52 -060031 , fUniqueID(CreateUniqueID())
32 , fFlags(0) {
Chris Dalton6b498102019-08-01 14:14:52 -060033}
34
35GrRenderTask::~GrRenderTask() {
Greg Daniel16f5c652019-10-29 11:26:01 -040036 GrSurfaceProxy* proxy = fTargetView.proxy();
37 if (proxy && this == proxy->getLastRenderTask()) {
Chris Dalton6b498102019-08-01 14:14:52 -060038 // Ensure the target proxy doesn't keep hold of a dangling back pointer.
Greg Daniel16f5c652019-10-29 11:26:01 -040039 proxy->setLastRenderTask(nullptr);
Chris Dalton6b498102019-08-01 14:14:52 -060040 }
41}
42
43#ifdef SK_DEBUG
44bool 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
Chris Daltonaa3cbb82019-08-21 00:01:21 -060055void GrRenderTask::makeClosed(const GrCaps& caps) {
56 if (this->isClosed()) {
57 return;
58 }
59
Chris Dalton16a33c62019-09-24 22:19:17 -060060 SkIRect targetUpdateBounds;
61 if (ExpectedOutcome::kTargetDirty == this->onMakeClosed(caps, &targetUpdateBounds)) {
Greg Daniel16f5c652019-10-29 11:26:01 -040062 GrSurfaceProxy* proxy = fTargetView.proxy();
63 SkASSERT(SkIRect::MakeSize(proxy->dimensions()).contains(targetUpdateBounds));
64 if (proxy->requiresManualMSAAResolve()) {
65 SkASSERT(fTargetView.asRenderTargetProxy());
66 fTargetView.asRenderTargetProxy()->markMSAADirty(targetUpdateBounds);
Chris Dalton4ece96d2019-08-30 11:26:39 -060067 }
Greg Daniel16f5c652019-10-29 11:26:01 -040068 GrTextureProxy* textureProxy = fTargetView.asTextureProxy();
Chris Daltonaa3cbb82019-08-21 00:01:21 -060069 if (textureProxy && GrMipMapped::kYes == textureProxy->mipMapped()) {
70 textureProxy->markMipMapsDirty();
71 }
72 }
73
Chris Daltone2a903e2019-09-18 13:41:50 -060074 if (fTextureResolveTask) {
75 this->addDependency(fTextureResolveTask);
76 fTextureResolveTask->makeClosed(caps);
77 fTextureResolveTask = nullptr;
78 }
79
Chris Daltonaa3cbb82019-08-21 00:01:21 -060080 this->setFlag(kClosed_Flag);
81}
82
Chris Dalton6b498102019-08-01 14:14:52 -060083void 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
92void GrRenderTask::addDependency(GrRenderTask* dependedOn) {
93 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
Chris Daltondc9a74f2019-09-18 10:26:16 -060094 SkASSERT(!this->dependsOn(dependedOn)); // caller should weed out duplicates
Chris Dalton6b498102019-08-01 14:14:52 -060095
96 fDependencies.push_back(dependedOn);
97 dependedOn->addDependent(this);
98
99 SkDEBUGCODE(this->validate());
100}
101
Greg Danielc30f1a92019-09-06 15:28:58 -0400102void GrRenderTask::addDependenciesFromOtherTask(GrRenderTask* otherTask) {
103 SkASSERT(otherTask);
Chris Daltondc9a74f2019-09-18 10:26:16 -0600104 for (GrRenderTask* task : otherTask->fDependencies) {
Greg Danielc30f1a92019-09-06 15:28:58 -0400105 // The task should not be adding a dependency to itself.
Chris Daltondc9a74f2019-09-18 10:26:16 -0600106 SkASSERT(task != this);
107 if (!this->dependsOn(task)) {
108 this->addDependency(task);
109 }
Greg Danielc30f1a92019-09-06 15:28:58 -0400110 }
111}
112
Chris Dalton6b498102019-08-01 14:14:52 -0600113// Convert from a GrSurface-based dependency to a GrRenderTask one
Chris Dalton3d770272019-08-14 09:24:37 -0600114void GrRenderTask::addDependency(GrSurfaceProxy* dependedOn, GrMipMapped mipMapped,
115 GrTextureResolveManager textureResolveManager,
Chris Dalton08755122019-08-05 16:13:47 -0600116 const GrCaps& caps) {
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600117 // If it is still receiving dependencies, this GrRenderTask shouldn't be closed
118 SkASSERT(!this->isClosed());
Chris Dalton3d770272019-08-14 09:24:37 -0600119
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600120 GrRenderTask* dependedOnTask = dependedOn->getLastRenderTask();
121
122 if (dependedOnTask == this) {
Chris Dalton4ece96d2019-08-30 11:26:39 -0600123 // 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.
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600125 SkASSERT(GrMipMapped::kNo == mipMapped);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600126 // We should never attempt a self-read on a surface that has a separate MSAA renderbuffer.
127 SkASSERT(!dependedOn->requiresManualMSAAResolve());
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600128 SkASSERT(!dependedOn->asTextureProxy() ||
129 !dependedOn->asTextureProxy()->texPriv().isDeferred());
130 return;
131 }
132
133 if (dependedOnTask) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600134 if (this->dependsOn(dependedOnTask) || fTextureResolveTask == dependedOnTask) {
135 return; // don't add duplicate dependencies
136 }
137
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600138 // 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
Chris Dalton4ece96d2019-08-30 11:26:39 -0600144 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
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600154 GrTextureProxy* textureProxy = dependedOn->asTextureProxy();
Chris Dalton3d770272019-08-14 09:24:37 -0600155 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;
Chris Dalton4ece96d2019-08-30 11:26:39 -0600161 } else if (textureProxy->mipMapsAreDirty()) {
162 resolveFlags |= GrSurfaceProxy::ResolveFlags::kMipMaps;
Chris Dalton3d770272019-08-14 09:24:37 -0600163 }
164 }
165
Chris Dalton4ece96d2019-08-30 11:26:39 -0600166 // Does this proxy have msaa to resolve and/or mipmaps to regenerate?
167 if (GrSurfaceProxy::ResolveFlags::kNone != resolveFlags) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600168 if (!fTextureResolveTask) {
169 fTextureResolveTask = textureResolveManager.newTextureResolveRenderTask(caps);
170 }
Greg Daniel16f5c652019-10-29 11:26:01 -0400171 fTextureResolveTask->addProxy(
172 GrSurfaceProxyView(sk_ref_sp(dependedOn), dependedOn->origin(), GrSwizzle()),
173 resolveFlags, caps);
Chris Daltone2a903e2019-09-18 13:41:50 -0600174
175 // addProxy() should have closed the texture proxy's previous task.
176 SkASSERT(!dependedOnTask || dependedOnTask->isClosed());
177 SkASSERT(dependedOn->getLastRenderTask() == fTextureResolveTask);
Chris Dalton3d770272019-08-14 09:24:37 -0600178
Chris Dalton4ece96d2019-08-30 11:26:39 -0600179#ifdef SK_DEBUG
Chris Daltone2a903e2019-09-18 13:41:50 -0600180 // addProxy() should have called addDependency (in this instance, recursively) on
181 // fTextureResolveTask.
Chris Dalton4ece96d2019-08-30 11:26:39 -0600182 if (dependedOnTask) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600183 SkASSERT(fTextureResolveTask->dependsOn(dependedOnTask));
Chris Dalton4ece96d2019-08-30 11:26:39 -0600184 }
185 if (textureProxy && textureProxy->texPriv().isDeferred()) {
Chris Daltone2a903e2019-09-18 13:41:50 -0600186 SkASSERT(fTextureResolveTask->fDeferredProxies.back() == textureProxy);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600187 }
Chris Dalton3d770272019-08-14 09:24:37 -0600188
Chris Dalton4ece96d2019-08-30 11:26:39 -0600189 // The GrTextureResolveRenderTask factory should have also marked the proxy clean, set the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400190 // last renderTask on the textureProxy to textureResolveTask, and closed textureResolveTask.
Chris Dalton4ece96d2019-08-30 11:26:39 -0600191 if (GrRenderTargetProxy* renderTargetProxy = dependedOn->asRenderTargetProxy()) {
192 SkASSERT(!renderTargetProxy->isMSAADirty());
193 }
194 if (textureProxy) {
195 SkASSERT(!textureProxy->mipMapsAreDirty());
196 }
Chris Daltone2a903e2019-09-18 13:41:50 -0600197 SkASSERT(dependedOn->getLastRenderTask() == fTextureResolveTask);
Chris Dalton4ece96d2019-08-30 11:26:39 -0600198#endif
Chris Daltone2a903e2019-09-18 13:41:50 -0600199 return;
200 }
Chris Dalton3d770272019-08-14 09:24:37 -0600201
Chris Daltone2a903e2019-09-18 13:41:50 -0600202 if (textureProxy && textureProxy->texPriv().isDeferred()) {
Chris Dalton3d770272019-08-14 09:24:37 -0600203 fDeferredProxies.push_back(textureProxy);
204 }
205
206 if (dependedOnTask) {
Chris Daltonaa3cbb82019-08-21 00:01:21 -0600207 this->addDependency(dependedOnTask);
Chris Dalton6b498102019-08-01 14:14:52 -0600208 }
Chris Dalton6b498102019-08-01 14:14:52 -0600209}
210
211bool GrRenderTask::dependsOn(const GrRenderTask* dependedOn) const {
212 for (int i = 0; i < fDependencies.count(); ++i) {
213 if (fDependencies[i] == dependedOn) {
214 return true;
215 }
216 }
217
218 return false;
219}
220
221
222void GrRenderTask::addDependent(GrRenderTask* dependent) {
223 fDependents.push_back(dependent);
224}
225
226#ifdef SK_DEBUG
227bool GrRenderTask::isDependedent(const GrRenderTask* dependent) const {
228 for (int i = 0; i < fDependents.count(); ++i) {
229 if (fDependents[i] == dependent) {
230 return true;
231 }
232 }
233
234 return false;
235}
236
237void GrRenderTask::validate() const {
238 // TODO: check for loops and duplicates
239
240 for (int i = 0; i < fDependencies.count(); ++i) {
241 SkASSERT(fDependencies[i]->isDependedent(this));
242 }
243}
244#endif
245
246void GrRenderTask::closeThoseWhoDependOnMe(const GrCaps& caps) {
247 for (int i = 0; i < fDependents.count(); ++i) {
248 if (!fDependents[i]->isClosed()) {
249 fDependents[i]->makeClosed(caps);
250 }
251 }
252}
253
254bool GrRenderTask::isInstantiated() const {
Greg Danielbbfec9d2019-08-20 10:56:51 -0400255 // Some renderTasks (e.g. GrTransferFromRenderTask) don't have a target.
Greg Daniel16f5c652019-10-29 11:26:01 -0400256 GrSurfaceProxy* proxy = fTargetView.proxy();
257 if (!proxy) {
Greg Danielbbfec9d2019-08-20 10:56:51 -0400258 return true;
259 }
260
Greg Daniel16f5c652019-10-29 11:26:01 -0400261 if (!proxy->isInstantiated()) {
Chris Dalton6b498102019-08-01 14:14:52 -0600262 return false;
263 }
264
Greg Daniel16f5c652019-10-29 11:26:01 -0400265 GrSurface* surface = proxy->peekSurface();
Chris Dalton6b498102019-08-01 14:14:52 -0600266 if (surface->wasDestroyed()) {
267 return false;
268 }
269
270 return true;
271}
272
273#ifdef SK_DEBUG
274void GrRenderTask::dump(bool printDependencies) const {
275 SkDebugf("--------------------------------------------------------------\n");
Greg Daniel16f5c652019-10-29 11:26:01 -0400276 GrSurfaceProxy* proxy = fTargetView.proxy();
Chris Dalton6b498102019-08-01 14:14:52 -0600277 SkDebugf("renderTaskID: %d - proxyID: %d - surfaceID: %d\n", fUniqueID,
Greg Daniel16f5c652019-10-29 11:26:01 -0400278 proxy ? proxy->uniqueID().asUInt() : -1,
279 proxy && proxy->peekSurface()
280 ? proxy->peekSurface()->uniqueID().asUInt()
Chris Dalton6b498102019-08-01 14:14:52 -0600281 : -1);
282
283 if (printDependencies) {
284 SkDebugf("I rely On (%d): ", fDependencies.count());
285 for (int i = 0; i < fDependencies.count(); ++i) {
286 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
287 }
288 SkDebugf("\n");
289
290 SkDebugf("(%d) Rely On Me: ", fDependents.count());
291 for (int i = 0; i < fDependents.count(); ++i) {
292 SkDebugf("%d, ", fDependents[i]->fUniqueID);
293 }
294 SkDebugf("\n");
295 }
296}
297#endif