blob: 0a72be4a8bfbd39452af089bc1fdf91ba596058a [file] [log] [blame]
robertphillips@google.combeeb97c2012-05-09 21:15:28 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
9#include "SkGpuDevice.h"
10#include "../../src/gpu/GrClipMaskManager.h"
11
12static const int X_SIZE = 12;
13static const int Y_SIZE = 12;
14
15////////////////////////////////////////////////////////////////////////////////
caryclark@google.com42639cd2012-06-06 12:03:39 +000016// note: this is unused
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000017static GrTexture* createTexture(GrContext* context) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000018 unsigned char textureData[X_SIZE][Y_SIZE][4];
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000019
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000020 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000021
22 GrTextureDesc desc;
23
24 // let Skia know we will be using this texture as a render target
25 desc.fFlags = kRenderTarget_GrTextureFlagBit;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000026 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000027 desc.fWidth = X_SIZE;
28 desc.fHeight = Y_SIZE;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000029
30 // We are initializing the texture with zeros here
31 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
32 if (!texture) {
33 return NULL;
34 }
35
36 return texture;
37}
38
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000039// Ensure that the 'getConservativeBounds' calls are returning bounds clamped
40// to the render target
41static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) {
42
43 static const int kXSize = 100;
44 static const int kYSize = 100;
45
46 GrTextureDesc desc;
47 desc.fFlags = kRenderTarget_GrTextureFlagBit;
48 desc.fConfig = kAlpha_8_GrPixelConfig;
49 desc.fWidth = kXSize;
50 desc.fHeight = kYSize;
51
52 GrTexture* texture = context->createUncachedTexture(desc, NULL, 0);
53 if (!texture) {
54 return;
55 }
56
57 GrAutoUnref au(texture);
58
59 SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
60 SkRect screen = SkRect::MakeWH(SkIntToScalar(kXSize),
61 SkIntToScalar(kYSize));
62 SkRect clipRect(screen);
63 clipRect.outset(10, 10);
64
65 // create a clip stack that will (trivially) reduce to a single rect that
66 // is larger than the screen
67 SkClipStack stack;
68 stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false);
69
70 bool isIntersectionOfRects = true;
robertphillips@google.com7b112892012-07-31 15:18:21 +000071 SkRect devStackBounds;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000072
73 stack.getConservativeBounds(0, 0, kXSize, kYSize,
robertphillips@google.com7b112892012-07-31 15:18:21 +000074 &devStackBounds,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000075 &isIntersectionOfRects);
76
77 // make sure that the SkClipStack is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000078 REPORTER_ASSERT(reporter, screen == devStackBounds);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000079 REPORTER_ASSERT(reporter, isIntersectionOfRects);
80
81 // convert the SkClipStack to a GrClip
82 SkGrClipIterator iter;
83 iter.reset(stack);
84
85 GrClip clip;
robertphillips@google.com7b112892012-07-31 15:18:21 +000086 clip.setFromIterator(&iter, devStackBounds);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000087
robertphillips@google.com7b112892012-07-31 15:18:21 +000088 const GrRect& canvGrClipBound = clip.getConservativeBounds();
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000089
90 // make sure that GrClip is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000091 REPORTER_ASSERT(reporter, clipRect == canvGrClipBound);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000092 REPORTER_ASSERT(reporter, clip.isRect());
93
94 // wrap the GrClip in a GrClipData
95 GrClipData clipData;
96 clipData.fClipStack = &clip;
97
robertphillips@google.com7b112892012-07-31 15:18:21 +000098 SkIRect devGrClipDataBound;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000099 clipData.getConservativeBounds(texture,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000100 &devGrClipDataBound,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +0000101 &isIntersectionOfRects);
102
103 // make sure that GrClipData is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +0000104 REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +0000105 REPORTER_ASSERT(reporter, isIntersectionOfRects);
106}
107
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000108////////////////////////////////////////////////////////////////////////////////
109// verify that the top state of the stack matches the passed in state
110static void check_state(skiatest::Reporter* reporter,
111 const GrClipMaskCache& cache,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000112 const GrClip& clip,
113 GrTexture* mask,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000114 const GrIRect& bound) {
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000115 GrClip cacheClip;
116 cache.getLastClip(&cacheClip);
117 REPORTER_ASSERT(reporter, clip == cacheClip);
118
119 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
120
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000121 GrIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000122 cache.getLastBound(&cacheBound);
123 REPORTER_ASSERT(reporter, bound == cacheBound);
124}
125
126////////////////////////////////////////////////////////////////////////////////
127// basic test of the cache's base functionality:
128// push, pop, set, canReuse & getters
129static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
130
caryclark@google.com42639cd2012-06-06 12:03:39 +0000131 if (false) { // avoid bit rot, suppress warning
132 createTexture(context);
133 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000134 GrClipMaskCache cache;
135
robertphillips@google.comf105b102012-05-14 12:18:26 +0000136 cache.setContext(context);
137
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000138 GrClip emptyClip;
139 emptyClip.setEmpty();
140
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000141 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000142 emptyBound.setEmpty();
143
144 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000145 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000146
147 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000148 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000149 bound1.set(0, 0, 100, 100);
150
151 GrClip clip1;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000152 clip1.setFromIRect(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000153
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000154 GrTextureDesc desc;
155 desc.fFlags = kRenderTarget_GrTextureFlagBit;
156 desc.fWidth = X_SIZE;
157 desc.fHeight = Y_SIZE;
158 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000159
robertphillips@google.comf105b102012-05-14 12:18:26 +0000160 cache.acquireMask(clip1, desc, bound1);
161
162 GrTexture* texture1 = cache.getLastMask();
163 REPORTER_ASSERT(reporter, texture1);
164 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000165 return;
166 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000167
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000168 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000169 check_state(reporter, cache, clip1, texture1, bound1);
170 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000171
172 // push the state
173 cache.push();
174
175 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000176 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000177 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000178
179 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000180 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000181 bound2.set(-10, -10, 10, 10);
182
183 GrClip clip2;
184 clip2.setEmpty();
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000185 clip2.setFromIRect(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000186
robertphillips@google.comf105b102012-05-14 12:18:26 +0000187 cache.acquireMask(clip2, desc, bound2);
188
189 GrTexture* texture2 = cache.getLastMask();
190 REPORTER_ASSERT(reporter, texture2);
191 if (NULL == texture2) {
192 return;
193 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000194
195 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000196 check_state(reporter, cache, clip2, texture2, bound2);
197 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
198 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000199
200 // check to make sure canReuse works
201 REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10));
202 REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10));
203
204 // pop the state
205 cache.pop();
206
207 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000208 check_state(reporter, cache, clip1, texture1, bound1);
209 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
210 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000211
212 // manually clear the state
213 cache.reset();
214
215 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000216 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000217 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
218 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000219
220 // pop again - so there is no state
221 cache.pop();
222
223#if !defined(SK_DEBUG)
224 // verify that the getters don't crash
225 // only do in release since it generates asserts in debug
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000226 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000227#endif
robertphillips@google.comf105b102012-05-14 12:18:26 +0000228 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
229 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000230}
231
232////////////////////////////////////////////////////////////////////////////////
233static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
234
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000235 test_cache(reporter, context);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +0000236 test_clip_bounds(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000237}
238
239////////////////////////////////////////////////////////////////////////////////
240#include "TestClassDef.h"
241DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)