blob: 795e861d372f22f953522b16fb8152a09c67d7f7 [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) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000105 SkClipStack cacheClip;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000106 cache.getLastClip(&cacheClip);
107 REPORTER_ASSERT(reporter, clip == cacheClip);
108
109 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
110
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000111 GrIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000112 cache.getLastBound(&cacheBound);
113 REPORTER_ASSERT(reporter, bound == cacheBound);
114}
115
116////////////////////////////////////////////////////////////////////////////////
117// basic test of the cache's base functionality:
118// push, pop, set, canReuse & getters
119static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
120
caryclark@google.com42639cd2012-06-06 12:03:39 +0000121 if (false) { // avoid bit rot, suppress warning
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122 createTexture(context);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000123 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000124 GrClipMaskCache cache;
125
robertphillips@google.comf105b102012-05-14 12:18:26 +0000126 cache.setContext(context);
127
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000128 SkClipStack emptyClip;
129 emptyClip.reset();
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000130
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000131 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000132 emptyBound.setEmpty();
133
134 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000135 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000136
137 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000138 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000139 bound1.set(0, 0, 100, 100);
140
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000141 SkClipStack clip1(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000142
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000143 GrTextureDesc desc;
144 desc.fFlags = kRenderTarget_GrTextureFlagBit;
145 desc.fWidth = X_SIZE;
146 desc.fHeight = Y_SIZE;
147 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000148
robertphillips@google.comf105b102012-05-14 12:18:26 +0000149 cache.acquireMask(clip1, desc, bound1);
150
151 GrTexture* texture1 = cache.getLastMask();
152 REPORTER_ASSERT(reporter, texture1);
153 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000154 return;
155 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000156
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000157 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000158 check_state(reporter, cache, clip1, texture1, bound1);
159 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000160
161 // push the state
162 cache.push();
163
164 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000165 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000166 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000167
168 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000169 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000170 bound2.set(-10, -10, 10, 10);
171
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000172 SkClipStack clip2(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000173
robertphillips@google.comf105b102012-05-14 12:18:26 +0000174 cache.acquireMask(clip2, desc, bound2);
175
176 GrTexture* texture2 = cache.getLastMask();
177 REPORTER_ASSERT(reporter, texture2);
178 if (NULL == texture2) {
179 return;
180 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000181
182 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000183 check_state(reporter, cache, clip2, texture2, bound2);
184 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
185 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000186
187 // check to make sure canReuse works
188 REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10));
189 REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10));
190
191 // pop the state
192 cache.pop();
193
194 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000195 check_state(reporter, cache, clip1, texture1, bound1);
196 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
197 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000198
199 // manually clear the state
200 cache.reset();
201
202 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000203 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000204 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
205 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000206
207 // pop again - so there is no state
208 cache.pop();
209
210#if !defined(SK_DEBUG)
211 // verify that the getters don't crash
212 // only do in release since it generates asserts in debug
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000213 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000214#endif
robertphillips@google.comf105b102012-05-14 12:18:26 +0000215 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
216 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000217}
218
219////////////////////////////////////////////////////////////////////////////////
220static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
221
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000222 test_cache(reporter, context);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +0000223 test_clip_bounds(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000224}
225
226////////////////////////////////////////////////////////////////////////////////
227#include "TestClassDef.h"
228DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000229
230#endif