2013-08-08 22:55:21 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2014-01-10 22:08:27 +00:00
|
|
|
|
2014-10-08 15:40:09 +00:00
|
|
|
#include "GrContext.h"
|
2013-08-08 22:55:21 +00:00
|
|
|
#include "GrContextFactory.h"
|
2014-10-08 15:40:09 +00:00
|
|
|
#include "GrGpu.h"
|
2014-11-05 22:47:41 +00:00
|
|
|
#include "GrResourceCache2.h"
|
2014-10-08 15:40:09 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-11-14 20:10:14 +00:00
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkMessageBus.h"
|
2014-09-18 13:09:44 +00:00
|
|
|
#include "SkSurface.h"
|
2014-01-10 22:08:27 +00:00
|
|
|
#include "Test.h"
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
static const int gWidth = 640;
|
|
|
|
static const int gHeight = 480;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-11-05 22:47:41 +00:00
|
|
|
static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
|
2013-08-08 22:55:21 +00:00
|
|
|
const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
|
|
|
|
|
|
|
|
SkBitmap src;
|
2014-02-13 14:41:43 +00:00
|
|
|
src.allocN32Pixels(size.width(), size.height());
|
2013-08-08 22:55:21 +00:00
|
|
|
src.eraseColor(SK_ColorBLACK);
|
|
|
|
size_t srcSize = src.getSize();
|
|
|
|
|
2014-05-09 14:29:32 +00:00
|
|
|
size_t initialCacheSize;
|
|
|
|
context->getResourceCacheUsage(NULL, &initialCacheSize);
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
int oldMaxNum;
|
|
|
|
size_t oldMaxBytes;
|
2014-05-09 14:29:32 +00:00
|
|
|
context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
|
2013-08-09 07:01:22 +00:00
|
|
|
|
2013-08-08 22:55:21 +00:00
|
|
|
// 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;
|
2014-05-09 14:29:32 +00:00
|
|
|
context->setResourceCacheLimits(1000, maxCacheSize);
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
SkBitmap readback;
|
2014-02-13 14:41:43 +00:00
|
|
|
readback.allocN32Pixels(size.width(), size.height());
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
canvas->drawBitmap(src, 0, 0);
|
|
|
|
canvas->readPixels(size, &readback);
|
|
|
|
|
|
|
|
// "modify" the src texture
|
|
|
|
src.notifyPixelsChanged();
|
|
|
|
|
2014-05-09 14:29:32 +00:00
|
|
|
size_t curCacheSize;
|
|
|
|
context->getResourceCacheUsage(NULL, &curCacheSize);
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
// we should never go over the size limit
|
|
|
|
REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:29:32 +00:00
|
|
|
context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
|
2013-08-08 22:55:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
class TestResource : public GrGpuResource {
|
2014-05-05 19:09:13 +00:00
|
|
|
static const size_t kDefaultSize = 100;
|
|
|
|
|
2014-01-15 23:09:01 +00:00
|
|
|
public:
|
|
|
|
SK_DECLARE_INST_COUNT(TestResource);
|
2014-11-17 15:34:06 +00:00
|
|
|
TestResource(GrGpu* gpu, bool isWrapped)
|
|
|
|
: INHERITED(gpu, isWrapped)
|
|
|
|
, fToDelete(NULL)
|
|
|
|
, fSize(kDefaultSize) {
|
|
|
|
++fNumAlive;
|
|
|
|
this->registerWithCache();
|
|
|
|
}
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
TestResource(GrGpu* gpu)
|
2014-07-25 14:32:33 +00:00
|
|
|
: INHERITED(gpu, false)
|
2014-05-05 19:09:13 +00:00
|
|
|
, fToDelete(NULL)
|
2014-11-10 18:19:06 +00:00
|
|
|
, fSize(kDefaultSize) {
|
|
|
|
++fNumAlive;
|
|
|
|
this->registerWithCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
TestResource(GrGpu* gpu, const GrResourceKey& scratchKey)
|
|
|
|
: INHERITED(gpu, false)
|
|
|
|
, fToDelete(NULL)
|
|
|
|
, fSize(kDefaultSize) {
|
|
|
|
this->setScratchKey(scratchKey);
|
2014-11-05 22:47:41 +00:00
|
|
|
++fNumAlive;
|
2014-08-26 21:01:07 +00:00
|
|
|
this->registerWithCache();
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~TestResource() {
|
2014-11-05 22:47:41 +00:00
|
|
|
--fNumAlive;
|
2014-11-14 20:10:14 +00:00
|
|
|
SkSafeUnref(fToDelete);
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 19:09:13 +00:00
|
|
|
void setSize(size_t size) {
|
|
|
|
fSize = size;
|
|
|
|
this->didChangeGpuMemorySize();
|
|
|
|
}
|
|
|
|
|
2014-11-05 22:47:41 +00:00
|
|
|
static int NumAlive() { return fNumAlive; }
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
void setUnrefWhenDestroyed(TestResource* resource) {
|
|
|
|
SkRefCnt_SafeAssign(fToDelete, resource);
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-11-12 19:13:39 +00:00
|
|
|
size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; }
|
|
|
|
|
2014-01-15 23:09:01 +00:00
|
|
|
TestResource* fToDelete;
|
2014-05-05 19:09:13 +00:00
|
|
|
size_t fSize;
|
2014-11-05 22:47:41 +00:00
|
|
|
static int fNumAlive;
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
typedef GrGpuResource INHERITED;
|
2014-01-15 23:09:01 +00:00
|
|
|
};
|
2014-11-05 22:47:41 +00:00
|
|
|
int TestResource::fNumAlive = 0;
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
static void test_no_key(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(context));
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context->setResourceCacheLimits(10, 30000);
|
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
|
|
|
|
|
|
|
// Create a bunch of resources with no keys
|
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
|
|
|
TestResource* c = new TestResource(context->getGpu());
|
|
|
|
TestResource* d = new 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 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
|
|
|
|
d->gpuMemorySize() == cache2->getResourceBytes());
|
|
|
|
|
|
|
|
// Should be safe to purge without deleting the resources since we still have refs.
|
|
|
|
cache2->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 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
|
|
|
|
cache2->getResourceBytes());
|
|
|
|
|
|
|
|
c->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
|
|
|
|
cache2->getResourceBytes());
|
|
|
|
|
|
|
|
d->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes());
|
|
|
|
|
|
|
|
b->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes());
|
|
|
|
}
|
|
|
|
|
2014-11-17 17:33:27 +00:00
|
|
|
static void test_budgeting(skiatest::Reporter* reporter) {
|
2014-11-17 15:34:06 +00:00
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(context));
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context->setResourceCacheLimits(10, 300);
|
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
|
|
|
SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgetedResourceBytes());
|
|
|
|
|
|
|
|
GrCacheID::Key keyData;
|
|
|
|
memset(&keyData, 0, sizeof(keyData));
|
|
|
|
GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
|
|
|
|
GrResourceKey scratchKey(GrCacheID(GrResourceKey::ScratchDomain(), keyData), t, 0);
|
|
|
|
GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), t, 0);
|
|
|
|
|
|
|
|
// Create a scratch, a content, and a wrapped resource
|
|
|
|
TestResource* scratch = new TestResource(context->getGpu(), scratchKey);
|
|
|
|
scratch->setSize(10);
|
|
|
|
TestResource* content = new TestResource(context->getGpu());
|
|
|
|
scratch->setSize(11);
|
|
|
|
REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey));
|
|
|
|
TestResource* wrapped = new TestResource(context->getGpu(), true);
|
|
|
|
scratch->setSize(12);
|
2014-11-17 17:33:27 +00:00
|
|
|
TestResource* unbudgeted = new TestResource(context->getGpu());
|
|
|
|
unbudgeted->setSize(13);
|
|
|
|
unbudgeted->cacheAccess().setBudgeted(false);
|
2014-11-17 15:34:06 +00:00
|
|
|
|
|
|
|
// Make sure we can't add a content key to the wrapped resource
|
|
|
|
keyData.fData8[0] = 1;
|
|
|
|
GrResourceKey contentKey2(GrCacheID(GrCacheID::GenerateDomain(), keyData), t, 0);
|
|
|
|
REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2));
|
|
|
|
REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentKey2));
|
|
|
|
|
|
|
|
// Make sure sizes are as we expect
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount());
|
2014-11-17 15:34:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
|
2014-11-17 17:33:27 +00:00
|
|
|
wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
|
|
|
|
cache2->getResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() ==
|
|
|
|
cache2->getBudgetedResourceBytes());
|
|
|
|
|
|
|
|
// Our refs mean that the resources are non purgable.
|
|
|
|
cache2->purgeAllUnlocked();
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount());
|
2014-11-17 15:34:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
|
2014-11-17 17:33:27 +00:00
|
|
|
wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
|
|
|
|
cache2->getResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() ==
|
|
|
|
cache2->getBudgetedResourceBytes());
|
|
|
|
|
|
|
|
// Unreffing the wrapped resource should free it right away.
|
|
|
|
wrapped->unref();
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() +
|
|
|
|
unbudgeted->gpuMemorySize() == cache2->getResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
|
2014-11-17 17:33:27 +00:00
|
|
|
// Now try freeing the budgeted resources first
|
2014-11-17 15:34:06 +00:00
|
|
|
wrapped = new TestResource(context->getGpu(), true);
|
|
|
|
scratch->setSize(12);
|
|
|
|
content->unref();
|
|
|
|
cache2->purgeAllUnlocked();
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
|
|
|
|
unbudgeted->gpuMemorySize() == cache2->getResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedResourceBytes());
|
|
|
|
|
|
|
|
scratch->unref();
|
|
|
|
cache2->purgeAllUnlocked();
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
|
|
|
|
cache2->getResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes());
|
|
|
|
|
|
|
|
wrapped->unref();
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache2->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes());
|
|
|
|
|
|
|
|
unbudgeted->unref();
|
2014-11-17 15:34:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes());
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(context));
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context->setResourceCacheLimits(5, 30000);
|
2014-11-14 20:10:14 +00:00
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
GrCacheID::Key keyData;
|
2014-11-12 18:28:17 +00:00
|
|
|
memset(&keyData, 0, sizeof(keyData));
|
2014-11-10 18:19:06 +00:00
|
|
|
GrCacheID::Domain domain = GrResourceKey::ScratchDomain();
|
|
|
|
GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
|
|
|
|
GrResourceKey scratchKey(GrCacheID(domain, keyData), t, 0);
|
|
|
|
|
|
|
|
// Create two resources that have the same scratch key.
|
|
|
|
TestResource* a = new TestResource(context->getGpu(), scratchKey);
|
|
|
|
TestResource* b = new TestResource(context->getGpu(), scratchKey);
|
|
|
|
a->setSize(11);
|
|
|
|
b->setSize(12);
|
|
|
|
// Scratch resources are registered with GrResourceCache2 just by existing. There are 2.
|
2014-11-14 14:47:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
|
|
|
|
cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
// Our refs mean that the resources are non purgable.
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
// Unref but don't purge
|
|
|
|
a->unref();
|
|
|
|
b->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
|
|
|
|
|
|
|
|
// Purge again. This time resources should be purgable.
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
|
2014-11-10 18:19:06 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey(scratchKey));)
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_duplicate_content_key(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(context));
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context->setResourceCacheLimits(5, 30000);
|
2014-11-14 20:10:14 +00:00
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2014-01-15 23:09:01 +00:00
|
|
|
GrCacheID::Domain domain = GrCacheID::GenerateDomain();
|
|
|
|
GrCacheID::Key keyData;
|
2014-11-10 18:19:06 +00:00
|
|
|
memset(&keyData, 0, sizeof(keyData));
|
2014-01-15 23:09:01 +00:00
|
|
|
GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
|
|
|
|
GrResourceKey key(GrCacheID(domain, keyData), t, 0);
|
2014-11-05 22:47:41 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
// Create two resources that we will attempt to register with the same content key.
|
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
|
|
|
a->setSize(11);
|
|
|
|
b->setSize(12);
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
// 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 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
|
|
|
|
cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
|
|
|
|
|
|
|
b->unref();
|
2014-11-14 20:10:14 +00:00
|
|
|
// Now b should be gone.
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
// Drop the ref on a but it isn't immediately purged as it still has a valid scratch key.
|
2014-11-10 18:19:06 +00:00
|
|
|
a->unref();
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_purge_invalidated(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(context));
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrCacheID::Domain domain = GrCacheID::GenerateDomain();
|
|
|
|
GrCacheID::Key keyData;
|
|
|
|
memset(&keyData, 0, sizeof(keyData));
|
|
|
|
|
|
|
|
GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
|
|
|
|
|
|
|
|
keyData.fData64[0] = 1;
|
|
|
|
GrResourceKey key1(GrCacheID(domain, keyData), t, 0);
|
|
|
|
keyData.fData64[0] = 2;
|
|
|
|
GrResourceKey key2(GrCacheID(domain, keyData), t, 0);
|
|
|
|
keyData.fData64[0] = 3;
|
|
|
|
GrResourceKey key3(GrCacheID(domain, keyData), t, 0);
|
|
|
|
|
2014-11-05 22:47:41 +00:00
|
|
|
context->setResourceCacheLimits(5, 30000);
|
2014-11-10 18:19:06 +00:00
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
// Add three resources to the cache.
|
2014-07-25 14:32:33 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2014-11-10 18:19:06 +00:00
|
|
|
TestResource* c = new TestResource(context->getGpu());
|
2014-11-14 20:10:14 +00:00
|
|
|
a->cacheAccess().setContentKey(key1);
|
|
|
|
b->cacheAccess().setContentKey(key2);
|
|
|
|
c->cacheAccess().setContentKey(key3);
|
2014-01-15 23:09:01 +00:00
|
|
|
a->unref();
|
|
|
|
b->unref();
|
2014-11-10 18:19:06 +00:00
|
|
|
c->unref();
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache2->hasContentKey(key1));
|
|
|
|
REPORTER_ASSERT(reporter, cache2->hasContentKey(key2));
|
|
|
|
REPORTER_ASSERT(reporter, cache2->hasContentKey(key3));
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
// Invalidate two of the three, they should be purged and destroyed.
|
2014-11-05 22:47:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
|
2014-11-10 18:19:06 +00:00
|
|
|
const GrResourceInvalidatedMessage msg1 = { key1 };
|
|
|
|
SkMessageBus<GrResourceInvalidatedMessage>::Post(msg1);
|
|
|
|
const GrResourceInvalidatedMessage msg2 = { key2 };
|
|
|
|
SkMessageBus<GrResourceInvalidatedMessage>::Post(msg2);
|
2014-11-11 15:27:16 +00:00
|
|
|
#if 0 // Disabled until reimplemented in GrResourceCache2.
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAsNeeded();
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
|
|
|
|
REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2));
|
|
|
|
REPORTER_ASSERT(reporter, cache2->hasContentKey(key3));
|
2014-11-11 15:27:16 +00:00
|
|
|
#endif
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
// Invalidate the third.
|
|
|
|
const GrResourceInvalidatedMessage msg3 = { key3 };
|
|
|
|
SkMessageBus<GrResourceInvalidatedMessage>::Post(msg3);
|
2014-11-11 15:27:16 +00:00
|
|
|
#if 0 // Disabled until reimplemented in GrResourceCache2.
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAsNeeded();
|
2014-11-05 22:47:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3));
|
2014-11-11 15:27:16 +00:00
|
|
|
#endif
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes());
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
static void test_cache_chained_purge(skiatest::Reporter* reporter) {
|
2014-11-10 18:19:06 +00:00
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(context));
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-17 17:56:21 +00:00
|
|
|
GrCacheID::Domain domain = GrCacheID::GenerateDomain();
|
|
|
|
GrCacheID::Key keyData;
|
2014-11-10 18:19:06 +00:00
|
|
|
memset(&keyData, 0, sizeof(keyData));
|
2014-01-17 17:56:21 +00:00
|
|
|
GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
keyData.fData64[0] = 1;
|
|
|
|
GrResourceKey key1(GrCacheID(domain, keyData), t, 0);
|
|
|
|
|
|
|
|
keyData.fData64[0] = 2;
|
|
|
|
GrResourceKey key2(GrCacheID(domain, keyData), t, 0);
|
2014-01-17 17:56:21 +00:00
|
|
|
|
|
|
|
{
|
2014-11-05 22:47:41 +00:00
|
|
|
context->setResourceCacheLimits(3, 30000);
|
2014-11-14 20:10:14 +00:00
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-11-05 22:47:41 +00:00
|
|
|
|
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2014-11-14 20:10:14 +00:00
|
|
|
a->cacheAccess().setContentKey(key1);
|
|
|
|
b->cacheAccess().setContentKey(key2);
|
|
|
|
|
|
|
|
// Make a cycle
|
|
|
|
a->setUnrefWhenDestroyed(b);
|
|
|
|
b->setUnrefWhenDestroyed(a);
|
2014-11-05 22:47:41 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-05 22:47:41 +00:00
|
|
|
|
|
|
|
a->unref();
|
|
|
|
b->unref();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2014-11-05 22:47:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-13 21:33:28 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
// Break the cycle
|
|
|
|
a->setUnrefWhenDestroyed(NULL);
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-14 14:47:39 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
2014-11-05 22:47:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2014-01-17 17:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
static void test_resource_size_changed(skiatest::Reporter* reporter) {
|
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(context));
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:09:13 +00:00
|
|
|
GrCacheID::Domain domain = GrCacheID::GenerateDomain();
|
|
|
|
GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
|
|
|
|
|
|
|
|
GrCacheID::Key key1Data;
|
|
|
|
key1Data.fData64[0] = 0;
|
|
|
|
key1Data.fData64[1] = 0;
|
|
|
|
GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
|
|
|
|
|
|
|
|
GrCacheID::Key key2Data;
|
|
|
|
key2Data.fData64[0] = 1;
|
|
|
|
key2Data.fData64[1] = 0;
|
|
|
|
GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
|
|
|
|
|
|
|
|
// Test changing resources sizes (both increase & decrease).
|
|
|
|
{
|
2014-11-05 22:47:41 +00:00
|
|
|
context->setResourceCacheLimits(3, 30000);
|
2014-11-10 18:19:06 +00:00
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2014-07-25 14:32:33 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
2014-11-14 20:10:14 +00:00
|
|
|
a->cacheAccess().setContentKey(key1);
|
2014-05-05 19:09:13 +00:00
|
|
|
a->unref();
|
|
|
|
|
2014-07-25 14:32:33 +00:00
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2014-11-14 20:10:14 +00:00
|
|
|
b->cacheAccess().setContentKey(key2);
|
2014-05-05 19:09:13 +00:00
|
|
|
b->unref();
|
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
|
2014-11-10 18:19:06 +00:00
|
|
|
{
|
|
|
|
SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2)));
|
|
|
|
find2->setSize(200);
|
|
|
|
SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->findAndRefContentResource(key1)));
|
|
|
|
find1->setSize(50);
|
|
|
|
}
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 250 == cache2->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
|
2014-05-05 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test increasing a resources size beyond the cache budget.
|
|
|
|
{
|
2014-11-05 22:47:41 +00:00
|
|
|
context->setResourceCacheLimits(2, 300);
|
2014-11-10 18:19:06 +00:00
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
a->setSize(100);
|
2014-11-14 20:10:14 +00:00
|
|
|
a->cacheAccess().setContentKey(key1);
|
2014-05-05 19:09:13 +00:00
|
|
|
a->unref();
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
|
|
|
b->setSize(100);
|
2014-11-14 20:10:14 +00:00
|
|
|
b->cacheAccess().setContentKey(key2);
|
2014-05-05 19:09:13 +00:00
|
|
|
b->unref();
|
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
{
|
|
|
|
SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->findAndRefContentResource(key2)));
|
|
|
|
find2->setSize(201);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
|
2014-05-05 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 23:09:01 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-01-10 22:08:27 +00:00
|
|
|
DEF_GPUTEST(ResourceCache, reporter, factory) {
|
2013-08-08 22:55:21 +00:00
|
|
|
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);
|
2014-11-05 20:30:32 +00:00
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-28 21:33:06 +00:00
|
|
|
GrSurfaceDesc desc;
|
2013-08-08 22:55:21 +00:00
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
2014-10-28 21:33:06 +00:00
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
2013-08-08 22:55:21 +00:00
|
|
|
desc.fWidth = gWidth;
|
|
|
|
desc.fHeight = gHeight;
|
2014-09-18 13:09:44 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
|
2014-09-22 14:29:03 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info));
|
2014-11-05 20:30:32 +00:00
|
|
|
test_cache(reporter, context, surface->getCanvas());
|
2014-11-05 20:09:45 +00:00
|
|
|
}
|
2014-11-05 22:47:41 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
// The below tests create their own mock contexts.
|
2014-11-14 20:10:14 +00:00
|
|
|
test_no_key(reporter);
|
2014-11-17 17:33:27 +00:00
|
|
|
test_budgeting(reporter);
|
2014-11-10 18:19:06 +00:00
|
|
|
test_duplicate_content_key(reporter);
|
|
|
|
test_duplicate_scratch_key(reporter);
|
|
|
|
test_purge_invalidated(reporter);
|
2014-11-14 20:10:14 +00:00
|
|
|
test_cache_chained_purge(reporter);
|
2014-11-10 18:19:06 +00:00
|
|
|
test_resource_size_changed(reporter);
|
2013-08-08 22:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|