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