blob: 976b5aecf2a6ad15dee9ec3af53c613b5d69f8a9 [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
Mike Reedb7dad442019-07-22 14:51:10 -040032#ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
scroggo@google.com8610d2c2013-11-05 21:10:58 +000033// This GM mimics a blurred RR seen in the wild.
scroggo@google.com7b056592013-11-05 15:57:21 +000034class BlurRoundRectGM : public skiagm::GM {
35public:
Hal Canary594fe852019-07-18 13:35:49 -040036 BlurRoundRectGM(int w, int h) : fWidth(w), fHeight(h) {}
scroggo@google.com7b056592013-11-05 15:57:21 +000037
Hal Canary594fe852019-07-18 13:35:49 -040038private:
mtklein36352bf2015-03-25 18:17:31 -070039 SkString onShortName() override {
Hal Canary594fe852019-07-18 13:35:49 -040040 return SkStringPrintf("blurroundrect-WH-%ix%i-unevenCorners", fWidth, fHeight);
scroggo@google.com7b056592013-11-05 15:57:21 +000041 }
42
Hal Canary594fe852019-07-18 13:35:49 -040043 SkISize onISize() override { return {fWidth, fHeight}; }
herbb10fe492016-01-08 13:48:43 -080044
45 void onOnceBeforeDraw() override {
46 SkVector radii[4];
47 radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
48 radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
49 radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
50 radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
51 SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight));
52 fRRect.setRectRadii(r, radii);
scroggo@google.com7b056592013-11-05 15:57:21 +000053 }
54
mtklein36352bf2015-03-25 18:17:31 -070055 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000056 SkLayerDrawLooper::Builder looperBuilder;
scroggo@google.com7b056592013-11-05 15:57:21 +000057 {
58 SkLayerDrawLooper::LayerInfo info;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000059 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
60 | SkLayerDrawLooper::kColorFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -040061 info.fColorMode = SkBlendMode::kSrc;
scroggo@google.com7b056592013-11-05 15:57:21 +000062 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
63 info.fPostTranslate = false;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000064 SkPaint* paint = looperBuilder.addLayerOnTop(info);
Mike Reed1be1f8d2018-03-14 13:01:17 -040065 paint->setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000066 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040067 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf)));
Mike Reedb286bc22019-04-08 16:23:20 -040068 paint->setColorFilter(SkColorFilters::Blend(SK_ColorLTGRAY, SkBlendMode::kSrcIn));
Mike Kleind46dce32018-08-16 10:17:03 -040069 paint->setColor(SK_ColorGRAY);
scroggo@google.com7b056592013-11-05 15:57:21 +000070 }
71 {
72 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000073 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000074 }
75 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000076 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000077
reed7b380d02016-03-21 13:25:16 -070078 paint.setLooper(looperBuilder.detach());
scroggo@google.com5fdef982013-11-05 16:28:38 +000079 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000080 paint.setAntiAlias(true);
81
scroggo@google.com8610d2c2013-11-05 21:10:58 +000082 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000083 }
84
Hal Canary594fe852019-07-18 13:35:49 -040085 SkRRect fRRect;
86 int fWidth, fHeight;
scroggo@google.com7b056592013-11-05 15:57:21 +000087};
Mike Reedb7dad442019-07-22 14:51:10 -040088// Rounded rect with two opposite corners with large radii, the other two
89// small.
90DEF_GM(return new BlurRoundRectGM(100, 100);)
91#endif
scroggo@google.com7b056592013-11-05 15:57:21 +000092
joshualitt341400e2014-12-18 11:54:13 -080093/*
94 * Spits out a dummy gradient to test blur with shader on paint
95 */
reed2ad1aa62016-03-09 09:50:50 -080096static sk_sp<SkShader> MakeRadial() {
joshualitt341400e2014-12-18 11:54:13 -080097 SkPoint pts[2] = {
98 { 0, 0 },
99 { SkIntToScalar(100), SkIntToScalar(100) }
100 };
Mike Reedfae8fce2019-04-03 10:27:45 -0400101 SkTileMode tm = SkTileMode::kClamp;
joshualitt341400e2014-12-18 11:54:13 -0800102 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
103 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
104 SkMatrix scale;
105 scale.setScale(0.5f, 0.5f);
106 scale.postTranslate(5.f, 5.f);
107 SkPoint center0, center1;
108 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
109 SkScalarAve(pts[0].fY, pts[1].fY));
110 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
111 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed2ad1aa62016-03-09 09:50:50 -0800112 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
113 center0, (pts[1].fX - pts[0].fX) / 2,
114 colors, pos, SK_ARRAY_COUNT(colors), tm,
115 0, &scale);
joshualitt341400e2014-12-18 11:54:13 -0800116}
117
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000118// Simpler blurred RR test cases where all the radii are the same.
119class SimpleBlurRoundRectGM : public skiagm::GM {
Hal Canary594fe852019-07-18 13:35:49 -0400120 SkString onShortName() override { return SkString("simpleblurroundrect"); }
scroggo@google.com7b056592013-11-05 15:57:21 +0000121
Hal Canary594fe852019-07-18 13:35:49 -0400122 SkISize onISize() override { return {1000, 500}; }
scroggo@google.com7b056592013-11-05 15:57:21 +0000123
mtklein36352bf2015-03-25 18:17:31 -0700124 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000125 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000126 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000127
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000128 const float blurRadii[] = { 1,5,10,20 };
129 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -0800130 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000131 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
132 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000133 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000134 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -0800135 for (int k = 0; k <= 1; k++) {
joshualitt341400e2014-12-18 11:54:13 -0800136 SkPaint paint;
137 paint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400138 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
139 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000140
joshualitt341400e2014-12-18 11:54:13 -0800141 bool useRadial = SkToBool(k);
142 if (useRadial) {
reed2ad1aa62016-03-09 09:50:50 -0800143 paint.setShader(MakeRadial());
joshualitt341400e2014-12-18 11:54:13 -0800144 }
145
146 SkRRect rrect;
147 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
148 SkIntToScalar(cornerRadii[j]));
149 canvas->drawRRect(rrect, paint);
150 canvas->translate(r.width() + SkIntToScalar(50), 0);
151 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000152 }
153 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000154 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000155};
156
157// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000158//
159// TODO(scroggo): Disabled in an attempt to rememdy
160// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
161// ran wrong number of tests')
162//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
163
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000164DEF_GM(return new SimpleBlurRoundRectGM();)