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