SkCachedData.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 SkCachedData_DEFINED
  8. #define SkCachedData_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #include "include/private/SkMutex.h"
  11. #include "include/private/SkNoncopyable.h"
  12. class SkDiscardableMemory;
  13. class SkCachedData : ::SkNoncopyable {
  14. public:
  15. SkCachedData(void* mallocData, size_t size);
  16. SkCachedData(size_t size, SkDiscardableMemory*);
  17. virtual ~SkCachedData();
  18. size_t size() const { return fSize; }
  19. const void* data() const { return fData; }
  20. void* writable_data() { return fData; }
  21. void ref() const { this->internalRef(false); }
  22. void unref() const { this->internalUnref(false); }
  23. int testing_only_getRefCnt() const { return fRefCnt; }
  24. bool testing_only_isLocked() const { return fIsLocked; }
  25. bool testing_only_isInCache() const { return fInCache; }
  26. SkDiscardableMemory* diagnostic_only_getDiscardable() const {
  27. return kDiscardableMemory_StorageType == fStorageType ? fStorage.fDM : nullptr;
  28. }
  29. protected:
  30. // called when fData changes. could be nullptr.
  31. virtual void onDataChange(void* oldData, void* newData) {}
  32. private:
  33. SkMutex fMutex; // could use a pool of these...
  34. enum StorageType {
  35. kDiscardableMemory_StorageType,
  36. kMalloc_StorageType
  37. };
  38. union {
  39. SkDiscardableMemory* fDM;
  40. void* fMalloc;
  41. } fStorage;
  42. void* fData;
  43. size_t fSize;
  44. int fRefCnt; // low-bit means we're owned by the cache
  45. StorageType fStorageType;
  46. bool fInCache;
  47. bool fIsLocked;
  48. void internalRef(bool fromCache) const;
  49. void internalUnref(bool fromCache) const;
  50. void inMutexRef(bool fromCache);
  51. bool inMutexUnref(bool fromCache); // returns true if we should delete "this"
  52. void inMutexLock();
  53. void inMutexUnlock();
  54. // called whenever our fData might change (lock or unlock)
  55. void setData(void* newData) {
  56. if (newData != fData) {
  57. // notify our subclasses of the change
  58. this->onDataChange(fData, newData);
  59. fData = newData;
  60. }
  61. }
  62. class AutoMutexWritable;
  63. public:
  64. #ifdef SK_DEBUG
  65. void validate() const;
  66. #else
  67. void validate() const {}
  68. #endif
  69. /*
  70. * Attaching a data to to a SkResourceCache (only one at a time) enables the data to be
  71. * unlocked when the cache is the only owner, thus freeing it to be purged (assuming the
  72. * data is backed by a SkDiscardableMemory).
  73. *
  74. * When attached, it also automatically attempts to "lock" the data when the first client
  75. * ref's the data (typically from a find(key, visitor) call).
  76. *
  77. * Thus the data will always be "locked" when a non-cache has a ref on it (whether or not
  78. * the lock succeeded to recover the memory -- check data() to see if it is nullptr).
  79. */
  80. /*
  81. * Call when adding this instance to a SkResourceCache::Rec subclass
  82. * (typically in the Rec's constructor).
  83. */
  84. void attachToCacheAndRef() const { this->internalRef(true); }
  85. /*
  86. * Call when removing this instance from a SkResourceCache::Rec subclass
  87. * (typically in the Rec's destructor).
  88. */
  89. void detachFromCacheAndUnref() const { this->internalUnref(true); }
  90. };
  91. #endif