Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 1 | /* |
| 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 Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 11 | #include "GrContext.h" |
| 12 | #include "GrContextPriv.h" |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 13 | #include "GrGpu.h" |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 14 | #include "GrMemoryPool.h" |
Robert Phillips | d375dbf | 2017-09-14 12:45:25 -0400 | [diff] [blame] | 15 | #include "GrResourceAllocator.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 16 | #include "GrTextureProxy.h" |
Brian Salomon | 69868af | 2016-12-22 15:42:51 -0500 | [diff] [blame] | 17 | #include "SkStringUtils.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 18 | #include "ops/GrCopySurfaceOp.h" |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 19 | |
| 20 | //////////////////////////////////////////////////////////////////////////////// |
| 21 | |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 22 | GrTextureOpList::GrTextureOpList(GrResourceProvider* resourceProvider, |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 23 | sk_sp<GrOpMemoryPool> opMemoryPool, |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 24 | GrTextureProxy* proxy, |
| 25 | GrAuditTrail* auditTrail) |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 26 | : INHERITED(resourceProvider, std::move(opMemoryPool), proxy, auditTrail) { |
| 27 | SkASSERT(fOpMemoryPool); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 28 | SkASSERT(!proxy->readOnly()); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void GrTextureOpList::deleteOp(int index) { |
| 32 | SkASSERT(index >= 0 && index < fRecordedOps.count()); |
| 33 | fOpMemoryPool->release(std::move(fRecordedOps[index])); |
| 34 | } |
| 35 | |
| 36 | void GrTextureOpList::deleteOps() { |
| 37 | for (int i = 0; i < fRecordedOps.count(); ++i) { |
Robert Phillips | d46f009 | 2018-07-12 11:49:25 -0400 | [diff] [blame] | 38 | if (fRecordedOps[i]) { |
| 39 | fOpMemoryPool->release(std::move(fRecordedOps[i])); |
| 40 | } |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 41 | } |
| 42 | fRecordedOps.reset(); |
| 43 | fOpMemoryPool = nullptr; |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | GrTextureOpList::~GrTextureOpList() { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 47 | this->deleteOps(); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | //////////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | #ifdef SK_DEBUG |
Robert Phillips | 2748391 | 2018-04-20 12:43:18 -0400 | [diff] [blame] | 53 | void GrTextureOpList::dump(bool printDependencies) const { |
| 54 | INHERITED::dump(printDependencies); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 55 | |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 56 | SkDebugf("ops (%d):\n", fRecordedOps.count()); |
| 57 | for (int i = 0; i < fRecordedOps.count(); ++i) { |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 58 | 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 Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 72 | |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 73 | #endif |
| 74 | |
Brian Osman | 407b342 | 2017-08-22 15:01:32 -0400 | [diff] [blame] | 75 | void GrTextureOpList::onPrepare(GrOpFlushState* flushState) { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 76 | SkASSERT(fTarget.get()->peekTexture()); |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 77 | SkASSERT(this->isClosed()); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 78 | |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 79 | // 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 Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 82 | SkASSERT(fRecordedOps[i]->isChainHead()); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 83 | GrOpFlushState::OpArgs opArgs = { |
| 84 | fRecordedOps[i].get(), |
| 85 | nullptr, |
| 86 | nullptr, |
| 87 | GrXferProcessor::DstProxy() |
| 88 | }; |
| 89 | flushState->setOpArgs(&opArgs); |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 90 | fRecordedOps[i]->prepare(flushState); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 91 | flushState->setOpArgs(nullptr); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Brian Osman | 407b342 | 2017-08-22 15:01:32 -0400 | [diff] [blame] | 96 | bool GrTextureOpList::onExecute(GrOpFlushState* flushState) { |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 97 | if (0 == fRecordedOps.count()) { |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 101 | SkASSERT(fTarget.get()->peekTexture()); |
Robert Phillips | 3a9710b | 2018-03-27 17:51:55 -0400 | [diff] [blame] | 102 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 103 | GrGpuTextureCommandBuffer* commandBuffer( |
| 104 | flushState->gpu()->getCommandBuffer(fTarget.get()->peekTexture(), |
| 105 | fTarget.get()->origin())); |
| 106 | flushState->setCommandBuffer(commandBuffer); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 107 | |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 108 | for (int i = 0; i < fRecordedOps.count(); ++i) { |
Robert Phillips | 3a9710b | 2018-03-27 17:51:55 -0400 | [diff] [blame] | 109 | if (!fRecordedOps[i]) { |
| 110 | continue; |
| 111 | } |
Brian Salomon | d25f5bc | 2018-08-08 11:25:17 -0400 | [diff] [blame] | 112 | SkASSERT(fRecordedOps[i]->isChainHead()); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 113 | GrOpFlushState::OpArgs opArgs = { |
| 114 | fRecordedOps[i].get(), |
| 115 | nullptr, |
| 116 | nullptr, |
| 117 | GrXferProcessor::DstProxy() |
| 118 | }; |
| 119 | flushState->setOpArgs(&opArgs); |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 120 | fRecordedOps[i]->execute(flushState, fRecordedOps[i].get()->bounds()); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 121 | flushState->setOpArgs(nullptr); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 124 | flushState->gpu()->submit(commandBuffer); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 125 | flushState->setCommandBuffer(nullptr); |
| 126 | |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
Chris Dalton | a84cacf | 2017-10-04 10:30:29 -0600 | [diff] [blame] | 130 | void GrTextureOpList::endFlush() { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 131 | this->deleteOps(); |
Chris Dalton | a84cacf | 2017-10-04 10:30:29 -0600 | [diff] [blame] | 132 | INHERITED::endFlush(); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | //////////////////////////////////////////////////////////////////////////////// |
| 136 | |
Robert Phillips | 81dd3e0 | 2017-06-23 11:59:24 -0400 | [diff] [blame] | 137 | // This closely parallels GrRenderTargetOpList::copySurface but renderTargetOpList |
| 138 | // stores extra data with the op |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 139 | bool GrTextureOpList::copySurface(GrContext* context, |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 140 | GrSurfaceProxy* dst, |
| 141 | GrSurfaceProxy* src, |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 142 | const SkIRect& srcRect, |
| 143 | const SkIPoint& dstPoint) { |
Robert Phillips | a16f6cb | 2017-06-01 11:06:13 -0400 | [diff] [blame] | 144 | SkASSERT(dst == fTarget.get()); |
| 145 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 146 | std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint); |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 147 | if (!op) { |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 148 | return false; |
| 149 | } |
Robert Phillips | 9d6c64f | 2017-09-14 10:56:45 -0400 | [diff] [blame] | 150 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 151 | const GrCaps* caps = context->contextPriv().caps(); |
| 152 | auto addDependency = [ caps, this ] (GrSurfaceProxy* p) { |
| 153 | this->addDependency(p, *caps); |
Robert Phillips | 9d6c64f | 2017-09-14 10:56:45 -0400 | [diff] [blame] | 154 | }; |
| 155 | op->visitProxies(addDependency); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 156 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 157 | this->recordOp(std::move(op)); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 161 | void GrTextureOpList::purgeOpsWithUninstantiatedProxies() { |
| 162 | bool hasUninstantiatedProxy = false; |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 163 | auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p) { |
| 164 | if (!p->isInstantiated()) { |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 165 | 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 Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 176 | this->deleteOp(i); |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Robert Phillips | d375dbf | 2017-09-14 12:45:25 -0400 | [diff] [blame] | 181 | void 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 Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 185 | 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 Phillips | d375dbf | 2017-09-14 12:45:25 -0400 | [diff] [blame] | 194 | |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 195 | auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p) { |
| 196 | alloc->addInterval(p SkDEBUGCODE(, p == fTarget.get())); |
Robert Phillips | d375dbf | 2017-09-14 12:45:25 -0400 | [diff] [blame] | 197 | }; |
| 198 | for (int i = 0; i < fRecordedOps.count(); ++i) { |
Robert Phillips | d375dbf | 2017-09-14 12:45:25 -0400 | [diff] [blame] | 199 | const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 200 | if (op) { |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 201 | op->visitProxies(gather, GrOp::VisitorType::kAllocatorGather); |
Greg Daniel | 065b41d | 2017-11-08 19:58:51 +0000 | [diff] [blame] | 202 | } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 203 | |
| 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 Phillips | d375dbf | 2017-09-14 12:45:25 -0400 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 210 | void GrTextureOpList::recordOp(std::unique_ptr<GrOp> op) { |
| 211 | SkASSERT(fTarget.get()); |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 212 | // A closed GrOpList should never receive new/more ops |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 213 | SkASSERT(!this->isClosed()); |
| 214 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 215 | GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget.get()->uniqueID()); |
Robert Phillips | f5442bb | 2017-04-17 14:18:34 -0400 | [diff] [blame] | 216 | GrOP_INFO("Re-Recording (%s, opID: %u)\n" |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 217 | "\tBounds LRTB (%f, %f, %f, %f)\n", |
Brian Salomon | 1e41f4a | 2016-12-07 15:05:04 -0500 | [diff] [blame] | 218 | 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 Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 223 | |
Brian Salomon | 2790c52 | 2016-12-09 16:32:23 -0500 | [diff] [blame] | 224 | fRecordedOps.emplace_back(std::move(op)); |
Brian Osman | 62e7b5f | 2016-10-26 12:02:18 -0400 | [diff] [blame] | 225 | } |