SkRecord.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2014 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 SkRecord_DEFINED
  8. #define SkRecord_DEFINED
  9. #include "include/private/SkTLogic.h"
  10. #include "include/private/SkTemplates.h"
  11. #include "src/core/SkArenaAlloc.h"
  12. #include "src/core/SkRecords.h"
  13. // SkRecord represents a sequence of SkCanvas calls, saved for future use.
  14. // These future uses may include: replay, optimization, serialization, or combinations of those.
  15. //
  16. // Though an enterprising user may find calling alloc(), append(), visit(), and mutate() enough to
  17. // work with SkRecord, you probably want to look at SkRecorder which presents an SkCanvas interface
  18. // for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into another SkCanvas.
  19. //
  20. // SkRecord often looks like it's compatible with any type T, but really it's compatible with any
  21. // type T which has a static const SkRecords::Type kType. That is to say, SkRecord is compatible
  22. // only with SkRecords::* structs defined in SkRecords.h. Your compiler will helpfully yell if you
  23. // get this wrong.
  24. class SkRecord : public SkRefCnt {
  25. public:
  26. SkRecord() = default;
  27. ~SkRecord();
  28. // Returns the number of canvas commands in this SkRecord.
  29. int count() const { return fCount; }
  30. // Visit the i-th canvas command with a functor matching this interface:
  31. // template <typename T>
  32. // R operator()(const T& record) { ... }
  33. // This operator() must be defined for at least all SkRecords::*.
  34. template <typename F>
  35. auto visit(int i, F&& f) const -> decltype(f(SkRecords::NoOp())) {
  36. return fRecords[i].visit(f);
  37. }
  38. // Mutate the i-th canvas command with a functor matching this interface:
  39. // template <typename T>
  40. // R operator()(T* record) { ... }
  41. // This operator() must be defined for at least all SkRecords::*.
  42. template <typename F>
  43. auto mutate(int i, F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) {
  44. return fRecords[i].mutate(f);
  45. }
  46. // Allocate contiguous space for count Ts, to be freed when the SkRecord is destroyed.
  47. // Here T can be any class, not just those from SkRecords. Throws on failure.
  48. template <typename T>
  49. T* alloc(size_t count = 1) {
  50. struct RawBytes {
  51. alignas(T) char data[sizeof(T)];
  52. };
  53. fApproxBytesAllocated += count * sizeof(T) + alignof(T);
  54. return (T*)fAlloc.makeArrayDefault<RawBytes>(count);
  55. }
  56. // Add a new command of type T to the end of this SkRecord.
  57. // You are expected to placement new an object of type T onto this pointer.
  58. template <typename T>
  59. T* append() {
  60. if (fCount == fReserved) {
  61. this->grow();
  62. }
  63. return fRecords[fCount++].set(this->allocCommand<T>());
  64. }
  65. // Replace the i-th command with a new command of type T.
  66. // You are expected to placement new an object of type T onto this pointer.
  67. // References to the original command are invalidated.
  68. template <typename T>
  69. T* replace(int i) {
  70. SkASSERT(i < this->count());
  71. Destroyer destroyer;
  72. this->mutate(i, destroyer);
  73. return fRecords[i].set(this->allocCommand<T>());
  74. }
  75. // Replace the i-th command with a new command of type T.
  76. // You are expected to placement new an object of type T onto this pointer.
  77. // You must show proof that you've already adopted the existing command.
  78. template <typename T, typename Existing>
  79. T* replace(int i, const SkRecords::Adopted<Existing>& proofOfAdoption) {
  80. SkASSERT(i < this->count());
  81. SkASSERT(Existing::kType == fRecords[i].type());
  82. SkASSERT(proofOfAdoption == fRecords[i].ptr());
  83. return fRecords[i].set(this->allocCommand<T>());
  84. }
  85. // Does not return the bytes in any pointers embedded in the Records; callers
  86. // need to iterate with a visitor to measure those they care for.
  87. size_t bytesUsed() const;
  88. // Rearrange and resize this record to eliminate any NoOps.
  89. // May change count() and the indices of ops, but preserves their order.
  90. void defrag();
  91. private:
  92. // An SkRecord is structured as an array of pointers into a big chunk of memory where
  93. // records representing each canvas draw call are stored:
  94. //
  95. // fRecords: [*][*][*]...
  96. // | | |
  97. // | | |
  98. // | | +---------------------------------------+
  99. // | +-----------------+ |
  100. // | | |
  101. // v v v
  102. // fAlloc: [SkRecords::DrawRect][SkRecords::DrawPosTextH][SkRecords::DrawRect]...
  103. //
  104. // We store the types of each of the pointers alongside the pointer.
  105. // The cost to append a T to this structure is 8 + sizeof(T) bytes.
  106. // A mutator that can be used with replace to destroy canvas commands.
  107. struct Destroyer {
  108. template <typename T>
  109. void operator()(T* record) { record->~T(); }
  110. };
  111. template <typename T>
  112. SK_WHEN(std::is_empty<T>::value, T*) allocCommand() {
  113. static T singleton = {};
  114. return &singleton;
  115. }
  116. template <typename T>
  117. SK_WHEN(!std::is_empty<T>::value, T*) allocCommand() { return this->alloc<T>(); }
  118. void grow();
  119. // A typed pointer to some bytes in fAlloc. visit() and mutate() allow polymorphic dispatch.
  120. struct Record {
  121. SkRecords::Type fType;
  122. void* fPtr;
  123. // Point this record to its data in fAlloc. Returns ptr for convenience.
  124. template <typename T>
  125. T* set(T* ptr) {
  126. fType = T::kType;
  127. fPtr = ptr;
  128. SkASSERT(this->ptr() == ptr && this->type() == T::kType);
  129. return ptr;
  130. }
  131. SkRecords::Type type() const { return fType; }
  132. void* ptr() const { return fPtr; }
  133. // Visit this record with functor F (see public API above).
  134. template <typename F>
  135. auto visit(F&& f) const -> decltype(f(SkRecords::NoOp())) {
  136. #define CASE(T) case SkRecords::T##_Type: return f(*(const SkRecords::T*)this->ptr());
  137. switch(this->type()) { SK_RECORD_TYPES(CASE) }
  138. #undef CASE
  139. SkDEBUGFAIL("Unreachable");
  140. static const SkRecords::NoOp noop{};
  141. return f(noop);
  142. }
  143. // Mutate this record with functor F (see public API above).
  144. template <typename F>
  145. auto mutate(F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) {
  146. #define CASE(T) case SkRecords::T##_Type: return f((SkRecords::T*)this->ptr());
  147. switch(this->type()) { SK_RECORD_TYPES(CASE) }
  148. #undef CASE
  149. SkDEBUGFAIL("Unreachable");
  150. static const SkRecords::NoOp noop{};
  151. return f(const_cast<SkRecords::NoOp*>(&noop));
  152. }
  153. };
  154. // fRecords needs to be a data structure that can append fixed length data, and need to
  155. // support efficient random access and forward iteration. (It doesn't need to be contiguous.)
  156. int fCount{0},
  157. fReserved{0};
  158. SkAutoTMalloc<Record> fRecords;
  159. // fAlloc needs to be a data structure which can append variable length data in contiguous
  160. // chunks, returning a stable handle to that data for later retrieval.
  161. SkArenaAlloc fAlloc{256};
  162. size_t fApproxBytesAllocated{0};
  163. };
  164. #endif//SkRecord_DEFINED