| 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" |
| tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame^] | 9 | #include "TestClassDef.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 | |
| commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 61 | SkAutoUnref au(texture); |
| robertphillips@google.com | c23a63b | 2012-07-31 11:47:29 +0000 | [diff] [blame] | 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, |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 108 | const SkIRect& bound) { |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 109 | REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID()); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 110 | |
| 111 | REPORTER_ASSERT(reporter, mask == cache.getLastMask()); |
| 112 | |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 113 | SkIRect cacheBound; |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 114 | cache.getLastBound(&cacheBound); |
| 115 | REPORTER_ASSERT(reporter, bound == cacheBound); |
| 116 | } |
| 117 | |
| commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 118 | static void check_empty_state(skiatest::Reporter* reporter, |
| 119 | const GrClipMaskCache& cache) { |
| 120 | REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID == cache.getLastClipGenID()); |
| 121 | REPORTER_ASSERT(reporter, NULL == cache.getLastMask()); |
| 122 | |
| 123 | SkIRect emptyBound; |
| 124 | emptyBound.setEmpty(); |
| 125 | |
| 126 | SkIRect cacheBound; |
| 127 | cache.getLastBound(&cacheBound); |
| 128 | REPORTER_ASSERT(reporter, emptyBound == cacheBound); |
| 129 | } |
| 130 | |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 131 | //////////////////////////////////////////////////////////////////////////////// |
| 132 | // basic test of the cache's base functionality: |
| 133 | // push, pop, set, canReuse & getters |
| 134 | static void test_cache(skiatest::Reporter* reporter, GrContext* context) { |
| 135 | |
| caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 136 | if (false) { // avoid bit rot, suppress warning |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 137 | createTexture(context); |
| caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 138 | } |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 139 | GrClipMaskCache cache; |
| 140 | |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 141 | cache.setContext(context); |
| 142 | |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 143 | // check initial state |
| commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 144 | check_empty_state(reporter, cache); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 145 | |
| 146 | // set the current state |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 147 | SkIRect bound1; |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 148 | bound1.set(0, 0, 100, 100); |
| 149 | |
| robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 150 | SkClipStack clip1(bound1); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 151 | |
| robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 152 | GrTextureDesc desc; |
| 153 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 154 | desc.fWidth = X_SIZE; |
| 155 | desc.fHeight = Y_SIZE; |
| bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 156 | desc.fConfig = kSkia8888_GrPixelConfig; |
| robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 157 | |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 158 | cache.acquireMask(clip1.getTopmostGenID(), desc, bound1); |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 159 | |
| 160 | GrTexture* texture1 = cache.getLastMask(); |
| 161 | REPORTER_ASSERT(reporter, texture1); |
| 162 | if (NULL == texture1) { |
| robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 165 | |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 166 | // check that the set took |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 167 | check_state(reporter, cache, clip1, texture1, bound1); |
| bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 169 | |
| 170 | // push the state |
| 171 | cache.push(); |
| 172 | |
| 173 | // verify that the pushed state is initially empty |
| commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 174 | check_empty_state(reporter, cache); |
| bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 175 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 176 | |
| 177 | // modify the new state |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 178 | SkIRect bound2; |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 179 | bound2.set(-10, -10, 10, 10); |
| 180 | |
| robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 181 | SkClipStack clip2(bound2); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 182 | |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 183 | cache.acquireMask(clip2.getTopmostGenID(), desc, bound2); |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 184 | |
| 185 | GrTexture* texture2 = cache.getLastMask(); |
| 186 | REPORTER_ASSERT(reporter, texture2); |
| 187 | if (NULL == texture2) { |
| 188 | return; |
| 189 | } |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 190 | |
| 191 | // check that the changes took |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 192 | check_state(reporter, cache, clip2, texture2, bound2); |
| bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 193 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| 194 | REPORTER_ASSERT(reporter, texture2->getRefCnt()); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 195 | |
| 196 | // check to make sure canReuse works |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2)); |
| 198 | REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1)); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 199 | |
| 200 | // pop the state |
| 201 | cache.pop(); |
| 202 | |
| 203 | // verify that the old state is restored |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 204 | check_state(reporter, cache, clip1, texture1, bound1); |
| bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 205 | REPORTER_ASSERT(reporter, texture1->getRefCnt()); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 206 | |
| 207 | // manually clear the state |
| 208 | cache.reset(); |
| 209 | |
| 210 | // verify it is now empty |
| commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 211 | check_empty_state(reporter, cache); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 212 | |
| 213 | // pop again - so there is no state |
| 214 | cache.pop(); |
| 215 | |
| 216 | #if !defined(SK_DEBUG) |
| 217 | // verify that the getters don't crash |
| 218 | // only do in release since it generates asserts in debug |
| commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 219 | check_empty_state(reporter, cache); |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 220 | #endif |
| robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame^] | 223 | DEF_GPUTEST(ClipCache, reporter, factory) { |
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 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 | |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 239 | #endif |