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-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
|
2015-01-14 17:25:01 +00:00
|
|
|
#include "GrClipMaskManager.h"
|
2013-02-04 16:13:32 +00:00
|
|
|
#include "GrContextFactory.h"
|
2012-05-09 21:15:28 +00:00
|
|
|
#include "SkGpuDevice.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
|
2015-05-29 16:45:57 +00:00
|
|
|
static GrTexture* create_texture(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
|
|
|
|
2014-10-28 21:33:06 +00:00
|
|
|
GrSurfaceDesc desc;
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// let Skia know we will be using this texture as a render target
|
2014-10-28 21:33:06 +00:00
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
2013-02-07 14:43:04 +00:00
|
|
|
desc.fConfig = kSkia8888_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
|
2015-04-30 21:18:54 +00:00
|
|
|
GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0);
|
2012-05-09 21:15:28 +00:00
|
|
|
if (!texture) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2012-05-09 21:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-10-28 21:33:06 +00:00
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
2012-07-31 11:47:29 +00:00
|
|
|
desc.fConfig = kAlpha_8_GrPixelConfig;
|
|
|
|
desc.fWidth = kXSize;
|
|
|
|
desc.fHeight = kYSize;
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
GrTexture* texture = context->textureProvider()->createTexture(desc, false, nullptr, 0);
|
2012-07-31 11:47:29 +00:00
|
|
|
if (!texture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-21 21:24:01 +00:00
|
|
|
SkAutoTUnref<GrTexture> au(texture);
|
2012-07-31 11:47:29 +00:00
|
|
|
|
|
|
|
SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
|
2013-03-04 14:59:55 +00:00
|
|
|
SkRect screen;
|
2013-03-05 07:09:08 +00:00
|
|
|
|
2013-03-04 14:59:55 +00:00
|
|
|
screen = SkRect::MakeWH(SkIntToScalar(kXSize),
|
|
|
|
SkIntToScalar(kYSize));
|
|
|
|
|
2012-07-31 11:47:29 +00:00
|
|
|
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);
|
|
|
|
|
2015-02-25 21:19:48 +00:00
|
|
|
// wrap the SkClipStack in a GrClip
|
2015-02-23 22:44:57 +00:00
|
|
|
GrClip clipData;
|
|
|
|
clipData.setClipStack(&stack);
|
2012-07-31 11:47:29 +00:00
|
|
|
|
2015-02-25 21:19:48 +00:00
|
|
|
SkIRect devGrClipBound;
|
2012-07-31 11:47:29 +00:00
|
|
|
clipData.getConservativeBounds(texture,
|
2015-02-25 21:19:48 +00:00
|
|
|
&devGrClipBound,
|
2012-07-31 11:47:29 +00:00
|
|
|
&isIntersectionOfRects);
|
|
|
|
|
2015-02-25 21:19:48 +00:00
|
|
|
// make sure that GrClip is behaving itself
|
|
|
|
REPORTER_ASSERT(reporter, intScreen == devGrClipBound);
|
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,
|
2013-07-17 21:39:42 +00:00
|
|
|
const SkIRect& bound) {
|
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());
|
|
|
|
|
2013-07-17 21:39:42 +00:00
|
|
|
SkIRect cacheBound;
|
2012-05-09 21:15:28 +00:00
|
|
|
cache.getLastBound(&cacheBound);
|
|
|
|
REPORTER_ASSERT(reporter, bound == cacheBound);
|
|
|
|
}
|
|
|
|
|
2013-11-05 15:03:08 +00:00
|
|
|
static void check_empty_state(skiatest::Reporter* reporter,
|
|
|
|
const GrClipMaskCache& cache) {
|
|
|
|
REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID == cache.getLastClipGenID());
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == cache.getLastMask());
|
2013-11-05 15:03:08 +00:00
|
|
|
|
|
|
|
SkIRect emptyBound;
|
|
|
|
emptyBound.setEmpty();
|
|
|
|
|
|
|
|
SkIRect cacheBound;
|
|
|
|
cache.getLastBound(&cacheBound);
|
|
|
|
REPORTER_ASSERT(reporter, emptyBound == cacheBound);
|
|
|
|
}
|
|
|
|
|
2012-05-09 21:15:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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
|
2015-05-29 16:45:57 +00:00
|
|
|
create_texture(context);
|
2012-06-06 12:03:39 +00:00
|
|
|
}
|
2015-05-29 16:45:57 +00:00
|
|
|
GrClipMaskCache cache(context->resourceProvider());
|
2012-05-14 12:18:26 +00:00
|
|
|
|
2012-05-09 21:15:28 +00:00
|
|
|
// check initial state
|
2013-11-05 15:03:08 +00:00
|
|
|
check_empty_state(reporter, cache);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// set the current state
|
2013-07-17 21:39:42 +00:00
|
|
|
SkIRect 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
|
|
|
|
2014-10-28 21:33:06 +00:00
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
2012-06-07 12:08:45 +00:00
|
|
|
desc.fWidth = X_SIZE;
|
|
|
|
desc.fHeight = Y_SIZE;
|
2013-02-07 14:43:04 +00:00
|
|
|
desc.fConfig = kSkia8888_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);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == 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);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// push the state
|
|
|
|
cache.push();
|
|
|
|
|
|
|
|
// verify that the pushed state is initially empty
|
2013-11-05 15:03:08 +00:00
|
|
|
check_empty_state(reporter, cache);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// modify the new state
|
2013-07-17 21:39:42 +00:00
|
|
|
SkIRect 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);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == texture2) {
|
2012-05-14 12:18:26 +00:00
|
|
|
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);
|
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);
|
2012-05-09 21:15:28 +00:00
|
|
|
|
|
|
|
// manually clear the state
|
|
|
|
cache.reset();
|
|
|
|
|
|
|
|
// verify it is now empty
|
2013-11-05 15:03:08 +00:00
|
|
|
check_empty_state(reporter, cache);
|
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
|
2013-11-05 15:03:08 +00:00
|
|
|
check_empty_state(reporter, cache);
|
2012-05-09 21:15:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-10 22:08:27 +00:00
|
|
|
DEF_GPUTEST(ClipCache, reporter, factory) {
|
2013-02-04 16:13:32 +00:00
|
|
|
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);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2013-02-04 16:13:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
test_cache(reporter, context);
|
|
|
|
test_clip_bounds(reporter, context);
|
|
|
|
}
|
2012-05-09 21:15:28 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 14:03:32 +00:00
|
|
|
#endif
|