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