blob: a8b220d1c43e36f42406f4b183a754bb8c7f1b48 [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,
45 const GrRect& 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
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
robertphillips@google.com8fff3562012-05-11 12:53:50 +000071 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000072
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));
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000081 REPORTER_ASSERT(reporter, texture.get());
82
83 if (NULL == texture.get()) {
84 return;
85 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000086
robertphillips@google.com8fff3562012-05-11 12:53:50 +000087 cache.set(clip1, texture.get(), bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000088
89 // check that the set took
robertphillips@google.com8fff3562012-05-11 12:53:50 +000090 check_state(reporter, cache, clip1, texture.get(), bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000091 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
robertphillips@google.com8fff3562012-05-11 12:53:50 +000097 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000098 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
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000108 cache.set(clip2, texture.get(), bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000109
110 // check that the changes took
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000111 check_state(reporter, cache, clip2, texture.get(), bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000112 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
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000122 check_state(reporter, cache, clip1, texture.get(), bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000123 REPORTER_ASSERT(reporter, 2 == texture.get()->getRefCnt());
124
125 // manually clear the state
126 cache.reset();
127
128 // verify it is now empty
robertphillips@google.com8fff3562012-05-11 12:53:50 +0000129 check_state(reporter, cache, emptyClip, NULL, emptyBound);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000130 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
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000146 test_cache(reporter, context);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000147}
148
149////////////////////////////////////////////////////////////////////////////////
150#include "TestClassDef.h"
151DEFINE_GPUTESTCLASS("ClipCache", ClipCacheTestClass, TestClipCache)