GrTextureOpList.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include "src/gpu/GrTextureOpList.h"
  8. #include "include/gpu/GrContext.h"
  9. #include "include/private/GrRecordingContext.h"
  10. #include "src/core/SkStringUtils.h"
  11. #include "src/gpu/GrAuditTrail.h"
  12. #include "src/gpu/GrContextPriv.h"
  13. #include "src/gpu/GrGpu.h"
  14. #include "src/gpu/GrMemoryPool.h"
  15. #include "src/gpu/GrRecordingContextPriv.h"
  16. #include "src/gpu/GrResourceAllocator.h"
  17. #include "src/gpu/GrTextureProxy.h"
  18. #include "src/gpu/ops/GrCopySurfaceOp.h"
  19. ////////////////////////////////////////////////////////////////////////////////
  20. GrTextureOpList::GrTextureOpList(sk_sp<GrOpMemoryPool> opMemoryPool,
  21. sk_sp<GrTextureProxy> proxy,
  22. GrAuditTrail* auditTrail)
  23. : INHERITED(std::move(opMemoryPool), proxy, auditTrail) {
  24. SkASSERT(fOpMemoryPool);
  25. SkASSERT(!proxy->readOnly());
  26. }
  27. void GrTextureOpList::deleteOp(int index) {
  28. SkASSERT(index >= 0 && index < fRecordedOps.count());
  29. fOpMemoryPool->release(std::move(fRecordedOps[index]));
  30. }
  31. void GrTextureOpList::deleteOps() {
  32. for (int i = 0; i < fRecordedOps.count(); ++i) {
  33. if (fRecordedOps[i]) {
  34. fOpMemoryPool->release(std::move(fRecordedOps[i]));
  35. }
  36. }
  37. fRecordedOps.reset();
  38. fOpMemoryPool = nullptr;
  39. }
  40. GrTextureOpList::~GrTextureOpList() {
  41. this->deleteOps();
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////
  44. #ifdef SK_DEBUG
  45. void GrTextureOpList::dump(bool printDependencies) const {
  46. INHERITED::dump(printDependencies);
  47. SkDebugf("ops (%d):\n", fRecordedOps.count());
  48. for (int i = 0; i < fRecordedOps.count(); ++i) {
  49. if (!fRecordedOps[i]) {
  50. SkDebugf("%d: <failed instantiation>\n", i);
  51. } else {
  52. SkDebugf("*******************************\n");
  53. SkDebugf("%d: %s\n", i, fRecordedOps[i]->name());
  54. SkString str = fRecordedOps[i]->dumpInfo();
  55. SkDebugf("%s\n", str.c_str());
  56. const SkRect& clippedBounds = fRecordedOps[i]->bounds();
  57. SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
  58. clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
  59. clippedBounds.fBottom);
  60. }
  61. }
  62. }
  63. #endif
  64. void GrTextureOpList::onPrepare(GrOpFlushState* flushState) {
  65. SkASSERT(fTarget->peekTexture());
  66. SkASSERT(this->isClosed());
  67. // Loop over the ops that haven't yet generated their geometry
  68. for (int i = 0; i < fRecordedOps.count(); ++i) {
  69. if (fRecordedOps[i]) {
  70. SkASSERT(fRecordedOps[i]->isChainHead());
  71. GrOpFlushState::OpArgs opArgs = {
  72. fRecordedOps[i].get(),
  73. nullptr,
  74. nullptr,
  75. GrSwizzle(),
  76. GrXferProcessor::DstProxy()
  77. };
  78. flushState->setOpArgs(&opArgs);
  79. fRecordedOps[i]->prepare(flushState);
  80. flushState->setOpArgs(nullptr);
  81. }
  82. }
  83. }
  84. bool GrTextureOpList::onExecute(GrOpFlushState* flushState) {
  85. if (0 == fRecordedOps.count()) {
  86. return false;
  87. }
  88. SkASSERT(fTarget->peekTexture());
  89. GrGpuTextureCommandBuffer* commandBuffer(
  90. flushState->gpu()->getCommandBuffer(fTarget->peekTexture(),
  91. fTarget->origin()));
  92. flushState->setCommandBuffer(commandBuffer);
  93. for (int i = 0; i < fRecordedOps.count(); ++i) {
  94. if (!fRecordedOps[i]) {
  95. continue;
  96. }
  97. SkASSERT(fRecordedOps[i]->isChainHead());
  98. GrOpFlushState::OpArgs opArgs = {
  99. fRecordedOps[i].get(),
  100. nullptr,
  101. nullptr,
  102. GrSwizzle(),
  103. GrXferProcessor::DstProxy()
  104. };
  105. flushState->setOpArgs(&opArgs);
  106. fRecordedOps[i]->execute(flushState, fRecordedOps[i].get()->bounds());
  107. flushState->setOpArgs(nullptr);
  108. }
  109. flushState->gpu()->submit(commandBuffer);
  110. flushState->setCommandBuffer(nullptr);
  111. return true;
  112. }
  113. void GrTextureOpList::endFlush() {
  114. this->deleteOps();
  115. INHERITED::endFlush();
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. // This closely parallels GrRenderTargetOpList::copySurface but renderTargetOpList
  119. // stores extra data with the op
  120. bool GrTextureOpList::copySurface(GrRecordingContext* context,
  121. GrSurfaceProxy* dst,
  122. GrSurfaceProxy* src,
  123. const SkIRect& srcRect,
  124. const SkIPoint& dstPoint) {
  125. SkASSERT(dst == fTarget.get());
  126. std::unique_ptr<GrOp> op = GrCopySurfaceOp::Make(context, dst, src, srcRect, dstPoint);
  127. if (!op) {
  128. return false;
  129. }
  130. const GrCaps* caps = context->priv().caps();
  131. auto addDependency = [ caps, this ] (GrSurfaceProxy* p, GrMipMapped) {
  132. this->addDependency(p, *caps);
  133. };
  134. op->visitProxies(addDependency);
  135. this->recordOp(std::move(op));
  136. return true;
  137. }
  138. void GrTextureOpList::purgeOpsWithUninstantiatedProxies() {
  139. bool hasUninstantiatedProxy = false;
  140. auto checkInstantiation = [&hasUninstantiatedProxy](GrSurfaceProxy* p, GrMipMapped) {
  141. if (!p->isInstantiated()) {
  142. hasUninstantiatedProxy = true;
  143. }
  144. };
  145. for (int i = 0; i < fRecordedOps.count(); ++i) {
  146. const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
  147. hasUninstantiatedProxy = false;
  148. if (op) {
  149. op->visitProxies(checkInstantiation);
  150. }
  151. if (hasUninstantiatedProxy) {
  152. // When instantiation of the proxy fails we drop the Op
  153. this->deleteOp(i);
  154. }
  155. }
  156. }
  157. bool GrTextureOpList::onIsUsed(GrSurfaceProxy* proxyToCheck) const {
  158. bool used = false;
  159. auto visit = [ proxyToCheck, &used ] (GrSurfaceProxy* p, GrMipMapped) {
  160. if (p == proxyToCheck) {
  161. used = true;
  162. }
  163. };
  164. for (int i = 0; i < fRecordedOps.count(); ++i) {
  165. const GrOp* op = fRecordedOps[i].get();
  166. if (op) {
  167. op->visitProxies(visit);
  168. }
  169. }
  170. return used;
  171. }
  172. void GrTextureOpList::gatherProxyIntervals(GrResourceAllocator* alloc) const {
  173. // Add the interval for all the writes to this opList's target
  174. if (fRecordedOps.count()) {
  175. unsigned int cur = alloc->curOp();
  176. alloc->addInterval(fTarget.get(), cur, cur+fRecordedOps.count()-1,
  177. GrResourceAllocator::ActualUse::kYes);
  178. } else {
  179. // This can happen if there is a loadOp (e.g., a clear) but no other draws. In this case we
  180. // still need to add an interval for the destination so we create a fake op# for
  181. // the missing clear op.
  182. alloc->addInterval(fTarget.get(), alloc->curOp(), alloc->curOp(),
  183. GrResourceAllocator::ActualUse::kYes);
  184. alloc->incOps();
  185. }
  186. auto gather = [ alloc SkDEBUGCODE(, this) ] (GrSurfaceProxy* p, GrMipMapped) {
  187. alloc->addInterval(p, alloc->curOp(), alloc->curOp(), GrResourceAllocator::ActualUse::kYes
  188. SkDEBUGCODE(, p == fTarget.get()));
  189. };
  190. for (int i = 0; i < fRecordedOps.count(); ++i) {
  191. const GrOp* op = fRecordedOps[i].get(); // only diff from the GrRenderTargetOpList version
  192. if (op) {
  193. op->visitProxies(gather);
  194. }
  195. // Even though the op may have been (re)moved we still need to increment the op count to
  196. // keep all the math consistent.
  197. alloc->incOps();
  198. }
  199. }
  200. void GrTextureOpList::recordOp(std::unique_ptr<GrOp> op) {
  201. SkASSERT(fTarget);
  202. // A closed GrOpList should never receive new/more ops
  203. SkASSERT(!this->isClosed());
  204. GR_AUDIT_TRAIL_ADD_OP(fAuditTrail, op.get(), fTarget->uniqueID());
  205. GrOP_INFO("Re-Recording (%s, opID: %u)\n"
  206. "\tBounds LRTB (%f, %f, %f, %f)\n",
  207. op->name(),
  208. op->uniqueID(),
  209. op->bounds().fLeft, op->bounds().fRight,
  210. op->bounds().fTop, op->bounds().fBottom);
  211. GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
  212. fRecordedOps.emplace_back(std::move(op));
  213. }