SkDiscardableMemoryPool.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2013 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 SkDiscardableMemoryPool_DEFINED
  8. #define SkDiscardableMemoryPool_DEFINED
  9. #include "include/private/SkMutex.h"
  10. #include "src/core/SkDiscardableMemory.h"
  11. #ifndef SK_LAZY_CACHE_STATS
  12. #ifdef SK_DEBUG
  13. #define SK_LAZY_CACHE_STATS 1
  14. #else
  15. #define SK_LAZY_CACHE_STATS 0
  16. #endif
  17. #endif
  18. /**
  19. * An implementation of Discardable Memory that manages a fixed-size
  20. * budget of memory. When the allocated memory exceeds this size,
  21. * unlocked blocks of memory are purged. If all memory is locked, it
  22. * can exceed the memory-use budget.
  23. */
  24. class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
  25. public:
  26. virtual ~SkDiscardableMemoryPool() { }
  27. virtual size_t getRAMUsed() = 0;
  28. virtual void setRAMBudget(size_t budget) = 0;
  29. virtual size_t getRAMBudget() = 0;
  30. /** purges all unlocked DMs */
  31. virtual void dumpPool() = 0;
  32. #if SK_LAZY_CACHE_STATS
  33. /**
  34. * These two values are a count of the number of successful and
  35. * failed calls to SkDiscardableMemory::lock() for all DMs managed
  36. * by this pool.
  37. */
  38. virtual int getCacheHits() = 0;
  39. virtual int getCacheMisses() = 0;
  40. virtual void resetCacheHitsAndMisses() = 0;
  41. #endif
  42. /**
  43. * This non-global pool can be used for unit tests to verify that
  44. * the pool works.
  45. */
  46. static sk_sp<SkDiscardableMemoryPool> Make(size_t size);
  47. };
  48. /**
  49. * Returns (and creates if needed) a threadsafe global
  50. * SkDiscardableMemoryPool.
  51. */
  52. SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool();
  53. #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE)
  54. #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024)
  55. #endif
  56. #endif // SkDiscardableMemoryPool_DEFINED