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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "Benchmark.h"
|
2014-07-25 15:35:45 +00:00
|
|
|
#include "GrGpuResource.h"
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "GrContext.h"
|
2013-11-21 06:21:58 +00:00
|
|
|
#include "GrResourceCache.h"
|
|
|
|
#include "GrStencilBuffer.h"
|
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CACHE_SIZE_COUNT = 2048,
|
|
|
|
CACHE_SIZE_BYTES = 2 * 1024 * 1024,
|
|
|
|
};
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
class StencilResource : public GrGpuResource {
|
2013-11-21 06:21:58 +00:00
|
|
|
public:
|
|
|
|
SK_DECLARE_INST_COUNT(StencilResource);
|
2014-07-25 14:32:33 +00:00
|
|
|
StencilResource(GrGpu* gpu, int id)
|
|
|
|
: INHERITED(gpu, false)
|
|
|
|
, fID(id) {
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 14:32:33 +00:00
|
|
|
virtual ~StencilResource() { this->release(); }
|
|
|
|
|
2014-05-02 21:38:22 +00:00
|
|
|
virtual size_t gpuMemorySize() const SK_OVERRIDE {
|
2013-11-21 06:21:58 +00:00
|
|
|
return 100 + ((fID % 1 == 0) ? -5 : 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
|
|
|
|
return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fID;
|
|
|
|
|
|
|
|
private:
|
2014-07-25 15:35:45 +00:00
|
|
|
typedef GrGpuResource INHERITED;
|
2013-11-21 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
class TextureResource : public GrGpuResource {
|
2013-11-21 06:21:58 +00:00
|
|
|
public:
|
|
|
|
SK_DECLARE_INST_COUNT(TextureResource);
|
2014-07-25 14:32:33 +00:00
|
|
|
TextureResource(GrGpu* gpu, int id)
|
|
|
|
: INHERITED(gpu, false)
|
|
|
|
, fID(id) {
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 14:32:33 +00:00
|
|
|
virtual ~TextureResource() { this->release(); }
|
|
|
|
|
2014-05-02 21:38:22 +00:00
|
|
|
virtual size_t gpuMemorySize() const SK_OVERRIDE {
|
2013-11-21 06:21:58 +00:00
|
|
|
return 100 + ((fID % 1 == 0) ? -40 : 33);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GrResourceKey ComputeKey(const GrTextureDesc& desc) {
|
2014-05-09 20:46:48 +00:00
|
|
|
return GrTextureImpl::ComputeScratchKey(desc);
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fID;
|
|
|
|
|
|
|
|
private:
|
2014-07-25 15:35:45 +00:00
|
|
|
typedef GrGpuResource INHERITED;
|
2013-11-21 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void get_stencil(int i, int* w, int* h, int* s) {
|
|
|
|
*w = i % 1024;
|
|
|
|
*h = i * 2 % 1024;
|
|
|
|
*s = i % 1 == 0 ? 0 : 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_texture_desc(int i, GrTextureDesc* desc) {
|
|
|
|
desc->fFlags = kRenderTarget_GrTextureFlagBit |
|
|
|
|
kNoStencil_GrTextureFlagBit;
|
|
|
|
desc->fWidth = i % 1024;
|
|
|
|
desc->fHeight = i * 2 % 1024;
|
|
|
|
desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
|
|
|
|
desc->fSampleCnt = i % 1 == 0 ? 0 : 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount) {
|
|
|
|
for (int i = 0; i < resourceCount; ++i) {
|
|
|
|
int w, h, s;
|
|
|
|
get_stencil(i, &w, &h, &s);
|
|
|
|
GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s);
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i));
|
2014-05-02 21:38:22 +00:00
|
|
|
cache->purgeAsNeeded(1, resource->gpuMemorySize());
|
2013-11-21 06:21:58 +00:00
|
|
|
cache->addResource(key, resource);
|
|
|
|
resource->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < resourceCount; ++i) {
|
|
|
|
GrTextureDesc desc;
|
|
|
|
get_texture_desc(i, &desc);
|
|
|
|
GrResourceKey key = TextureResource::ComputeKey(desc);
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
|
2014-05-02 21:38:22 +00:00
|
|
|
cache->purgeAsNeeded(1, resource->gpuMemorySize());
|
2013-11-21 06:21:58 +00:00
|
|
|
cache->addResource(key, resource);
|
|
|
|
resource->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_cache_contents_or_die(GrResourceCache* cache, int k) {
|
|
|
|
// Benchmark find calls that succeed.
|
|
|
|
{
|
|
|
|
GrTextureDesc desc;
|
|
|
|
get_texture_desc(k, &desc);
|
|
|
|
GrResourceKey key = TextureResource::ComputeKey(desc);
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource* item = cache->find(key);
|
2013-11-21 06:21:58 +00:00
|
|
|
if (NULL == item) {
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("cache add does not work as expected");
|
2013-11-21 06:21:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (static_cast<TextureResource*>(item)->fID != k) {
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("cache add does not work as expected");
|
2013-11-21 06:21:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int w, h, s;
|
|
|
|
get_stencil(k, &w, &h, &s);
|
|
|
|
GrResourceKey key = StencilResource::ComputeKey(w, h, s);
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource* item = cache->find(key);
|
2013-11-21 06:21:58 +00:00
|
|
|
if (NULL == item) {
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("cache add does not work as expected");
|
2013-11-21 06:21:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (static_cast<TextureResource*>(item)->fID != k) {
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("cache add does not work as expected");
|
2013-11-21 06:21:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmark also find calls that always fail.
|
|
|
|
{
|
|
|
|
GrTextureDesc desc;
|
|
|
|
get_texture_desc(k, &desc);
|
|
|
|
desc.fHeight |= 1;
|
|
|
|
GrResourceKey key = TextureResource::ComputeKey(desc);
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource* item = cache->find(key);
|
2013-11-21 06:21:58 +00:00
|
|
|
if (NULL != item) {
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("cache add does not work as expected");
|
2013-11-21 06:21:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int w, h, s;
|
|
|
|
get_stencil(k, &w, &h, &s);
|
|
|
|
h |= 1;
|
|
|
|
GrResourceKey key = StencilResource::ComputeKey(w, h, s);
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource* item = cache->find(key);
|
2013-11-21 06:21:58 +00:00
|
|
|
if (NULL != item) {
|
2014-04-30 14:17:00 +00:00
|
|
|
SkFAIL("cache add does not work as expected");
|
2013-11-21 06:21:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrResourceCacheBenchAdd : public Benchmark {
|
2013-11-21 06:21:58 +00:00
|
|
|
enum {
|
|
|
|
RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
|
|
|
|
DUPLICATE_COUNT = CACHE_SIZE_COUNT / 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kGPU_Backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() SK_OVERRIDE {
|
|
|
|
return "grresourcecache_add";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
|
2013-11-21 06:21:58 +00:00
|
|
|
GrGpu* gpu = canvas->getGrContext()->getGpu();
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2013-11-21 06:21:58 +00:00
|
|
|
GrResourceCache cache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES);
|
|
|
|
populate_cache(&cache, gpu, DUPLICATE_COUNT);
|
|
|
|
populate_cache(&cache, gpu, RESOURCE_COUNT);
|
|
|
|
|
|
|
|
// Check that cache works.
|
|
|
|
for (int k = 0; k < RESOURCE_COUNT; k += 33) {
|
|
|
|
check_cache_contents_or_die(&cache, k);
|
|
|
|
}
|
|
|
|
cache.purgeAllUnlocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
enum {
|
|
|
|
RESOURCE_COUNT = (CACHE_SIZE_COUNT / 2) - 100,
|
|
|
|
DUPLICATE_COUNT = 100
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kGPU_Backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() SK_OVERRIDE {
|
|
|
|
return "grresourcecache_find";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
|
2013-11-21 06:21:58 +00:00
|
|
|
GrGpu* gpu = canvas->getGrContext()->getGpu();
|
|
|
|
GrResourceCache cache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES);
|
|
|
|
populate_cache(&cache, gpu, DUPLICATE_COUNT);
|
|
|
|
populate_cache(&cache, gpu, RESOURCE_COUNT);
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2013-11-21 06:21:58 +00:00
|
|
|
for (int k = 0; k < RESOURCE_COUNT; ++k) {
|
|
|
|
check_cache_contents_or_die(&cache, k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
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
|