blob: a4c60682d5ff9f7ee28823bd69acaa8e3f19a9cb [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);
28}
29
30void GrTextureOpList::deleteOp(int index) {
31 SkASSERT(index >= 0 && index < fRecordedOps.count());
32 fOpMemoryPool->release(std::move(fRecordedOps[index]));
33}
34
35void GrTextureOpList::deleteOps() {
36 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd46f0092018-07-12 11:49:25 -040037 if (fRecordedOps[i]) {
38 fOpMemoryPool->release(std::move(fRecordedOps[i]));
39 }
Robert Phillipsc994a932018-06-19 13:09:54 -040040 }
41 fRecordedOps.reset();
42 fOpMemoryPool = nullptr;
Brian Osman62e7b5f2016-10-26 12:02:18 -040043}
44
45GrTextureOpList::~GrTextureOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -040046 this->deleteOps();
Brian Osman62e7b5f2016-10-26 12:02:18 -040047}
48
49////////////////////////////////////////////////////////////////////////////////
50
51#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -040052void GrTextureOpList::dump(bool printDependencies) const {
53 INHERITED::dump(printDependencies);
Brian Osman62e7b5f2016-10-26 12:02:18 -040054
Brian Salomon1e41f4a2016-12-07 15:05:04 -050055 SkDebugf("ops (%d):\n", fRecordedOps.count());
56 for (int i = 0; i < fRecordedOps.count(); ++i) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -050057 if (!fRecordedOps[i]) {
58 SkDebugf("%d: <failed instantiation>\n", i);
59 } else {
60 SkDebugf("*******************************\n");
61 SkDebugf("%d: %s\n", i, fRecordedOps[i]->name());
62 SkString str = fRecordedOps[i]->dumpInfo();
63 SkDebugf("%s\n", str.c_str());
64 const SkRect& clippedBounds = fRecordedOps[i]->bounds();
65 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
66 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
67 clippedBounds.fBottom);
68 }
Brian Osman62e7b5f2016-10-26 12:02:18 -040069 }
70}
Robert Phillipsc589b0b2017-04-17 07:53:07 -040071
Brian Osman62e7b5f2016-10-26 12:02:18 -040072#endif
73
Brian Osman407b3422017-08-22 15:01:32 -040074void GrTextureOpList::onPrepare(GrOpFlushState* flushState) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040075 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips6cdc22c2017-05-11 16:29:14 -040076 SkASSERT(this->isClosed());
Brian Osman62e7b5f2016-10-26 12:02:18 -040077
Brian Salomon1e41f4a2016-12-07 15:05:04 -050078 // Loop over the ops that haven't yet generated their geometry
79 for (int i = 0; i < fRecordedOps.count(); ++i) {
80 if (fRecordedOps[i]) {
Brian Salomond25f5bc2018-08-08 11:25:17 -040081 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -040082 GrOpFlushState::OpArgs opArgs = {
83 fRecordedOps[i].get(),
84 nullptr,
85 nullptr,
86 GrXferProcessor::DstProxy()
87 };
88 flushState->setOpArgs(&opArgs);
Brian Salomon1e41f4a2016-12-07 15:05:04 -050089 fRecordedOps[i]->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -040090 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -040091 }
92 }
93}
94
Brian Osman407b3422017-08-22 15:01:32 -040095bool GrTextureOpList::onExecute(GrOpFlushState* flushState) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -050096 if (0 == fRecordedOps.count()) {
Brian Osman62e7b5f2016-10-26 12:02:18 -040097 return false;
98 }
99
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400100 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips3a9710b2018-03-27 17:51:55 -0400101
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400102 GrGpuTextureCommandBuffer* commandBuffer(
103 flushState->gpu()->getCommandBuffer(fTarget.get()->peekTexture(),
104 fTarget.get()->origin()));
105 flushState->setCommandBuffer(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400106
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500107 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillips3a9710b2018-03-27 17:51:55 -0400108 if (!fRecordedOps[i]) {
109 continue;
110 }
Brian Salomond25f5bc2018-08-08 11:25:17 -0400111 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -0400112 GrOpFlushState::OpArgs opArgs = {
113 fRecordedOps[i].get(),
114 nullptr,
115 nullptr,
116 GrXferProcessor::DstProxy()
117 };
118 flushState->setOpArgs(&opArgs);
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500119 fRecordedOps[i]->execute(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -0400120 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400121 }
122
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400123 flushState->gpu()->submit(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400124 flushState->setCommandBuffer(nullptr);
125
Brian Osman62e7b5f2016-10-26 12:02:18 -0400126 return true;
127}
128
Chris Daltona84cacf2017-10-04 10:30:29 -0600129void GrTextureOpList::endFlush() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400130 this->deleteOps();
Chris Daltona84cacf2017-10-04 10:30:29 -0600131 INHERITED::endFlush();
Brian Osman62e7b5f2016-10-26 12:02:18 -0400132}
133
134////////////////////////////////////////////////////////////////////////////////
135
Robert Phillips81dd3e02017-06-23 11:59:24 -0400136// This closely parallels GrRenderTargetOpList::copySurface but renderTargetOpList
137// stores extra data with the op
Robert Phillips7c525e62018-06-12 10:11:12 -0400138bool GrTextureOpList::copySurface(GrContext* context,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400139 GrSurfaceProxy* dst,
140 GrSurfaceProxy* src,
Brian Osman62e7b5f2016-10-26 12:02:18 -0400141 const SkIRect& srcRect,
142 const SkIPoint& dstPoint) {
Robert Phillipsa16f6cb2017-06-01 11:06:13 -0400143 SkASSERT(dst == fTarget.get());
144
Robert Phillips7c525e62018-06-12 10:11:12 -0400145 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500146 if (!op) {
Brian Osman62e7b5f2016-10-26 12:02:18 -0400147 return false;
148 }
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400149
Robert Phillips7c525e62018-06-12 10:11:12 -0400150 const GrCaps* caps = context->contextPriv().caps();
151 auto addDependency = [ caps, this ] (GrSurfaceProxy* p) {
152 this->addDependency(p, *caps);
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400153 };
154 op->visitProxies(addDependency);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400155
Robert Phillips318c4192017-05-17 09:36:38 -0400156 this->recordOp(std::move(op));
Brian Osman62e7b5f2016-10-26 12:02:18 -0400157 return true;
158}
159
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500160void GrTextureOpList::purgeOpsWithUninstantiatedProxies() {
161 bool hasUninstantiatedProxy = false;
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400162 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
163 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500164 hasUninstantiatedProxy = true;
165 }
166 };
167 for (int i = 0; i < fRecordedOps.count(); ++i) {
168 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
169 hasUninstantiatedProxy = false;
170 if (op) {
171 op->visitProxies(checkInstantiation);
172 }
173 if (hasUninstantiatedProxy) {
174 // When instantiation of the proxy fails we drop the Op
Robert Phillipsc994a932018-06-19 13:09:54 -0400175 this->deleteOp(i);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500176 }
177 }
178}
179
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400180void GrTextureOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
181 unsigned int cur = alloc->numOps();
182
183 // Add the interval for all the writes to this opList's target
Robert Phillipsf8e25022017-11-08 15:24:31 -0500184 if (fRecordedOps.count()) {
185 alloc->addInterval(fTarget.get(), cur, cur+fRecordedOps.count()-1);
186 } else {
187 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
188 // still need to add an interval for the destination so we create a fake op# for
189 // the missing clear op.
190 alloc->addInterval(fTarget.get());
191 alloc->incOps();
192 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400193
Chris Dalton8816b932017-11-29 16:48:25 -0700194 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
195 alloc->addInterval(p SkDEBUGCODE(, p == fTarget.get()));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400196 };
197 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400198 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
Robert Phillips8186cbe2017-11-01 17:32:39 -0400199 if (op) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400200 op->visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
Greg Daniel065b41d2017-11-08 19:58:51 +0000201 }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500202
203 // Even though the op may have been moved we still need to increment the op count to
204 // keep all the math consistent.
205 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400206 }
207}
208
Robert Phillips318c4192017-05-17 09:36:38 -0400209void GrTextureOpList::recordOp(std::unique_ptr<GrOp> op) {
210 SkASSERT(fTarget.get());
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500211 // A closed GrOpList should never receive new/more ops
Brian Osman62e7b5f2016-10-26 12:02:18 -0400212 SkASSERT(!this->isClosed());
213
Robert Phillips318c4192017-05-17 09:36:38 -0400214 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400215 GrOP_INFO("Re-Recording (%s, opID: %u)\n"
Brian Osman62e7b5f2016-10-26 12:02:18 -0400216 "\tBounds LRTB (%f, %f, %f, %f)\n",
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500217 op->name(),
218 op->uniqueID(),
219 op->bounds().fLeft, op->bounds().fRight,
220 op->bounds().fTop, op->bounds().fBottom);
221 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon42ad83a2016-12-20 16:14:45 -0500222 GR_AUDIT_TRAIL_OP_RESULT_NEW(fAuditTrail, op.get());
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}