blob: a0888d85e4a7419a3b52c04368605a39eb08c477 [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) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000017 unsigned char textureData[X_SIZE][Y_SIZE][4];
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000018
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000019 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000020
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.comd82f3fa2012-05-10 21:26:48 +000025 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000026 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,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000043 const GrClip& clip,
44 GrTexture* mask,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000045 const GrIRect& bound) {
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000046 GrClip cacheClip;
47 cache.getLastClip(&cacheClip);
48 REPORTER_ASSERT(reporter, clip == cacheClip);
49
50 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
51
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000052 GrIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000053 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
robertphillips@google.comf105b102012-05-14 12:18:26 +000064 cache.setContext(context);
65
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000066 GrClip emptyClip;
67 emptyClip.setEmpty();
68
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000069 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000070 emptyBound.setEmpty();
71
72 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +000073 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000074
75 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000076 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000077 bound1.set(0, 0, 100, 100);
78
79 GrClip clip1;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000080 clip1.setFromIRect(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000081
robertphillips@google.comf105b102012-05-14 12:18:26 +000082 const GrTextureDesc desc = {
83 kRenderTarget_GrTextureFlagBit,
84 X_SIZE,
85 Y_SIZE,
86 kSkia8888_PM_GrPixelConfig,
87 0
88 };
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000089
robertphillips@google.comf105b102012-05-14 12:18:26 +000090 cache.acquireMask(clip1, desc, bound1);
91
92 GrTexture* texture1 = cache.getLastMask();
93 REPORTER_ASSERT(reporter, texture1);
94 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000095 return;
96 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000097
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000098 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +000099 check_state(reporter, cache, clip1, texture1, bound1);
100 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000101
102 // push the state
103 cache.push();
104
105 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000106 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000107 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000108
109 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000110 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000111 bound2.set(-10, -10, 10, 10);
112
113 GrClip clip2;
114 clip2.setEmpty();
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000115 clip2.setFromIRect(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000116
robertphillips@google.comf105b102012-05-14 12:18:26 +0000117 cache.acquireMask(clip2, desc, bound2);
118
119 GrTexture* texture2 = cache.getLastMask();
120 REPORTER_ASSERT(reporter, texture2);
121 if (NULL == texture2) {
122 return;
123 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000124
125 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000126 check_state(reporter, cache, clip2, texture2, bound2);
127 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
128 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000129
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
robertphillips@google.comf105b102012-05-14 12:18:26 +0000138 check_state(reporter, cache, clip1, texture1, bound1);
139 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
140 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000141
142 // manually clear the state
143 cache.reset();
144
145 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000146 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000147 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
148 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000149
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
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000156 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000157#endif
robertphillips@google.comf105b102012-05-14 12:18:26 +0000158 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
159 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000160}
161
162////////////////////////////////////////////////////////////////////////////////
163static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
164
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000165 test_cache(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000166}
167
168////////////////////////////////////////////////////////////////////////////////
169#include "TestClassDef.h"
170DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)