blob: 249bd585cc169dd602797d8e8a73bc0bf0d2b1a0 [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////////////////////////////////////////////////////////////////////////////////
caryclark@google.com42639cd2012-06-06 12:03:39 +000016// note: this is unused
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000017static GrTexture* createTexture(GrContext* context) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000018 unsigned char textureData[X_SIZE][Y_SIZE][4];
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000019
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000020 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000021
22 GrTextureDesc desc;
23
24 // let Skia know we will be using this texture as a render target
25 desc.fFlags = kRenderTarget_GrTextureFlagBit;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000026 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000027 desc.fWidth = X_SIZE;
28 desc.fHeight = Y_SIZE;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000029
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
caryclark@google.com42639cd2012-06-06 12:03:39 +000062 if (false) { // avoid bit rot, suppress warning
63 createTexture(context);
64 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000065 GrClipMaskCache cache;
66
robertphillips@google.comf105b102012-05-14 12:18:26 +000067 cache.setContext(context);
68
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000069 GrClip emptyClip;
70 emptyClip.setEmpty();
71
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000072 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000073 emptyBound.setEmpty();
74
75 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +000076 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000077
78 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000079 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000080 bound1.set(0, 0, 100, 100);
81
82 GrClip clip1;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000083 clip1.setFromIRect(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000084
robertphillips@google.com75b3c962012-06-07 12:08:45 +000085 GrTextureDesc desc;
86 desc.fFlags = kRenderTarget_GrTextureFlagBit;
87 desc.fWidth = X_SIZE;
88 desc.fHeight = Y_SIZE;
89 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000090
robertphillips@google.comf105b102012-05-14 12:18:26 +000091 cache.acquireMask(clip1, desc, bound1);
92
93 GrTexture* texture1 = cache.getLastMask();
94 REPORTER_ASSERT(reporter, texture1);
95 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000096 return;
97 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000098
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000099 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000100 check_state(reporter, cache, clip1, texture1, bound1);
101 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000102
103 // push the state
104 cache.push();
105
106 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000107 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000108 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000109
110 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000111 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000112 bound2.set(-10, -10, 10, 10);
113
114 GrClip clip2;
115 clip2.setEmpty();
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000116 clip2.setFromIRect(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000117
robertphillips@google.comf105b102012-05-14 12:18:26 +0000118 cache.acquireMask(clip2, desc, bound2);
119
120 GrTexture* texture2 = cache.getLastMask();
121 REPORTER_ASSERT(reporter, texture2);
122 if (NULL == texture2) {
123 return;
124 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000125
126 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000127 check_state(reporter, cache, clip2, texture2, bound2);
128 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
129 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000130
131 // check to make sure canReuse works
132 REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10));
133 REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10));
134
135 // pop the state
136 cache.pop();
137
138 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000139 check_state(reporter, cache, clip1, texture1, bound1);
140 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
141 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000142
143 // manually clear the state
144 cache.reset();
145
146 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000147 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000148 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
149 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000150
151 // pop again - so there is no state
152 cache.pop();
153
154#if !defined(SK_DEBUG)
155 // verify that the getters don't crash
156 // only do in release since it generates asserts in debug
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000157 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000158#endif
robertphillips@google.comf105b102012-05-14 12:18:26 +0000159 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
160 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000161}
162
163////////////////////////////////////////////////////////////////////////////////
164static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
165
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000166 test_cache(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000167}
168
169////////////////////////////////////////////////////////////////////////////////
170#include "TestClassDef.h"
171DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)