skia2/tests/ResourceCacheTest.cpp

917 lines
38 KiB
C++
Raw Normal View History

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrContextFactory.h"
#include "GrGpu.h"
#include "GrResourceCache.h"
#include "SkCanvas.h"
#include "SkGr.h"
#include "SkMessageBus.h"
#include "SkSurface.h"
#include "Test.h"
static const int gWidth = 640;
static const int gHeight = 480;
////////////////////////////////////////////////////////////////////////////////
static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
SkBitmap src;
src.allocN32Pixels(size.width(), size.height());
src.eraseColor(SK_ColorBLACK);
size_t srcSize = src.getSize();
size_t initialCacheSize;
context->getResourceCacheUsage(NULL, &initialCacheSize);
int oldMaxNum;
size_t oldMaxBytes;
context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
// Set the cache limits so we can fit 10 "src" images and the
// max number of textures doesn't matter
size_t maxCacheSize = initialCacheSize + 10*srcSize;
context->setResourceCacheLimits(1000, maxCacheSize);
SkBitmap readback;
readback.allocN32Pixels(size.width(), size.height());
for (int i = 0; i < 100; ++i) {
canvas->drawBitmap(src, 0, 0);
canvas->readPixels(size, &readback);
// "modify" the src texture
src.notifyPixelsChanged();
size_t curCacheSize;
context->getResourceCacheUsage(NULL, &curCacheSize);
// we should never go over the size limit
REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
}
context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
}
class TestResource : public GrGpuResource {
static const size_t kDefaultSize = 100;
enum ScratchConstructor { kScratchConstructor };
public:
SK_DECLARE_INST_COUNT(TestResource);
/** Property that distinctly categorizes the resource.
* For example, textures have width, height, ... */
enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
: INHERITED(gpu, lifeCycle)
, fToDelete(NULL)
, fSize(size)
, fProperty(kA_SimulatedProperty) {
++fNumAlive;
this->registerWithCache();
}
TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
: INHERITED(gpu, lifeCycle)
, fToDelete(NULL)
, fSize(kDefaultSize)
, fProperty(kA_SimulatedProperty) {
++fNumAlive;
this->registerWithCache();
}
TestResource(GrGpu* gpu)
: INHERITED(gpu, kCached_LifeCycle)
, fToDelete(NULL)
, fSize(kDefaultSize)
, fProperty(kA_SimulatedProperty) {
++fNumAlive;
this->registerWithCache();
}
static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor));
}
~TestResource() {
--fNumAlive;
SkSafeUnref(fToDelete);
}
void setSize(size_t size) {
fSize = size;
this->didChangeGpuMemorySize();
}
static int NumAlive() { return fNumAlive; }
void setUnrefWhenDestroyed(TestResource* resource) {
SkRefCnt_SafeAssign(fToDelete, resource);
}
static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
builder[i] = static_cast<uint32_t>(i + property);
}
}
static size_t ExpectedScratchKeySize() {
return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
}
private:
static const int kScratchKeyFieldCnt = 6;
TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
: INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
, fToDelete(NULL)
, fSize(kDefaultSize)
, fProperty(property) {
GrScratchKey scratchKey;
ComputeScratchKey(fProperty, &scratchKey);
this->setScratchKey(scratchKey);
++fNumAlive;
this->registerWithCache();
}
size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; }
TestResource* fToDelete;
size_t fSize;
static int fNumAlive;
SimulatedProperty fProperty;
typedef GrGpuResource INHERITED;
};
int TestResource::fNumAlive = 0;
class Mock {
public:
Mock(int maxCnt, size_t maxBytes) {
fContext.reset(GrContext::CreateMockContext());
SkASSERT(fContext);
fContext->setResourceCacheLimits(maxCnt, maxBytes);
GrResourceCache* cache = fContext->getResourceCache();
cache->purgeAllUnlocked();
SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
}
GrResourceCache* cache() { return fContext->getResourceCache(); }
GrContext* context() { return fContext; }
private:
SkAutoTUnref<GrContext> fContext;
};
static void test_no_key(skiatest::Reporter* reporter) {
Mock mock(10, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
// Create a bunch of resources with no keys
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu()));
TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu()));
a->setSize(11);
b->setSize(12);
c->setSize(13);
d->setSize(14);
REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
d->gpuMemorySize() == cache->getResourceBytes());
// Should be safe to purge without deleting the resources since we still have refs.
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
// Since the resources have neither content nor scratch keys, delete immediately upon unref.
a->unref();
REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
cache->getResourceBytes());
c->unref();
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
cache->getResourceBytes());
d->unref();
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
b->unref();
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
}
// Each integer passed as a template param creates a new domain.
template <int> static void make_content_key(GrContentKey* key, int data) {
static GrContentKey::Domain d = GrContentKey::GenerateDomain();
GrContentKey::Builder builder(key, d, 1);
builder[0] = data;
}
static void test_budgeting(skiatest::Reporter* reporter) {
Mock mock(10, 300);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
GrContentKey contentKey;
make_content_key<0>(&contentKey, 0);
// Create a scratch, a content, and a wrapped resource
TestResource* scratch =
TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
scratch->setSize(10);
TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu()));
content->setSize(11);
REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey));
TestResource* wrapped = SkNEW_ARGS(TestResource,
(context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
wrapped->setSize(12);
TestResource* unbudgeted = SkNEW_ARGS(TestResource,
(context->getGpu(), GrGpuResource::kUncached_LifeCycle));
unbudgeted->setSize(13);
// Make sure we can't add a content key to the wrapped resource
GrContentKey contentKey2;
make_content_key<0>(&contentKey2, 1);
REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2));
REPORTER_ASSERT(reporter, NULL == cache->findAndRefContentResource(contentKey2));
// Make sure sizes are as we expect
REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() ==
cache->getBudgetedResourceBytes());
// Our refs mean that the resources are non purgeable.
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() ==
cache->getBudgetedResourceBytes());
// Unreffing the wrapped resource should free it right away.
wrapped->unref();
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
unbudgeted->gpuMemorySize() == cache->getResourceBytes());
// Now try freeing the budgeted resources first
wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle));
scratch->setSize(12);
content->unref();
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
unbudgeted->gpuMemorySize() == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
scratch->unref();
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
cache->getResourceBytes());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
wrapped->unref();
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
unbudgeted->unref();
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
}
static void test_unbudgeted(skiatest::Reporter* reporter) {
Mock mock(10, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
GrContentKey contentKey;
make_content_key<0>(&contentKey, 0);
TestResource* scratch;
TestResource* content;
TestResource* wrapped;
TestResource* unbudgeted;
// A large uncached or wrapped resource shouldn't evict anything.
scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
scratch->setSize(10);
scratch->unref();
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
content = SkNEW_ARGS(TestResource, (context->getGpu()));
content->setSize(11);
REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey));
content->unref();
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
size_t large = 2 * cache->getResourceBytes();
unbudgeted = SkNEW_ARGS(TestResource,
(context->getGpu(), large, GrGpuResource::kUncached_LifeCycle));
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
unbudgeted->unref();
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
wrapped = SkNEW_ARGS(TestResource,
(context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle));
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
wrapped->unref();
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
}
static void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
Mock mock(10, 300);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
TestResource* resource =
TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
GrScratchKey key;
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
size_t size = resource->gpuMemorySize();
for (int i = 0; i < 2; ++i) {
// Since this resource is unbudgeted, it should not be reachable as scratch.
REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key);
REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
REPORTER_ASSERT(reporter, !resource->cacheAccess().isBudgeted());
REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key));
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
// Once it is unrefed, it should become available as scratch.
resource->unref();
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key));
REPORTER_ASSERT(reporter, resource);
REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key);
REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted());
if (0 == i) {
// If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
// the above tests again.
resource->cacheAccess().makeUnbudgeted();
} else {
// After the second time around, try removing the scratch key
resource->cacheAccess().removeScratchKey();
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
REPORTER_ASSERT(reporter, !resource->cacheAccess().getScratchKey().isValid());
REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted());
// now when it is unrefed it should die since it has no key.
resource->unref();
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
}
}
}
static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
Mock mock(5, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
// Create two resources that have the same scratch key.
TestResource* a = TestResource::CreateScratch(context->getGpu(),
TestResource::kB_SimulatedProperty);
TestResource* b = TestResource::CreateScratch(context->getGpu(),
TestResource::kB_SimulatedProperty);
a->setSize(11);
b->setSize(12);
GrScratchKey scratchKey1;
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
// Check for negative case consistency. (leaks upon test failure.)
REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1));
GrScratchKey scratchKey;
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
// Scratch resources are registered with GrResourceCache just by existing. There are 2.
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
cache->getResourceBytes());
// Our refs mean that the resources are non purgeable.
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
// Unref but don't purge
a->unref();
b->unref();
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
// Purge again. This time resources should be purgeable.
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
}
static void test_remove_scratch_key(skiatest::Reporter* reporter) {
Mock mock(5, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
// Create two resources that have the same scratch key.
TestResource* a = TestResource::CreateScratch(context->getGpu(),
TestResource::kB_SimulatedProperty);
TestResource* b = TestResource::CreateScratch(context->getGpu(),
TestResource::kB_SimulatedProperty);
a->unref();
b->unref();
GrScratchKey scratchKey;
// Ensure that scratch key lookup is correct for negative case.
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
// (following leaks upon test failure).
REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
// Scratch resources are registered with GrResourceCache just by existing. There are 2.
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
// Find the first resource and remove its scratch key
GrGpuResource* find;
find = cache->findAndRefScratchResource(scratchKey);
find->cacheAccess().removeScratchKey();
// It's still alive, but not cached by scratch key anymore
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
// The cache should immediately delete it when it's unrefed since it isn't accessible.
find->unref();
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
// Repeat for the second resource.
find = cache->findAndRefScratchResource(scratchKey);
find->cacheAccess().removeScratchKey();
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
// Should be able to call this multiple times with no problem.
find->cacheAccess().removeScratchKey();
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
find->unref();
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
}
static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
Mock mock(5, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
// Create two resources that have the same scratch key.
TestResource* a = TestResource::CreateScratch(context->getGpu(),
TestResource::kB_SimulatedProperty);
TestResource* b = TestResource::CreateScratch(context->getGpu(),
TestResource::kB_SimulatedProperty);
a->unref();
b->unref();
GrScratchKey scratchKey;
// Ensure that scratch key comparison and assignment is consistent.
GrScratchKey scratchKey1;
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
GrScratchKey scratchKey2;
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
scratchKey = scratchKey1;
REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
scratchKey = scratchKey2;
REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
// Ensure that scratch key lookup is correct for negative case.
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
// (following leaks upon test failure).
REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL);
// Find the first resource with a scratch key and a copy of a scratch key.
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
GrGpuResource* find = cache->findAndRefScratchResource(scratchKey);
REPORTER_ASSERT(reporter, find != NULL);
find->unref();
scratchKey2 = scratchKey;
find = cache->findAndRefScratchResource(scratchKey2);
REPORTER_ASSERT(reporter, find != NULL);
REPORTER_ASSERT(reporter, find == a || find == b);
GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2);
REPORTER_ASSERT(reporter, find2 != NULL);
REPORTER_ASSERT(reporter, find2 == a || find2 == b);
REPORTER_ASSERT(reporter, find2 != find);
find2->unref();
find->unref();
}
static void test_duplicate_content_key(skiatest::Reporter* reporter) {
Mock mock(5, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
GrContentKey key;
make_content_key<0>(&key, 0);
// Create two resources that we will attempt to register with the same content key.
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
a->setSize(11);
b->setSize(12);
// Can't set the same content key on two resources.
REPORTER_ASSERT(reporter, a->cacheAccess().setContentKey(key));
REPORTER_ASSERT(reporter, !b->cacheAccess().setContentKey(key));
// Still have two resources because b is still reffed.
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
b->unref();
// Now b should be gone.
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
// Drop the ref on a but it isn't immediately purged as it still has a valid scratch key.
a->unref();
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
}
static void test_purge_invalidated(skiatest::Reporter* reporter) {
Mock mock(5, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
GrContentKey key1, key2, key3;
make_content_key<0>(&key1, 1);
make_content_key<0>(&key2, 2);
make_content_key<0>(&key3, 3);
// Add three resources to the cache. Only c is usable as scratch.
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
TestResource* c = TestResource::CreateScratch(context->getGpu(),
TestResource::kA_SimulatedProperty);
a->cacheAccess().setContentKey(key1);
b->cacheAccess().setContentKey(key2);
c->cacheAccess().setContentKey(key3);
a->unref();
// hold b until *after* the message is sent.
c->unref();
REPORTER_ASSERT(reporter, cache->hasContentKey(key1));
REPORTER_ASSERT(reporter, cache->hasContentKey(key2));
REPORTER_ASSERT(reporter, cache->hasContentKey(key3));
REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
typedef GrContentKeyInvalidatedMessage Msg;
typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus;
// Invalidate two of the three, they should be purged and no longer accessible via their keys.
Bus::Post(Msg(key1));
Bus::Post(Msg(key2));
cache->purgeAsNeeded();
// a should be deleted now, but we still have a ref on b.
REPORTER_ASSERT(reporter, !cache->hasContentKey(key1));
REPORTER_ASSERT(reporter, !cache->hasContentKey(key2));
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, cache->hasContentKey(key3));
// Invalidate the third.
Bus::Post(Msg(key3));
cache->purgeAsNeeded();
// we still have a ref on b, c should be recycled as scratch.
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, !cache->hasContentKey(key3));
// make b purgeable. It should be immediately deleted since it has no key.
b->unref();
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
// Make sure we actually get to c via it's scratch key, before we say goodbye.
GrScratchKey scratchKey;
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey);
REPORTER_ASSERT(reporter, scratch == c);
SkSafeUnref(scratch);
// Get rid of c.
cache->purgeAllUnlocked();
scratch = cache->findAndRefScratchResource(scratchKey);
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, !scratch);
SkSafeUnref(scratch);
}
static void test_cache_chained_purge(skiatest::Reporter* reporter) {
Mock mock(3, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
GrContentKey key1, key2;
make_content_key<0>(&key1, 1);
make_content_key<0>(&key2, 2);
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
a->cacheAccess().setContentKey(key1);
b->cacheAccess().setContentKey(key2);
// Make a cycle
a->setUnrefWhenDestroyed(b);
b->setUnrefWhenDestroyed(a);
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
a->unref();
b->unref();
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
// Break the cycle
a->setUnrefWhenDestroyed(NULL);
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
}
static void test_resource_size_changed(skiatest::Reporter* reporter) {
GrContentKey key1, key2;
make_content_key<0>(&key1, 1);
make_content_key<0>(&key2, 2);
// Test changing resources sizes (both increase & decrease).
{
Mock mock(3, 30000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
a->cacheAccess().setContentKey(key1);
a->unref();
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
b->cacheAccess().setContentKey(key2);
b->unref();
REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
{
SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->findAndRefContentResource(key2)));
find2->setSize(200);
SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache->findAndRefContentResource(key1)));
find1->setSize(50);
}
REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
}
// Test increasing a resources size beyond the cache budget.
{
Mock mock(2, 300);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu()));
a->setSize(100);
a->cacheAccess().setContentKey(key1);
a->unref();
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu()));
b->setSize(100);
b->cacheAccess().setContentKey(key2);
b->unref();
REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
{
SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->findAndRefContentResource(key2)));
find2->setSize(201);
}
REPORTER_ASSERT(reporter, !cache->hasContentKey(key1));
REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
}
}
static void test_large_resource_count(skiatest::Reporter* reporter) {
// Set the cache size to double the resource count because we're going to create 2x that number
// resources, using two different key domains. Add a little slop to the bytes because we resize
// down to 1 byte after creating the resource.
static const int kResourceCnt = 2000;
Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
GrContext* context = mock.context();
GrResourceCache* cache = mock.cache();
for (int i = 0; i < kResourceCnt; ++i) {
GrContentKey key1, key2;
make_content_key<1>(&key1, i);
make_content_key<2>(&key2, i);
TestResource* resource;
resource = SkNEW_ARGS(TestResource, (context->getGpu()));
resource->cacheAccess().setContentKey(key1);
resource->setSize(1);
resource->unref();
resource = SkNEW_ARGS(TestResource, (context->getGpu()));
resource->cacheAccess().setContentKey(key2);
resource->setSize(1);
resource->unref();
}
REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
for (int i = 0; i < kResourceCnt; ++i) {
GrContentKey key1, key2;
make_content_key<1>(&key1, i);
make_content_key<2>(&key2, i);
REPORTER_ASSERT(reporter, cache->hasContentKey(key1));
REPORTER_ASSERT(reporter, cache->hasContentKey(key2));
}
cache->purgeAllUnlocked();
REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
for (int i = 0; i < kResourceCnt; ++i) {
GrContentKey key1, key2;
make_content_key<1>(&key1, i);
make_content_key<2>(&key2, i);
REPORTER_ASSERT(reporter, !cache->hasContentKey(key1));
REPORTER_ASSERT(reporter, !cache->hasContentKey(key2));
}
}
////////////////////////////////////////////////////////////////////////////////
DEF_GPUTEST(ResourceCache, reporter, factory) {
for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
if (!GrContextFactory::IsRenderingGLContext(glType)) {
continue;
}
GrContext* context = factory->get(glType);
if (NULL == context) {
continue;
}
GrSurfaceDesc desc;
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fFlags = kRenderTarget_GrSurfaceFlag;
desc.fWidth = gWidth;
desc.fHeight = gHeight;
SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
SkSurface::kNo_Budgeted, info));
test_cache(reporter, context, surface->getCanvas());
}
// The below tests create their own mock contexts.
test_no_key(reporter);
test_budgeting(reporter);
test_unbudgeted(reporter);
test_unbudgeted_to_scratch(reporter);
test_duplicate_content_key(reporter);
test_duplicate_scratch_key(reporter);
test_remove_scratch_key(reporter);
test_scratch_key_consistency(reporter);
test_purge_invalidated(reporter);
test_cache_chained_purge(reporter);
test_resource_size_changed(reporter);
test_large_resource_count(reporter);
}
#endif