GrProxyRef.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2018 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. #ifndef GrProxyRef_DEFINED
  8. #define GrProxyRef_DEFINED
  9. #include "GrSurfaceProxy.h"
  10. #include "GrTextureProxy.h"
  11. #include "GrTypesPriv.h"
  12. /**
  13. * Helper for owning a ref and/or pending IO on a GrSurfaceProxy. This is useful when ownership
  14. * must transform from ref'ed to pending IO when the owner is recorded into a GrOpList.
  15. */
  16. template <typename T> class GrProxyRef {
  17. public:
  18. GrProxyRef() = default;
  19. GrProxyRef(const GrProxyRef&) = delete;
  20. GrProxyRef& operator=(const GrProxyRef&) = delete;
  21. /** ioType expresses what type of IO operations will be marked as pending on the proxy when
  22. markPendingIO is called. */
  23. GrProxyRef(sk_sp<T> proxy, GrIOType ioType) { this->setProxy(std::move(proxy), ioType); }
  24. ~GrProxyRef() { this->reset(); }
  25. /** ioType expresses what type of IO operations will be marked as
  26. pending on the proxy when markPendingIO is called. */
  27. void setProxy(sk_sp<T> proxy, GrIOType ioType) {
  28. SkASSERT(!fPendingIO);
  29. SkASSERT(SkToBool(fProxy) == fOwnRef);
  30. SkSafeUnref(fProxy);
  31. if (!proxy) {
  32. fProxy = nullptr;
  33. fOwnRef = false;
  34. } else {
  35. fProxy = proxy.release(); // due to the semantics of this class we unpack from sk_sp
  36. fOwnRef = true;
  37. fIOType = ioType;
  38. }
  39. }
  40. T* get() const { return fProxy; }
  41. /** Does this object own a pending read or write on the resource it is wrapping. */
  42. bool ownsPendingIO() const { return fPendingIO; }
  43. /** What type of IO does this represent? This is independent of whether a normal ref or a
  44. pending IO is currently held. */
  45. GrIOType ioType() const { return fIOType; }
  46. /** Shortcut for calling setProxy() with NULL. It cannot be called after markingPendingIO
  47. is called. */
  48. void reset() {
  49. if (fPendingIO) {
  50. SkASSERT(fProxy);
  51. switch (fIOType) {
  52. case kRead_GrIOType:
  53. fProxy->completedRead();
  54. break;
  55. case kWrite_GrIOType:
  56. fProxy->completedWrite();
  57. break;
  58. case kRW_GrIOType:
  59. fProxy->completedRead();
  60. fProxy->completedWrite();
  61. break;
  62. }
  63. fPendingIO = false;
  64. }
  65. if (fOwnRef) {
  66. SkASSERT(fProxy);
  67. fProxy->unref();
  68. fOwnRef = false;
  69. }
  70. fProxy = nullptr;
  71. }
  72. /**
  73. * Called when transferring into an op list and therefore scheduled for an IO operation. It can
  74. * only be called once.
  75. */
  76. void markPendingIO() const {
  77. // This should only be called when the owning GrProgramElement gets its first
  78. // pendingExecution ref.
  79. SkASSERT(!fPendingIO);
  80. SkASSERT(fProxy);
  81. fPendingIO = true;
  82. switch (fIOType) {
  83. case kRead_GrIOType:
  84. fProxy->addPendingRead();
  85. break;
  86. case kWrite_GrIOType:
  87. fProxy->addPendingWrite();
  88. break;
  89. case kRW_GrIOType:
  90. fProxy->addPendingRead();
  91. fProxy->addPendingWrite();
  92. break;
  93. }
  94. }
  95. /** Called when the program element/draw state is no longer owned by GrOpList-client code.
  96. This lets the cache know that the drawing code will no longer schedule additional reads or
  97. writes to the resource using the program element or draw state. It can only be called once.
  98. */
  99. void removeRef() const {
  100. SkASSERT(fOwnRef);
  101. SkASSERT(fPendingIO);
  102. SkASSERT(fProxy);
  103. fProxy->unref();
  104. fOwnRef = false;
  105. }
  106. /** Called to indicate that the previous pending IO is complete. Useful when the owning object
  107. still has refs, so it is not about to destroy this GrGpuResourceRef, but its previously
  108. pending executions have been complete. Can only be called if removeRef() was not previously
  109. called. */
  110. void pendingIOComplete() const {
  111. SkASSERT(fOwnRef);
  112. SkASSERT(fPendingIO);
  113. switch (fIOType) {
  114. case kRead_GrIOType:
  115. fProxy->completedRead();
  116. break;
  117. case kWrite_GrIOType:
  118. fProxy->completedWrite();
  119. break;
  120. case kRW_GrIOType:
  121. fProxy->completedRead();
  122. fProxy->completedWrite();
  123. break;
  124. }
  125. fPendingIO = false;
  126. }
  127. private:
  128. T* fProxy = nullptr;
  129. mutable bool fOwnRef = false;
  130. mutable bool fPendingIO = false;
  131. GrIOType fIOType = kRead_GrIOType;
  132. };
  133. using GrSurfaceProxyRef = GrProxyRef<GrSurfaceProxy>;
  134. using GrTextureProxyRef = GrProxyRef<GrTextureProxy>;
  135. #endif