ClipCacheTest.cpp revision 6623fcd1ee33b35fa18e304e9c76272faa603cbf
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 12static const int X_SIZE = 12; 13static const int Y_SIZE = 12; 14 15//////////////////////////////////////////////////////////////////////////////// 16static GrTexture* createTexture(GrContext* context) { 17 unsigned char textureData[X_SIZE][Y_SIZE][4]; 18 19 memset(textureData, 0, 4* X_SIZE * Y_SIZE); 20 21 GrTextureDesc desc; 22 23 // let Skia know we will be using this texture as a render target 24 desc.fFlags = kRenderTarget_GrTextureFlagBit; 25 desc.fConfig = kSkia8888_PM_GrPixelConfig; 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 41static void check_state(skiatest::Reporter* reporter, 42 const GrClipMaskCache& cache, 43 const GrClip& clip, 44 GrTexture* mask, 45 const GrIRect& bound) { 46 GrClip cacheClip; 47 cache.getLastClip(&cacheClip); 48 REPORTER_ASSERT(reporter, clip == cacheClip); 49 50 REPORTER_ASSERT(reporter, mask == cache.getLastMask()); 51 52 GrIRect 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 60static void test_cache(skiatest::Reporter* reporter, GrContext* context) { 61 62 GrClipMaskCache cache; 63 64 cache.setContext(context); 65 66 GrClip emptyClip; 67 emptyClip.setEmpty(); 68 69 GrIRect emptyBound; 70 emptyBound.setEmpty(); 71 72 // check initial state 73 check_state(reporter, cache, emptyClip, NULL, emptyBound); 74 75 // set the current state 76 GrIRect bound1; 77 bound1.set(0, 0, 100, 100); 78 79 GrClip clip1; 80 clip1.setFromIRect(bound1); 81 82 const GrTextureDesc desc = { 83 kRenderTarget_GrTextureFlagBit, 84 X_SIZE, 85 Y_SIZE, 86 kSkia8888_PM_GrPixelConfig, 87 0 88 }; 89 90 cache.acquireMask(clip1, desc, bound1); 91 92 GrTexture* texture1 = cache.getLastMask(); 93 REPORTER_ASSERT(reporter, texture1); 94 if (NULL == texture1) { 95 return; 96 } 97 98 // check that the set took 99 check_state(reporter, cache, clip1, texture1, bound1); 100 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); 101 102 // push the state 103 cache.push(); 104 105 // verify that the pushed state is initially empty 106 check_state(reporter, cache, emptyClip, NULL, emptyBound); 107 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); 108 109 // modify the new state 110 GrIRect bound2; 111 bound2.set(-10, -10, 10, 10); 112 113 GrClip clip2; 114 clip2.setEmpty(); 115 clip2.setFromIRect(bound2); 116 117 cache.acquireMask(clip2, desc, bound2); 118 119 GrTexture* texture2 = cache.getLastMask(); 120 REPORTER_ASSERT(reporter, texture2); 121 if (NULL == texture2) { 122 return; 123 } 124 125 // check that the changes took 126 check_state(reporter, cache, clip2, texture2, bound2); 127 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); 128 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); 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 138 check_state(reporter, cache, clip1, texture1, bound1); 139 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); 140 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); 141 142 // manually clear the state 143 cache.reset(); 144 145 // verify it is now empty 146 check_state(reporter, cache, emptyClip, NULL, emptyBound); 147 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); 148 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); 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 156 check_state(reporter, cache, emptyClip, NULL, emptyBound); 157#endif 158 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); 159 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); 160} 161 162//////////////////////////////////////////////////////////////////////////////// 163static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) { 164 165 test_cache(reporter, context); 166} 167 168//////////////////////////////////////////////////////////////////////////////// 169#include "TestClassDef.h" 170DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache) 171