blob: bf3b0afb1da62555734e98446dda315cfaa5b8f9 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkMatrix.h"
12#include "include/core/SkPaint.h"
13#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/private/GrSharedEnums.h"
22#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "src/gpu/GrFragmentProcessor.h"
25#include "src/gpu/GrPaint.h"
26#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/effects/GrRRectEffect.h"
30#include "src/gpu/ops/GrDrawOp.h"
31#include "src/gpu/ops/GrFillRectOp.h"
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000032
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include <memory>
34#include <utility>
35
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000036namespace skiagm {
37
38///////////////////////////////////////////////////////////////////////////////
39
40class RRectGM : public GM {
41public:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000042 enum Type {
43 kBW_Draw_Type,
44 kAA_Draw_Type,
45 kBW_Clip_Type,
46 kAA_Clip_Type,
47 kEffect_Type,
48 };
herbb10fe492016-01-08 13:48:43 -080049 RRectGM(Type type) : fType(type) { }
50
51protected:
52
53 void onOnceBeforeDraw() override {
Mike Kleind46dce32018-08-16 10:17:03 -040054 this->setBGColor(0xFFDDDDDD);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000055 this->setUpRRects();
56 }
57
mtklein36352bf2015-03-25 18:17:31 -070058 SkString onShortName() override {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000059 SkString name("rrect");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000060 switch (fType) {
61 case kBW_Draw_Type:
62 name.append("_draw_bw");
63 break;
64 case kAA_Draw_Type:
65 name.append("_draw_aa");
66 break;
67 case kBW_Clip_Type:
68 name.append("_clip_bw");
69 break;
70 case kAA_Clip_Type:
71 name.append("_clip_aa");
72 break;
73 case kEffect_Type:
74 name.append("_effect");
75 break;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000076 }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000077 return name;
78 }
79
mtklein36352bf2015-03-25 18:17:31 -070080 SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000081
Chris Dalton50e24d72019-02-07 16:20:09 -070082 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Brian Osman11052242016-10-27 14:47:55 -040083 GrRenderTargetContext* renderTargetContext =
84 canvas->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips7c525e62018-06-12 10:11:12 -040085 GrContext* context = canvas->getGrContext();
Chris Dalton50e24d72019-02-07 16:20:09 -070086 if (kEffect_Type == fType && (!renderTargetContext || !context)) {
87 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
88 return DrawResult::kSkip;
Robert Phillips7c525e62018-06-12 10:11:12 -040089 }
90
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +000091 SkPaint paint;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000092 if (kAA_Draw_Type == fType) {
93 paint.setAntiAlias(true);
94 }
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000095
mtkleindbfd7ab2016-09-01 11:24:54 -070096 const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX),
97 SkIntToScalar(kTileY));
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +000098#ifdef SK_DEBUG
mtkleindbfd7ab2016-09-01 11:24:54 -070099 const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
100 SkIntToScalar(kImageHeight));
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000101#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000102
Ethan Nicholas1706f842017-11-10 11:58:19 -0500103 int lastEdgeType = (kEffect_Type == fType) ? (int) GrClipEdgeType::kLast: 0;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000104
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000105 int y = 1;
bsalomon@google.comb0ba39d2014-03-10 19:51:46 +0000106 for (int et = 0; et <= lastEdgeType; ++et) {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000107 int x = 1;
108 for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) {
109 bool drew = true;
110#ifdef SK_DEBUG
111 SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds()));
112 SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
113 imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
114 SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
115#endif
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000116 canvas->save();
117 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000118 if (kEffect_Type == fType) {
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000119 SkRRect rrect = fRRects[curRRect];
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000120 rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500121 GrClipEdgeType edgeType = (GrClipEdgeType) et;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000122 const auto& caps = *renderTargetContext->caps()->shaderCaps();
123 auto fp = GrRRectEffect::Make(edgeType, rrect, caps);
joshualittb0a8a372014-09-23 09:50:21 -0700124 if (fp) {
Brian Salomon82f44312017-01-11 13:42:54 -0500125 GrPaint grPaint;
126 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips28a838e2016-06-23 14:07:00 -0700127 grPaint.addCoverageFragmentProcessor(std::move(fp));
Brian Osmancb3d0872018-10-16 15:19:28 -0400128 grPaint.setColor4f({ 0, 0, 0, 1.f });
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000129
130 SkRect bounds = rrect.getBounds();
131 bounds.outset(2.f, 2.f);
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000132
Brian Salomonac70f842017-05-08 10:43:33 -0400133 renderTargetContext->priv().testingOnly_addDrawOp(
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400134 GrFillRectOp::MakeNonAARect(context, std::move(grPaint),
135 SkMatrix::I(), bounds));
commit-bot@chromium.orgbfce48e2014-03-10 19:33:16 +0000136 } else {
137 drew = false;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000138 }
139 } else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) {
140 bool aaClip = (kAA_Clip_Type == fType);
reed66998382016-09-21 11:15:07 -0700141 canvas->clipRRect(fRRects[curRRect], 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 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700159 return DrawResult::kOk;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000160 }
161
162 void setUpRRects() {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000163 // 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 +0000164 // the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA.
165
166 // simple cases
167 fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2));
168 fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2));
169 fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000170 fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5);
171 // small circular corners are an interesting test case for gpu clipping
172 fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1);
173 fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f);
174 fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f);
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000175
176 // The first complex case needs special handling since it is a square
177 fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]);
mike@reedtribe.orgf6100c82012-12-24 13:56:17 +0000178 for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) {
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000179 fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]);
180 }
181 }
182
183private:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000184 Type fType;
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +0000185
mtkleindbfd7ab2016-09-01 11:24:54 -0700186 static constexpr int kImageWidth = 640;
187 static constexpr int kImageHeight = 480;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000188
mtkleindbfd7ab2016-09-01 11:24:54 -0700189 static constexpr int kTileX = 80;
190 static constexpr int kTileY = 40;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000191
mtkleindbfd7ab2016-09-01 11:24:54 -0700192 static constexpr int kNumSimpleCases = 7;
193 static constexpr int kNumComplexCases = 35;
robertphillips@google.com5683d422012-12-17 21:58:02 +0000194 static const SkVector gRadii[kNumComplexCases][4];
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000195
mtkleindbfd7ab2016-09-01 11:24:54 -0700196 static constexpr int kNumRRects = kNumSimpleCases + kNumComplexCases;
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000197 SkRRect fRRects[kNumRRects];
198
199 typedef GM INHERITED;
200};
201
202// Radii for the various test cases. Order is UL, UR, LR, LL
203const SkVector RRectGM::gRadii[kNumComplexCases][4] = {
204 // a circle
205 { { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } },
206
207 // odd ball cases
208 { { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } },
209 { { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } },
210 { { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } },
211
212 // UL
213 { { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
214 { { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
215 { { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
216
217 // UR
218 { { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } },
219 { { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } },
220 { { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } },
221
222 // LR
223 { { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } },
224 { { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } },
225 { { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } },
226
227 // LL
228 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } },
229 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } },
230 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } },
231
232 // over-sized radii
233 { { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } },
234 { { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } },
235 { { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } },
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000236
237 // circular corner tabs
238 { { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } },
239 { { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } },
240 { { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } },
241 { { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } },
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000242
commit-bot@chromium.org04eff722014-03-24 14:53:09 +0000243 // small radius circular corner tabs
244 { { 0, 0 }, { 0.2f, 0.2f }, { 0.2f, 0.2f }, { 0, 0 } },
245 { { 0.3f, 0.3f }, { 0.3f, .3f }, { 0, 0 }, { 0, 0 } },
246
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000247 // single circular corner cases
248 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 15 } },
249 { { 0, 0 }, { 0, 0 }, { 15, 15 }, { 0, 0 } },
250 { { 0, 0 }, { 15, 15 }, { 0, 0 }, { 0, 0 } },
251 { { 15, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000252
253 // nine patch elliptical
254 { { 5, 7 }, { 8, 7 }, { 8, 12 }, { 5, 12 } },
255 { { 0, 7 }, { 8, 7 }, { 8, 12 }, { 0, 12 } },
commit-bot@chromium.org04eff722014-03-24 14:53:09 +0000256
257 // nine patch elliptical, small radii
258 { { 0.4f, 7 }, { 8, 7 }, { 8, 12 }, { 0.4f, 12 } },
259 { { 0.4f, 0.4f }, { 8, 0.4f }, { 8, 12 }, { 0.4f, 12 } },
260 { { 20, 0.4f }, { 18, 0.4f }, { 18, 0.4f }, { 20, 0.4f } },
261 { { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f } },
262
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000263};
264
265///////////////////////////////////////////////////////////////////////////////
266
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000267DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); )
268DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); )
269DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); )
270DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); )
271DEF_GM( return new RRectGM(RRectGM::kEffect_Type); )
robertphillips@google.com4e18c7a2012-12-17 21:48:19 +0000272
273}