SkPtrRecorder.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2008 The Android Open Source Project
  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 SkPtrSet_DEFINED
  8. #define SkPtrSet_DEFINED
  9. #include "include/core/SkFlattenable.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "include/private/SkTDArray.h"
  12. /**
  13. * Maintains a set of ptrs, assigning each a unique ID [1...N]. Duplicate ptrs
  14. * return the same ID (since its a set). Subclasses can override inPtr()
  15. * and decPtr(). incPtr() is called each time a unique ptr is added ot the
  16. * set. decPtr() is called on each ptr when the set is destroyed or reset.
  17. */
  18. class SkPtrSet : public SkRefCnt {
  19. public:
  20. /**
  21. * Search for the specified ptr in the set. If it is found, return its
  22. * 32bit ID [1..N], or if not found, return 0. Always returns 0 for nullptr.
  23. */
  24. uint32_t find(void*) const;
  25. /**
  26. * Add the specified ptr to the set, returning a unique 32bit ID for it
  27. * [1...N]. Duplicate ptrs will return the same ID.
  28. *
  29. * If the ptr is nullptr, it is not added, and 0 is returned.
  30. */
  31. uint32_t add(void*);
  32. /**
  33. * Return the number of (non-null) ptrs in the set.
  34. */
  35. int count() const { return fList.count(); }
  36. /**
  37. * Copy the ptrs in the set into the specified array (allocated by the
  38. * caller). The ptrs are assgined to the array based on their corresponding
  39. * ID. e.g. array[ptr.ID - 1] = ptr.
  40. *
  41. * incPtr() and decPtr() are not called during this operation.
  42. */
  43. void copyToArray(void* array[]) const;
  44. /**
  45. * Call decPtr() on each ptr in the set, and the reset the size of the set
  46. * to 0.
  47. */
  48. void reset();
  49. /**
  50. * Set iterator.
  51. */
  52. class Iter {
  53. public:
  54. Iter(const SkPtrSet& set)
  55. : fSet(set)
  56. , fIndex(0) {}
  57. /**
  58. * Return the next ptr in the set or null if the end was reached.
  59. */
  60. void* next() {
  61. return fIndex < fSet.fList.count() ? fSet.fList[fIndex++].fPtr : nullptr;
  62. }
  63. private:
  64. const SkPtrSet& fSet;
  65. int fIndex;
  66. };
  67. protected:
  68. virtual void incPtr(void*) {}
  69. virtual void decPtr(void*) {}
  70. private:
  71. struct Pair {
  72. void* fPtr; // never nullptr
  73. uint32_t fIndex; // 1...N
  74. };
  75. // we store the ptrs in sorted-order (using Cmp) so that we can efficiently
  76. // detect duplicates when add() is called. Hence we need to store the
  77. // ptr and its ID/fIndex explicitly, since the ptr's position in the array
  78. // is not related to its "index".
  79. SkTDArray<Pair> fList;
  80. static bool Less(const Pair& a, const Pair& b);
  81. typedef SkRefCnt INHERITED;
  82. };
  83. /**
  84. * Templated wrapper for SkPtrSet, just meant to automate typecasting
  85. * parameters to and from void* (which the base class expects).
  86. */
  87. template <typename T> class SkTPtrSet : public SkPtrSet {
  88. public:
  89. uint32_t find(T ptr) {
  90. return this->INHERITED::find((void*)ptr);
  91. }
  92. uint32_t add(T ptr) {
  93. return this->INHERITED::add((void*)ptr);
  94. }
  95. void copyToArray(T* array) const {
  96. this->INHERITED::copyToArray((void**)array);
  97. }
  98. private:
  99. typedef SkPtrSet INHERITED;
  100. };
  101. /**
  102. * Subclass of SkTPtrSet specialed to call ref() and unref() when the
  103. * base class's incPtr() and decPtr() are called. This makes it a valid owner
  104. * of each ptr, which is released when the set is reset or destroyed.
  105. */
  106. class SkRefCntSet : public SkTPtrSet<SkRefCnt*> {
  107. public:
  108. virtual ~SkRefCntSet();
  109. protected:
  110. // overrides
  111. virtual void incPtr(void*);
  112. virtual void decPtr(void*);
  113. };
  114. class SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {};
  115. /**
  116. * Similar to SkFactorySet, but only allows Factorys that have registered names.
  117. * Also has a function to return the next added Factory's name.
  118. */
  119. class SkNamedFactorySet : public SkRefCnt {
  120. public:
  121. SkNamedFactorySet();
  122. /**
  123. * Find the specified Factory in the set. If it is not already in the set,
  124. * and has registered its name, add it to the set, and return its index.
  125. * If the Factory has no registered name, return 0.
  126. */
  127. uint32_t find(SkFlattenable::Factory);
  128. /**
  129. * If new Factorys have been added to the set, return the name of the first
  130. * Factory added after the Factory name returned by the last call to this
  131. * function.
  132. */
  133. const char* getNextAddedFactoryName();
  134. private:
  135. int fNextAddedFactory;
  136. SkFactorySet fFactorySet;
  137. SkTDArray<const char*> fNames;
  138. typedef SkRefCnt INHERITED;
  139. };
  140. #endif