blob: c0ff7632c73749ada196c6a2579716ce731cc5d5 [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"
bsalomon@google.coma68937c2012-08-03 15:00:52 +00009// This is a GR test
10#if SK_SUPPORT_GPU
mtklein7c348a82015-01-14 09:25:01 -080011#include "GrClipMaskManager.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000012#include "GrContextFactory.h"
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000013#include "SkGpuDevice.h"
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000014
15static const int X_SIZE = 12;
16static const int Y_SIZE = 12;
17
18////////////////////////////////////////////////////////////////////////////////
caryclark@google.com42639cd2012-06-06 12:03:39 +000019// note: this is unused
bsalomonedd77a12015-05-29 09:45:57 -070020static GrTexture* create_texture(GrContext* context) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000021 unsigned char textureData[X_SIZE][Y_SIZE][4];
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000022
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +000023 memset(textureData, 0, 4* X_SIZE * Y_SIZE);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000024
bsalomonf2703d82014-10-28 14:33:06 -070025 GrSurfaceDesc desc;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000026
27 // let Skia know we will be using this texture as a render target
bsalomonf2703d82014-10-28 14:33:06 -070028 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000029 desc.fConfig = kSkia8888_GrPixelConfig;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000030 desc.fWidth = X_SIZE;
31 desc.fHeight = Y_SIZE;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000032
33 // We are initializing the texture with zeros here
bsalomond309e7a2015-04-30 14:18:54 -070034 GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +000035 if (!texture) {
36 return NULL;
37 }
38
39 return texture;
40}
41
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000042// Ensure that the 'getConservativeBounds' calls are returning bounds clamped
43// to the render target
44static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) {
45
46 static const int kXSize = 100;
47 static const int kYSize = 100;
48
bsalomonf2703d82014-10-28 14:33:06 -070049 GrSurfaceDesc desc;
50 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000051 desc.fConfig = kAlpha_8_GrPixelConfig;
52 desc.fWidth = kXSize;
53 desc.fHeight = kYSize;
54
bsalomond309e7a2015-04-30 14:18:54 -070055 GrTexture* texture = context->textureProvider()->createTexture(desc, false, NULL, 0);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000056 if (!texture) {
57 return;
58 }
59
bsalomondcabb052014-07-21 14:24:01 -070060 SkAutoTUnref<GrTexture> au(texture);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000061
62 SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize);
robertphillips@google.comb755a2a2013-03-04 14:59:55 +000063 SkRect screen;
skia.committer@gmail.com075b0892013-03-05 07:09:08 +000064
robertphillips@google.comb755a2a2013-03-04 14:59:55 +000065 screen = SkRect::MakeWH(SkIntToScalar(kXSize),
66 SkIntToScalar(kYSize));
67
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000068 SkRect clipRect(screen);
69 clipRect.outset(10, 10);
70
71 // create a clip stack that will (trivially) reduce to a single rect that
72 // is larger than the screen
73 SkClipStack stack;
74 stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false);
75
76 bool isIntersectionOfRects = true;
robertphillips@google.com7b112892012-07-31 15:18:21 +000077 SkRect devStackBounds;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000078
rmistry@google.comd6176b02012-08-23 18:14:13 +000079 stack.getConservativeBounds(0, 0, kXSize, kYSize,
80 &devStackBounds,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000081 &isIntersectionOfRects);
82
83 // make sure that the SkClipStack is behaving itself
robertphillips@google.com7b112892012-07-31 15:18:21 +000084 REPORTER_ASSERT(reporter, screen == devStackBounds);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000085 REPORTER_ASSERT(reporter, isIntersectionOfRects);
86
joshualitt570d2f82015-02-25 13:19:48 -080087 // wrap the SkClipStack in a GrClip
joshualitt44701df2015-02-23 14:44:57 -080088 GrClip clipData;
89 clipData.setClipStack(&stack);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000090
joshualitt570d2f82015-02-25 13:19:48 -080091 SkIRect devGrClipBound;
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000092 clipData.getConservativeBounds(texture,
joshualitt570d2f82015-02-25 13:19:48 -080093 &devGrClipBound,
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000094 &isIntersectionOfRects);
95
joshualitt570d2f82015-02-25 13:19:48 -080096 // make sure that GrClip is behaving itself
97 REPORTER_ASSERT(reporter, intScreen == devGrClipBound);
robertphillips@google.comc23a63b2012-07-31 11:47:29 +000098 REPORTER_ASSERT(reporter, isIntersectionOfRects);
99}
100
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000101////////////////////////////////////////////////////////////////////////////////
102// verify that the top state of the stack matches the passed in state
103static void check_state(skiatest::Reporter* reporter,
104 const GrClipMaskCache& cache,
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000105 const SkClipStack& clip,
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000106 GrTexture* mask,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000107 const SkIRect& bound) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000108 REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID());
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000109
110 REPORTER_ASSERT(reporter, mask == cache.getLastMask());
111
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000112 SkIRect cacheBound;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000113 cache.getLastBound(&cacheBound);
114 REPORTER_ASSERT(reporter, bound == cacheBound);
115}
116
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000117static void check_empty_state(skiatest::Reporter* reporter,
118 const GrClipMaskCache& cache) {
119 REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID == cache.getLastClipGenID());
120 REPORTER_ASSERT(reporter, NULL == cache.getLastMask());
121
122 SkIRect emptyBound;
123 emptyBound.setEmpty();
124
125 SkIRect cacheBound;
126 cache.getLastBound(&cacheBound);
127 REPORTER_ASSERT(reporter, emptyBound == cacheBound);
128}
129
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000130////////////////////////////////////////////////////////////////////////////////
131// basic test of the cache's base functionality:
132// push, pop, set, canReuse & getters
133static void test_cache(skiatest::Reporter* reporter, GrContext* context) {
134
caryclark@google.com42639cd2012-06-06 12:03:39 +0000135 if (false) { // avoid bit rot, suppress warning
bsalomonedd77a12015-05-29 09:45:57 -0700136 create_texture(context);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000137 }
bsalomonedd77a12015-05-29 09:45:57 -0700138 GrClipMaskCache cache(context->resourceProvider());
robertphillips@google.comf105b102012-05-14 12:18:26 +0000139
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000140 // check initial state
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000141 check_empty_state(reporter, cache);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000142
143 // set the current state
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000144 SkIRect bound1;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000145 bound1.set(0, 0, 100, 100);
146
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000147 SkClipStack clip1(bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000148
bsalomonf2703d82014-10-28 14:33:06 -0700149 GrSurfaceDesc desc;
150 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000151 desc.fWidth = X_SIZE;
152 desc.fHeight = Y_SIZE;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000153 desc.fConfig = kSkia8888_GrPixelConfig;
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000154
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000155 cache.acquireMask(clip1.getTopmostGenID(), desc, bound1);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000156
157 GrTexture* texture1 = cache.getLastMask();
158 REPORTER_ASSERT(reporter, texture1);
159 if (NULL == texture1) {
robertphillips@google.comd82f3fa2012-05-10 21:26:48 +0000160 return;
161 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000162
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000163 // check that the set took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000164 check_state(reporter, cache, clip1, texture1, bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000165
166 // push the state
167 cache.push();
168
169 // verify that the pushed state is initially empty
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000170 check_empty_state(reporter, cache);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000171
172 // modify the new state
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000173 SkIRect bound2;
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000174 bound2.set(-10, -10, 10, 10);
175
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000176 SkClipStack clip2(bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000177
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000178 cache.acquireMask(clip2.getTopmostGenID(), desc, bound2);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000179
180 GrTexture* texture2 = cache.getLastMask();
181 REPORTER_ASSERT(reporter, texture2);
182 if (NULL == texture2) {
183 return;
184 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000185
186 // check that the changes took
robertphillips@google.comf105b102012-05-14 12:18:26 +0000187 check_state(reporter, cache, clip2, texture2, bound2);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000188
189 // check to make sure canReuse works
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000190 REPORTER_ASSERT(reporter, cache.canReuse(clip2.getTopmostGenID(), bound2));
191 REPORTER_ASSERT(reporter, !cache.canReuse(clip1.getTopmostGenID(), bound1));
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000192
193 // pop the state
194 cache.pop();
195
196 // verify that the old state is restored
robertphillips@google.comf105b102012-05-14 12:18:26 +0000197 check_state(reporter, cache, clip1, texture1, bound1);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000198
199 // manually clear the state
200 cache.reset();
201
202 // verify it is now empty
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000203 check_empty_state(reporter, cache);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000204
205 // pop again - so there is no state
206 cache.pop();
207
208#if !defined(SK_DEBUG)
209 // verify that the getters don't crash
210 // only do in release since it generates asserts in debug
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000211 check_empty_state(reporter, cache);
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000212#endif
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000213}
214
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000215DEF_GPUTEST(ClipCache, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000216 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
217 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
218 if (!GrContextFactory::IsRenderingGLContext(glType)) {
219 continue;
220 }
221 GrContext* context = factory->get(glType);
222 if (NULL == context) {
223 continue;
224 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000225
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000226 test_cache(reporter, context);
227 test_clip_bounds(reporter, context);
228 }
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000229}
230
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000231#endif