blob: 23f576b5e946aa74538fcba6cc9ac0b41e3c3d6c [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
bsalomon@google.com707bd602014-03-04 16:52:20 +000010#if SK_SUPPORT_GPU
joshualitt04194f32016-01-13 10:08:27 -080011#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContextPriv.h"
bsalomon@google.com707bd602014-03-04 16:52:20 +000013#include "effects/GrRRectEffect.h"
Brian Salomon89527432016-12-16 09:52:16 -050014#include "ops/GrDrawOp.h"
Brian Salomonac70f842017-05-08 10:43:33 -040015#include "ops/GrNonAAFillRectOp.h"
bsalomon@google.com707bd602014-03-04 16:52:20 +000016#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000017#include "SkRRect.h"
18
19namespace skiagm {
20
21///////////////////////////////////////////////////////////////////////////////
22
23class RRectGM : public GM {
24public:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000025 enum Type {
26 kBW_Draw_Type,
27 kAA_Draw_Type,
28 kBW_Clip_Type,
29 kAA_Clip_Type,
30 kEffect_Type,
31 };
herbb10fe492016-01-08 13:48:43 -080032 RRectGM(Type type) : fType(type) { }
33
34protected:
35
36 void onOnceBeforeDraw() override {
caryclark65cdba62015-06-15 06:51:08 -070037 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000038 this->setUpRRects();
39 }
40
mtklein36352bf2015-03-25 18:17:31 -070041 SkString onShortName() override {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000042 SkString name("rrect");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000043 switch (fType) {
44 case kBW_Draw_Type:
45 name.append("_draw_bw");
46 break;
47 case kAA_Draw_Type:
48 name.append("_draw_aa");
49 break;
50 case kBW_Clip_Type:
51 name.append("_clip_bw");
52 break;
53 case kAA_Clip_Type:
54 name.append("_clip_aa");
55 break;
56 case kEffect_Type:
57 name.append("_effect");
58 break;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000059 }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000060 return name;
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000064
mtklein36352bf2015-03-25 18:17:31 -070065 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -040066 GrRenderTargetContext* renderTargetContext =
67 canvas->internal_private_accessTopLayerRenderTargetContext();
68 if (kEffect_Type == fType && !renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -070069 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000070 return;
71 }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000072
73 SkPaint paint;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000074 if (kAA_Draw_Type == fType) {
75 paint.setAntiAlias(true);
76 }
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000077
mtkleindbfd7ab2016-09-01 11:24:54 -070078 const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX),
79 SkIntToScalar(kTileY));
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000080#ifdef SK_DEBUG
mtkleindbfd7ab2016-09-01 11:24:54 -070081 const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
82 SkIntToScalar(kImageHeight));
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000083#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000084
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000085#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -070086 int lastEdgeType = (kEffect_Type == fType) ? kLast_GrProcessorEdgeType: 0;
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000087#else
88 int lastEdgeType = 0;
89#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000090
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000091 int y = 1;
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +000092 for (int et = 0; et <= lastEdgeType; ++et) {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000093 int x = 1;
94 for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) {
95 bool drew = true;
96#ifdef SK_DEBUG
97 SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds()));
98 SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
99 imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
100 SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
101#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000102 canvas->save();
103 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000104 if (kEffect_Type == fType) {
bsalomon@google.com707bd602014-03-04 16:52:20 +0000105#if SK_SUPPORT_GPU
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000106 SkRRect rrect = fRRects[curRRect];
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000107 rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
joshualittb0a8a372014-09-23 09:50:21 -0700108 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
bungeman06ca8ec2016-06-09 08:01:03 -0700109 sk_sp<GrFragmentProcessor> fp(GrRRectEffect::Make(edgeType, rrect));
joshualittb0a8a372014-09-23 09:50:21 -0700110 if (fp) {
Brian Salomon82f44312017-01-11 13:42:54 -0500111 GrPaint grPaint;
112 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips28a838e2016-06-23 14:07:00 -0700113 grPaint.addCoverageFragmentProcessor(std::move(fp));
Brian Salomonac70f842017-05-08 10:43:33 -0400114 grPaint.setColor4f(GrColor4f(0, 0, 0, 1.f));
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000115
116 SkRect bounds = rrect.getBounds();
117 bounds.outset(2.f, 2.f);
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000118
Brian Salomonac70f842017-05-08 10:43:33 -0400119 renderTargetContext->priv().testingOnly_addDrawOp(
120 GrNonAAFillRectOp::Make(std::move(grPaint),
121 SkMatrix::I(),
122 bounds,
123 nullptr,
124 nullptr,
125 GrAAType::kNone));
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000126 } else {
127 drew = false;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000128 }
bsalomon@google.com707bd602014-03-04 16:52:20 +0000129#endif
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000130 } else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) {
131 bool aaClip = (kAA_Clip_Type == fType);
reed66998382016-09-21 11:15:07 -0700132 canvas->clipRRect(fRRects[curRRect], aaClip);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000133 canvas->drawRect(kMaxTileBound, paint);
134 } else {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000135 canvas->drawRRect(fRRects[curRRect], paint);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000136 }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000137 canvas->restore();
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000138 if (drew) {
139 x = x + kTileX;
140 if (x > kImageWidth) {
141 x = 1;
142 y += kTileY;
143 }
144 }
145 }
146 if (x != 1) {
147 y += kTileY;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000148 }
149 }
150 }
151
152 void setUpRRects() {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000153 // 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 +0000154 // the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA.
155
156 // simple cases
157 fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2));
158 fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2));
159 fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000160 fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5);
161 // small circular corners are an interesting test case for gpu clipping
162 fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1);
163 fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f);
164 fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000165
166 // The first complex case needs special handling since it is a square
167 fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]);
mike@reedtribe.orgf6100c82012-12-24 13:56:17 +0000168 for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000169 fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]);
170 }
171 }
172
173private:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000174 Type fType;
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000175
mtkleindbfd7ab2016-09-01 11:24:54 -0700176 static constexpr int kImageWidth = 640;
177 static constexpr int kImageHeight = 480;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000178
mtkleindbfd7ab2016-09-01 11:24:54 -0700179 static constexpr int kTileX = 80;
180 static constexpr int kTileY = 40;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000181
mtkleindbfd7ab2016-09-01 11:24:54 -0700182 static constexpr int kNumSimpleCases = 7;
183 static constexpr int kNumComplexCases = 35;
robertphillips@google.com5683d422012-12-17 21:58:02 +0000184 static const SkVector gRadii[kNumComplexCases][4];
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000185
mtkleindbfd7ab2016-09-01 11:24:54 -0700186 static constexpr int kNumRRects = kNumSimpleCases + kNumComplexCases;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000187 SkRRect fRRects[kNumRRects];
188
189 typedef GM INHERITED;
190};
191
192// Radii for the various test cases. Order is UL, UR, LR, LL
193const SkVector RRectGM::gRadii[kNumComplexCases][4] = {
194 // a circle
195 { { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } },
196
197 // odd ball cases
198 { { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } },
199 { { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } },
200 { { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } },
201
202 // UL
203 { { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
204 { { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
205 { { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
206
207 // UR
208 { { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } },
209 { { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } },
210 { { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } },
211
212 // LR
213 { { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } },
214 { { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } },
215 { { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } },
216
217 // LL
218 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } },
219 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } },
220 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } },
221
222 // over-sized radii
223 { { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } },
224 { { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } },
225 { { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } },
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000226
227 // circular corner tabs
228 { { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } },
229 { { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } },
230 { { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } },
231 { { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } },
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000232
commit-bot@chromium.org04eff722014-03-24 14:53:09 +0000233 // small radius circular corner tabs
234 { { 0, 0 }, { 0.2f, 0.2f }, { 0.2f, 0.2f }, { 0, 0 } },
235 { { 0.3f, 0.3f }, { 0.3f, .3f }, { 0, 0 }, { 0, 0 } },
236
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000237 // single circular corner cases
238 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 15 } },
239 { { 0, 0 }, { 0, 0 }, { 15, 15 }, { 0, 0 } },
240 { { 0, 0 }, { 15, 15 }, { 0, 0 }, { 0, 0 } },
241 { { 15, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000242
243 // nine patch elliptical
244 { { 5, 7 }, { 8, 7 }, { 8, 12 }, { 5, 12 } },
245 { { 0, 7 }, { 8, 7 }, { 8, 12 }, { 0, 12 } },
commit-bot@chromium.org04eff722014-03-24 14:53:09 +0000246
247 // nine patch elliptical, small radii
248 { { 0.4f, 7 }, { 8, 7 }, { 8, 12 }, { 0.4f, 12 } },
249 { { 0.4f, 0.4f }, { 8, 0.4f }, { 8, 12 }, { 0.4f, 12 } },
250 { { 20, 0.4f }, { 18, 0.4f }, { 18, 0.4f }, { 20, 0.4f } },
251 { { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f } },
252
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000253};
254
255///////////////////////////////////////////////////////////////////////////////
256
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000257DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); )
258DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); )
259DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); )
260DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); )
bsalomon@google.com707bd602014-03-04 16:52:20 +0000261#if SK_SUPPORT_GPU
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000262DEF_GM( return new RRectGM(RRectGM::kEffect_Type); )
bsalomon@google.com707bd602014-03-04 16:52:20 +0000263#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000264
265}