ClipCacheTest.cpp revision 8fff356c8505f2ac78e1fc9dc17c1192e3a608e4
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 GrRect& bound) { 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 60static 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 71 check_state(reporter, cache, emptyClip, NULL, emptyBound); 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)); 81 REPORTER_ASSERT(reporter, texture.get()); 82 83 if (NULL == texture.get()) { 84 return; 85 } 86 87 cache.set(clip1, texture.get(), bound1); 88 89 // check that the set took 90 check_state(reporter, cache, clip1, texture.get(), bound1); 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 97 check_state(reporter, cache, emptyClip, NULL, emptyBound); 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 108 cache.set(clip2, texture.get(), bound2); 109 110 // check that the changes took 111 check_state(reporter, cache, clip2, texture.get(), bound2); 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 122 check_state(reporter, cache, clip1, texture.get(), bound1); 123 REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt()); 124 125 // manually clear the state 126 cache.reset(); 127 128 // verify it is now empty 129 check_state(reporter, cache, emptyClip, NULL, emptyBound); 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//////////////////////////////////////////////////////////////////////////////// 144static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) { 145 146 test_cache(reporter, context); 147} 148 149//////////////////////////////////////////////////////////////////////////////// 150#include "TestClassDef.h" 151DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache) 152