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