blob: ae518073351c1780bf173ca4dd159ac289687dc1 [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 Phillipsd375dbf2017-09-14 12:45:25 -040015#include "GrResourceAllocator.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040016#include "GrTextureProxy.h"
Brian Salomon69868af2016-12-22 15:42:51 -050017#include "SkStringUtils.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrCopySurfaceOp.h"
Brian Osman62e7b5f2016-10-26 12:02:18 -040019
20////////////////////////////////////////////////////////////////////////////////
21
Robert Phillips5efd5ea2017-05-30 13:47:32 -040022GrTextureOpList::GrTextureOpList(GrResourceProvider* resourceProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -040023 sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips5efd5ea2017-05-30 13:47:32 -040024 GrTextureProxy* proxy,
25 GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -040026 : INHERITED(resourceProvider, std::move(opMemoryPool), proxy, auditTrail) {
27 SkASSERT(fOpMemoryPool);
Brian Salomonc67c31c2018-12-06 10:00:03 -050028 SkASSERT(!proxy->readOnly());
Robert Phillipsc994a932018-06-19 13:09:54 -040029}
30
31void GrTextureOpList::deleteOp(int index) {
32 SkASSERT(index >= 0 && index < fRecordedOps.count());
33 fOpMemoryPool->release(std::move(fRecordedOps[index]));
34}
35
36void GrTextureOpList::deleteOps() {
37 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd46f0092018-07-12 11:49:25 -040038 if (fRecordedOps[i]) {
39 fOpMemoryPool->release(std::move(fRecordedOps[i]));
40 }
Robert Phillipsc994a932018-06-19 13:09:54 -040041 }
42 fRecordedOps.reset();
43 fOpMemoryPool = nullptr;
Brian Osman62e7b5f2016-10-26 12:02:18 -040044}
45
46GrTextureOpList::~GrTextureOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -040047 this->deleteOps();
Brian Osman62e7b5f2016-10-26 12:02:18 -040048}
49
50////////////////////////////////////////////////////////////////////////////////
51
52#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -040053void GrTextureOpList::dump(bool printDependencies) const {
54 INHERITED::dump(printDependencies);
Brian Osman62e7b5f2016-10-26 12:02:18 -040055
Brian Salomon1e41f4a2016-12-07 15:05:04 -050056 SkDebugf("ops (%d):\n", fRecordedOps.count());
57 for (int i = 0; i < fRecordedOps.count(); ++i) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -050058 if (!fRecordedOps[i]) {
59 SkDebugf("%d: <failed instantiation>\n", i);
60 } else {
61 SkDebugf("*******************************\n");
62 SkDebugf("%d: %s\n", i, fRecordedOps[i]->name());
63 SkString str = fRecordedOps[i]->dumpInfo();
64 SkDebugf("%s\n", str.c_str());
65 const SkRect& clippedBounds = fRecordedOps[i]->bounds();
66 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
67 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
68 clippedBounds.fBottom);
69 }
Brian Osman62e7b5f2016-10-26 12:02:18 -040070 }
71}
Robert Phillipsc589b0b2017-04-17 07:53:07 -040072
Brian Osman62e7b5f2016-10-26 12:02:18 -040073#endif
74
Brian Osman407b3422017-08-22 15:01:32 -040075void GrTextureOpList::onPrepare(GrOpFlushState* flushState) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040076 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips6cdc22c2017-05-11 16:29:14 -040077 SkASSERT(this->isClosed());
Brian Osman62e7b5f2016-10-26 12:02:18 -040078
Brian Salomon1e41f4a2016-12-07 15:05:04 -050079 // Loop over the ops that haven't yet generated their geometry
80 for (int i = 0; i < fRecordedOps.count(); ++i) {
81 if (fRecordedOps[i]) {
Brian Salomond25f5bc2018-08-08 11:25:17 -040082 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -040083 GrOpFlushState::OpArgs opArgs = {
84 fRecordedOps[i].get(),
85 nullptr,
86 nullptr,
87 GrXferProcessor::DstProxy()
88 };
89 flushState->setOpArgs(&opArgs);
Brian Salomon1e41f4a2016-12-07 15:05:04 -050090 fRecordedOps[i]->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -040091 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -040092 }
93 }
94}
95
Brian Osman407b3422017-08-22 15:01:32 -040096bool GrTextureOpList::onExecute(GrOpFlushState* flushState) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -050097 if (0 == fRecordedOps.count()) {
Brian Osman62e7b5f2016-10-26 12:02:18 -040098 return false;
99 }
100
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400101 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips3a9710b2018-03-27 17:51:55 -0400102
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400103 GrGpuTextureCommandBuffer* commandBuffer(
104 flushState->gpu()->getCommandBuffer(fTarget.get()->peekTexture(),
105 fTarget.get()->origin()));
106 flushState->setCommandBuffer(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400107
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500108 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillips3a9710b2018-03-27 17:51:55 -0400109 if (!fRecordedOps[i]) {
110 continue;
111 }
Brian Salomond25f5bc2018-08-08 11:25:17 -0400112 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -0400113 GrOpFlushState::OpArgs opArgs = {
114 fRecordedOps[i].get(),
115 nullptr,
116 nullptr,
117 GrXferProcessor::DstProxy()
118 };
119 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500120 fRecordedOps[i]->execute(flushState, fRecordedOps[i].get()->bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400121 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400122 }
123
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400124 flushState->gpu()->submit(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400125 flushState->setCommandBuffer(nullptr);
126
Brian Osman62e7b5f2016-10-26 12:02:18 -0400127 return true;
128}
129
Chris Daltona84cacf2017-10-04 10:30:29 -0600130void GrTextureOpList::endFlush() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400131 this->deleteOps();
Chris Daltona84cacf2017-10-04 10:30:29 -0600132 INHERITED::endFlush();
Brian Osman62e7b5f2016-10-26 12:02:18 -0400133}
134
135////////////////////////////////////////////////////////////////////////////////
136
Robert Phillips81dd3e02017-06-23 11:59:24 -0400137// This closely parallels GrRenderTargetOpList::copySurface but renderTargetOpList
138// stores extra data with the op
Robert Phillips7c525e62018-06-12 10:11:12 -0400139bool GrTextureOpList::copySurface(GrContext* context,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400140 GrSurfaceProxy* dst,
141 GrSurfaceProxy* src,
Brian Osman62e7b5f2016-10-26 12:02:18 -0400142 const SkIRect& srcRect,
143 const SkIPoint& dstPoint) {
Robert Phillipsa16f6cb2017-06-01 11:06:13 -0400144 SkASSERT(dst == fTarget.get());
145
Robert Phillips7c525e62018-06-12 10:11:12 -0400146 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500147 if (!op) {
Brian Osman62e7b5f2016-10-26 12:02:18 -0400148 return false;
149 }
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400150
Robert Phillips7c525e62018-06-12 10:11:12 -0400151 const GrCaps* caps = context->contextPriv().caps();
152 auto addDependency = [ caps, this ] (GrSurfaceProxy* p) {
153 this->addDependency(p, *caps);
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400154 };
155 op->visitProxies(addDependency);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400156
Robert Phillips318c4192017-05-17 09:36:38 -0400157 this->recordOp(std::move(op));
Brian Osman62e7b5f2016-10-26 12:02:18 -0400158 return true;
159}
160
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500161void GrTextureOpList::purgeOpsWithUninstantiatedProxies() {
162 bool hasUninstantiatedProxy = false;
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400163 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
164 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500165 hasUninstantiatedProxy = true;
166 }
167 };
168 for (int i = 0; i < fRecordedOps.count(); ++i) {
169 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
170 hasUninstantiatedProxy = false;
171 if (op) {
172 op->visitProxies(checkInstantiation);
173 }
174 if (hasUninstantiatedProxy) {
175 // When instantiation of the proxy fails we drop the Op
Robert Phillipsc994a932018-06-19 13:09:54 -0400176 this->deleteOp(i);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500177 }
178 }
179}
180
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400181void GrTextureOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
182 unsigned int cur = alloc->numOps();
183
184 // Add the interval for all the writes to this opList's target
Robert Phillipsf8e25022017-11-08 15:24:31 -0500185 if (fRecordedOps.count()) {
186 alloc->addInterval(fTarget.get(), cur, cur+fRecordedOps.count()-1);
187 } else {
188 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
189 // still need to add an interval for the destination so we create a fake op# for
190 // the missing clear op.
191 alloc->addInterval(fTarget.get());
192 alloc->incOps();
193 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400194
Chris Dalton8816b932017-11-29 16:48:25 -0700195 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
196 alloc->addInterval(p SkDEBUGCODE(, p == fTarget.get()));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400197 };
198 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400199 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
Robert Phillips8186cbe2017-11-01 17:32:39 -0400200 if (op) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400201 op->visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
Greg Daniel065b41d2017-11-08 19:58:51 +0000202 }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500203
204 // Even though the op may have been moved we still need to increment the op count to
205 // keep all the math consistent.
206 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400207 }
208}
209
Robert Phillips318c4192017-05-17 09:36:38 -0400210void GrTextureOpList::recordOp(std::unique_ptr<GrOp> op) {
211 SkASSERT(fTarget.get());
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500212 // A closed GrOpList should never receive new/more ops
Brian Osman62e7b5f2016-10-26 12:02:18 -0400213 SkASSERT(!this->isClosed());
214
Robert Phillips318c4192017-05-17 09:36:38 -0400215 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400216 GrOP_INFO("Re-Recording (%s, opID: %u)\n"
Brian Osman62e7b5f2016-10-26 12:02:18 -0400217 "\tBounds LRTB (%f, %f, %f, %f)\n",
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500218 op->name(),
219 op->uniqueID(),
220 op->bounds().fLeft, op->bounds().fRight,
221 op->bounds().fTop, op->bounds().fBottom);
222 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Osman62e7b5f2016-10-26 12:02:18 -0400223
Brian Salomon2790c522016-12-09 16:32:23 -0500224 fRecordedOps.emplace_back(std::move(op));
Brian Osman62e7b5f2016-10-26 12:02:18 -0400225}