blob: ae3337e74007a24f3a3c6e1a6b1da26bcc324401 [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;
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,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000044 const GrClip& clip,
45 GrTexture* mask,
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000046 const GrIRect& bound) {
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000047 GrClip cacheClip;
48 cache.getLastClip(&cacheClip);
49 REPORTER_ASSERT(reporter, clip == cacheClip);
50
51 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
52
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000053 GrIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000054 cache.getLastBound(&cacheBound);
55 REPORTER_ASSERT(reporter, bound == cacheBound);
56}
57
58////////////////////////////////////////////////////////////////////////////////
59// basic test of the cache's base functionality:
60// push, pop, set, canReuse & getters
61static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
62
caryclark@google.com42639cd2012-06-06 12:03:39 +000063 if (false) { // avoid bit rot, suppress warning
64 createTexture(context);
65 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000066 GrClipMaskCache cache;
67
robertphillips@google.comf105b102012-05-14 12:18:26 +000068 cache.setContext(context);
69
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000070 GrClip emptyClip;
71 emptyClip.setEmpty();
72
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000073 GrIRect emptyBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000074 emptyBound.setEmpty();
75
76 // check initial state
robertphillips@google.com8fff3562012-05-11 12:53:50 +000077 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000078
79 // set the current state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000080 GrIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000081 bound1.set(0, 0, 100, 100);
82
83 GrClip clip1;
robertphillips@google.com6623fcd2012-05-15 16:47:23 +000084 clip1.setFromIRect(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000085
robertphillips@google.comf105b102012-05-14 12:18:26 +000086 const GrTextureDesc desc = {
87 kRenderTarget_GrTextureFlagBit,
88 X_SIZE,
89 Y_SIZE,
90 kSkia8888_PM_GrPixelConfig,
91 0
92 };
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000093
robertphillips@google.comf105b102012-05-14 12:18:26 +000094 cache.acquireMask(clip1, desc, bound1);
95
96 GrTexture* texture1 = cache.getLastMask();
97 REPORTER_ASSERT(reporter, texture1);
98 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000099 return;
100 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000101
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000102 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000103 check_state(reporter, cache, clip1, texture1, bound1);
104 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000105
106 // push the state
107 cache.push();
108
109 // verify that the pushed state is initially empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000110 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000111 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000112
113 // modify the new state
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000114 GrIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000115 bound2.set(-10, -10, 10, 10);
116
117 GrClip clip2;
118 clip2.setEmpty();
robertphillips@google.com6623fcd2012-05-15 16:47:23 +0000119 clip2.setFromIRect(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000120
robertphillips@google.comf105b102012-05-14 12:18:26 +0000121 cache.acquireMask(clip2, desc, bound2);
122
123 GrTexture* texture2 = cache.getLastMask();
124 REPORTER_ASSERT(reporter, texture2);
125 if (NULL == texture2) {
126 return;
127 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000128
129 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000130 check_state(reporter, cache, clip2, texture2, bound2);
131 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
132 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000133
134 // check to make sure canReuse works
135 REPORTER_ASSERT(reporter, cache.canReuse(clip2, 10, 10));
136 REPORTER_ASSERT(reporter, !cache.canReuse(clip1, 10, 10));
137
138 // pop the state
139 cache.pop();
140
141 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000142 check_state(reporter, cache, clip1, texture1, bound1);
143 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
144 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000145
146 // manually clear the state
147 cache.reset();
148
149 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000150 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000151 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
152 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000153
154 // pop again - so there is no state
155 cache.pop();
156
157#if !defined(SK_DEBUG)
158 // verify that the getters don't crash
159 // only do in release since it generates asserts in debug
robertphillips@google.comf21c7042012-05-11 13:01:16 +0000160 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000161#endif
robertphillips@google.comf105b102012-05-14 12:18:26 +0000162 REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt());
163 REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000164}
165
166////////////////////////////////////////////////////////////////////////////////
167static void TestClipCache(skiatest::Reporter* reporter, GrContext* context) {
168
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000169 test_cache(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000170}
171
172////////////////////////////////////////////////////////////////////////////////
173#include "TestClassDef.h"
174DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)