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; |
| 29 | desc.fSampleCnt = 0; |
| 30 | |
| 31 | // We are initializing the texture with zeros here |
| 32 | GrTexture* texture = context->createUncachedTexture(desc, textureData, 0); |
| 33 | if (!texture) { |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | return texture; |
| 38 | } |
| 39 | |
| 40 | //////////////////////////////////////////////////////////////////////////////// |
| 41 | // verify that the top state of the stack matches the passed in state |
| 42 | static void check_state(skiatest::Reporter* reporter, |
| 43 | const GrClipMaskCache& cache, |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 44 | const GrClip& clip, |
| 45 | GrTexture* mask, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 46 | const GrIRect& bound) { |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 47 | GrClip cacheClip; |
| 48 | cache.getLastClip(&cacheClip); |
| 49 | REPORTER_ASSERT(reporter, clip == cacheClip); |
| 50 | |
| 51 | REPORTER_ASSERT(reporter, mask == cache.getLastMask()); |
| 52 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 53 | GrIRect cacheBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 54 | cache.getLastBound(&cacheBound); |
| 55 | REPORTER_ASSERT(reporter, bound == cacheBound); |
| 56 | } |
| 57 | |
| 58 | //////////////////////////////////////////////////////////////////////////////// |
| 59 | // basic test of the cache's base functionality: |
| 60 | // push, pop, set, canReuse & getters |
| 61 | static void test_cache(skiatest::Reporter* reporter, GrContext* context) { |
| 62 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 63 | if (false) { // avoid bit rot, suppress warning |
| 64 | createTexture(context); |
| 65 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 66 | GrClipMaskCache cache; |
| 67 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 68 | cache.setContext(context); |
| 69 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 70 | GrClip emptyClip; |
| 71 | emptyClip.setEmpty(); |
| 72 | |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 73 | GrIRect emptyBound; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 74 | emptyBound.setEmpty(); |
| 75 | |
| 76 | // check initial state |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 77 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 78 | |
| 79 | // set the current state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 80 | GrIRect bound1; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 81 | bound1.set(0, 0, 100, 100); |
| 82 | |
| 83 | GrClip clip1; |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 84 | clip1.setFromIRect(bound1); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 85 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 86 | const GrTextureDesc desc = { |
| 87 | kRenderTarget_GrTextureFlagBit, |
| 88 | X_SIZE, |
| 89 | Y_SIZE, |
| 90 | kSkia8888_PM_GrPixelConfig, |
| 91 | 0 |
| 92 | }; |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 93 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 94 | cache.acquireMask(clip1, desc, bound1); |
| 95 | |
| 96 | GrTexture* texture1 = cache.getLastMask(); |
| 97 | REPORTER_ASSERT(reporter, texture1); |
| 98 | if (NULL == texture1) { |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 101 | |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 102 | // check that the set took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 103 | check_state(reporter, cache, clip1, texture1, bound1); |
| 104 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 105 | |
| 106 | // push the state |
| 107 | cache.push(); |
| 108 | |
| 109 | // verify that the pushed state is initially empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 110 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 112 | |
| 113 | // modify the new state |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 114 | GrIRect bound2; |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 115 | bound2.set(-10, -10, 10, 10); |
| 116 | |
| 117 | GrClip clip2; |
| 118 | clip2.setEmpty(); |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 119 | clip2.setFromIRect(bound2); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 120 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 121 | cache.acquireMask(clip2, desc, bound2); |
| 122 | |
| 123 | GrTexture* texture2 = cache.getLastMask(); |
| 124 | REPORTER_ASSERT(reporter, texture2); |
| 125 | if (NULL == texture2) { |
| 126 | return; |
| 127 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 128 | |
| 129 | // check that the changes took |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 130 | check_state(reporter, cache, clip2, texture2, bound2); |
| 131 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 132 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 133 | |
| 134 | // check to make sure canReuse works |
| 135 | REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10)); |
| 136 | REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10)); |
| 137 | |
| 138 | // pop the state |
| 139 | cache.pop(); |
| 140 | |
| 141 | // verify that the old state is restored |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 142 | check_state(reporter, cache, clip1, texture1, bound1); |
| 143 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 144 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 145 | |
| 146 | // manually clear the state |
| 147 | cache.reset(); |
| 148 | |
| 149 | // verify it is now empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 150 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 152 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 153 | |
| 154 | // pop again - so there is no state |
| 155 | cache.pop(); |
| 156 | |
| 157 | #if !defined(SK_DEBUG) |
| 158 | // verify that the getters don't crash |
| 159 | // only do in release since it generates asserts in debug |
robertphillips@google.com | f21c704 | 2012-05-11 13:01:16 +0000 | [diff] [blame] | 160 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 161 | #endif |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); |
| 163 | REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | //////////////////////////////////////////////////////////////////////////////// |
| 167 | static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) { |
| 168 | |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 169 | test_cache(reporter, context); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | //////////////////////////////////////////////////////////////////////////////// |
| 173 | #include "TestClassDef.h" |
| 174 | DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache) |