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 | |
| 8 | #include "Test.h" |
| 9 | #include "SkGpuDevice.h" |
| 10 | #include "../../src/gpu/GrClipMaskManager.h" |
| 11 | |
| 12 | static const int X_SIZE = 12; |
| 13 | static const int Y_SIZE = 12; |
| 14 | |
| 15 | //////////////////////////////////////////////////////////////////////////////// |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 16 | // note: this is unused |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 17 | static GrTexture* createTexture(GrContext* context) { |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 18 | unsigned char textureData[X_SIZE][Y_SIZE][4]; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 20 | memset(textureData, 0, 4* X_SIZE * Y_SIZE); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 21 | |
| 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.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 26 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 27 | desc.fWidth = X_SIZE; |
| 28 | desc.fHeight = Y_SIZE; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 29 | |
| 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.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame^] | 39 | // Ensure that the 'getConservativeBounds' calls are returning bounds clamped |
| 40 | // to the render target |
| 41 | static 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; |
| 71 | SkRect stackBounds; |
| 72 | |
| 73 | stack.getConservativeBounds(0, 0, kXSize, kYSize, |
| 74 | &stackBounds, |
| 75 | &isIntersectionOfRects); |
| 76 | |
| 77 | // make sure that the SkClipStack is behaving itself |
| 78 | REPORTER_ASSERT(reporter, screen == stackBounds); |
| 79 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 80 | |
| 81 | // convert the SkClipStack to a GrClip |
| 82 | SkGrClipIterator iter; |
| 83 | iter.reset(stack); |
| 84 | |
| 85 | GrClip clip; |
| 86 | clip.setFromIterator(&iter, 0, 0, stackBounds); |
| 87 | |
| 88 | const GrRect& grBound = clip.getConservativeBounds(); |
| 89 | |
| 90 | // make sure that GrClip is behaving itself |
| 91 | REPORTER_ASSERT(reporter, clipRect == grBound); |
| 92 | REPORTER_ASSERT(reporter, clip.isRect()); |
| 93 | |
| 94 | // wrap the GrClip in a GrClipData |
| 95 | GrClipData clipData; |
| 96 | clipData.fClipStack = &clip; |
| 97 | |
| 98 | SkIRect intGrBound; |
| 99 | clipData.getConservativeBounds(texture, |
| 100 | &intGrBound, |
| 101 | &isIntersectionOfRects); |
| 102 | |
| 103 | // make sure that GrClipData is behaving itself |
| 104 | REPORTER_ASSERT(reporter, intScreen == intGrBound); |
| 105 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 106 | } |
| 107 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 108 | //////////////////////////////////////////////////////////////////////////////// |
| 109 | // verify that the top state of the stack matches the passed in state |
| 110 | static void check_state(skiatest::Reporter* reporter, |
| 111 | const GrClipMaskCache& cache, |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 112 | const GrClip& clip, |
| 113 | GrTexture* mask, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 114 | const GrIRect& bound) { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 115 | GrClip cacheClip; |
| 116 | cache.getLastClip(&cacheClip); |
| 117 | REPORTER_ASSERT(reporter, clip == cacheClip); |
| 118 | |
| 119 | REPORTER_ASSERT(reporter, mask == cache.getLastMask()); |
| 120 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 121 | GrIRect cacheBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 122 | cache.getLastBound(&cacheBound); |
| 123 | REPORTER_ASSERT(reporter, bound == cacheBound); |
| 124 | } |
| 125 | |
| 126 | //////////////////////////////////////////////////////////////////////////////// |
| 127 | // basic test of the cache's base functionality: |
| 128 | // push, pop, set, canReuse & getters |
| 129 | static void test_cache(skiatest::Reporter* reporter, GrContext* context) { |
| 130 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 131 | if (false) { // avoid bit rot, suppress warning |
| 132 | createTexture(context); |
| 133 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 134 | GrClipMaskCache cache; |
| 135 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 136 | cache.setContext(context); |
| 137 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 138 | GrClip emptyClip; |
| 139 | emptyClip.setEmpty(); |
| 140 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 141 | GrIRect emptyBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 142 | emptyBound.setEmpty(); |
| 143 | |
| 144 | // check initial state |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 145 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 146 | |
| 147 | // set the current state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 148 | GrIRect bound1; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 149 | bound1.set(0, 0, 100, 100); |
| 150 | |
| 151 | GrClip clip1; |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 152 | clip1.setFromIRect(bound1); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 153 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 154 | GrTextureDesc desc; |
| 155 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 156 | desc.fWidth = X_SIZE; |
| 157 | desc.fHeight = Y_SIZE; |
| 158 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 159 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 160 | cache.acquireMask(clip1, desc, bound1); |
| 161 | |
| 162 | GrTexture* texture1 = cache.getLastMask(); |
| 163 | REPORTER_ASSERT(reporter, texture1); |
| 164 | if (NULL == texture1) { |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 167 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 168 | // check that the set took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 169 | check_state(reporter, cache, clip1, texture1, bound1); |
| 170 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 171 | |
| 172 | // push the state |
| 173 | cache.push(); |
| 174 | |
| 175 | // verify that the pushed state is initially empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 176 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 177 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 178 | |
| 179 | // modify the new state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 180 | GrIRect bound2; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 181 | bound2.set(-10, -10, 10, 10); |
| 182 | |
| 183 | GrClip clip2; |
| 184 | clip2.setEmpty(); |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 185 | clip2.setFromIRect(bound2); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 186 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 187 | cache.acquireMask(clip2, desc, bound2); |
| 188 | |
| 189 | GrTexture* texture2 = cache.getLastMask(); |
| 190 | REPORTER_ASSERT(reporter, texture2); |
| 191 | if (NULL == texture2) { |
| 192 | return; |
| 193 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 194 | |
| 195 | // check that the changes took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 196 | check_state(reporter, cache, clip2, texture2, bound2); |
| 197 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 198 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 199 | |
| 200 | // check to make sure canReuse works |
| 201 | REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10)); |
| 202 | REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10)); |
| 203 | |
| 204 | // pop the state |
| 205 | cache.pop(); |
| 206 | |
| 207 | // verify that the old state is restored |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 208 | check_state(reporter, cache, clip1, texture1, bound1); |
| 209 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 210 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 211 | |
| 212 | // manually clear the state |
| 213 | cache.reset(); |
| 214 | |
| 215 | // verify it is now empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 216 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 217 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 218 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 219 | |
| 220 | // pop again - so there is no state |
| 221 | cache.pop(); |
| 222 | |
| 223 | #if !defined(SK_DEBUG) |
| 224 | // verify that the getters don't crash |
| 225 | // only do in release since it generates asserts in debug |
robertphillips@google.com | f21c704 | 2012-05-11 13:01:16 +0000 | [diff] [blame] | 226 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 227 | #endif |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 228 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 229 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | //////////////////////////////////////////////////////////////////////////////// |
| 233 | static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) { |
| 234 | |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 235 | test_cache(reporter, context); |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame^] | 236 | test_clip_bounds(reporter, context); |
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) |