robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 1 | /* |
| 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.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 8 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 9 | #include "Test.h" |
bsalomon@google.com | a68937c | 2012-08-03 15:00:52 +0000 | [diff] [blame] | 10 | // This is a GR test |
| 11 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 12 | #include "GrContextFactory.h" |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 13 | #include "SkGpuDevice.h" |
| 14 | #include "../../src/gpu/GrClipMaskManager.h" |
| 15 | |
| 16 | static const int X_SIZE = 12; |
| 17 | static const int Y_SIZE = 12; |
| 18 | |
| 19 | //////////////////////////////////////////////////////////////////////////////// |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 20 | // note: this is unused |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 21 | static GrTexture* createTexture(GrContext* context) { |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 22 | unsigned char textureData[X_SIZE][Y_SIZE][4]; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 23 | |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 24 | memset(textureData, 0, 4* X_SIZE * Y_SIZE); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 25 | |
| 26 | GrTextureDesc desc; |
| 27 | |
| 28 | // let Skia know we will be using this texture as a render target |
| 29 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 30 | desc.fConfig = kSkia8888_GrPixelConfig; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 31 | desc.fWidth = X_SIZE; |
| 32 | desc.fHeight = Y_SIZE; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 33 | |
| 34 | // We are initializing the texture with zeros here |
| 35 | GrTexture* texture = context->createUncachedTexture(desc, textureData, 0); |
| 36 | if (!texture) { |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | return texture; |
| 41 | } |
| 42 | |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 43 | // Ensure that the 'getConservativeBounds' calls are returning bounds clamped |
| 44 | // to the render target |
| 45 | static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) { |
| 46 | |
| 47 | static const int kXSize = 100; |
| 48 | static const int kYSize = 100; |
| 49 | |
| 50 | GrTextureDesc desc; |
| 51 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 52 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 53 | desc.fWidth = kXSize; |
| 54 | desc.fHeight = kYSize; |
| 55 | |
| 56 | GrTexture* texture = context->createUncachedTexture(desc, NULL, 0); |
| 57 | if (!texture) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | GrAutoUnref au(texture); |
| 62 | |
| 63 | SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize); |
robertphillips@google.com | b755a2a | 2013-03-04 14:59:55 +0000 | [diff] [blame] | 64 | SkRect screen; |
skia.committer@gmail.com | 075b089 | 2013-03-05 07:09:08 +0000 | [diff] [blame] | 65 | |
robertphillips@google.com | b755a2a | 2013-03-04 14:59:55 +0000 | [diff] [blame] | 66 | screen = SkRect::MakeWH(SkIntToScalar(kXSize), |
| 67 | SkIntToScalar(kYSize)); |
| 68 | |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 69 | SkRect clipRect(screen); |
| 70 | clipRect.outset(10, 10); |
| 71 | |
| 72 | // create a clip stack that will (trivially) reduce to a single rect that |
| 73 | // is larger than the screen |
| 74 | SkClipStack stack; |
| 75 | stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false); |
| 76 | |
| 77 | bool isIntersectionOfRects = true; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 78 | SkRect devStackBounds; |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 79 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 80 | stack.getConservativeBounds(0, 0, kXSize, kYSize, |
| 81 | &devStackBounds, |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 82 | &isIntersectionOfRects); |
| 83 | |
| 84 | // make sure that the SkClipStack is behaving itself |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, screen == devStackBounds); |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 86 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 87 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 88 | // wrap the SkClipStack in a GrClipData |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 89 | GrClipData clipData; |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 90 | clipData.fClipStack = &stack; |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 91 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 92 | SkIRect devGrClipDataBound; |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 93 | clipData.getConservativeBounds(texture, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 94 | &devGrClipDataBound, |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 95 | &isIntersectionOfRects); |
| 96 | |
| 97 | // make sure that GrClipData is behaving itself |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound); |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 99 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 100 | } |
| 101 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 102 | //////////////////////////////////////////////////////////////////////////////// |
| 103 | // verify that the top state of the stack matches the passed in state |
| 104 | static void check_state(skiatest::Reporter* reporter, |
| 105 | const GrClipMaskCache& cache, |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 106 | const SkClipStack& clip, |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 107 | GrTexture* mask, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 108 | const GrIRect& bound) { |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 109 | SkClipStack cacheClip; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 111 | |
| 112 | REPORTER_ASSERT(reporter, mask == cache.getLastMask()); |
| 113 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 114 | GrIRect cacheBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 115 | cache.getLastBound(&cacheBound); |
| 116 | REPORTER_ASSERT(reporter, bound == cacheBound); |
| 117 | } |
| 118 | |
| 119 | //////////////////////////////////////////////////////////////////////////////// |
| 120 | // basic test of the cache's base functionality: |
| 121 | // push, pop, set, canReuse & getters |
| 122 | static void test_cache(skiatest::Reporter* reporter, GrContext* context) { |
| 123 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 124 | if (false) { // avoid bit rot, suppress warning |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 125 | createTexture(context); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 126 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 127 | GrClipMaskCache cache; |
| 128 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 129 | cache.setContext(context); |
| 130 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 131 | SkClipStack emptyClip; |
| 132 | emptyClip.reset(); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 133 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 134 | GrIRect emptyBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 135 | emptyBound.setEmpty(); |
| 136 | |
| 137 | // check initial state |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 138 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 139 | |
| 140 | // set the current state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 141 | GrIRect bound1; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 142 | bound1.set(0, 0, 100, 100); |
| 143 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 144 | SkClipStack clip1(bound1); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 145 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 146 | GrTextureDesc desc; |
| 147 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 148 | desc.fWidth = X_SIZE; |
| 149 | desc.fHeight = Y_SIZE; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 150 | desc.fConfig = kSkia8888_GrPixelConfig; |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 151 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 152 | cache.acquireMask(clip1.getTopmostGenID(), desc, bound1); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 153 | |
| 154 | GrTexture* texture1 = cache.getLastMask(); |
| 155 | REPORTER_ASSERT(reporter, texture1); |
| 156 | if (NULL == texture1) { |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 159 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 160 | // check that the set took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 161 | check_state(reporter, cache, clip1, texture1, bound1); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 163 | |
| 164 | // push the state |
| 165 | cache.push(); |
| 166 | |
| 167 | // verify that the pushed state is initially empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 168 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 169 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 170 | |
| 171 | // modify the new state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 172 | GrIRect bound2; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 173 | bound2.set(-10, -10, 10, 10); |
| 174 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 175 | SkClipStack clip2(bound2); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 176 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 177 | cache.acquireMask(clip2.getTopmostGenID(), desc, bound2); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 178 | |
| 179 | GrTexture* texture2 = cache.getLastMask(); |
| 180 | REPORTER_ASSERT(reporter, texture2); |
| 181 | if (NULL == texture2) { |
| 182 | return; |
| 183 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 184 | |
| 185 | // check that the changes took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 186 | check_state(reporter, cache, clip2, texture2, bound2); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 187 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| 188 | REPORTER_ASSERT(reporter, texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 189 | |
| 190 | // check to make sure canReuse works |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 191 | REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2)); |
| 192 | REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1)); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 193 | |
| 194 | // pop the state |
| 195 | cache.pop(); |
| 196 | |
| 197 | // verify that the old state is restored |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 198 | check_state(reporter, cache, clip1, texture1, bound1); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 199 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| 200 | REPORTER_ASSERT(reporter, texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 201 | |
| 202 | // manually clear the state |
| 203 | cache.reset(); |
| 204 | |
| 205 | // verify it is now empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 206 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 207 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| 208 | REPORTER_ASSERT(reporter, texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 209 | |
| 210 | // pop again - so there is no state |
| 211 | cache.pop(); |
| 212 | |
| 213 | #if !defined(SK_DEBUG) |
| 214 | // verify that the getters don't crash |
| 215 | // only do in release since it generates asserts in debug |
robertphillips@google.com | f21c704 | 2012-05-11 13:01:16 +0000 | [diff] [blame] | 216 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 217 | #endif |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 218 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| 219 | REPORTER_ASSERT(reporter, texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 223 | static void TestClipCache(skiatest::Reporter* reporter, GrContextFactory* factory) { |
| 224 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 225 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 226 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 227 | continue; |
| 228 | } |
| 229 | GrContext* context = factory->get(glType); |
| 230 | if (NULL == context) { |
| 231 | continue; |
| 232 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 233 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 234 | test_cache(reporter, context); |
| 235 | test_clip_bounds(reporter, context); |
| 236 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | //////////////////////////////////////////////////////////////////////////////// |
| 240 | #include "TestClassDef.h" |
| 241 | DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache) |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 242 | |
| 243 | #endif |