blob: a02f0d3ec98cbb2c113f4d52d5f317f3e81605e9 [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
robertphillips@google.com641f8b12012-07-31 19:15:58 +000081 // wrap the SkClipStack in a GrClipData
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000082 GrClipData clipData;
robertphillips@google.com641f8b12012-07-31 19:15:58 +000083 clipData.fClipStack = &stack;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000084
robertphillips@google.com7b112892012-07-31 15:18:21 +000085 SkIRect devGrClipDataBound;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000086 clipData.getConservativeBounds(texture,
robertphillips@google.com7b112892012-07-31 15:18:21 +000087 &devGrClipDataBound,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000088 &isIntersectionOfRects);
89
90 // make sure that GrClipData is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000091 REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000092 REPORTER_ASSERT(reporter, isIntersectionOfRects);
93}
94
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000095////////////////////////////////////////////////////////////////////////////////
96// verify that the top state of the stack matches the passed in state
97static void check_state(skiatest::Reporter* reporter,
98 const GrClipMaskCache& cache,
robertphillips@google.com641f8b12012-07-31 19:15:58 +000099 const SkClipStack& clip,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000100 GrTexture* mask,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000101 const GrIRect& bound) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000102 SkClipStack cacheClip;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000103 cache.getLastClip(&cacheClip);
104 REPORTER_ASSERT(reporter, clip == cacheClip);
105
106 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
107
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000108 GrIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000109 cache.getLastBound(&cacheBound);
110 REPORTER_ASSERT(reporter, bound == cacheBound);
111}
112
113////////////////////////////////////////////////////////////////////////////////
114// basic test of the cache's base functionality:
115// push, pop, set, canReuse & getters
116static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
117
caryclark@google.com42639cd2012-06-06 12:03:39 +0000118 if (false) { // avoid bit rot, suppress warning
119 createTexture(context);
120 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000121 GrClipMaskCache cache;
122
robertphillips@google.comf105b102012-05-14 12:18:26 +0000123 cache.setContext(context);
124
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000125 SkClipStack emptyClip;
126 emptyClip.reset();
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000127
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000128 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000129 emptyBound.setEmpty();
130
131 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000132 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000133
134 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000135 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000136 bound1.set(0, 0, 100, 100);
137
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000138 SkClipStack clip1(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000139
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000140 GrTextureDesc desc;
141 desc.fFlags = kRenderTarget_GrTextureFlagBit;
142 desc.fWidth = X_SIZE;
143 desc.fHeight = Y_SIZE;
144 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000145
robertphillips@google.comf105b102012-05-14 12:18:26 +0000146 cache.acquireMask(clip1, desc, bound1);
147
148 GrTexture* texture1 = cache.getLastMask();
149 REPORTER_ASSERT(reporter, texture1);
150 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000151 return;
152 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000153
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000154 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000155 check_state(reporter, cache, clip1, texture1, bound1);
156 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000157
158 // push the state
159 cache.push();
160
161 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000162 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000163 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000164
165 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000166 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000167 bound2.set(-10, -10, 10, 10);
168
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000169 SkClipStack clip2(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000170
robertphillips@google.comf105b102012-05-14 12:18:26 +0000171 cache.acquireMask(clip2, desc, bound2);
172
173 GrTexture* texture2 = cache.getLastMask();
174 REPORTER_ASSERT(reporter, texture2);
175 if (NULL == texture2) {
176 return;
177 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000178
179 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000180 check_state(reporter, cache, clip2, texture2, bound2);
181 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
182 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000183
184 // check to make sure canReuse works
185 REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10));
186 REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10));
187
188 // pop the state
189 cache.pop();
190
191 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000192 check_state(reporter, cache, clip1, texture1, bound1);
193 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
194 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000195
196 // manually clear the state
197 cache.reset();
198
199 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000200 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000201 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
202 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000203
204 // pop again - so there is no state
205 cache.pop();
206
207#if !defined(SK_DEBUG)
208 // verify that the getters don't crash
209 // only do in release since it generates asserts in debug
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000210 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000211#endif
robertphillips@google.comf105b102012-05-14 12:18:26 +0000212 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
213 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000214}
215
216////////////////////////////////////////////////////////////////////////////////
217static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
218
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000219 test_cache(reporter, context);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +0000220 test_clip_bounds(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000221}
222
223////////////////////////////////////////////////////////////////////////////////
224#include "TestClassDef.h"
225DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)