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