blob: ba07add32c93dd94e0f9e7c47778d92e250d9277 [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 Phillips5efd5ea2017-05-30 13:47:32 -040024GrTextureOpList::GrTextureOpList(GrResourceProvider* resourceProvider,
Robert Phillipsc994a932018-06-19 13:09:54 -040025 sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips5efd5ea2017-05-30 13:47:32 -040026 GrTextureProxy* proxy,
27 GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -040028 : INHERITED(resourceProvider, std::move(opMemoryPool), proxy, auditTrail) {
29 SkASSERT(fOpMemoryPool);
Brian Salomonc67c31c2018-12-06 10:00:03 -050030 SkASSERT(!proxy->readOnly());
Robert Phillipsc994a932018-06-19 13:09:54 -040031}
32
33void GrTextureOpList::deleteOp(int index) {
34 SkASSERT(index >= 0 && index < fRecordedOps.count());
35 fOpMemoryPool->release(std::move(fRecordedOps[index]));
36}
37
38void GrTextureOpList::deleteOps() {
39 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd46f0092018-07-12 11:49:25 -040040 if (fRecordedOps[i]) {
41 fOpMemoryPool->release(std::move(fRecordedOps[i]));
42 }
Robert Phillipsc994a932018-06-19 13:09:54 -040043 }
44 fRecordedOps.reset();
45 fOpMemoryPool = nullptr;
Brian Osman62e7b5f2016-10-26 12:02:18 -040046}
47
48GrTextureOpList::~GrTextureOpList() {
Robert Phillipsc994a932018-06-19 13:09:54 -040049 this->deleteOps();
Brian Osman62e7b5f2016-10-26 12:02:18 -040050}
51
52////////////////////////////////////////////////////////////////////////////////
53
54#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -040055void GrTextureOpList::dump(bool printDependencies) const {
56 INHERITED::dump(printDependencies);
Brian Osman62e7b5f2016-10-26 12:02:18 -040057
Brian Salomon1e41f4a2016-12-07 15:05:04 -050058 SkDebugf("ops (%d):\n", fRecordedOps.count());
59 for (int i = 0; i < fRecordedOps.count(); ++i) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -050060 if (!fRecordedOps[i]) {
61 SkDebugf("%d: <failed instantiation>\n", i);
62 } else {
63 SkDebugf("*******************************\n");
64 SkDebugf("%d: %s\n", i, fRecordedOps[i]->name());
65 SkString str = fRecordedOps[i]->dumpInfo();
66 SkDebugf("%s\n", str.c_str());
67 const SkRect& clippedBounds = fRecordedOps[i]->bounds();
68 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
69 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
70 clippedBounds.fBottom);
71 }
Brian Osman62e7b5f2016-10-26 12:02:18 -040072 }
73}
Robert Phillipsc589b0b2017-04-17 07:53:07 -040074
Brian Osman62e7b5f2016-10-26 12:02:18 -040075#endif
76
Brian Osman407b3422017-08-22 15:01:32 -040077void GrTextureOpList::onPrepare(GrOpFlushState* flushState) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040078 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips6cdc22c2017-05-11 16:29:14 -040079 SkASSERT(this->isClosed());
Brian Osman62e7b5f2016-10-26 12:02:18 -040080
Brian Salomon1e41f4a2016-12-07 15:05:04 -050081 // Loop over the ops that haven't yet generated their geometry
82 for (int i = 0; i < fRecordedOps.count(); ++i) {
83 if (fRecordedOps[i]) {
Brian Salomond25f5bc2018-08-08 11:25:17 -040084 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -040085 GrOpFlushState::OpArgs opArgs = {
86 fRecordedOps[i].get(),
87 nullptr,
88 nullptr,
89 GrXferProcessor::DstProxy()
90 };
91 flushState->setOpArgs(&opArgs);
Brian Salomon1e41f4a2016-12-07 15:05:04 -050092 fRecordedOps[i]->prepare(flushState);
Brian Salomon29b60c92017-10-31 14:42:10 -040093 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -040094 }
95 }
96}
97
Brian Osman407b3422017-08-22 15:01:32 -040098bool GrTextureOpList::onExecute(GrOpFlushState* flushState) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -050099 if (0 == fRecordedOps.count()) {
Brian Osman62e7b5f2016-10-26 12:02:18 -0400100 return false;
101 }
102
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400103 SkASSERT(fTarget.get()->peekTexture());
Robert Phillips3a9710b2018-03-27 17:51:55 -0400104
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400105 GrGpuTextureCommandBuffer* commandBuffer(
106 flushState->gpu()->getCommandBuffer(fTarget.get()->peekTexture(),
107 fTarget.get()->origin()));
108 flushState->setCommandBuffer(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400109
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500110 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillips3a9710b2018-03-27 17:51:55 -0400111 if (!fRecordedOps[i]) {
112 continue;
113 }
Brian Salomond25f5bc2018-08-08 11:25:17 -0400114 SkASSERT(fRecordedOps[i]->isChainHead());
Brian Salomon29b60c92017-10-31 14:42:10 -0400115 GrOpFlushState::OpArgs opArgs = {
116 fRecordedOps[i].get(),
117 nullptr,
118 nullptr,
119 GrXferProcessor::DstProxy()
120 };
121 flushState->setOpArgs(&opArgs);
Brian Salomon588cec72018-11-14 13:56:37 -0500122 fRecordedOps[i]->execute(flushState, fRecordedOps[i].get()->bounds());
Brian Salomon29b60c92017-10-31 14:42:10 -0400123 flushState->setOpArgs(nullptr);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400124 }
125
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400126 flushState->gpu()->submit(commandBuffer);
Greg Daniel500d58b2017-08-24 15:59:33 -0400127 flushState->setCommandBuffer(nullptr);
128
Brian Osman62e7b5f2016-10-26 12:02:18 -0400129 return true;
130}
131
Chris Daltona84cacf2017-10-04 10:30:29 -0600132void GrTextureOpList::endFlush() {
Robert Phillipsc994a932018-06-19 13:09:54 -0400133 this->deleteOps();
Chris Daltona84cacf2017-10-04 10:30:29 -0600134 INHERITED::endFlush();
Brian Osman62e7b5f2016-10-26 12:02:18 -0400135}
136
137////////////////////////////////////////////////////////////////////////////////
138
Robert Phillips81dd3e02017-06-23 11:59:24 -0400139// This closely parallels GrRenderTargetOpList::copySurface but renderTargetOpList
140// stores extra data with the op
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500141bool GrTextureOpList::copySurface(GrRecordingContext* context,
Robert Phillipsbf25d432017-04-07 10:08:53 -0400142 GrSurfaceProxy* dst,
143 GrSurfaceProxy* src,
Brian Osman62e7b5f2016-10-26 12:02:18 -0400144 const SkIRect& srcRect,
145 const SkIPoint& dstPoint) {
Robert Phillipsa16f6cb2017-06-01 11:06:13 -0400146 SkASSERT(dst == fTarget.get());
147
Robert Phillips7c525e62018-06-12 10:11:12 -0400148 std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500149 if (!op) {
Brian Osman62e7b5f2016-10-26 12:02:18 -0400150 return false;
151 }
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400152
Robert Phillips9da87e02019-02-04 13:26:26 -0500153 const GrCaps* caps = context->priv().caps();
Robert Phillips7c525e62018-06-12 10:11:12 -0400154 auto addDependency = [ caps, this ] (GrSurfaceProxy* p) {
155 this->addDependency(p, *caps);
Robert Phillips9d6c64f2017-09-14 10:56:45 -0400156 };
157 op->visitProxies(addDependency);
Brian Osman62e7b5f2016-10-26 12:02:18 -0400158
Robert Phillips318c4192017-05-17 09:36:38 -0400159 this->recordOp(std::move(op));
Brian Osman62e7b5f2016-10-26 12:02:18 -0400160 return true;
161}
162
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500163void GrTextureOpList::purgeOpsWithUninstantiatedProxies() {
164 bool hasUninstantiatedProxy = false;
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400165 auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) {
166 if (!p->isInstantiated()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500167 hasUninstantiatedProxy = true;
168 }
169 };
170 for (int i = 0; i < fRecordedOps.count(); ++i) {
171 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
172 hasUninstantiatedProxy = false;
173 if (op) {
174 op->visitProxies(checkInstantiation);
175 }
176 if (hasUninstantiatedProxy) {
177 // When instantiation of the proxy fails we drop the Op
Robert Phillipsc994a932018-06-19 13:09:54 -0400178 this->deleteOp(i);
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500179 }
180 }
181}
182
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400183void GrTextureOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
184 unsigned int cur = alloc->numOps();
185
186 // Add the interval for all the writes to this opList's target
Robert Phillipsf8e25022017-11-08 15:24:31 -0500187 if (fRecordedOps.count()) {
188 alloc->addInterval(fTarget.get(), cur, cur+fRecordedOps.count()-1);
189 } else {
190 // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
191 // still need to add an interval for the destination so we create a fake op# for
192 // the missing clear op.
193 alloc->addInterval(fTarget.get());
194 alloc->incOps();
195 }
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400196
Chris Dalton8816b932017-11-29 16:48:25 -0700197 auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) {
198 alloc->addInterval(p SkDEBUGCODE(, p == fTarget.get()));
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400199 };
200 for (int i = 0; i < fRecordedOps.count(); ++i) {
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400201 const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
Robert Phillips8186cbe2017-11-01 17:32:39 -0400202 if (op) {
Brian Salomon7d94bb52018-10-12 14:37:19 -0400203 op->visitProxies(gather, GrOp::VisitorType::kAllocatorGather);
Greg Daniel065b41d2017-11-08 19:58:51 +0000204 }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500205
206 // Even though the op may have been moved we still need to increment the op count to
207 // keep all the math consistent.
208 alloc->incOps();
Robert Phillipsd375dbf2017-09-14 12:45:25 -0400209 }
210}
211
Robert Phillips318c4192017-05-17 09:36:38 -0400212void GrTextureOpList::recordOp(std::unique_ptr<GrOp> op) {
213 SkASSERT(fTarget.get());
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500214 // A closed GrOpList should never receive new/more ops
Brian Osman62e7b5f2016-10-26 12:02:18 -0400215 SkASSERT(!this->isClosed());
216
Robert Phillips318c4192017-05-17 09:36:38 -0400217 GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID());
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400218 GrOP_INFO("Re-Recording (%s, opID: %u)\n"
Brian Osman62e7b5f2016-10-26 12:02:18 -0400219 "\tBounds LRTB (%f, %f, %f, %f)\n",
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500220 op->name(),
221 op->uniqueID(),
222 op->bounds().fLeft, op->bounds().fRight,
223 op->bounds().fTop, op->bounds().fBottom);
224 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Osman62e7b5f2016-10-26 12:02:18 -0400225
Brian Salomon2790c522016-12-09 16:32:23 -0500226 fRecordedOps.emplace_back(std::move(op));
Brian Osman62e7b5f2016-10-26 12:02:18 -0400227}