blob: 289e1563456d9225c9b60b19d2a627c666874e38 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000010#include "SkBlurMask.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000011#include "SkCanvas.h"
12#include "SkColorFilter.h"
13#include "SkLayerDrawLooper.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040014#include "SkMaskFilter.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000015#include "SkPaint.h"
16#include "SkPath.h"
17#include "SkPoint.h"
18#include "SkRect.h"
19#include "SkRRect.h"
20#include "SkString.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000021
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;
Mike Reedfaba3712016-11-03 14:45:31 -040054 info.fColorMode = SkBlendMode::kSrc;
scroggo@google.com7b056592013-11-05 15:57:21 +000055 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);
Mike Reed1be1f8d2018-03-14 13:01:17 -040058 paint->setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000059 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040060 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf)));
reedd053ce92016-03-22 10:17:23 -070061 paint->setColorFilter(SkColorFilter::MakeModeFilter(
caryclarkf597c422015-07-28 10:37:53 -070062 sk_tool_utils::color_to_565(SK_ColorLTGRAY),
Mike Reed7d954ad2016-10-28 15:42:34 -040063 SkBlendMode::kSrcIn));
caryclarkf597c422015-07-28 10:37:53 -070064 paint->setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
scroggo@google.com7b056592013-11-05 15:57:21 +000065 }
66 {
67 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000068 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000069 }
70 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000071 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000072
reed7b380d02016-03-21 13:25:16 -070073 paint.setLooper(looperBuilder.detach());
scroggo@google.com5fdef982013-11-05 16:28:38 +000074 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000075 paint.setAntiAlias(true);
76
scroggo@google.com8610d2c2013-11-05 21:10:58 +000077 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000078 }
79
80private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000081 SkString fName;
82 SkRRect fRRect;
herbb10fe492016-01-08 13:48:43 -080083 int fWidth, fHeight;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000084
85 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000086};
87
joshualitt341400e2014-12-18 11:54:13 -080088#include "SkGradientShader.h"
89/*
90 * Spits out a dummy gradient to test blur with shader on paint
91 */
reed2ad1aa62016-03-09 09:50:50 -080092static sk_sp<SkShader> MakeRadial() {
joshualitt341400e2014-12-18 11:54:13 -080093 SkPoint pts[2] = {
94 { 0, 0 },
95 { SkIntToScalar(100), SkIntToScalar(100) }
96 };
97 SkShader::TileMode tm = SkShader::kClamp_TileMode;
98 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
99 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
100 SkMatrix scale;
101 scale.setScale(0.5f, 0.5f);
102 scale.postTranslate(5.f, 5.f);
103 SkPoint center0, center1;
104 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
105 SkScalarAve(pts[0].fY, pts[1].fY));
106 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
107 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed2ad1aa62016-03-09 09:50:50 -0800108 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
109 center0, (pts[1].fX - pts[0].fX) / 2,
110 colors, pos, SK_ARRAY_COUNT(colors), tm,
111 0, &scale);
joshualitt341400e2014-12-18 11:54:13 -0800112}
113
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000114// Simpler blurred RR test cases where all the radii are the same.
115class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +0000116public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000117 SimpleBlurRoundRectGM()
118 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000119 }
120
121protected:
joshualitt341400e2014-12-18 11:54:13 -0800122
mtklein36352bf2015-03-25 18:17:31 -0700123 SkString onShortName() override {
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000124 return fName;
125 }
126
mtklein36352bf2015-03-25 18:17:31 -0700127 SkISize onISize() override {
joshualitt341400e2014-12-18 11:54:13 -0800128 return SkISize::Make(1000, 500);
scroggo@google.com7b056592013-11-05 15:57:21 +0000129 }
130
mtklein36352bf2015-03-25 18:17:31 -0700131 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000132 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000133 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000134
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000135 const float blurRadii[] = { 1,5,10,20 };
136 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -0800137 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000138 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
139 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000140 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000141 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -0800142 for (int k = 0; k <= 1; k++) {
joshualitt341400e2014-12-18 11:54:13 -0800143 SkPaint paint;
144 paint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400145 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
146 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000147
joshualitt341400e2014-12-18 11:54:13 -0800148 bool useRadial = SkToBool(k);
149 if (useRadial) {
reed2ad1aa62016-03-09 09:50:50 -0800150 paint.setShader(MakeRadial());
joshualitt341400e2014-12-18 11:54:13 -0800151 }
152
153 SkRRect rrect;
154 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
155 SkIntToScalar(cornerRadii[j]));
156 canvas->drawRRect(rrect, paint);
157 canvas->translate(r.width() + SkIntToScalar(50), 0);
158 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000159 }
160 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000161 }
162private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000163 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000164
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000165 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000166};
167
168// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000169//
170// TODO(scroggo): Disabled in an attempt to rememdy
171// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
172// ran wrong number of tests')
173//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
174
scroggo@google.com7b056592013-11-05 15:57:21 +0000175// Rounded rect with two opposite corners with large radii, the other two
176// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000177DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000178
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000179DEF_GM(return new SimpleBlurRoundRectGM();)