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 |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 12 | #include "SkGpuDevice.h" |
| 13 | #include "../../src/gpu/GrClipMaskManager.h" |
| 14 | |
| 15 | static const int X_SIZE = 12; |
| 16 | static const int Y_SIZE = 12; |
| 17 | |
| 18 | //////////////////////////////////////////////////////////////////////////////// |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 19 | // note: this is unused |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 20 | static GrTexture* createTexture(GrContext* context) { |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 21 | unsigned char textureData[X_SIZE][Y_SIZE][4]; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 22 | |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 23 | memset(textureData, 0, 4* X_SIZE * Y_SIZE); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 24 | |
| 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.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 29 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 30 | desc.fWidth = X_SIZE; |
| 31 | desc.fHeight = Y_SIZE; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 32 | |
| 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.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 42 | // Ensure that the 'getConservativeBounds' calls are returning bounds clamped |
| 43 | // to the render target |
| 44 | static 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 63 | SkRect screen = SkRect::MakeWH(SkIntToScalar(kXSize), |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 64 | 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.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 74 | SkRect devStackBounds; |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 75 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 76 | stack.getConservativeBounds(0, 0, kXSize, kYSize, |
| 77 | &devStackBounds, |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 78 | &isIntersectionOfRects); |
| 79 | |
| 80 | // make sure that the SkClipStack is behaving itself |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, screen == devStackBounds); |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 83 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 84 | // wrap the SkClipStack in a GrClipData |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 85 | GrClipData clipData; |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 86 | clipData.fClipStack = &stack; |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 87 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 88 | SkIRect devGrClipDataBound; |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 89 | clipData.getConservativeBounds(texture, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 90 | &devGrClipDataBound, |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 91 | &isIntersectionOfRects); |
| 92 | |
| 93 | // make sure that GrClipData is behaving itself |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound); |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 96 | } |
| 97 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 98 | //////////////////////////////////////////////////////////////////////////////// |
| 99 | // verify that the top state of the stack matches the passed in state |
| 100 | static void check_state(skiatest::Reporter* reporter, |
| 101 | const GrClipMaskCache& cache, |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 102 | const SkClipStack& clip, |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 103 | GrTexture* mask, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 104 | const GrIRect& bound) { |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 105 | SkClipStack cacheClip; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 107 | |
| 108 | REPORTER_ASSERT(reporter, mask == cache.getLastMask()); |
| 109 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 110 | GrIRect cacheBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 111 | 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 |
| 118 | static void test_cache(skiatest::Reporter* reporter, GrContext* context) { |
| 119 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 120 | if (false) { // avoid bit rot, suppress warning |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 121 | createTexture(context); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 122 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 123 | GrClipMaskCache cache; |
| 124 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 125 | cache.setContext(context); |
| 126 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 127 | SkClipStack emptyClip; |
| 128 | emptyClip.reset(); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 129 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 130 | GrIRect emptyBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 131 | emptyBound.setEmpty(); |
| 132 | |
| 133 | // check initial state |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 134 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 135 | |
| 136 | // set the current state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 137 | GrIRect bound1; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 138 | bound1.set(0, 0, 100, 100); |
| 139 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 140 | SkClipStack clip1(bound1); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 141 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 142 | 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.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 147 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 148 | cache.acquireMask(clip1.getTopmostGenID(), desc, bound1); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 149 | |
| 150 | GrTexture* texture1 = cache.getLastMask(); |
| 151 | REPORTER_ASSERT(reporter, texture1); |
| 152 | if (NULL == texture1) { |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 155 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 156 | // check that the set took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 157 | check_state(reporter, cache, clip1, texture1, bound1); |
| 158 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 159 | |
| 160 | // push the state |
| 161 | cache.push(); |
| 162 | |
| 163 | // verify that the pushed state is initially empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 164 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 166 | |
| 167 | // modify the new state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 168 | GrIRect bound2; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 169 | bound2.set(-10, -10, 10, 10); |
| 170 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 171 | SkClipStack clip2(bound2); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 172 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 173 | cache.acquireMask(clip2.getTopmostGenID(), desc, bound2); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 174 | |
| 175 | GrTexture* texture2 = cache.getLastMask(); |
| 176 | REPORTER_ASSERT(reporter, texture2); |
| 177 | if (NULL == texture2) { |
| 178 | return; |
| 179 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 180 | |
| 181 | // check that the changes took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 182 | check_state(reporter, cache, clip2, texture2, bound2); |
| 183 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 184 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 185 | |
| 186 | // check to make sure canReuse works |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 187 | REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2)); |
| 188 | REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1)); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 189 | |
| 190 | // pop the state |
| 191 | cache.pop(); |
| 192 | |
| 193 | // verify that the old state is restored |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 194 | check_state(reporter, cache, clip1, texture1, bound1); |
| 195 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 196 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 197 | |
| 198 | // manually clear the state |
| 199 | cache.reset(); |
| 200 | |
| 201 | // verify it is now empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 202 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 203 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 204 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 205 | |
| 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.com | f21c704 | 2012-05-11 13:01:16 +0000 | [diff] [blame] | 212 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 213 | #endif |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 214 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 215 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | //////////////////////////////////////////////////////////////////////////////// |
| 219 | static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) { |
| 220 | |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 221 | test_cache(reporter, context); |
robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 222 | test_clip_bounds(reporter, context); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | //////////////////////////////////////////////////////////////////////////////// |
| 226 | #include "TestClassDef.h" |
| 227 | DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache) |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 228 | |
| 229 | #endif |