2012-05-09 21:15:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#include "../../src/gpu/GrClipMaskManager.h"
|
|
|
|
|
|
|
|
static const int X_SIZE = 12;
|
|
|
|
static const int Y_SIZE = 12;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static GrTexture* createTexture(GrContext* context) {
|
2012-05-10 21:26:48 +00:00
|
|
|
unsigned char textureData[X_SIZE][Y_SIZE][4];
|
2012-05-09 21:15:28 +00:00
|
|
|
|
2012-05-10 21:26:48 +00:00
|
|
|
memset(textureData, 0, 4* X_SIZE * Y_SIZE);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
GrTextureDesc desc;
|
|
|
|
|
|
|
|
// let Skia know we will be using this texture as a render target
|
|
|
|
desc.fFlags = kRenderTarget_GrTextureFlagBit;
|
2012-05-10 21:26:48 +00:00
|
|
|
desc.fConfig = kSkia8888_PM_GrPixelConfig;
|
2012-05-09 21:15:28 +00:00
|
|
|
desc.fWidth = X_SIZE;
|
|
|
|
desc.fHeight = Y_SIZE;
|
|
|
|
desc.fSampleCnt = 0;
|
|
|
|
|
|
|
|
// We are initializing the texture with zeros here
|
|
|
|
GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
|
|
|
|
if (!texture) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// verify that the top state of the stack matches the passed in state
|
|
|
|
static void check_state(skiatest::Reporter* reporter,
|
|
|
|
const GrClipMaskCache& cache,
|
|
|
|
const GrClip& clip,
|
|
|
|
GrTexture* mask,
|
|
|
|
const GrRect& bound) {
|
|
|
|
GrClip cacheClip;
|
|
|
|
cache.getLastClip(&cacheClip);
|
|
|
|
REPORTER_ASSERT(reporter, clip == cacheClip);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, mask == cache.getLastMask());
|
|
|
|
|
|
|
|
GrRect cacheBound;
|
|
|
|
cache.getLastBound(&cacheBound);
|
|
|
|
REPORTER_ASSERT(reporter, bound == cacheBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// basic test of the cache's base functionality:
|
|
|
|
// push, pop, set, canReuse & getters
|
|
|
|
static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
|
|
|
|
GrClipMaskCache cache;
|
|
|
|
|
|
|
|
GrClip emptyClip;
|
|
|
|
emptyClip.setEmpty();
|
|
|
|
|
|
|
|
GrRect emptyBound;
|
|
|
|
emptyBound.setEmpty();
|
|
|
|
|
|
|
|
// check initial state
|
2012-05-11 12:53:50 +00:00
|
|
|
check_state(reporter, cache, emptyClip, NULL, emptyBound);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// set the current state
|
|
|
|
GrRect bound1;
|
|
|
|
bound1.set(0, 0, 100, 100);
|
|
|
|
|
|
|
|
GrClip clip1;
|
|
|
|
clip1.setFromRect(bound1);
|
|
|
|
|
|
|
|
SkAutoTUnref<GrTexture> texture(createTexture(context));
|
2012-05-10 21:26:48 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture.get());
|
|
|
|
|
|
|
|
if (NULL == texture.get()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-09 21:15:28 +00:00
|
|
|
|
2012-05-11 12:53:50 +00:00
|
|
|
cache.set(clip1, texture.get(), bound1);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// check that the set took
|
2012-05-11 12:53:50 +00:00
|
|
|
check_state(reporter, cache, clip1, texture.get(), bound1);
|
2012-05-09 21:15:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt());
|
|
|
|
|
|
|
|
// push the state
|
|
|
|
cache.push();
|
|
|
|
|
|
|
|
// verify that the pushed state is initially empty
|
2012-05-11 12:53:50 +00:00
|
|
|
check_state(reporter, cache, emptyClip, NULL, emptyBound);
|
2012-05-09 21:15:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt());
|
|
|
|
|
|
|
|
// modify the new state
|
|
|
|
GrRect bound2;
|
|
|
|
bound2.set(-10, -10, 10, 10);
|
|
|
|
|
|
|
|
GrClip clip2;
|
|
|
|
clip2.setEmpty();
|
|
|
|
clip2.setFromRect(bound2);
|
|
|
|
|
2012-05-11 12:53:50 +00:00
|
|
|
cache.set(clip2, texture.get(), bound2);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// check that the changes took
|
2012-05-11 12:53:50 +00:00
|
|
|
check_state(reporter, cache, clip2, texture.get(), bound2);
|
2012-05-09 21:15:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == texture.get()->getRefCnt());
|
|
|
|
|
|
|
|
// check to make sure canReuse works
|
|
|
|
REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10));
|
|
|
|
REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10));
|
|
|
|
|
|
|
|
// pop the state
|
|
|
|
cache.pop();
|
|
|
|
|
|
|
|
// verify that the old state is restored
|
2012-05-11 12:53:50 +00:00
|
|
|
check_state(reporter, cache, clip1, texture.get(), bound1);
|
2012-05-09 21:15:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt());
|
|
|
|
|
|
|
|
// manually clear the state
|
|
|
|
cache.reset();
|
|
|
|
|
|
|
|
// verify it is now empty
|
2012-05-11 12:53:50 +00:00
|
|
|
check_state(reporter, cache, emptyClip, NULL, emptyBound);
|
2012-05-09 21:15:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == texture.get()->getRefCnt());
|
|
|
|
|
|
|
|
// pop again - so there is no state
|
|
|
|
cache.pop();
|
|
|
|
|
|
|
|
#if !defined(SK_DEBUG)
|
|
|
|
// verify that the getters don't crash
|
|
|
|
// only do in release since it generates asserts in debug
|
2012-05-11 13:01:16 +00:00
|
|
|
check_state(reporter, cache, emptyClip, NULL, emptyBound);
|
2012-05-09 21:15:28 +00:00
|
|
|
#endif
|
|
|
|
REPORTER_ASSERT(reporter, 1 == texture.get()->getRefCnt());
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
|
2012-05-10 21:26:48 +00:00
|
|
|
test_cache(reporter, context);
|
2012-05-09 21:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "TestClassDef.h"
|
|
|
|
DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)
|