blob: 285bb3c6d6118b0314bea5d6cd422687bfdfc2b0 [file] [log] [blame]
Brian Osman62e7b5f2016-10-26 12:02:18 -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 "GrTextureOpList.h"
9
10#include "GrAuditTrail.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040011#include "GrContext.h"
12#include "GrContextPriv.h"
Brian Osman62e7b5f2016-10-26 12:02:18 -040013#include "GrGpu.h"
Robert Phillipsc994a932018-06-19 13:09:54 -040014#include "GrMemoryPool.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -050015#include "GrRecordingContext.h"
16#include "GrRecordingContextPriv.h"
Robert Phillipsd375dbf2017-09-14 12:45:25 -040017#include "GrResourceAllocator.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040018#include "GrTextureProxy.h"
Brian Salomon69868af2016-12-22 15:42:51 -050019#include "SkStringUtils.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrCopySurfaceOp.h"
Brian Osman62e7b5f2016-10-26 12:02:18 -040021
22////////////////////////////////////////////////////////////////////////////////
23
Robert Phillips12c46292019-04-23 07:36:17 -040024GrTextureOpList::GrTextureOpList(sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips831a2932019-04-12 17:18:39 -040025 sk_sp<GrTextureProxy> proxy,
Robert Phillips5efd5ea2017-05-30 13:47:32 -040026 GrAuditTrail* auditTrail)
Robert Phillips12c46292019-04-23 07:36:17 -040027 : INHERITED(std::move(opMemoryPool), proxy, auditTrail) {
Robert Phillipsc994a932018-06-19 13:09:54 -040028 SkASSERT(fOpMemoryPool);
Brian Salomonc67c31c2018-12-06 10:00:03 -050029 SkASSERT(!proxy->readOnly());
Robert Phillipsc994a932018-06-19 13:09:54 -040030}
31
32void GrTextureOpList::deleteOp(int index) {
33 SkASSERT(index >= 0 && index < fRecordedOps.count());
34 fOpMemoryPool->release(std::move(fRecordedOps[index]));
35}
36
37void GrTextureOpList::deleteOps() {
38 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd46f0092018-07-12 11:49:25 -040039 if (fRecordedOps[i]) {
40 fOpMemoryPool->release(std::move(fRecordedOps[i]));
41 }
Robert Phillipsc994a932018-06-19 13:09:54 -040042 }
43 fRecordedOps.reset();
44 fOpMemoryPool = nullptr;
Brian Osman62e7b5f2016-10-26 12:02:18 -040045}
46
47GrTextureOpList::~GrTextureOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -040048 this->deleteOps();
Brian Osman62e7b5f2016-10-26 12:02:18 -040049}
50
51////////////////////////////////////////////////////////////////////////////////
52
53#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -040054void GrTextureOpList::dump(bool printDependencies) const {
55 INHERITED::dump(printDependencies);
Brian Osman62e7b5f2016-10-26 12:02:18 -040056
Brian Salomon1e41f4a2016-12-07 15:05:04 -050057 SkDebugf("ops (%d):\n", fRecordedOps.count());
58 for (int i = 0; i < fRecordedOps.count(); ++i) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -050059 if (!fRecordedOps[i]) {
60 SkDebugf("%d: <failed instantiation>\n", i);
61 } else {
62 SkDebugf("*******************************\n");
63 SkDebugf("%d: %s\n", i, fRecordedOps[i]->name());
64 SkString str = fRecordedOps[i]->dumpInfo();
65 SkDebugf("%s\n", str.c_str());
66 const SkRect& clippedBounds = fRecordedOps[i]->bounds();
67 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
68 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
69 clippedBounds.fBottom);
70 }
Brian Osman62e7b5f2016-10-26 12:02:18 -040071 }
72}
Robert Phillipsc589b0b2017-04-17 07:53:07 -040073
Brian Osman62e7b5f2016-10-26 12:02:18 -040074#endif
75
Brian Osman407b3422017-08-22 15:01:32 -040076void GrTextureOpList::onPrepare(GrOpFlushState* flushState) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040077 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips6cdc22c2017-05-11 16:29:14 -040078 SkASSERT(this->isClosed());
Brian Osman62e7b5f2016-10-26 12:02:18 -040079
Brian Salomon1e41f4a2016-12-07 15:05:04 -050080 // Loop over the ops that haven't yet generated their geometry
81 for (int i = 0; i < fRecordedOps.count(); ++i) {
82 if (fRecordedOps[i]) {
Brian Salomond25f5bc2018-08-08 11:25:17 -040083 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -040084 GrOpFlushState::OpArgs opArgs = {
85 fRecordedOps[i].get(),
86 nullptr,
87 nullptr,
88 GrXferProcessor::DstProxy()
89 };
90 flushState->setOpArgs(&opArgs);
Brian Salomon1e41f4a2016-12-07 15:05:04 -050091 fRecordedOps[i]->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -040092 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -040093 }
94 }
95}
96
Brian Osman407b3422017-08-22 15:01:32 -040097bool GrTextureOpList::onExecute(GrOpFlushState* flushState) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -050098 if (0 == fRecordedOps.count()) {
Brian Osman62e7b5f2016-10-26 12:02:18 -040099 return false;
100 }
101
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400102 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips3a9710b2018-03-27 17:51:55 -0400103
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400104 GrGpuTextureCommandBuffer* commandBuffer(
105 flushState->gpu()->getCommandBuffer(fTarget.get()->peekTexture(),
106 fTarget.get()->origin()));
107 flushState->setCommandBuffer(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400108
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500109 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillips3a9710b2018-03-27 17:51:55 -0400110 if (!fRecordedOps[i]) {
111 continue;
112 }
Brian Salomond25f5bc2018-08-08 11:25:17 -0400113 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -0400114 GrOpFlushState::OpArgs opArgs = {
115 fRecordedOps[i].get(),
116 nullptr,
117 nullptr,
118 GrXferProcessor::DstProxy()
119 };
120 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500121 fRecordedOps[i]->execute(flushState, fRecordedOps[i].get()->bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400122 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400123 }
124
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400125 flushState->gpu()->submit(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400126 flushState->setCommandBuffer(nullptr);
127
Brian Osman62e7b5f2016-10-26 12:02:18 -0400128 return true;
129}
130
Chris Daltona84cacf2017-10-04 10:30:29 -0600131void GrTextureOpList::endFlush() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400132 this->deleteOps();
Chris Daltona84cacf2017-10-04 10:30:29 -0600133 INHERITED::endFlush();
Brian Osman62e7b5f2016-10-26 12:02:18 -0400134}
135
136////////////////////////////////////////////////////////////////////////////////
137
Robert Phillips81dd3e02017-06-23 11:59:24 -0400138// This closely parallels GrRenderTargetOpList::copySurface but renderTargetOpList
139// stores extra data with the op
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500140bool GrTextureOpList::copySurface(GrRecordingContext* context,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400141 GrSurfaceProxy* dst,
142 GrSurfaceProxy* src,
Brian Osman62e7b5f2016-10-26 12:02:18 -0400143 const SkIRect& srcRect,
144 const SkIPoint& dstPoint) {
Robert Phillipsa16f6cb2017-06-01 11:06:13 -0400145 SkASSERT(dst == fTarget.get());
146
Robert Phillips7c525e62018-06-12 10:11:12 -0400147 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500148 if (!op) {
Brian Osman62e7b5f2016-10-26 12:02:18 -0400149 return false;
150 }
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400151
Robert Phillips9da87e02019-02-04 13:26:26 -0500152 const GrCaps* caps = context->priv().caps();
Robert Phillips7c525e62018-06-12 10:11:12 -0400153 auto addDependency = [ caps, this ] (GrSurfaceProxy* p) {
154 this->addDependency(p, *caps);
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400155 };
156 op->visitProxies(addDependency);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400157
Robert Phillips318c4192017-05-17 09:36:38 -0400158 this->recordOp(std::move(op));
Brian Osman62e7b5f2016-10-26 12:02:18 -0400159 return true;
160}
161
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500162void GrTextureOpList::purgeOpsWithUninstantiatedProxies() {
163 bool hasUninstantiatedProxy = false;
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400164 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
165 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500166 hasUninstantiatedProxy = true;
167 }
168 };
169 for (int i = 0; i < fRecordedOps.count(); ++i) {
170 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
171 hasUninstantiatedProxy = false;
172 if (op) {
173 op->visitProxies(checkInstantiation);
174 }
175 if (hasUninstantiatedProxy) {
176 // When instantiation of the proxy fails we drop the Op
Robert Phillipsc994a932018-06-19 13:09:54 -0400177 this->deleteOp(i);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500178 }
179 }
180}
181
Robert Phillips9313aa72019-04-09 18:41:27 -0400182bool GrTextureOpList::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
183 bool used = false;
184
185 auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p) {
186 if (p == proxyToCheck) {
187 used = true;
188 }
189 };
190 for (int i = 0; i < fRecordedOps.count(); ++i) {
191 const GrOp* op = fRecordedOps[i].get();
192 if (op) {
193 op->visitProxies(visit, GrOp::VisitorType::kOther);
194 }
195 }
196
197 return used;
198}
199
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400200void GrTextureOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400201
202 // Add the interval for all the writes to this opList's target
Robert Phillipsf8e25022017-11-08 15:24:31 -0500203 if (fRecordedOps.count()) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400204 unsigned int cur = alloc->curOp();
205
Robert Phillipsf8e25022017-11-08 15:24:31 -0500206 alloc->addInterval(fTarget.get(), cur, cur+fRecordedOps.count()-1);
207 } else {
208 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
209 // still need to add an interval for the destination so we create a fake op# for
210 // the missing clear op.
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400211 alloc->addInterval(fTarget.get(), alloc->curOp(), alloc->curOp());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500212 alloc->incOps();
213 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400214
Chris Dalton8816b932017-11-29 16:48:25 -0700215 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400216 alloc->addInterval(p, alloc->curOp(), alloc->curOp() SkDEBUGCODE(, p == fTarget.get()));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400217 };
218 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400219 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
Robert Phillips8186cbe2017-11-01 17:32:39 -0400220 if (op) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400221 op->visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
Greg Daniel065b41d2017-11-08 19:58:51 +0000222 }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500223
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400224 // Even though the op may have been (re)moved we still need to increment the op count to
Robert Phillipsf8e25022017-11-08 15:24:31 -0500225 // keep all the math consistent.
226 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400227 }
228}
229
Robert Phillips318c4192017-05-17 09:36:38 -0400230void GrTextureOpList::recordOp(std::unique_ptr<GrOp> op) {
231 SkASSERT(fTarget.get());
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500232 // A closed GrOpList should never receive new/more ops
Brian Osman62e7b5f2016-10-26 12:02:18 -0400233 SkASSERT(!this->isClosed());
234
Robert Phillips318c4192017-05-17 09:36:38 -0400235 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400236 GrOP_INFO("Re-Recording (%s, opID: %u)\n"
Brian Osman62e7b5f2016-10-26 12:02:18 -0400237 "\tBounds LRTB (%f, %f, %f, %f)\n",
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500238 op->name(),
239 op->uniqueID(),
240 op->bounds().fLeft, op->bounds().fRight,
241 op->bounds().fTop, op->bounds().fBottom);
242 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Osman62e7b5f2016-10-26 12:02:18 -0400243
Brian Salomon2790c522016-12-09 16:32:23 -0500244 fRecordedOps.emplace_back(std::move(op));
Brian Osman62e7b5f2016-10-26 12:02:18 -0400245}