blob: 84564c4489d9df3f6b41e45f4541186155884970 [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"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00009#include "TestClassDef.h"
bsalomon@google.coma68937c2012-08-03 15:00:52 +000010// This is a GR test
11#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +000012#include "GrContextFactory.h"
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000013#include "SkGpuDevice.h"
14#include "../../src/gpu/GrClipMaskManager.h"
15
16static const int X_SIZE = 12;
17static const int Y_SIZE = 12;
18
19////////////////////////////////////////////////////////////////////////////////
caryclark@google.com42639cd2012-06-06 12:03:39 +000020// note: this is unused
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000021static GrTexture* createTexture(GrContext* context) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000022 unsigned char textureData[X_SIZE][Y_SIZE][4];
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000023
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000024 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000025
26 GrTextureDesc desc;
27
28 // let Skia know we will be using this texture as a render target
29 desc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000030 desc.fConfig = kSkia8888_GrPixelConfig;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000031 desc.fWidth = X_SIZE;
32 desc.fHeight = Y_SIZE;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000033
34 // We are initializing the texture with zeros here
35 GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
36 if (!texture) {
37 return NULL;
38 }
39
40 return texture;
41}
42
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000043// Ensure that the 'getConservativeBounds' calls are returning bounds clamped
44// to the render target
45static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) {
46
47 static const int kXSize = 100;
48 static const int kYSize = 100;
49
50 GrTextureDesc desc;
51 desc.fFlags = kRenderTarget_GrTextureFlagBit;
52 desc.fConfig = kAlpha_8_GrPixelConfig;
53 desc.fWidth = kXSize;
54 desc.fHeight = kYSize;
55
56 GrTexture* texture = context->createUncachedTexture(desc, NULL, 0);
57 if (!texture) {
58 return;
59 }
60
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000061 SkAutoUnref au(texture);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000062
63 SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
robertphillips@google.comb755a2a2013-03-04 14:59:55 +000064 SkRect screen;
skia.committer@gmail.com075b0892013-03-05 07:09:08 +000065
robertphillips@google.comb755a2a2013-03-04 14:59:55 +000066 screen = SkRect::MakeWH(SkIntToScalar(kXSize),
67 SkIntToScalar(kYSize));
68
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000069 SkRect clipRect(screen);
70 clipRect.outset(10, 10);
71
72 // create a clip stack that will (trivially) reduce to a single rect that
73 // is larger than the screen
74 SkClipStack stack;
75 stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false);
76
77 bool isIntersectionOfRects = true;
robertphillips@google.com7b112892012-07-31 15:18:21 +000078 SkRect devStackBounds;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000079
rmistry@google.comd6176b02012-08-23 18:14:13 +000080 stack.getConservativeBounds(0, 0, kXSize, kYSize,
81 &devStackBounds,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000082 &isIntersectionOfRects);
83
84 // make sure that the SkClipStack is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000085 REPORTER_ASSERT(reporter, screen == devStackBounds);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000086 REPORTER_ASSERT(reporter, isIntersectionOfRects);
87
robertphillips@google.com641f8b12012-07-31 19:15:58 +000088 // wrap the SkClipStack in a GrClipData
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000089 GrClipData clipData;
robertphillips@google.com641f8b12012-07-31 19:15:58 +000090 clipData.fClipStack = &stack;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000091
robertphillips@google.com7b112892012-07-31 15:18:21 +000092 SkIRect devGrClipDataBound;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000093 clipData.getConservativeBounds(texture,
robertphillips@google.com7b112892012-07-31 15:18:21 +000094 &devGrClipDataBound,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000095 &isIntersectionOfRects);
96
97 // make sure that GrClipData is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000098 REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000099 REPORTER_ASSERT(reporter, isIntersectionOfRects);
100}
101
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000102////////////////////////////////////////////////////////////////////////////////
103// verify that the top state of the stack matches the passed in state
104static void check_state(skiatest::Reporter* reporter,
105 const GrClipMaskCache& cache,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000106 const SkClipStack& clip,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000107 GrTexture* mask,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000108 const SkIRect& bound) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000109 REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000110
111 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
112
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000113 SkIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000114 cache.getLastBound(&cacheBound);
115 REPORTER_ASSERT(reporter, bound == cacheBound);
116}
117
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000118static void check_empty_state(skiatest::Reporter* reporter,
119 const GrClipMaskCache& cache) {
120 REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID == cache.getLastClipGenID());
121 REPORTER_ASSERT(reporter, NULL == cache.getLastMask());
122
123 SkIRect emptyBound;
124 emptyBound.setEmpty();
125
126 SkIRect cacheBound;
127 cache.getLastBound(&cacheBound);
128 REPORTER_ASSERT(reporter, emptyBound == cacheBound);
129}
130
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000131////////////////////////////////////////////////////////////////////////////////
132// basic test of the cache's base functionality:
133// push, pop, set, canReuse & getters
134static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
135
caryclark@google.com42639cd2012-06-06 12:03:39 +0000136 if (false) { // avoid bit rot, suppress warning
rmistry@google.comd6176b02012-08-23 18:14:13 +0000137 createTexture(context);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000138 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000139 GrClipMaskCache cache;
140
robertphillips@google.comf105b102012-05-14 12:18:26 +0000141 cache.setContext(context);
142
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000143 // check initial state
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000144 check_empty_state(reporter, cache);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000145
146 // set the current state
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000147 SkIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000148 bound1.set(0, 0, 100, 100);
149
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000150 SkClipStack clip1(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000151
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000152 GrTextureDesc desc;
153 desc.fFlags = kRenderTarget_GrTextureFlagBit;
154 desc.fWidth = X_SIZE;
155 desc.fHeight = Y_SIZE;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000156 desc.fConfig = kSkia8888_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000157
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000158 cache.acquireMask(clip1.getTopmostGenID(), desc, bound1);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000159
160 GrTexture* texture1 = cache.getLastMask();
161 REPORTER_ASSERT(reporter, texture1);
162 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000163 return;
164 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000165
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000166 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000167 check_state(reporter, cache, clip1, texture1, bound1);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000168 REPORTER_ASSERT(reporter, texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000169
170 // push the state
171 cache.push();
172
173 // verify that the pushed state is initially empty
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000174 check_empty_state(reporter, cache);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000175 REPORTER_ASSERT(reporter, texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000176
177 // modify the new state
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000178 SkIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000179 bound2.set(-10, -10, 10, 10);
180
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000181 SkClipStack clip2(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000182
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000183 cache.acquireMask(clip2.getTopmostGenID(), desc, bound2);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000184
185 GrTexture* texture2 = cache.getLastMask();
186 REPORTER_ASSERT(reporter, texture2);
187 if (NULL == texture2) {
188 return;
189 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000190
191 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000192 check_state(reporter, cache, clip2, texture2, bound2);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000193 REPORTER_ASSERT(reporter, texture1->getRefCnt());
194 REPORTER_ASSERT(reporter, texture2->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000195
196 // check to make sure canReuse works
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000197 REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2));
198 REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1));
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000199
200 // pop the state
201 cache.pop();
202
203 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000204 check_state(reporter, cache, clip1, texture1, bound1);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000205 REPORTER_ASSERT(reporter, texture1->getRefCnt());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000206
207 // manually clear the state
208 cache.reset();
209
210 // verify it is now empty
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000211 check_empty_state(reporter, cache);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000212
213 // pop again - so there is no state
214 cache.pop();
215
216#if !defined(SK_DEBUG)
217 // verify that the getters don't crash
218 // only do in release since it generates asserts in debug
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000219 check_empty_state(reporter, cache);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000220#endif
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000221}
222
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000223DEF_GPUTEST(ClipCache, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000224 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
225 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
226 if (!GrContextFactory::IsRenderingGLContext(glType)) {
227 continue;
228 }
229 GrContext* context = factory->get(glType);
230 if (NULL == context) {
231 continue;
232 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000233
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000234 test_cache(reporter, context);
235 test_clip_bounds(reporter, context);
236 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000237}
238
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000239#endif