blob: db6ffd06318977c2fd086b1e24d11797dcb440f6 [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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00008
9// This is a GR test
10
11#if SK_SUPPORT_GPU
12
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000013#include "Test.h"
14#include "SkGpuDevice.h"
15#include "../../src/gpu/GrClipMaskManager.h"
16
17static const int X_SIZE = 12;
18static const int Y_SIZE = 12;
19
20////////////////////////////////////////////////////////////////////////////////
caryclark@google.com42639cd2012-06-06 12:03:39 +000021// note: this is unused
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000022static GrTexture* createTexture(GrContext* context) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000023 unsigned char textureData[X_SIZE][Y_SIZE][4];
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000024
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000025 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000026
27 GrTextureDesc desc;
28
29 // let Skia know we will be using this texture as a render target
30 desc.fFlags = kRenderTarget_GrTextureFlagBit;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000031 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000032 desc.fWidth = X_SIZE;
33 desc.fHeight = Y_SIZE;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000034
35 // We are initializing the texture with zeros here
36 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
37 if (!texture) {
38 return NULL;
39 }
40
41 return texture;
42}
43
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000044// Ensure that the 'getConservativeBounds' calls are returning bounds clamped
45// to the render target
46static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) {
47
48 static const int kXSize = 100;
49 static const int kYSize = 100;
50
51 GrTextureDesc desc;
52 desc.fFlags = kRenderTarget_GrTextureFlagBit;
53 desc.fConfig = kAlpha_8_GrPixelConfig;
54 desc.fWidth = kXSize;
55 desc.fHeight = kYSize;
56
57 GrTexture* texture = context->createUncachedTexture(desc, NULL, 0);
58 if (!texture) {
59 return;
60 }
61
62 GrAutoUnref au(texture);
63
64 SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
65 SkRect screen = SkRect::MakeWH(SkIntToScalar(kXSize),
66 SkIntToScalar(kYSize));
67 SkRect clipRect(screen);
68 clipRect.outset(10, 10);
69
70 // create a clip stack that will (trivially) reduce to a single rect that
71 // is larger than the screen
72 SkClipStack stack;
73 stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false);
74
75 bool isIntersectionOfRects = true;
robertphillips@google.com7b112892012-07-31 15:18:21 +000076 SkRect devStackBounds;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000077
78 stack.getConservativeBounds(0, 0, kXSize, kYSize,
robertphillips@google.com7b112892012-07-31 15:18:21 +000079 &devStackBounds,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000080 &isIntersectionOfRects);
81
82 // make sure that the SkClipStack is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000083 REPORTER_ASSERT(reporter, screen == devStackBounds);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000084 REPORTER_ASSERT(reporter, isIntersectionOfRects);
85
robertphillips@google.com641f8b12012-07-31 19:15:58 +000086 // wrap the SkClipStack in a GrClipData
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000087 GrClipData clipData;
robertphillips@google.com641f8b12012-07-31 19:15:58 +000088 clipData.fClipStack = &stack;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000089
robertphillips@google.com7b112892012-07-31 15:18:21 +000090 SkIRect devGrClipDataBound;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000091 clipData.getConservativeBounds(texture,
robertphillips@google.com7b112892012-07-31 15:18:21 +000092 &devGrClipDataBound,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000093 &isIntersectionOfRects);
94
95 // make sure that GrClipData is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000096 REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000097 REPORTER_ASSERT(reporter, isIntersectionOfRects);
98}
99
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000100////////////////////////////////////////////////////////////////////////////////
101// verify that the top state of the stack matches the passed in state
102static void check_state(skiatest::Reporter* reporter,
103 const GrClipMaskCache& cache,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000104 const SkClipStack& clip,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000105 GrTexture* mask,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000106 const GrIRect& bound) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000107 SkClipStack cacheClip;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000108 cache.getLastClip(&cacheClip);
109 REPORTER_ASSERT(reporter, clip == cacheClip);
110
111 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
112
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000113 GrIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000114 cache.getLastBound(&cacheBound);
115 REPORTER_ASSERT(reporter, bound == cacheBound);
116}
117
118////////////////////////////////////////////////////////////////////////////////
119// basic test of the cache's base functionality:
120// push, pop, set, canReuse & getters
121static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
122
caryclark@google.com42639cd2012-06-06 12:03:39 +0000123 if (false) { // avoid bit rot, suppress warning
124 createTexture(context);
125 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000126 GrClipMaskCache cache;
127
robertphillips@google.comf105b102012-05-14 12:18:26 +0000128 cache.setContext(context);
129
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000130 SkClipStack emptyClip;
131 emptyClip.reset();
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000132
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000133 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000134 emptyBound.setEmpty();
135
136 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000137 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000138
139 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000140 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000141 bound1.set(0, 0, 100, 100);
142
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000143 SkClipStack clip1(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000144
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000145 GrTextureDesc desc;
146 desc.fFlags = kRenderTarget_GrTextureFlagBit;
147 desc.fWidth = X_SIZE;
148 desc.fHeight = Y_SIZE;
149 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000150
robertphillips@google.comf105b102012-05-14 12:18:26 +0000151 cache.acquireMask(clip1, desc, bound1);
152
153 GrTexture* texture1 = cache.getLastMask();
154 REPORTER_ASSERT(reporter, texture1);
155 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000156 return;
157 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000158
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000159 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000160 check_state(reporter, cache, clip1, texture1, bound1);
161 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000162
163 // push the state
164 cache.push();
165
166 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000167 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000168 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000169
170 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000171 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000172 bound2.set(-10, -10, 10, 10);
173
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000174 SkClipStack clip2(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000175
robertphillips@google.comf105b102012-05-14 12:18:26 +0000176 cache.acquireMask(clip2, desc, bound2);
177
178 GrTexture* texture2 = cache.getLastMask();
179 REPORTER_ASSERT(reporter, texture2);
180 if (NULL == texture2) {
181 return;
182 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000183
184 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000185 check_state(reporter, cache, clip2, texture2, bound2);
186 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
187 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000188
189 // check to make sure canReuse works
190 REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10));
191 REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10));
192
193 // pop the state
194 cache.pop();
195
196 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000197 check_state(reporter, cache, clip1, texture1, bound1);
198 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
199 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000200
201 // manually clear the state
202 cache.reset();
203
204 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000205 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000206 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
207 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000208
209 // pop again - so there is no state
210 cache.pop();
211
212#if !defined(SK_DEBUG)
213 // verify that the getters don't crash
214 // only do in release since it generates asserts in debug
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000215 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000216#endif
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
221////////////////////////////////////////////////////////////////////////////////
222static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
223
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000224 test_cache(reporter, context);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +0000225 test_clip_bounds(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000226}
227
228////////////////////////////////////////////////////////////////////////////////
229#include "TestClassDef.h"
230DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000231
232#endif