blob: c80ba631da588ef36b5c6e7cf68a9327d409b752 [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
robertphillips@google.combeeb97c2012-05-09 21:15:28 +00009#include "Test.h"
bsalomon@google.coma68937c2012-08-03 15:00:52 +000010// This is a GR test
11#if SK_SUPPORT_GPU
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000012#include "SkGpuDevice.h"
13#include "../../src/gpu/GrClipMaskManager.h"
14
15static const int X_SIZE = 12;
16static const int Y_SIZE = 12;
17
18////////////////////////////////////////////////////////////////////////////////
caryclark@google.com42639cd2012-06-06 12:03:39 +000019// note: this is unused
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000020static GrTexture* createTexture(GrContext* context) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000021 unsigned char textureData[X_SIZE][Y_SIZE][4];
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000022
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000023 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000024
25 GrTextureDesc desc;
26
27 // let Skia know we will be using this texture as a render target
28 desc.fFlags = kRenderTarget_GrTextureFlagBit;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000029 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000030 desc.fWidth = X_SIZE;
31 desc.fHeight = Y_SIZE;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000032
33 // We are initializing the texture with zeros here
34 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
35 if (!texture) {
36 return NULL;
37 }
38
39 return texture;
40}
41
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000042// Ensure that the 'getConservativeBounds' calls are returning bounds clamped
43// to the render target
44static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) {
45
46 static const int kXSize = 100;
47 static const int kYSize = 100;
48
49 GrTextureDesc desc;
50 desc.fFlags = kRenderTarget_GrTextureFlagBit;
51 desc.fConfig = kAlpha_8_GrPixelConfig;
52 desc.fWidth = kXSize;
53 desc.fHeight = kYSize;
54
55 GrTexture* texture = context->createUncachedTexture(desc, NULL, 0);
56 if (!texture) {
57 return;
58 }
59
60 GrAutoUnref au(texture);
61
62 SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +000063 SkRect screen = SkRect::MakeWH(SkIntToScalar(kXSize),
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000064 SkIntToScalar(kYSize));
65 SkRect clipRect(screen);
66 clipRect.outset(10, 10);
67
68 // create a clip stack that will (trivially) reduce to a single rect that
69 // is larger than the screen
70 SkClipStack stack;
71 stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false);
72
73 bool isIntersectionOfRects = true;
robertphillips@google.com7b112892012-07-31 15:18:21 +000074 SkRect devStackBounds;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000075
rmistry@google.comd6176b02012-08-23 18:14:13 +000076 stack.getConservativeBounds(0, 0, kXSize, kYSize,
77 &devStackBounds,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000078 &isIntersectionOfRects);
79
80 // make sure that the SkClipStack is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000081 REPORTER_ASSERT(reporter, screen == devStackBounds);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000082 REPORTER_ASSERT(reporter, isIntersectionOfRects);
83
robertphillips@google.com641f8b12012-07-31 19:15:58 +000084 // wrap the SkClipStack in a GrClipData
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000085 GrClipData clipData;
robertphillips@google.com641f8b12012-07-31 19:15:58 +000086 clipData.fClipStack = &stack;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000087
robertphillips@google.com7b112892012-07-31 15:18:21 +000088 SkIRect devGrClipDataBound;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000089 clipData.getConservativeBounds(texture,
robertphillips@google.com7b112892012-07-31 15:18:21 +000090 &devGrClipDataBound,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000091 &isIntersectionOfRects);
92
93 // make sure that GrClipData is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000094 REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000095 REPORTER_ASSERT(reporter, isIntersectionOfRects);
96}
97
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000098////////////////////////////////////////////////////////////////////////////////
99// verify that the top state of the stack matches the passed in state
100static void check_state(skiatest::Reporter* reporter,
101 const GrClipMaskCache& cache,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000102 const SkClipStack& clip,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000103 GrTexture* mask,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000104 const GrIRect& bound) {
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000105 SkClipStack cacheClip;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000106 REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000107
108 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
109
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000110 GrIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000111 cache.getLastBound(&cacheBound);
112 REPORTER_ASSERT(reporter, bound == cacheBound);
113}
114
115////////////////////////////////////////////////////////////////////////////////
116// basic test of the cache's base functionality:
117// push, pop, set, canReuse & getters
118static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
119
caryclark@google.com42639cd2012-06-06 12:03:39 +0000120 if (false) { // avoid bit rot, suppress warning
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121 createTexture(context);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000122 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000123 GrClipMaskCache cache;
124
robertphillips@google.comf105b102012-05-14 12:18:26 +0000125 cache.setContext(context);
126
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000127 SkClipStack emptyClip;
128 emptyClip.reset();
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000129
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000130 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000131 emptyBound.setEmpty();
132
133 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000134 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000135
136 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000137 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000138 bound1.set(0, 0, 100, 100);
139
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000140 SkClipStack clip1(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000141
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000142 GrTextureDesc desc;
143 desc.fFlags = kRenderTarget_GrTextureFlagBit;
144 desc.fWidth = X_SIZE;
145 desc.fHeight = Y_SIZE;
146 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000147
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000148 cache.acquireMask(clip1.getTopmostGenID(), desc, bound1);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000149
150 GrTexture* texture1 = cache.getLastMask();
151 REPORTER_ASSERT(reporter, texture1);
152 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000153 return;
154 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000155
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000156 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000157 check_state(reporter, cache, clip1, texture1, bound1);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000158 REPORTER_ASSERT(reporter, texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000159
160 // push the state
161 cache.push();
162
163 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000164 check_state(reporter, cache, emptyClip, NULL, emptyBound);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000165 REPORTER_ASSERT(reporter, texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000166
167 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000168 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000169 bound2.set(-10, -10, 10, 10);
170
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000171 SkClipStack clip2(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000172
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000173 cache.acquireMask(clip2.getTopmostGenID(), desc, bound2);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000174
175 GrTexture* texture2 = cache.getLastMask();
176 REPORTER_ASSERT(reporter, texture2);
177 if (NULL == texture2) {
178 return;
179 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000180
181 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000182 check_state(reporter, cache, clip2, texture2, bound2);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000183 REPORTER_ASSERT(reporter, texture1->getRefCnt());
184 REPORTER_ASSERT(reporter, texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000185
186 // check to make sure canReuse works
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000187 REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2));
188 REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1));
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000189
190 // pop the state
191 cache.pop();
192
193 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000194 check_state(reporter, cache, clip1, texture1, bound1);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000195 REPORTER_ASSERT(reporter, texture1->getRefCnt());
196 REPORTER_ASSERT(reporter, texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000197
198 // manually clear the state
199 cache.reset();
200
201 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000202 check_state(reporter, cache, emptyClip, NULL, emptyBound);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000203 REPORTER_ASSERT(reporter, texture1->getRefCnt());
204 REPORTER_ASSERT(reporter, texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000205
206 // pop again - so there is no state
207 cache.pop();
208
209#if !defined(SK_DEBUG)
210 // verify that the getters don't crash
211 // only do in release since it generates asserts in debug
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000212 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000213#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000214 REPORTER_ASSERT(reporter, texture1->getRefCnt());
215 REPORTER_ASSERT(reporter, texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000216}
217
218////////////////////////////////////////////////////////////////////////////////
219static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
220
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000221 test_cache(reporter, context);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +0000222 test_clip_bounds(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000223}
224
225////////////////////////////////////////////////////////////////////////////////
226#include "TestClassDef.h"
227DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000228
229#endif