Make GrScratchKey memory buffer correct size on copy

Scratch key memory buffer of a copy of a key was too big. The (new) copy
was N times uint32_t bytes instead of N bytes.

Adds few tests to resource cache. These tests would not catch the too
big buffer. This is just a precaution for too small buffers. The main
idea of the test change is that the scratch key should contain some
information, so that lookup with a scratch key can also return no
match. Otherwise testing of scratch lookup result is not indicative of
correct code (eg. no-information scratch key will always match).

Review URL: https://codereview.chromium.org/860333002
This commit is contained in:
kkinnunen 2015-01-21 06:39:14 -08:00 committed by Commit bot
parent 034e94877b
commit 711ef48313
2 changed files with 139 additions and 38 deletions

View File

@ -46,9 +46,9 @@ public:
const uint32_t* data() const { return &fKey[kMetaDataCnt]; } const uint32_t* data() const { return &fKey[kMetaDataCnt]; }
GrScratchKey& operator=(const GrScratchKey& that) { GrScratchKey& operator=(const GrScratchKey& that) {
size_t size = that.size(); size_t bytes = that.size();
fKey.reset(SkToInt(size)); fKey.reset(SkToInt(bytes / sizeof(uint32_t)));
memcpy(fKey.get(), that.fKey.get(), size); memcpy(fKey.get(), that.fKey.get(), bytes);
return *this; return *this;
} }

View File

@ -63,13 +63,18 @@ static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanva
class TestResource : public GrGpuResource { class TestResource : public GrGpuResource {
static const size_t kDefaultSize = 100; static const size_t kDefaultSize = 100;
enum ScratchConstructor { kScratchConstructor };
public: public:
SK_DECLARE_INST_COUNT(TestResource); SK_DECLARE_INST_COUNT(TestResource);
/** Property that distinctly categorizes the resource.
* For example, textures have width, height, ... */
enum SimulatedProperty { kProperty1_SimulatedProperty, kProperty2_SimulatedProperty };
TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
: INHERITED(gpu, lifeCycle) : INHERITED(gpu, lifeCycle)
, fToDelete(NULL) , fToDelete(NULL)
, fSize(size) { , fSize(size)
, fProperty(kProperty1_SimulatedProperty) {
++fNumAlive; ++fNumAlive;
this->registerWithCache(); this->registerWithCache();
} }
@ -77,7 +82,8 @@ public:
TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
: INHERITED(gpu, lifeCycle) : INHERITED(gpu, lifeCycle)
, fToDelete(NULL) , fToDelete(NULL)
, fSize(kDefaultSize) { , fSize(kDefaultSize)
, fProperty(kProperty1_SimulatedProperty) {
++fNumAlive; ++fNumAlive;
this->registerWithCache(); this->registerWithCache();
} }
@ -85,18 +91,14 @@ public:
TestResource(GrGpu* gpu) TestResource(GrGpu* gpu)
: INHERITED(gpu, kCached_LifeCycle) : INHERITED(gpu, kCached_LifeCycle)
, fToDelete(NULL) , fToDelete(NULL)
, fSize(kDefaultSize) { , fSize(kDefaultSize)
, fProperty(kProperty1_SimulatedProperty) {
++fNumAlive; ++fNumAlive;
this->registerWithCache(); this->registerWithCache();
} }
TestResource(GrGpu* gpu, const GrScratchKey& scratchKey) static TestResource* CreateScratchTestResource(GrGpu* gpu, SimulatedProperty property) {
: INHERITED(gpu, kCached_LifeCycle) return SkNEW_ARGS(TestResource, (gpu, property, kScratchConstructor));
, fToDelete(NULL)
, fSize(kDefaultSize) {
this->setScratchKey(scratchKey);
++fNumAlive;
this->registerWithCache();
} }
~TestResource() { ~TestResource() {
@ -115,13 +117,34 @@ public:
SkRefCnt_SafeAssign(fToDelete, resource); SkRefCnt_SafeAssign(fToDelete, resource);
} }
static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
static const size_t kTestAmount = 6;
GrScratchKey::Builder builder(key, t, kTestAmount);
for (size_t i = 0; i < kTestAmount; ++i) {
builder[i] = i + static_cast<int>(property);
}
}
private: private:
TestResource(GrGpu* gpu, SimulatedProperty property, ScratchConstructor)
: INHERITED(gpu, kCached_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; } size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; }
TestResource* fToDelete; TestResource* fToDelete;
size_t fSize; size_t fSize;
static int fNumAlive; static int fNumAlive;
SimulatedProperty fProperty;
typedef GrGpuResource INHERITED; typedef GrGpuResource INHERITED;
}; };
int TestResource::fNumAlive = 0; int TestResource::fNumAlive = 0;
@ -181,11 +204,6 @@ static void test_no_key(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes());
} }
static void make_scratch_key(GrScratchKey* key) {
static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
GrScratchKey::Builder builder(key, t, 0);
}
static void test_budgeting(skiatest::Reporter* reporter) { static void test_budgeting(skiatest::Reporter* reporter) {
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
REPORTER_ASSERT(reporter, SkToBool(context)); REPORTER_ASSERT(reporter, SkToBool(context));
@ -198,15 +216,14 @@ static void test_budgeting(skiatest::Reporter* reporter) {
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgetedResourceBytes()); SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgetedResourceBytes());
GrScratchKey scratchKey;
make_scratch_key(&scratchKey);
GrCacheID::Key keyData; GrCacheID::Key keyData;
memset(&keyData, 0, sizeof(keyData)); memset(&keyData, 0, sizeof(keyData));
GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0); GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0);
// Create a scratch, a content, and a wrapped resource // Create a scratch, a content, and a wrapped resource
TestResource* scratch = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); TestResource* scratch =
TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
scratch->setSize(10); scratch->setSize(10);
TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu()));
content->setSize(11); content->setSize(11);
@ -293,9 +310,6 @@ static void test_unbudgeted(skiatest::Reporter* reporter) {
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgetedResourceBytes()); SkASSERT(0 == cache2->getBudgetedResourceCount() && 0 == cache2->getBudgetedResourceBytes());
GrScratchKey scratchKey;
make_scratch_key(&scratchKey);
GrCacheID::Key keyData; GrCacheID::Key keyData;
memset(&keyData, 0, sizeof(keyData)); memset(&keyData, 0, sizeof(keyData));
GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0); GrResourceKey contentKey(GrCacheID(GrCacheID::GenerateDomain(), keyData), 0);
@ -306,7 +320,8 @@ static void test_unbudgeted(skiatest::Reporter* reporter) {
TestResource* unbudgeted; TestResource* unbudgeted;
// A large uncached or wrapped resource shouldn't evict anything. // A large uncached or wrapped resource shouldn't evict anything.
scratch = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); scratch = TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
scratch->setSize(10); scratch->setSize(10);
scratch->unref(); scratch->unref();
REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount());
@ -368,14 +383,23 @@ static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
cache2->purgeAllUnlocked(); cache2->purgeAllUnlocked();
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
GrScratchKey scratchKey;
make_scratch_key(&scratchKey);
// Create two resources that have the same scratch key. // Create two resources that have the same scratch key.
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); TestResource* a =
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
TestResource* b =
TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
a->setSize(11); a->setSize(11);
b->setSize(12); b->setSize(12);
GrScratchKey scratchKey1;
TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty, &scratchKey1);
// Check for negative case consistency. (leaks upon test failure.)
REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(scratchKey1));
GrScratchKey scratchKey;
TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty, &scratchKey);
// Scratch resources are registered with GrResourceCache2 just by existing. There are 2. // Scratch resources are registered with GrResourceCache2 just by existing. There are 2.
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
@ -412,16 +436,25 @@ static void test_remove_scratch_key(skiatest::Reporter* reporter) {
cache2->purgeAllUnlocked(); cache2->purgeAllUnlocked();
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
GrScratchKey scratchKey;
make_scratch_key(&scratchKey);
// Create two resources that have the same scratch key. // Create two resources that have the same scratch key.
TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); TestResource* a =
TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu(), scratchKey)); TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
TestResource* b =
TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
a->unref(); a->unref();
b->unref(); b->unref();
GrScratchKey scratchKey;
// Ensure that scratch key lookup is correct for negative case.
TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty, &scratchKey);
// (following leaks upon test failure).
REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == NULL);
// Scratch resources are registered with GrResourceCache2 just by existing. There are 2. // Scratch resources are registered with GrResourceCache2 just by existing. There are 2.
TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty, &scratchKey);
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));) SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey(scratchKey));)
REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount());
@ -460,6 +493,73 @@ static void test_remove_scratch_key(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount());
} }
static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
REPORTER_ASSERT(reporter, SkToBool(context));
if (NULL == context) {
return;
}
context->setResourceCacheLimits(5, 30000);
GrResourceCache2* cache2 = context->getResourceCache2();
cache2->purgeAllUnlocked();
SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
// Create two resources that have the same scratch key.
TestResource* a =
TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
TestResource* b =
TestResource::CreateScratchTestResource(context->getGpu(),
TestResource::kProperty2_SimulatedProperty);
a->unref();
b->unref();
GrScratchKey scratchKey;
// Ensure that scratch key comparison and assignment is consistent.
GrScratchKey scratchKey1;
TestResource::ComputeScratchKey(TestResource::kProperty1_SimulatedProperty, &scratchKey1);
GrScratchKey scratchKey2;
TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty, &scratchKey2);
REPORTER_ASSERT(reporter, scratchKey1.size() <= sizeof(scratchKey1));
REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
scratchKey = scratchKey1;
REPORTER_ASSERT(reporter, scratchKey.size() <= sizeof(scratchKey));
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() <= sizeof(scratchKey));
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::kProperty1_SimulatedProperty, &scratchKey);
// (following leaks upon test failure).
REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == NULL);
// Find the first resource with a scratch key and a copy of a scratch key.
TestResource::ComputeScratchKey(TestResource::kProperty2_SimulatedProperty, &scratchKey);
GrGpuResource* find = cache2->findAndRefScratchResource(scratchKey);
REPORTER_ASSERT(reporter, find != NULL);
find->unref();
scratchKey2 = scratchKey;
find = cache2->findAndRefScratchResource(scratchKey2);
REPORTER_ASSERT(reporter, find != NULL);
REPORTER_ASSERT(reporter, find == a || find == b);
GrGpuResource* find2 = cache2->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) { static void test_duplicate_content_key(skiatest::Reporter* reporter) {
SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
REPORTER_ASSERT(reporter, SkToBool(context)); REPORTER_ASSERT(reporter, SkToBool(context));
@ -809,6 +909,7 @@ DEF_GPUTEST(ResourceCache, reporter, factory) {
test_duplicate_content_key(reporter); test_duplicate_content_key(reporter);
test_duplicate_scratch_key(reporter); test_duplicate_scratch_key(reporter);
test_remove_scratch_key(reporter); test_remove_scratch_key(reporter);
test_scratch_key_consistency(reporter);
test_purge_invalidated(reporter); test_purge_invalidated(reporter);
test_cache_chained_purge(reporter); test_cache_chained_purge(reporter);
test_resource_size_changed(reporter); test_resource_size_changed(reporter);