blob: b0496e3d77a787d8b4fed6a8c510169e4f8ef323 [file] [log] [blame]
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +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 "gm.h"
bsalomon@google.com707bd602014-03-04 16:52:20 +00009#if SK_SUPPORT_GPU
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000010#include "GrTest.h"
bsalomon@google.com707bd602014-03-04 16:52:20 +000011#include "effects/GrRRectEffect.h"
12#endif
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000013#include "SkDevice.h"
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000014#include "SkRRect.h"
15
16namespace skiagm {
17
18///////////////////////////////////////////////////////////////////////////////
19
20class RRectGM : public GM {
21public:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000022 enum Type {
23 kBW_Draw_Type,
24 kAA_Draw_Type,
25 kBW_Clip_Type,
26 kAA_Clip_Type,
27 kEffect_Type,
28 };
29 RRectGM(Type type) : fType(type) {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000030 this->setBGColor(0xFFDDDDDD);
31 this->setUpRRects();
32 }
33
34protected:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000035 SkString onShortName() SK_OVERRIDE {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000036 SkString name("rrect");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000037 switch (fType) {
38 case kBW_Draw_Type:
39 name.append("_draw_bw");
40 break;
41 case kAA_Draw_Type:
42 name.append("_draw_aa");
43 break;
44 case kBW_Clip_Type:
45 name.append("_clip_bw");
46 break;
47 case kAA_Clip_Type:
48 name.append("_clip_aa");
49 break;
50 case kEffect_Type:
51 name.append("_effect");
52 break;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000053 }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000054 return name;
55 }
56
tfarinaf5393182014-06-09 23:59:03 -070057 virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(kImageWidth, kImageHeight); }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000058
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000059 virtual uint32_t onGetFlags() const SK_OVERRIDE {
60 if (kEffect_Type == fType) {
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000061 return kGPUOnly_Flag | kSkipTiled_Flag;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000062 } else {
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000063 return kSkipTiled_Flag;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000064 }
65 }
66
67 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
bsalomon@google.com707bd602014-03-04 16:52:20 +000068#if SK_SUPPORT_GPU
reed@google.com9c135db2014-03-12 18:28:35 +000069 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
70 GrContext* context = rt ? rt->getContext() : NULL;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000071 if (kEffect_Type == fType && NULL == context) {
72 return;
73 }
bsalomon@google.com707bd602014-03-04 16:52:20 +000074#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000075
76 SkPaint paint;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000077 if (kAA_Draw_Type == fType) {
78 paint.setAntiAlias(true);
79 }
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000080
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000081 static const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX),
82 SkIntToScalar(kTileY));
83#ifdef SK_DEBUG
84 static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
85 SkIntToScalar(kImageHeight));
86#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000087
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000088#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -070089 int lastEdgeType = (kEffect_Type == fType) ? kLast_GrProcessorEdgeType: 0;
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000090#else
91 int lastEdgeType = 0;
92#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000093
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000094 int y = 1;
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000095 for (int et = 0; et <= lastEdgeType; ++et) {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000096 int x = 1;
97 for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) {
98 bool drew = true;
99#ifdef SK_DEBUG
100 SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds()));
101 SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
102 imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
103 SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
104#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000105 canvas->save();
106 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000107 if (kEffect_Type == fType) {
bsalomon@google.com707bd602014-03-04 16:52:20 +0000108#if SK_SUPPORT_GPU
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000109 GrTestTarget tt;
110 context->getTestTarget(&tt);
111 if (NULL == tt.target()) {
112 SkDEBUGFAIL("Couldn't get Gr test target.");
113 return;
114 }
115 GrDrawState* drawState = tt.target()->drawState();
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000116
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000117 SkRRect rrect = fRRects[curRRect];
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000118 rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
joshualittb0a8a372014-09-23 09:50:21 -0700119 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
120 SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType,
121 rrect));
122 if (fp) {
123 drawState->addCoverageProcessor(fp);
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000124 drawState->setIdentityViewMatrix();
125 drawState->setRenderTarget(rt);
126 drawState->setColor(0xff000000);
127
128 SkRect bounds = rrect.getBounds();
129 bounds.outset(2.f, 2.f);
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000130
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000131 tt.target()->drawSimpleRect(bounds);
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000132 } else {
133 drew = false;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000134 }
bsalomon@google.com707bd602014-03-04 16:52:20 +0000135#endif
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000136 } else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) {
137 bool aaClip = (kAA_Clip_Type == fType);
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000138 canvas->clipRRect(fRRects[curRRect], SkRegion::kReplace_Op, aaClip);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000139 canvas->drawRect(kMaxTileBound, paint);
140 } else {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000141 canvas->drawRRect(fRRects[curRRect], paint);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000142 }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000143 canvas->restore();
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000144 if (drew) {
145 x = x + kTileX;
146 if (x > kImageWidth) {
147 x = 1;
148 y += kTileY;
149 }
150 }
151 }
152 if (x != 1) {
153 y += kTileY;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000154 }
155 }
156 }
157
158 void setUpRRects() {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000159 // each RRect must fit in a 0x0 -> (kTileX-2)x(kTileY-2) block. These will be tiled across
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000160 // the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA.
161
162 // simple cases
163 fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2));
164 fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2));
165 fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000166 fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5);
167 // small circular corners are an interesting test case for gpu clipping
168 fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1);
169 fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f);
170 fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000171
172 // The first complex case needs special handling since it is a square
173 fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]);
mike@reedtribe.orgf6100c82012-12-24 13:56:17 +0000174 for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000175 fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]);
176 }
177 }
178
179private:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000180 Type fType;
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000181
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000182 static const int kImageWidth = 640;
183 static const int kImageHeight = 480;
184
185 static const int kTileX = 80;
186 static const int kTileY = 40;
187
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000188 static const int kNumSimpleCases = 7;
commit-bot@chromium.org04eff722014-03-24 14:53:09 +0000189 static const int kNumComplexCases = 35;
robertphillips@google.com5683d422012-12-17 21:58:02 +0000190 static const SkVector gRadii[kNumComplexCases][4];
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000191
192 static const int kNumRRects = kNumSimpleCases + kNumComplexCases;
193 SkRRect fRRects[kNumRRects];
194
195 typedef GM INHERITED;
196};
197
198// Radii for the various test cases. Order is UL, UR, LR, LL
199const SkVector RRectGM::gRadii[kNumComplexCases][4] = {
200 // a circle
201 { { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } },
202
203 // odd ball cases
204 { { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } },
205 { { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } },
206 { { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } },
207
208 // UL
209 { { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
210 { { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
211 { { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
212
213 // UR
214 { { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } },
215 { { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } },
216 { { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } },
217
218 // LR
219 { { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } },
220 { { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } },
221 { { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } },
222
223 // LL
224 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } },
225 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } },
226 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } },
227
228 // over-sized radii
229 { { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } },
230 { { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } },
231 { { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } },
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000232
233 // circular corner tabs
234 { { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } },
235 { { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } },
236 { { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } },
237 { { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } },
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000238
commit-bot@chromium.org04eff722014-03-24 14:53:09 +0000239 // small radius circular corner tabs
240 { { 0, 0 }, { 0.2f, 0.2f }, { 0.2f, 0.2f }, { 0, 0 } },
241 { { 0.3f, 0.3f }, { 0.3f, .3f }, { 0, 0 }, { 0, 0 } },
242
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000243 // single circular corner cases
244 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 15 } },
245 { { 0, 0 }, { 0, 0 }, { 15, 15 }, { 0, 0 } },
246 { { 0, 0 }, { 15, 15 }, { 0, 0 }, { 0, 0 } },
247 { { 15, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000248
249 // nine patch elliptical
250 { { 5, 7 }, { 8, 7 }, { 8, 12 }, { 5, 12 } },
251 { { 0, 7 }, { 8, 7 }, { 8, 12 }, { 0, 12 } },
commit-bot@chromium.org04eff722014-03-24 14:53:09 +0000252
253 // nine patch elliptical, small radii
254 { { 0.4f, 7 }, { 8, 7 }, { 8, 12 }, { 0.4f, 12 } },
255 { { 0.4f, 0.4f }, { 8, 0.4f }, { 8, 12 }, { 0.4f, 12 } },
256 { { 20, 0.4f }, { 18, 0.4f }, { 18, 0.4f }, { 20, 0.4f } },
257 { { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f } },
258
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000259};
260
261///////////////////////////////////////////////////////////////////////////////
262
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000263DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); )
264DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); )
265DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); )
266DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); )
bsalomon@google.com707bd602014-03-04 16:52:20 +0000267#if SK_SUPPORT_GPU
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000268DEF_GM( return new RRectGM(RRectGM::kEffect_Type); )
bsalomon@google.com707bd602014-03-04 16:52:20 +0000269#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000270
271}