aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--bench/GrResourceCacheBench.cpp197
-rw-r--r--tests/ResourceCacheTest.cpp71
2 files changed, 108 insertions, 160 deletions
diff --git a/bench/GrResourceCacheBench.cpp b/bench/GrResourceCacheBench.cpp
index 272e7bf01e..e30dd3052f 100644
--- a/bench/GrResourceCacheBench.cpp
+++ b/bench/GrResourceCacheBench.cpp
@@ -17,182 +17,60 @@
#include "SkCanvas.h"
enum {
- CACHE_SIZE_COUNT = 2048,
- CACHE_SIZE_BYTES = 2 * 1024 * 1024,
+ CACHE_SIZE_COUNT = 4096,
};
-class FooResource : public GrGpuResource {
+class BenchResource : public GrGpuResource {
public:
- SK_DECLARE_INST_COUNT(FooResource);
- FooResource(GrGpu* gpu, int id)
- : INHERITED(gpu, false)
- , fID(id) {
+ SK_DECLARE_INST_COUNT(BenchResource);
+ BenchResource (GrGpu* gpu)
+ : INHERITED(gpu, false) {
this->registerWithCache();
}
- static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
+ static GrResourceKey ComputeKey(int i) {
GrCacheID::Key key;
memset(&key, 0, sizeof(key));
- key.fData32[0] = width;
- key.fData32[1] = height;
- key.fData32[2] = sampleCnt;
+ key.fData32[0] = i;
static int gType = GrResourceKey::GenerateResourceType();
static int gDomain = GrCacheID::GenerateDomain();
return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
}
- int fID;
private:
- virtual size_t onGpuMemorySize() const SK_OVERRIDE {
- return 100 + ((fID % 1 == 0) ? -5 : 6);
- }
-
- typedef GrGpuResource INHERITED;
-};
-
-class BarResource : public GrGpuResource {
-public:
- SK_DECLARE_INST_COUNT(BarResource);
- BarResource(GrGpu* gpu, int id)
- : INHERITED(gpu, false)
- , fID(id) {
- this->registerWithCache();
- }
-
- static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) {
- GrCacheID::Key key;
- memset(&key, 0, sizeof(key));
- key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
- key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
- key.fData32[2] = desc.fFlags;
- static int gType = GrResourceKey::GenerateResourceType();
- static int gDomain = GrCacheID::GenerateDomain();
- return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
- }
-
- int fID;
-
-private:
- virtual size_t onGpuMemorySize() const SK_OVERRIDE {
- return 100 + ((fID % 1 == 0) ? -40 : 33);
- }
+ size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
typedef GrGpuResource INHERITED;
};
-static void get_foo_params(int i, int* w, int* h, int* s) {
- *w = i % 1024;
- *h = i * 2 % 1024;
- *s = i % 2 == 0 ? 0 : 4;
-}
-
-static void get_bar_surf_desc(int i, GrSurfaceDesc* desc) {
- desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
- desc->fWidth = i % 1024;
- desc->fHeight = i * 2 % 1024;
- desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
- desc->fSampleCnt = ((i % 2) == 0) ? 0 : 4;
-}
-
static void populate_cache(GrGpu* gpu, int resourceCount) {
for (int i = 0; i < resourceCount; ++i) {
- int w, h, s;
- get_foo_params(i, &w, &h, &s);
- GrResourceKey key = FooResource::ComputeKey(w, h, s);
- GrGpuResource* resource = SkNEW_ARGS(FooResource, (gpu, i));
- resource->cacheAccess().setContentKey(key);
- resource->unref();
- }
-
- for (int i = 0; i < resourceCount; ++i) {
- GrSurfaceDesc desc;
- get_bar_surf_desc(i, &desc);
- GrResourceKey key = BarResource::ComputeKey(desc);
- GrGpuResource* resource = SkNEW_ARGS(BarResource, (gpu, i));
+ GrResourceKey key = BenchResource::ComputeKey(i);
+ GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
resource->cacheAccess().setContentKey(key);
resource->unref();
}
}
-static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
- // Benchmark find calls that succeed.
- {
- GrSurfaceDesc desc;
- get_bar_surf_desc(k, &desc);
- GrResourceKey key = BarResource::ComputeKey(desc);
- SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
- if (!item) {
- SkFAIL("cache add does not work as expected");
- return;
- }
- if (static_cast<BarResource*>(item.get())->fID != k) {
- SkFAIL("cache add does not work as expected");
- return;
- }
- }
- {
- int w, h, s;
- get_foo_params(k, &w, &h, &s);
- GrResourceKey key = FooResource::ComputeKey(w, h, s);
- SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
- if (!item) {
- SkFAIL("cache add does not work as expected");
- return;
- }
- if (static_cast<FooResource*>(item.get())->fID != k) {
- SkFAIL("cache add does not work as expected");
- return;
- }
- }
-
- // Benchmark also find calls that always fail.
- {
- GrSurfaceDesc desc;
- get_bar_surf_desc(k, &desc);
- desc.fHeight |= 1;
- GrResourceKey key = BarResource::ComputeKey(desc);
- SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
- if (item) {
- SkFAIL("cache add does not work as expected");
- return;
- }
- }
- {
- int w, h, s;
- get_foo_params(k, &w, &h, &s);
- h |= 1;
- GrResourceKey key = FooResource::ComputeKey(w, h, s);
- SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
- if (item) {
- SkFAIL("cache add does not work as expected");
- return;
- }
- }
-}
-
class GrResourceCacheBenchAdd : public Benchmark {
- enum {
- RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
- };
-
public:
- virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
+ bool isSuitableFor(Backend backend) SK_OVERRIDE {
return backend == kNonRendering_Backend;
}
protected:
- virtual const char* onGetName() SK_OVERRIDE {
+ const char* onGetName() SK_OVERRIDE {
return "grresourcecache_add";
}
- virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
+ void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
if (NULL == context) {
return;
}
// Set the cache budget to be very large so no purging occurs.
- context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
+ context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
GrResourceCache2* cache2 = context->getResourceCache2();
@@ -203,15 +81,8 @@ protected:
GrGpu* gpu = context->getGpu();
for (int i = 0; i < loops; ++i) {
- SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
-
- populate_cache(gpu, RESOURCE_COUNT);
-
- // Check that cache works.
- for (int k = 0; k < RESOURCE_COUNT; k += 33) {
- check_cache_contents_or_die(cache2, k);
- }
- cache2->purgeAllUnlocked();
+ populate_cache(gpu, CACHE_SIZE_COUNT);
+ SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
}
}
@@ -220,46 +91,52 @@ private:
};
class GrResourceCacheBenchFind : public Benchmark {
- enum {
- RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
- };
-
public:
- virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
+ bool isSuitableFor(Backend backend) SK_OVERRIDE {
return backend == kNonRendering_Backend;
}
protected:
- virtual const char* onGetName() SK_OVERRIDE {
+ const char* onGetName() SK_OVERRIDE {
return "grresourcecache_find";
}
- virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
- SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
- if (NULL == context) {
+ void onPreDraw() SK_OVERRIDE {
+ fContext.reset(GrContext::CreateMockContext());
+ if (!fContext) {
return;
}
// Set the cache budget to be very large so no purging occurs.
- context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
+ fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
- GrResourceCache2* cache2 = context->getResourceCache2();
+ GrResourceCache2* cache2 = fContext->getResourceCache2();
// Make sure the cache is empty.
cache2->purgeAllUnlocked();
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
- GrGpu* gpu = context->getGpu();
+ GrGpu* gpu = fContext->getGpu();
- populate_cache(gpu, RESOURCE_COUNT);
+ populate_cache(gpu, CACHE_SIZE_COUNT);
+ }
+ void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
+ if (!fContext) {
+ return;
+ }
+ GrResourceCache2* cache2 = fContext->getResourceCache2();
+ SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
for (int i = 0; i < loops; ++i) {
- for (int k = 0; k < RESOURCE_COUNT; ++k) {
- check_cache_contents_or_die(cache2, k);
+ for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
+ GrResourceKey key = BenchResource::ComputeKey(k);
+ SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentResource(key));
+ SkASSERT(resource);
}
}
}
private:
+ SkAutoTUnref<GrContext> fContext;
typedef Benchmark INHERITED;
};
diff --git a/tests/ResourceCacheTest.cpp b/tests/ResourceCacheTest.cpp
index 930bd1217c..45e28425d0 100644
--- a/tests/ResourceCacheTest.cpp
+++ b/tests/ResourceCacheTest.cpp
@@ -569,6 +569,76 @@ static void test_resource_size_changed(skiatest::Reporter* reporter) {
}
}
+static void test_large_resource_count(skiatest::Reporter* reporter) {
+ SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
+ REPORTER_ASSERT(reporter, SkToBool(context));
+ if (NULL == context) {
+ return;
+ }
+
+ static const int kResourceCnt = 2000;
+ // 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.
+ context->setResourceCacheLimits(2 * kResourceCnt, 2 * kResourceCnt + 1000);
+ GrResourceCache2* cache2 = context->getResourceCache2();
+ cache2->purgeAllUnlocked();
+ SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
+
+ GrCacheID::Domain domain0 = GrCacheID::GenerateDomain();
+ GrCacheID::Domain domain1 = GrCacheID::GenerateDomain();
+ GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
+
+ GrCacheID::Key keyData;
+ memset(&keyData, 0, sizeof(keyData));
+
+ for (int i = 0; i < kResourceCnt; ++i) {
+ TestResource* resource;
+ keyData.fData32[0] = i;
+
+ GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
+ resource = SkNEW_ARGS(TestResource, (context->getGpu()));
+ resource->cacheAccess().setContentKey(key0);
+ resource->setSize(1);
+ resource->unref();
+
+ GrResourceKey key1(GrCacheID(domain1, keyData), t, 0);
+ resource = SkNEW_ARGS(TestResource, (context->getGpu()));
+ resource->cacheAccess().setContentKey(key1);
+ resource->setSize(1);
+ resource->unref();
+ }
+
+ REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
+ REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 2 * kResourceCnt);
+ REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 2 * kResourceCnt);
+ REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 2 * kResourceCnt);
+ REPORTER_ASSERT(reporter, cache2->getResourceCount() == 2 * kResourceCnt);
+ for (int i = 0; i < kResourceCnt; ++i) {
+ keyData.fData32[0] = i;
+ GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
+ REPORTER_ASSERT(reporter, cache2->hasContentKey(key0));
+ GrResourceKey key1(GrCacheID(domain0, keyData), t, 0);
+ REPORTER_ASSERT(reporter, cache2->hasContentKey(key1));
+ }
+
+ cache2->purgeAllUnlocked();
+ REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
+ REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 0);
+ REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 0);
+ REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 0);
+ REPORTER_ASSERT(reporter, cache2->getResourceCount() == 0);
+
+ for (int i = 0; i < kResourceCnt; ++i) {
+ keyData.fData32[0] = i;
+ GrResourceKey key0(GrCacheID(domain0, keyData), t, 0);
+ REPORTER_ASSERT(reporter, !cache2->hasContentKey(key0));
+ GrResourceKey key1(GrCacheID(domain0, keyData), t, 0);
+ REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1));
+ }
+}
+
+
////////////////////////////////////////////////////////////////////////////////
DEF_GPUTEST(ResourceCache, reporter, factory) {
for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
@@ -598,6 +668,7 @@ DEF_GPUTEST(ResourceCache, reporter, factory) {
test_purge_invalidated(reporter);
test_cache_chained_purge(reporter);
test_resource_size_changed(reporter);
+ test_large_resource_count(reporter);
}
#endif