blob: c32771b02b222cf8bcc22339ffcaa4d28288e39b [file] [log] [blame]
scroggo@google.com7b056592013-11-05 15:57:21 +00001/*
2* Copyright 2013 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 Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkBlurTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040012#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorFilter.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040014#include "include/core/SkDrawLooper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkMaskFilter.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040016#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkPoint.h"
19#include "include/core/SkRRect.h"
20#include "include/core/SkRect.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040021#include "include/core/SkRefCnt.h"
22#include "include/core/SkScalar.h"
23#include "include/core/SkShader.h"
24#include "include/core/SkSize.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkString.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040026#include "include/core/SkTileMode.h"
27#include "include/core/SkTypes.h"
28#include "include/effects/SkGradientShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/effects/SkLayerDrawLooper.h"
30#include "src/core/SkBlurMask.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000031
scroggo@google.com8610d2c2013-11-05 21:10:58 +000032// This GM mimics a blurred RR seen in the wild.
scroggo@google.com7b056592013-11-05 15:57:21 +000033class BlurRoundRectGM : public skiagm::GM {
34public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000035 BlurRoundRectGM(int width, int height)
herbb10fe492016-01-08 13:48:43 -080036 : fName("blurroundrect"), fWidth(width), fHeight(height) {
scroggoef470f12014-06-03 06:50:26 -070037 fName.appendf("-WH-%ix%i-unevenCorners", width, height);
scroggo@google.com7b056592013-11-05 15:57:21 +000038 }
39
mtklein36352bf2015-03-25 18:17:31 -070040 SkString onShortName() override {
scroggo@google.com7b056592013-11-05 15:57:21 +000041 return fName;
42 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 SkISize onISize() override {
herbb10fe492016-01-08 13:48:43 -080045 return SkISize::Make(fWidth, fHeight);
46 }
47
48 void onOnceBeforeDraw() override {
49 SkVector radii[4];
50 radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
51 radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
52 radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
53 radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
54 SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight));
55 fRRect.setRectRadii(r, radii);
scroggo@google.com7b056592013-11-05 15:57:21 +000056 }
57
mtklein36352bf2015-03-25 18:17:31 -070058 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000059 SkLayerDrawLooper::Builder looperBuilder;
scroggo@google.com7b056592013-11-05 15:57:21 +000060 {
61 SkLayerDrawLooper::LayerInfo info;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000062 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
63 | SkLayerDrawLooper::kColorFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -040064 info.fColorMode = SkBlendMode::kSrc;
scroggo@google.com7b056592013-11-05 15:57:21 +000065 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
66 info.fPostTranslate = false;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000067 SkPaint* paint = looperBuilder.addLayerOnTop(info);
Mike Reed1be1f8d2018-03-14 13:01:17 -040068 paint->setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000069 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040070 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf)));
Mike Reedb286bc22019-04-08 16:23:20 -040071 paint->setColorFilter(SkColorFilters::Blend(SK_ColorLTGRAY, SkBlendMode::kSrcIn));
Mike Kleind46dce32018-08-16 10:17:03 -040072 paint->setColor(SK_ColorGRAY);
scroggo@google.com7b056592013-11-05 15:57:21 +000073 }
74 {
75 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000076 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000077 }
78 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000079 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000080
reed7b380d02016-03-21 13:25:16 -070081 paint.setLooper(looperBuilder.detach());
scroggo@google.com5fdef982013-11-05 16:28:38 +000082 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000083 paint.setAntiAlias(true);
84
scroggo@google.com8610d2c2013-11-05 21:10:58 +000085 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000086 }
87
88private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000089 SkString fName;
90 SkRRect fRRect;
herbb10fe492016-01-08 13:48:43 -080091 int fWidth, fHeight;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000092
93 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000094};
95
joshualitt341400e2014-12-18 11:54:13 -080096/*
97 * Spits out a dummy gradient to test blur with shader on paint
98 */
reed2ad1aa62016-03-09 09:50:50 -080099static sk_sp<SkShader> MakeRadial() {
joshualitt341400e2014-12-18 11:54:13 -0800100 SkPoint pts[2] = {
101 { 0, 0 },
102 { SkIntToScalar(100), SkIntToScalar(100) }
103 };
Mike Reedfae8fce2019-04-03 10:27:45 -0400104 SkTileMode tm = SkTileMode::kClamp;
joshualitt341400e2014-12-18 11:54:13 -0800105 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
106 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
107 SkMatrix scale;
108 scale.setScale(0.5f, 0.5f);
109 scale.postTranslate(5.f, 5.f);
110 SkPoint center0, center1;
111 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
112 SkScalarAve(pts[0].fY, pts[1].fY));
113 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
114 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed2ad1aa62016-03-09 09:50:50 -0800115 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
116 center0, (pts[1].fX - pts[0].fX) / 2,
117 colors, pos, SK_ARRAY_COUNT(colors), tm,
118 0, &scale);
joshualitt341400e2014-12-18 11:54:13 -0800119}
120
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000121// Simpler blurred RR test cases where all the radii are the same.
122class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +0000123public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000124 SimpleBlurRoundRectGM()
125 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000126 }
127
128protected:
joshualitt341400e2014-12-18 11:54:13 -0800129
mtklein36352bf2015-03-25 18:17:31 -0700130 SkString onShortName() override {
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000131 return fName;
132 }
133
mtklein36352bf2015-03-25 18:17:31 -0700134 SkISize onISize() override {
joshualitt341400e2014-12-18 11:54:13 -0800135 return SkISize::Make(1000, 500);
scroggo@google.com7b056592013-11-05 15:57:21 +0000136 }
137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000139 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000140 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000141
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000142 const float blurRadii[] = { 1,5,10,20 };
143 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -0800144 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000145 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
146 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000147 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000148 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -0800149 for (int k = 0; k <= 1; k++) {
joshualitt341400e2014-12-18 11:54:13 -0800150 SkPaint paint;
151 paint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400152 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
153 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000154
joshualitt341400e2014-12-18 11:54:13 -0800155 bool useRadial = SkToBool(k);
156 if (useRadial) {
reed2ad1aa62016-03-09 09:50:50 -0800157 paint.setShader(MakeRadial());
joshualitt341400e2014-12-18 11:54:13 -0800158 }
159
160 SkRRect rrect;
161 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
162 SkIntToScalar(cornerRadii[j]));
163 canvas->drawRRect(rrect, paint);
164 canvas->translate(r.width() + SkIntToScalar(50), 0);
165 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000166 }
167 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000168 }
169private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000170 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000171
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000172 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000173};
174
175// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000176//
177// TODO(scroggo): Disabled in an attempt to rememdy
178// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
179// ran wrong number of tests')
180//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
181
scroggo@google.com7b056592013-11-05 15:57:21 +0000182// Rounded rect with two opposite corners with large radii, the other two
183// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000184DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000185
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000186DEF_GM(return new SimpleBlurRoundRectGM();)