blob: cc76718b0a5f114c98a2b2a7bbc18f1910ffe74b [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
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000057 virtual SkISize onISize() SK_OVERRIDE { return make_isize(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) {
61 return kGPUOnly_Flag;
62 } else {
63 return 0;
64 }
65 }
66
67 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
bsalomon@google.com707bd602014-03-04 16:52:20 +000068#if SK_SUPPORT_GPU
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000069 SkBaseDevice* device = canvas->getTopDevice();
70 GrContext* context = NULL;
71 GrRenderTarget* rt = device->accessRenderTarget();
72 if (NULL != rt) {
73 context = rt->getContext();
74 }
75 if (kEffect_Type == fType && NULL == context) {
76 return;
77 }
bsalomon@google.com707bd602014-03-04 16:52:20 +000078#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000079
80 SkPaint paint;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000081 if (kAA_Draw_Type == fType) {
82 paint.setAntiAlias(true);
83 }
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000084
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000085 static const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX),
86 SkIntToScalar(kTileY));
87#ifdef SK_DEBUG
88 static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
89 SkIntToScalar(kImageHeight));
90#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000091
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000092#if SK_SUPPORT_GPU
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000093 int lastEdgeType = (kEffect_Type == fType) ? kLast_GrEffectEdgeType: 0;
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000094#else
95 int lastEdgeType = 0;
96#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000097
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000098 int y = 1;
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000099 for (int et = 0; et <= lastEdgeType; ++et) {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000100 int x = 1;
101 for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) {
102 bool drew = true;
103#ifdef SK_DEBUG
104 SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds()));
105 SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
106 imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
107 SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
108#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000109 canvas->save();
110 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000111 if (kEffect_Type == fType) {
bsalomon@google.com707bd602014-03-04 16:52:20 +0000112#if SK_SUPPORT_GPU
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000113 GrTestTarget tt;
114 context->getTestTarget(&tt);
115 if (NULL == tt.target()) {
116 SkDEBUGFAIL("Couldn't get Gr test target.");
117 return;
118 }
119 GrDrawState* drawState = tt.target()->drawState();
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000120
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000121 SkRRect rrect = fRRects[curRRect];
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000122 rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000123 GrEffectEdgeType edgeType = (GrEffectEdgeType) et;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000124 SkAutoTUnref<GrEffectRef> effect(GrRRectEffect::Create(edgeType, rrect));
125 if (effect) {
126 drawState->addCoverageEffect(effect);
127 drawState->setIdentityViewMatrix();
128 drawState->setRenderTarget(rt);
129 drawState->setColor(0xff000000);
130
131 SkRect bounds = rrect.getBounds();
132 bounds.outset(2.f, 2.f);
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000133
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000134 tt.target()->drawSimpleRect(bounds);
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000135 } else {
136 drew = false;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000137 }
bsalomon@google.com707bd602014-03-04 16:52:20 +0000138#endif
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000139 } else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) {
140 bool aaClip = (kAA_Clip_Type == fType);
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000141 canvas->clipRRect(fRRects[curRRect], SkRegion::kReplace_Op, aaClip);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000142 canvas->drawRect(kMaxTileBound, paint);
143 } else {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000144 canvas->drawRRect(fRRects[curRRect], paint);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000145 }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000146 canvas->restore();
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000147 if (drew) {
148 x = x + kTileX;
149 if (x > kImageWidth) {
150 x = 1;
151 y += kTileY;
152 }
153 }
154 }
155 if (x != 1) {
156 y += kTileY;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000157 }
158 }
159 }
160
161 void setUpRRects() {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000162 // 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 +0000163 // the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA.
164
165 // simple cases
166 fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2));
167 fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2));
168 fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000169 fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5);
170 // small circular corners are an interesting test case for gpu clipping
171 fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1);
172 fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f);
173 fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000174
175 // The first complex case needs special handling since it is a square
176 fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]);
mike@reedtribe.orgf6100c82012-12-24 13:56:17 +0000177 for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000178 fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]);
179 }
180 }
181
182private:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000183 Type fType;
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000184
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000185 static const int kImageWidth = 640;
186 static const int kImageHeight = 480;
187
188 static const int kTileX = 80;
189 static const int kTileY = 40;
190
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000191 static const int kNumSimpleCases = 7;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000192 static const int kNumComplexCases = 23;
robertphillips@google.com5683d422012-12-17 21:58:02 +0000193 static const SkVector gRadii[kNumComplexCases][4];
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000194
195 static const int kNumRRects = kNumSimpleCases + kNumComplexCases;
196 SkRRect fRRects[kNumRRects];
197
198 typedef GM INHERITED;
199};
200
201// Radii for the various test cases. Order is UL, UR, LR, LL
202const SkVector RRectGM::gRadii[kNumComplexCases][4] = {
203 // a circle
204 { { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } },
205
206 // odd ball cases
207 { { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } },
208 { { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } },
209 { { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } },
210
211 // UL
212 { { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
213 { { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
214 { { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
215
216 // UR
217 { { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } },
218 { { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } },
219 { { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } },
220
221 // LR
222 { { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } },
223 { { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } },
224 { { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } },
225
226 // LL
227 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } },
228 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } },
229 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } },
230
231 // over-sized radii
232 { { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } },
233 { { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } },
234 { { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } },
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000235
236 // circular corner tabs
237 { { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } },
238 { { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } },
239 { { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } },
240 { { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } },
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000241};
242
243///////////////////////////////////////////////////////////////////////////////
244
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000245DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); )
246DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); )
247DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); )
248DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); )
bsalomon@google.com707bd602014-03-04 16:52:20 +0000249#if SK_SUPPORT_GPU
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000250DEF_GM( return new RRectGM(RRectGM::kEffect_Type); )
bsalomon@google.com707bd602014-03-04 16:52:20 +0000251#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000252
253}