blob: c3cdb66673e19e3bde2ec4bf6aa1812cbd19fe7a [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);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000058 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(
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),
scroggo@google.com7b056592013-11-05 15:57:21 +000061 SkBlurMaskFilter::kHighQuality_BlurFlag);
62 paint->setMaskFilter(maskFilter)->unref();
caryclarkf597c422015-07-28 10:37:53 -070063 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(
64 sk_tool_utils::color_to_565(SK_ColorLTGRAY),
scroggo@google.com7b056592013-11-05 15:57:21 +000065 SkXfermode::kSrcIn_Mode);
66 paint->setColorFilter(colorFilter)->unref();
caryclarkf597c422015-07-28 10:37:53 -070067 paint->setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
scroggo@google.com7b056592013-11-05 15:57:21 +000068 }
69 {
70 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000071 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000072 }
73 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000074 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000075
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000076 paint.setLooper(looperBuilder.detachLooper())->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000077 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000078 paint.setAntiAlias(true);
79
scroggo@google.com8610d2c2013-11-05 21:10:58 +000080 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000081 }
82
83private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000084 SkString fName;
85 SkRRect fRRect;
herbb10fe492016-01-08 13:48:43 -080086 int fWidth, fHeight;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000087
88 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000089};
90
joshualitt341400e2014-12-18 11:54:13 -080091#include "SkGradientShader.h"
92/*
93 * Spits out a dummy gradient to test blur with shader on paint
94 */
95static SkShader* MakeRadial() {
96 SkPoint pts[2] = {
97 { 0, 0 },
98 { SkIntToScalar(100), SkIntToScalar(100) }
99 };
100 SkShader::TileMode tm = SkShader::kClamp_TileMode;
101 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
102 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
103 SkMatrix scale;
104 scale.setScale(0.5f, 0.5f);
105 scale.postTranslate(5.f, 5.f);
106 SkPoint center0, center1;
107 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
108 SkScalarAve(pts[0].fY, pts[1].fY));
109 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
110 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed71a6cbf2015-05-04 08:32:51 -0700111 return SkGradientShader::CreateTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
112 center0, (pts[1].fX - pts[0].fX) / 2,
113 colors, pos, SK_ARRAY_COUNT(colors), tm,
114 0, &scale);
joshualitt341400e2014-12-18 11:54:13 -0800115}
116
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000117// Simpler blurred RR test cases where all the radii are the same.
118class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +0000119public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000120 SimpleBlurRoundRectGM()
121 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000122 }
123
124protected:
joshualitt341400e2014-12-18 11:54:13 -0800125
mtklein36352bf2015-03-25 18:17:31 -0700126 SkString onShortName() override {
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000127 return fName;
128 }
129
mtklein36352bf2015-03-25 18:17:31 -0700130 SkISize onISize() override {
joshualitt341400e2014-12-18 11:54:13 -0800131 return SkISize::Make(1000, 500);
scroggo@google.com7b056592013-11-05 15:57:21 +0000132 }
133
mtklein36352bf2015-03-25 18:17:31 -0700134 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000135 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000136 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000137
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000138 const float blurRadii[] = { 1,5,10,20 };
139 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -0800140 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000141 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
142 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000143 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000144 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -0800145 for (int k = 0; k <= 1; k++) {
146 SkMaskFilter* filter = SkBlurMaskFilter::Create(
147 kNormal_SkBlurStyle,
148 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])),
149 SkBlurMaskFilter::kHighQuality_BlurFlag);
150 SkPaint paint;
151 paint.setColor(SK_ColorBLACK);
152 paint.setMaskFilter(filter)->unref();
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000153
joshualitt341400e2014-12-18 11:54:13 -0800154 bool useRadial = SkToBool(k);
155 if (useRadial) {
156 paint.setShader(MakeRadial())->unref();
157 }
158
159 SkRRect rrect;
160 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
161 SkIntToScalar(cornerRadii[j]));
162 canvas->drawRRect(rrect, paint);
163 canvas->translate(r.width() + SkIntToScalar(50), 0);
164 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000165 }
166 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000167 }
168private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000169 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000170
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000171 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000172};
173
174// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000175//
176// TODO(scroggo): Disabled in an attempt to rememdy
177// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
178// ran wrong number of tests')
179//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
180
scroggo@google.com7b056592013-11-05 15:57:21 +0000181// Rounded rect with two opposite corners with large radii, the other two
182// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000183DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000184
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000185DEF_GM(return new SimpleBlurRoundRectGM();)