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, |
| 45 | const GrRect& 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 | |
| 52 | GrRect cacheBound; |
| 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 | |
| 64 | GrClip emptyClip; |
| 65 | emptyClip.setEmpty(); |
| 66 | |
| 67 | GrRect emptyBound; |
| 68 | emptyBound.setEmpty(); |
| 69 | |
| 70 | // check initial state |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 71 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 72 | |
| 73 | // set the current state |
| 74 | GrRect bound1; |
| 75 | bound1.set(0, 0, 100, 100); |
| 76 | |
| 77 | GrClip clip1; |
| 78 | clip1.setFromRect(bound1); |
| 79 | |
| 80 | SkAutoTUnref<GrTexture> texture(createTexture(context)); |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, texture.get()); |
| 82 | |
| 83 | if (NULL == texture.get()) { |
| 84 | return; |
| 85 | } |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 86 | |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 87 | cache.set(clip1, texture.get(), bound1); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 88 | |
| 89 | // check that the set took |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 90 | check_state(reporter, cache, clip1, texture.get(), bound1); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt()); |
| 92 | |
| 93 | // push the state |
| 94 | cache.push(); |
| 95 | |
| 96 | // verify that the pushed state is initially empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 97 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt()); |
| 99 | |
| 100 | // modify the new state |
| 101 | GrRect bound2; |
| 102 | bound2.set(-10, -10, 10, 10); |
| 103 | |
| 104 | GrClip clip2; |
| 105 | clip2.setEmpty(); |
| 106 | clip2.setFromRect(bound2); |
| 107 | |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 108 | cache.set(clip2, texture.get(), bound2); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 109 | |
| 110 | // check that the changes took |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 111 | check_state(reporter, cache, clip2, texture.get(), bound2); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 112 | REPORTER_ASSERT(reporter, 3 == texture.get()->getRefCnt()); |
| 113 | |
| 114 | // check to make sure canReuse works |
| 115 | REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10)); |
| 116 | REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10)); |
| 117 | |
| 118 | // pop the state |
| 119 | cache.pop(); |
| 120 | |
| 121 | // verify that the old state is restored |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 122 | check_state(reporter, cache, clip1, texture.get(), bound1); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt()); |
| 124 | |
| 125 | // manually clear the state |
| 126 | cache.reset(); |
| 127 | |
| 128 | // verify it is now empty |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame^] | 129 | check_state(reporter, cache, emptyClip, NULL, emptyBound); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 130 | REPORTER_ASSERT(reporter, 1 == texture.get()->getRefCnt()); |
| 131 | |
| 132 | // pop again - so there is no state |
| 133 | cache.pop(); |
| 134 | |
| 135 | #if !defined(SK_DEBUG) |
| 136 | // verify that the getters don't crash |
| 137 | // only do in release since it generates asserts in debug |
| 138 | check_state(reporter, cache, -1, -1, emptyClip, NULL, emptyBound); |
| 139 | #endif |
| 140 | REPORTER_ASSERT(reporter, 1 == texture.get()->getRefCnt()); |
| 141 | } |
| 142 | |
| 143 | //////////////////////////////////////////////////////////////////////////////// |
| 144 | static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) { |
| 145 | |
robertphillips@google.com | d82f3fa | 2012-05-10 21:26:48 +0000 | [diff] [blame] | 146 | test_cache(reporter, context); |
robertphillips@google.com | beeb97c | 2012-05-09 21:15:28 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | //////////////////////////////////////////////////////////////////////////////// |
| 150 | #include "TestClassDef.h" |
| 151 | DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache) |