ImageCacheTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "src/core/SkDiscardableMemory.h"
  8. #include "src/core/SkResourceCache.h"
  9. #include "tests/Test.h"
  10. namespace {
  11. static void* gGlobalAddress;
  12. struct TestingKey : public SkResourceCache::Key {
  13. intptr_t fValue;
  14. TestingKey(intptr_t value, uint64_t sharedID = 0) : fValue(value) {
  15. this->init(&gGlobalAddress, sharedID, sizeof(fValue));
  16. }
  17. };
  18. struct TestingRec : public SkResourceCache::Rec {
  19. TestingRec(const TestingKey& key, uint32_t value) : fKey(key), fValue(value) {}
  20. TestingKey fKey;
  21. intptr_t fValue;
  22. const Key& getKey() const override { return fKey; }
  23. size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
  24. const char* getCategory() const override { return "test_cache"; }
  25. SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
  26. static bool Visitor(const SkResourceCache::Rec& baseRec, void* context) {
  27. const TestingRec& rec = static_cast<const TestingRec&>(baseRec);
  28. intptr_t* result = (intptr_t*)context;
  29. *result = rec.fValue;
  30. return true;
  31. }
  32. };
  33. }
  34. static const int COUNT = 10;
  35. static const int DIM = 256;
  36. static void test_cache(skiatest::Reporter* reporter, SkResourceCache& cache, bool testPurge) {
  37. for (int i = 0; i < COUNT; ++i) {
  38. TestingKey key(i);
  39. intptr_t value = -1;
  40. REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
  41. REPORTER_ASSERT(reporter, -1 == value);
  42. cache.add(new TestingRec(key, i));
  43. REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
  44. REPORTER_ASSERT(reporter, i == value);
  45. }
  46. if (testPurge) {
  47. // stress test, should trigger purges
  48. for (int i = 0; i < COUNT * 100; ++i) {
  49. TestingKey key(i);
  50. cache.add(new TestingRec(key, i));
  51. }
  52. }
  53. // test the originals after all that purging
  54. for (int i = 0; i < COUNT; ++i) {
  55. intptr_t value;
  56. (void)cache.find(TestingKey(i), TestingRec::Visitor, &value);
  57. }
  58. cache.setTotalByteLimit(0);
  59. }
  60. static void test_cache_purge_shared_id(skiatest::Reporter* reporter, SkResourceCache& cache) {
  61. for (int i = 0; i < COUNT; ++i) {
  62. TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
  63. cache.add(new TestingRec(key, i));
  64. }
  65. // Ensure that everyone is present
  66. for (int i = 0; i < COUNT; ++i) {
  67. TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
  68. intptr_t value = -1;
  69. REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
  70. REPORTER_ASSERT(reporter, value == i);
  71. }
  72. // Now purge the ones that had a non-zero sharedID (the odd-indexed ones)
  73. cache.purgeSharedID(1);
  74. // Ensure that only the even ones are still present
  75. for (int i = 0; i < COUNT; ++i) {
  76. TestingKey key(i, i & 1); // every other key will have a 1 for its sharedID
  77. intptr_t value = -1;
  78. if (i & 1) {
  79. REPORTER_ASSERT(reporter, !cache.find(key, TestingRec::Visitor, &value));
  80. } else {
  81. REPORTER_ASSERT(reporter, cache.find(key, TestingRec::Visitor, &value));
  82. REPORTER_ASSERT(reporter, value == i);
  83. }
  84. }
  85. }
  86. #include "src/lazy/SkDiscardableMemoryPool.h"
  87. static SkDiscardableMemoryPool* gPool;
  88. static SkDiscardableMemory* pool_factory(size_t bytes) {
  89. SkASSERT(gPool);
  90. return gPool->create(bytes);
  91. }
  92. DEF_TEST(ImageCache, reporter) {
  93. static const size_t defLimit = DIM * DIM * 4 * COUNT + 1024; // 1K slop
  94. {
  95. SkResourceCache cache(defLimit);
  96. test_cache(reporter, cache, true);
  97. }
  98. {
  99. sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(defLimit));
  100. gPool = pool.get();
  101. SkResourceCache cache(pool_factory);
  102. test_cache(reporter, cache, true);
  103. }
  104. {
  105. SkResourceCache cache(SkDiscardableMemory::Create);
  106. test_cache(reporter, cache, false);
  107. }
  108. {
  109. SkResourceCache cache(defLimit);
  110. test_cache_purge_shared_id(reporter, cache);
  111. }
  112. }
  113. DEF_TEST(ImageCache_doubleAdd, r) {
  114. // Adding the same key twice should be safe.
  115. SkResourceCache cache(4096);
  116. TestingKey key(1);
  117. cache.add(new TestingRec(key, 2));
  118. cache.add(new TestingRec(key, 3));
  119. // Lookup can return either value.
  120. intptr_t value = -1;
  121. REPORTER_ASSERT(r, cache.find(key, TestingRec::Visitor, &value));
  122. REPORTER_ASSERT(r, 2 == value || 3 == value);
  123. }