2013-11-21 06:21:58 +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.
|
|
|
|
*/
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
#include "Benchmark.h"
|
|
|
|
|
2013-11-21 06:21:58 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
#include "GrGpuResource.h"
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "GrContext.h"
|
2014-10-08 15:40:09 +00:00
|
|
|
#include "GrGpu.h"
|
2014-11-10 18:19:06 +00:00
|
|
|
#include "GrResourceCache2.h"
|
2013-11-21 06:21:58 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
|
|
|
|
enum {
|
2014-11-25 13:52:06 +00:00
|
|
|
CACHE_SIZE_COUNT = 4096,
|
2013-11-21 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
class BenchResource : public GrGpuResource {
|
2013-11-21 06:21:58 +00:00
|
|
|
public:
|
2014-11-25 13:52:06 +00:00
|
|
|
SK_DECLARE_INST_COUNT(BenchResource);
|
|
|
|
BenchResource (GrGpu* gpu)
|
|
|
|
: INHERITED(gpu, false) {
|
2014-08-26 21:01:07 +00:00
|
|
|
this->registerWithCache();
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
static GrResourceKey ComputeKey(int i) {
|
2014-11-24 15:05:15 +00:00
|
|
|
GrCacheID::Key key;
|
|
|
|
memset(&key, 0, sizeof(key));
|
2014-11-25 13:52:06 +00:00
|
|
|
key.fData32[0] = i;
|
2014-11-24 15:05:15 +00:00
|
|
|
static int gDomain = GrCacheID::GenerateDomain();
|
2014-12-30 20:50:52 +00:00
|
|
|
return GrResourceKey(GrCacheID(gDomain, key), 0);
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2014-11-25 13:52:06 +00:00
|
|
|
size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
|
2014-11-12 19:13:39 +00:00
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
typedef GrGpuResource INHERITED;
|
2013-11-21 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
static void populate_cache(GrGpu* gpu, int resourceCount) {
|
2013-11-21 06:21:58 +00:00
|
|
|
for (int i = 0; i < resourceCount; ++i) {
|
2014-11-25 13:52:06 +00:00
|
|
|
GrResourceKey key = BenchResource::ComputeKey(i);
|
|
|
|
GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
|
2014-11-14 20:10:14 +00:00
|
|
|
resource->cacheAccess().setContentKey(key);
|
2013-11-21 06:21:58 +00:00
|
|
|
resource->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrResourceCacheBenchAdd : public Benchmark {
|
2013-11-21 06:21:58 +00:00
|
|
|
public:
|
2014-11-25 13:52:06 +00:00
|
|
|
bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
2014-11-10 18:19:06 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-11-25 13:52:06 +00:00
|
|
|
const char* onGetName() SK_OVERRIDE {
|
2013-11-21 06:21:58 +00:00
|
|
|
return "grresourcecache_add";
|
|
|
|
}
|
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
|
2014-11-10 18:19:06 +00:00
|
|
|
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set the cache budget to be very large so no purging occurs.
|
2014-11-25 13:52:06 +00:00
|
|
|
context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
GrResourceCache2* cache2 = context->getResourceCache2();
|
|
|
|
|
|
|
|
// Make sure the cache is empty.
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
GrGpu* gpu = context->getGpu();
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2014-11-25 13:52:06 +00:00
|
|
|
populate_cache(gpu, CACHE_SIZE_COUNT);
|
|
|
|
SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-11-21 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrResourceCacheBenchFind : public Benchmark {
|
2013-11-21 06:21:58 +00:00
|
|
|
public:
|
2014-11-25 13:52:06 +00:00
|
|
|
bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
2014-11-10 18:19:06 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-11-25 13:52:06 +00:00
|
|
|
const char* onGetName() SK_OVERRIDE {
|
2013-11-21 06:21:58 +00:00
|
|
|
return "grresourcecache_find";
|
|
|
|
}
|
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
void onPreDraw() SK_OVERRIDE {
|
|
|
|
fContext.reset(GrContext::CreateMockContext());
|
|
|
|
if (!fContext) {
|
2014-11-10 18:19:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set the cache budget to be very large so no purging occurs.
|
2014-11-25 13:52:06 +00:00
|
|
|
fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
GrResourceCache2* cache2 = fContext->getResourceCache2();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
// Make sure the cache is empty.
|
2014-11-14 20:10:14 +00:00
|
|
|
cache2->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
GrGpu* gpu = fContext->getGpu();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
populate_cache(gpu, CACHE_SIZE_COUNT);
|
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
if (!fContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrResourceCache2* cache2 = fContext->getResourceCache2();
|
|
|
|
SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2014-11-25 13:52:06 +00:00
|
|
|
for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
|
|
|
|
GrResourceKey key = BenchResource::ComputeKey(k);
|
|
|
|
SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentResource(key));
|
|
|
|
SkASSERT(resource);
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-11-25 13:52:06 +00:00
|
|
|
SkAutoTUnref<GrContext> fContext;
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-11-21 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_BENCH( return new GrResourceCacheBenchAdd(); )
|
|
|
|
DEF_BENCH( return new GrResourceCacheBenchFind(); )
|
|
|
|
|
|
|
|
#endif
|