GrAuditTrail.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/GrAuditTrail.h"
  8. #include "src/gpu/ops/GrOp.h"
  9. #include "src/utils/SkJSONWriter.h"
  10. const int GrAuditTrail::kGrAuditTrailInvalidID = -1;
  11. void GrAuditTrail::addOp(const GrOp* op, GrRenderTargetProxy::UniqueID proxyID) {
  12. SkASSERT(fEnabled);
  13. Op* auditOp = new Op;
  14. fOpPool.emplace_back(auditOp);
  15. auditOp->fName = op->name();
  16. auditOp->fBounds = op->bounds();
  17. auditOp->fClientID = kGrAuditTrailInvalidID;
  18. auditOp->fOpListID = kGrAuditTrailInvalidID;
  19. auditOp->fChildID = kGrAuditTrailInvalidID;
  20. // consume the current stack trace if any
  21. auditOp->fStackTrace = fCurrentStackTrace;
  22. fCurrentStackTrace.reset();
  23. if (fClientID != kGrAuditTrailInvalidID) {
  24. auditOp->fClientID = fClientID;
  25. Ops** opsLookup = fClientIDLookup.find(fClientID);
  26. Ops* ops = nullptr;
  27. if (!opsLookup) {
  28. ops = new Ops;
  29. fClientIDLookup.set(fClientID, ops);
  30. } else {
  31. ops = *opsLookup;
  32. }
  33. ops->push_back(auditOp);
  34. }
  35. // Our algorithm doesn't bother to reorder inside of an OpNode so the ChildID will start at 0
  36. auditOp->fOpListID = fOpList.count();
  37. auditOp->fChildID = 0;
  38. // We use the op pointer as a key to find the OpNode we are 'glomming' ops onto
  39. fIDLookup.set(op->uniqueID(), auditOp->fOpListID);
  40. OpNode* opNode = new OpNode(proxyID);
  41. opNode->fBounds = op->bounds();
  42. opNode->fChildren.push_back(auditOp);
  43. fOpList.emplace_back(opNode);
  44. }
  45. void GrAuditTrail::opsCombined(const GrOp* consumer, const GrOp* consumed) {
  46. // Look up the op we are going to glom onto
  47. int* indexPtr = fIDLookup.find(consumer->uniqueID());
  48. SkASSERT(indexPtr);
  49. int index = *indexPtr;
  50. SkASSERT(index < fOpList.count() && fOpList[index]);
  51. OpNode& consumerOp = *fOpList[index];
  52. // Look up the op which will be glommed
  53. int* consumedPtr = fIDLookup.find(consumed->uniqueID());
  54. SkASSERT(consumedPtr);
  55. int consumedIndex = *consumedPtr;
  56. SkASSERT(consumedIndex < fOpList.count() && fOpList[consumedIndex]);
  57. OpNode& consumedOp = *fOpList[consumedIndex];
  58. // steal all of consumed's ops
  59. for (int i = 0; i < consumedOp.fChildren.count(); i++) {
  60. Op* childOp = consumedOp.fChildren[i];
  61. // set the ids for the child op
  62. childOp->fOpListID = index;
  63. childOp->fChildID = consumerOp.fChildren.count();
  64. consumerOp.fChildren.push_back(childOp);
  65. }
  66. // Update the bounds for the combineWith node
  67. consumerOp.fBounds = consumer->bounds();
  68. // remove the old node from our opList and clear the combinee's lookup
  69. // NOTE: because we can't change the shape of the oplist, we use a sentinel
  70. fOpList[consumedIndex].reset(nullptr);
  71. fIDLookup.remove(consumed->uniqueID());
  72. }
  73. void GrAuditTrail::copyOutFromOpList(OpInfo* outOpInfo, int opListID) {
  74. SkASSERT(opListID < fOpList.count());
  75. const OpNode* bn = fOpList[opListID].get();
  76. SkASSERT(bn);
  77. outOpInfo->fBounds = bn->fBounds;
  78. outOpInfo->fProxyUniqueID = bn->fProxyUniqueID;
  79. for (int j = 0; j < bn->fChildren.count(); j++) {
  80. OpInfo::Op& outOp = outOpInfo->fOps.push_back();
  81. const Op* currentOp = bn->fChildren[j];
  82. outOp.fBounds = currentOp->fBounds;
  83. outOp.fClientID = currentOp->fClientID;
  84. }
  85. }
  86. void GrAuditTrail::getBoundsByClientID(SkTArray<OpInfo>* outInfo, int clientID) {
  87. Ops** opsLookup = fClientIDLookup.find(clientID);
  88. if (opsLookup) {
  89. // We track which oplistID we're currently looking at. If it changes, then we need to push
  90. // back a new op info struct. We happen to know that ops are in sequential order in the
  91. // oplist, otherwise we'd have to do more bookkeeping
  92. int currentOpListID = kGrAuditTrailInvalidID;
  93. for (int i = 0; i < (*opsLookup)->count(); i++) {
  94. const Op* op = (**opsLookup)[i];
  95. // Because we will copy out all of the ops associated with a given op list id everytime
  96. // the id changes, we only have to update our struct when the id changes.
  97. if (kGrAuditTrailInvalidID == currentOpListID || op->fOpListID != currentOpListID) {
  98. OpInfo& outOpInfo = outInfo->push_back();
  99. // copy out all of the ops so the client can display them even if they have a
  100. // different clientID
  101. this->copyOutFromOpList(&outOpInfo, op->fOpListID);
  102. }
  103. }
  104. }
  105. }
  106. void GrAuditTrail::getBoundsByOpListID(OpInfo* outInfo, int opListID) {
  107. this->copyOutFromOpList(outInfo, opListID);
  108. }
  109. void GrAuditTrail::fullReset() {
  110. SkASSERT(fEnabled);
  111. fOpList.reset();
  112. fIDLookup.reset();
  113. // free all client ops
  114. fClientIDLookup.foreach ([](const int&, Ops** ops) { delete *ops; });
  115. fClientIDLookup.reset();
  116. fOpPool.reset(); // must be last, frees all of the memory
  117. }
  118. template <typename T>
  119. void GrAuditTrail::JsonifyTArray(SkJSONWriter& writer, const char* name, const T& array) {
  120. if (array.count()) {
  121. writer.beginArray(name);
  122. for (int i = 0; i < array.count(); i++) {
  123. // Handle sentinel nullptrs
  124. if (array[i]) {
  125. array[i]->toJson(writer);
  126. }
  127. }
  128. writer.endArray();
  129. }
  130. }
  131. void GrAuditTrail::toJson(SkJSONWriter& writer) const {
  132. writer.beginObject();
  133. JsonifyTArray(writer, "Ops", fOpList);
  134. writer.endObject();
  135. }
  136. void GrAuditTrail::toJson(SkJSONWriter& writer, int clientID) const {
  137. writer.beginObject();
  138. Ops** ops = fClientIDLookup.find(clientID);
  139. if (ops) {
  140. JsonifyTArray(writer, "Ops", **ops);
  141. }
  142. writer.endObject();
  143. }
  144. static void skrect_to_json(SkJSONWriter& writer, const char* name, const SkRect& rect) {
  145. writer.beginObject(name);
  146. writer.appendFloat("Left", rect.fLeft);
  147. writer.appendFloat("Right", rect.fRight);
  148. writer.appendFloat("Top", rect.fTop);
  149. writer.appendFloat("Bottom", rect.fBottom);
  150. writer.endObject();
  151. }
  152. void GrAuditTrail::Op::toJson(SkJSONWriter& writer) const {
  153. writer.beginObject();
  154. writer.appendString("Name", fName.c_str());
  155. writer.appendS32("ClientID", fClientID);
  156. writer.appendS32("OpListID", fOpListID);
  157. writer.appendS32("ChildID", fChildID);
  158. skrect_to_json(writer, "Bounds", fBounds);
  159. if (fStackTrace.count()) {
  160. writer.beginArray("Stack");
  161. for (int i = 0; i < fStackTrace.count(); i++) {
  162. writer.appendString(fStackTrace[i].c_str());
  163. }
  164. writer.endArray();
  165. }
  166. writer.endObject();
  167. }
  168. void GrAuditTrail::OpNode::toJson(SkJSONWriter& writer) const {
  169. writer.beginObject();
  170. writer.appendU32("ProxyID", fProxyUniqueID.asUInt());
  171. skrect_to_json(writer, "Bounds", fBounds);
  172. JsonifyTArray(writer, "Ops", fChildren);
  173. writer.endObject();
  174. }