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.
|
|
|
|
*/
|
|
|
|
|
2012-08-02 14:03:32 +00:00
|
|
|
|
2012-08-03 15:00:52 +00:00
|
|
|
#include "Test.h"
|
2012-08-02 14:03:32 +00:00
|
|
|
// This is a GR test
|
|
|
|
#if SK_SUPPORT_GPU
|
2013-02-04 16:13:32 +00:00
|
|
|
#include "GrContextFactory.h"
|
2012-05-09 21:15:28 +00:00
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#include "../../src/gpu/GrClipMaskManager.h"
|
|
|
|
|
|
|
|
static const int X_SIZE = 12;
|
|
|
|
static const int Y_SIZE = 12;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-06 12:03:39 +00:00
|
|
|
// note: this is unused
|
2012-05-09 21:15:28 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
// We are initializing the texture with zeros here
|
|
|
|
GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
|
|
|
|
if (!texture) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2012-07-31 11:47:29 +00:00
|
|
|
// Ensure that the 'getConservativeBounds' calls are returning bounds clamped
|
|
|
|
// to the render target
|
|
|
|
static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
|
|
|
|
static const int kXSize = 100;
|
|
|
|
static const int kYSize = 100;
|
|
|
|
|
|
|
|
GrTextureDesc desc;
|
|
|
|
desc.fFlags = kRenderTarget_GrTextureFlagBit;
|
|
|
|
desc.fConfig = kAlpha_8_GrPixelConfig;
|
|
|
|
desc.fWidth = kXSize;
|
|
|
|
desc.fHeight = kYSize;
|
|
|
|
|
|
|
|
GrTexture* texture = context->createUncachedTexture(desc, NULL, 0);
|
|
|
|
if (!texture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrAutoUnref au(texture);
|
|
|
|
|
|
|
|
SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
|
2012-08-23 18:14:13 +00:00
|
|
|
SkRect screen = SkRect::MakeWH(SkIntToScalar(kXSize),
|
2012-07-31 11:47:29 +00:00
|
|
|
SkIntToScalar(kYSize));
|
|
|
|
SkRect clipRect(screen);
|
|
|
|
clipRect.outset(10, 10);
|
|
|
|
|
|
|
|
// create a clip stack that will (trivially) reduce to a single rect that
|
|
|
|
// is larger than the screen
|
|
|
|
SkClipStack stack;
|
|
|
|
stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false);
|
|
|
|
|
|
|
|
bool isIntersectionOfRects = true;
|
2012-07-31 15:18:21 +00:00
|
|
|
SkRect devStackBounds;
|
2012-07-31 11:47:29 +00:00
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
stack.getConservativeBounds(0, 0, kXSize, kYSize,
|
|
|
|
&devStackBounds,
|
2012-07-31 11:47:29 +00:00
|
|
|
&isIntersectionOfRects);
|
|
|
|
|
|
|
|
// make sure that the SkClipStack is behaving itself
|
2012-07-31 15:18:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, screen == devStackBounds);
|
2012-07-31 11:47:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, isIntersectionOfRects);
|
|
|
|
|
2012-07-31 19:15:58 +00:00
|
|
|
// wrap the SkClipStack in a GrClipData
|
2012-07-31 11:47:29 +00:00
|
|
|
GrClipData clipData;
|
2012-07-31 19:15:58 +00:00
|
|
|
clipData.fClipStack = &stack;
|
2012-07-31 11:47:29 +00:00
|
|
|
|
2012-07-31 15:18:21 +00:00
|
|
|
SkIRect devGrClipDataBound;
|
2012-07-31 11:47:29 +00:00
|
|
|
clipData.getConservativeBounds(texture,
|
2012-07-31 15:18:21 +00:00
|
|
|
&devGrClipDataBound,
|
2012-07-31 11:47:29 +00:00
|
|
|
&isIntersectionOfRects);
|
|
|
|
|
|
|
|
// make sure that GrClipData is behaving itself
|
2012-07-31 15:18:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound);
|
2012-07-31 11:47:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, isIntersectionOfRects);
|
|
|
|
}
|
|
|
|
|
2012-05-09 21:15:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// verify that the top state of the stack matches the passed in state
|
|
|
|
static void check_state(skiatest::Reporter* reporter,
|
|
|
|
const GrClipMaskCache& cache,
|
2012-07-31 19:15:58 +00:00
|
|
|
const SkClipStack& clip,
|
2012-05-09 21:15:28 +00:00
|
|
|
GrTexture* mask,
|
2012-05-15 16:47:23 +00:00
|
|
|
const GrIRect& bound) {
|
2012-12-07 02:01:25 +00:00
|
|
|
SkClipStack cacheClip;
|
2012-12-06 20:58:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID());
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, mask == cache.getLastMask());
|
|
|
|
|
2012-05-15 16:47:23 +00:00
|
|
|
GrIRect cacheBound;
|
2012-05-09 21:15:28 +00:00
|
|
|
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) {
|
|
|
|
|
2012-06-06 12:03:39 +00:00
|
|
|
if (false) { // avoid bit rot, suppress warning
|
2012-08-23 18:14:13 +00:00
|
|
|
createTexture(context);
|
2012-06-06 12:03:39 +00:00
|
|
|
}
|
2012-05-09 21:15:28 +00:00
|
|
|
GrClipMaskCache cache;
|
|
|
|
|
2012-05-14 12:18:26 +00:00
|
|
|
cache.setContext(context);
|
|
|
|
|
2012-07-31 19:15:58 +00:00
|
|
|
SkClipStack emptyClip;
|
|
|
|
emptyClip.reset();
|
2012-05-09 21:15:28 +00:00
|
|
|
|
2012-05-15 16:47:23 +00:00
|
|
|
GrIRect emptyBound;
|
2012-05-09 21:15:28 +00:00
|
|
|
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
|
2012-05-15 16:47:23 +00:00
|
|
|
GrIRect bound1;
|
2012-05-09 21:15:28 +00:00
|
|
|
bound1.set(0, 0, 100, 100);
|
|
|
|
|
2012-07-31 19:15:58 +00:00
|
|
|
SkClipStack clip1(bound1);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
2012-06-07 12:08:45 +00:00
|
|
|
GrTextureDesc desc;
|
|
|
|
desc.fFlags = kRenderTarget_GrTextureFlagBit;
|
|
|
|
desc.fWidth = X_SIZE;
|
|
|
|
desc.fHeight = Y_SIZE;
|
|
|
|
desc.fConfig = kSkia8888_PM_GrPixelConfig;
|
2012-05-14 12:18:26 +00:00
|
|
|
|
2012-12-06 20:58:57 +00:00
|
|
|
cache.acquireMask(clip1.getTopmostGenID(), desc, bound1);
|
2012-05-10 21:26:48 +00:00
|
|
|
|
2012-05-14 12:18:26 +00:00
|
|
|
GrTexture* texture1 = cache.getLastMask();
|
|
|
|
REPORTER_ASSERT(reporter, texture1);
|
|
|
|
if (NULL == texture1) {
|
2012-05-10 21:26:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// check that the set took
|
2012-05-14 12:18:26 +00:00
|
|
|
check_state(reporter, cache, clip1, texture1, bound1);
|
2013-01-24 14:46:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture1->getRefCnt());
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// 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);
|
2013-01-24 14:46:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture1->getRefCnt());
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// modify the new state
|
2012-05-15 16:47:23 +00:00
|
|
|
GrIRect bound2;
|
2012-05-09 21:15:28 +00:00
|
|
|
bound2.set(-10, -10, 10, 10);
|
|
|
|
|
2012-07-31 19:15:58 +00:00
|
|
|
SkClipStack clip2(bound2);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
2012-12-06 20:58:57 +00:00
|
|
|
cache.acquireMask(clip2.getTopmostGenID(), desc, bound2);
|
2012-05-14 12:18:26 +00:00
|
|
|
|
|
|
|
GrTexture* texture2 = cache.getLastMask();
|
|
|
|
REPORTER_ASSERT(reporter, texture2);
|
|
|
|
if (NULL == texture2) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// check that the changes took
|
2012-05-14 12:18:26 +00:00
|
|
|
check_state(reporter, cache, clip2, texture2, bound2);
|
2013-01-24 14:46:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture1->getRefCnt());
|
|
|
|
REPORTER_ASSERT(reporter, texture2->getRefCnt());
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// check to make sure canReuse works
|
2012-12-06 20:58:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2));
|
|
|
|
REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1));
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// pop the state
|
|
|
|
cache.pop();
|
|
|
|
|
|
|
|
// verify that the old state is restored
|
2012-05-14 12:18:26 +00:00
|
|
|
check_state(reporter, cache, clip1, texture1, bound1);
|
2013-01-24 14:46:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture1->getRefCnt());
|
|
|
|
REPORTER_ASSERT(reporter, texture2->getRefCnt());
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// 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);
|
2013-01-24 14:46:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture1->getRefCnt());
|
|
|
|
REPORTER_ASSERT(reporter, texture2->getRefCnt());
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// 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
|
2013-01-24 14:46:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, texture1->getRefCnt());
|
|
|
|
REPORTER_ASSERT(reporter, texture2->getRefCnt());
|
2012-05-09 21:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-02-04 16:13:32 +00:00
|
|
|
static void TestClipCache(skiatest::Reporter* reporter, GrContextFactory* factory) {
|
|
|
|
for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
|
|
|
|
GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GrContext* context = factory->get(glType);
|
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
test_cache(reporter, context);
|
|
|
|
test_clip_bounds(reporter, context);
|
|
|
|
}
|
2012-05-09 21:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "TestClassDef.h"
|
|
|
|
DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)
|
2012-08-02 14:03:32 +00:00
|
|
|
|
|
|
|
#endif
|