2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "../../src/gpu/GrClipMaskManager.h"
12 #include "GrContextFactory.h"
13 #include "SkGpuDevice.h"
15 static const int X_SIZE = 12;
16 static const int Y_SIZE = 12;
18 ////////////////////////////////////////////////////////////////////////////////
19 // note: this is unused
20 static GrTexture* createTexture(GrContext* context) {
21 unsigned char textureData[X_SIZE][Y_SIZE][4];
23 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
27 // let Skia know we will be using this texture as a render target
28 desc.fFlags = kRenderTarget_GrSurfaceFlag;
29 desc.fConfig = kSkia8888_GrPixelConfig;
31 desc.fHeight = Y_SIZE;
33 // We are initializing the texture with zeros here
34 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
42 // Ensure that the 'getConservativeBounds' calls are returning bounds clamped
43 // to the render target
44 static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) {
46 static const int kXSize = 100;
47 static const int kYSize = 100;
50 desc.fFlags = kRenderTarget_GrSurfaceFlag;
51 desc.fConfig = kAlpha_8_GrPixelConfig;
53 desc.fHeight = kYSize;
55 GrTexture* texture = context->createUncachedTexture(desc, NULL, 0);
60 SkAutoTUnref<GrTexture> au(texture);
62 SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
65 screen = SkRect::MakeWH(SkIntToScalar(kXSize),
66 SkIntToScalar(kYSize));
68 SkRect clipRect(screen);
69 clipRect.outset(10, 10);
71 // create a clip stack that will (trivially) reduce to a single rect that
72 // is larger than the screen
74 stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false);
76 bool isIntersectionOfRects = true;
77 SkRect devStackBounds;
79 stack.getConservativeBounds(0, 0, kXSize, kYSize,
81 &isIntersectionOfRects);
83 // make sure that the SkClipStack is behaving itself
84 REPORTER_ASSERT(reporter, screen == devStackBounds);
85 REPORTER_ASSERT(reporter, isIntersectionOfRects);
87 // wrap the SkClipStack in a GrClipData
89 clipData.fClipStack = &stack;
91 SkIRect devGrClipDataBound;
92 clipData.getConservativeBounds(texture,
94 &isIntersectionOfRects);
96 // make sure that GrClipData is behaving itself
97 REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound);
98 REPORTER_ASSERT(reporter, isIntersectionOfRects);
101 ////////////////////////////////////////////////////////////////////////////////
102 // verify that the top state of the stack matches the passed in state
103 static void check_state(skiatest::Reporter* reporter,
104 const GrClipMaskCache& cache,
105 const SkClipStack& clip,
107 const SkIRect& bound) {
108 REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID());
110 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
113 cache.getLastBound(&cacheBound);
114 REPORTER_ASSERT(reporter, bound == cacheBound);
117 static void check_empty_state(skiatest::Reporter* reporter,
118 const GrClipMaskCache& cache) {
119 REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID == cache.getLastClipGenID());
120 REPORTER_ASSERT(reporter, NULL == cache.getLastMask());
123 emptyBound.setEmpty();
126 cache.getLastBound(&cacheBound);
127 REPORTER_ASSERT(reporter, emptyBound == cacheBound);
130 ////////////////////////////////////////////////////////////////////////////////
131 // basic test of the cache's base functionality:
132 // push, pop, set, canReuse & getters
133 static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
135 if (false) { // avoid bit rot, suppress warning
136 createTexture(context);
138 GrClipMaskCache cache;
140 cache.setContext(context);
142 // check initial state
143 check_empty_state(reporter, cache);
145 // set the current state
147 bound1.set(0, 0, 100, 100);
149 SkClipStack clip1(bound1);
152 desc.fFlags = kRenderTarget_GrSurfaceFlag;
153 desc.fWidth = X_SIZE;
154 desc.fHeight = Y_SIZE;
155 desc.fConfig = kSkia8888_GrPixelConfig;
157 cache.acquireMask(clip1.getTopmostGenID(), desc, bound1);
159 GrTexture* texture1 = cache.getLastMask();
160 REPORTER_ASSERT(reporter, texture1);
161 if (NULL == texture1) {
165 // check that the set took
166 check_state(reporter, cache, clip1, texture1, bound1);
171 // verify that the pushed state is initially empty
172 check_empty_state(reporter, cache);
174 // modify the new state
176 bound2.set(-10, -10, 10, 10);
178 SkClipStack clip2(bound2);
180 cache.acquireMask(clip2.getTopmostGenID(), desc, bound2);
182 GrTexture* texture2 = cache.getLastMask();
183 REPORTER_ASSERT(reporter, texture2);
184 if (NULL == texture2) {
188 // check that the changes took
189 check_state(reporter, cache, clip2, texture2, bound2);
191 // check to make sure canReuse works
192 REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2));
193 REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1));
198 // verify that the old state is restored
199 check_state(reporter, cache, clip1, texture1, bound1);
201 // manually clear the state
204 // verify it is now empty
205 check_empty_state(reporter, cache);
207 // pop again - so there is no state
210 #if !defined(SK_DEBUG)
211 // verify that the getters don't crash
212 // only do in release since it generates asserts in debug
213 check_empty_state(reporter, cache);
217 DEF_GPUTEST(ClipCache, reporter, factory) {
218 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
219 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
220 if (!GrContextFactory::IsRenderingGLContext(glType)) {
223 GrContext* context = factory->get(glType);
224 if (NULL == context) {
228 test_cache(reporter, context);
229 test_clip_bounds(reporter, context);