blob: 5982d06be3a760c506c7f539855dac5d77cc32df [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
8#include "gm.h"
9#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
12#include "SkColorFilter.h"
13#include "SkLayerDrawLooper.h"
14#include "SkPaint.h"
15#include "SkPath.h"
16#include "SkPoint.h"
17#include "SkRect.h"
18#include "SkRRect.h"
19#include "SkString.h"
20#include "SkXfermode.h"
21
scroggo@google.com8610d2c2013-11-05 21:10:58 +000022// This GM mimics a blurred RR seen in the wild.
scroggo@google.com7b056592013-11-05 15:57:21 +000023class BlurRoundRectGM : public skiagm::GM {
24public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000025 BlurRoundRectGM(int width, int height)
herbb10fe492016-01-08 13:48:43 -080026 : fName("blurroundrect"), fWidth(width), fHeight(height) {
scroggoef470f12014-06-03 06:50:26 -070027 fName.appendf("-WH-%ix%i-unevenCorners", width, height);
scroggo@google.com7b056592013-11-05 15:57:21 +000028 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
scroggo@google.com7b056592013-11-05 15:57:21 +000031 return fName;
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
herbb10fe492016-01-08 13:48:43 -080035 return SkISize::Make(fWidth, fHeight);
36 }
37
38 void onOnceBeforeDraw() override {
39 SkVector radii[4];
40 radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
41 radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
42 radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
43 radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
44 SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight));
45 fRRect.setRectRadii(r, radii);
scroggo@google.com7b056592013-11-05 15:57:21 +000046 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000049 SkLayerDrawLooper::Builder looperBuilder;
scroggo@google.com7b056592013-11-05 15:57:21 +000050 {
51 SkLayerDrawLooper::LayerInfo info;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000052 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
53 | SkLayerDrawLooper::kColorFilter_Bit;
scroggo@google.com7b056592013-11-05 15:57:21 +000054 info.fColorMode = SkXfermode::kSrc_Mode;
55 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
56 info.fPostTranslate = false;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000057 SkPaint* paint = looperBuilder.addLayerOnTop(info);
reedefdfd512016-04-04 10:02:58 -070058 paint->setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000059 kNormal_SkBlurStyle,
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000060 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf),
reedefdfd512016-04-04 10:02:58 -070061 SkBlurMaskFilter::kHighQuality_BlurFlag));
reedd053ce92016-03-22 10:17:23 -070062 paint->setColorFilter(SkColorFilter::MakeModeFilter(
caryclarkf597c422015-07-28 10:37:53 -070063 sk_tool_utils::color_to_565(SK_ColorLTGRAY),
reedd053ce92016-03-22 10:17:23 -070064 SkXfermode::kSrcIn_Mode));
caryclarkf597c422015-07-28 10:37:53 -070065 paint->setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
scroggo@google.com7b056592013-11-05 15:57:21 +000066 }
67 {
68 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000069 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000070 }
71 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000072 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000073
reed7b380d02016-03-21 13:25:16 -070074 paint.setLooper(looperBuilder.detach());
scroggo@google.com5fdef982013-11-05 16:28:38 +000075 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000076 paint.setAntiAlias(true);
77
scroggo@google.com8610d2c2013-11-05 21:10:58 +000078 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000079 }
80
81private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000082 SkString fName;
83 SkRRect fRRect;
herbb10fe492016-01-08 13:48:43 -080084 int fWidth, fHeight;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000085
86 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000087};
88
joshualitt341400e2014-12-18 11:54:13 -080089#include "SkGradientShader.h"
90/*
91 * Spits out a dummy gradient to test blur with shader on paint
92 */
reed2ad1aa62016-03-09 09:50:50 -080093static sk_sp<SkShader> MakeRadial() {
joshualitt341400e2014-12-18 11:54:13 -080094 SkPoint pts[2] = {
95 { 0, 0 },
96 { SkIntToScalar(100), SkIntToScalar(100) }
97 };
98 SkShader::TileMode tm = SkShader::kClamp_TileMode;
99 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
100 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
101 SkMatrix scale;
102 scale.setScale(0.5f, 0.5f);
103 scale.postTranslate(5.f, 5.f);
104 SkPoint center0, center1;
105 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
106 SkScalarAve(pts[0].fY, pts[1].fY));
107 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
108 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed2ad1aa62016-03-09 09:50:50 -0800109 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
110 center0, (pts[1].fX - pts[0].fX) / 2,
111 colors, pos, SK_ARRAY_COUNT(colors), tm,
112 0, &scale);
joshualitt341400e2014-12-18 11:54:13 -0800113}
114
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000115// Simpler blurred RR test cases where all the radii are the same.
116class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +0000117public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000118 SimpleBlurRoundRectGM()
119 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000120 }
121
122protected:
joshualitt341400e2014-12-18 11:54:13 -0800123
mtklein36352bf2015-03-25 18:17:31 -0700124 SkString onShortName() override {
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000125 return fName;
126 }
127
mtklein36352bf2015-03-25 18:17:31 -0700128 SkISize onISize() override {
joshualitt341400e2014-12-18 11:54:13 -0800129 return SkISize::Make(1000, 500);
scroggo@google.com7b056592013-11-05 15:57:21 +0000130 }
131
mtklein36352bf2015-03-25 18:17:31 -0700132 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000133 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000134 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000135
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000136 const float blurRadii[] = { 1,5,10,20 };
137 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -0800138 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000139 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
140 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000141 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000142 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -0800143 for (int k = 0; k <= 1; k++) {
joshualitt341400e2014-12-18 11:54:13 -0800144 SkPaint paint;
145 paint.setColor(SK_ColorBLACK);
reedefdfd512016-04-04 10:02:58 -0700146 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
147 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])),
148 SkBlurMaskFilter::kHighQuality_BlurFlag));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000149
joshualitt341400e2014-12-18 11:54:13 -0800150 bool useRadial = SkToBool(k);
151 if (useRadial) {
reed2ad1aa62016-03-09 09:50:50 -0800152 paint.setShader(MakeRadial());
joshualitt341400e2014-12-18 11:54:13 -0800153 }
154
155 SkRRect rrect;
156 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
157 SkIntToScalar(cornerRadii[j]));
158 canvas->drawRRect(rrect, paint);
159 canvas->translate(r.width() + SkIntToScalar(50), 0);
160 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000161 }
162 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000163 }
164private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000165 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000166
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000167 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000168};
169
170// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000171//
172// TODO(scroggo): Disabled in an attempt to rememdy
173// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
174// ran wrong number of tests')
175//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
176
scroggo@google.com7b056592013-11-05 15:57:21 +0000177// Rounded rect with two opposite corners with large radii, the other two
178// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000179DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000180
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000181DEF_GM(return new SimpleBlurRoundRectGM();)