blob: b7d0800a866d42e66fa178718ef5aab72d58e606 [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"
scroggo@google.com7b056592013-11-05 15:57:21 +000010#include "SkCanvas.h"
11#include "SkColorFilter.h"
12#include "SkLayerDrawLooper.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040013#include "SkMaskFilter.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000014#include "SkPaint.h"
15#include "SkPath.h"
16#include "SkPoint.h"
17#include "SkRect.h"
18#include "SkRRect.h"
19#include "SkString.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000020
scroggo@google.com8610d2c2013-11-05 21:10:58 +000021// This GM mimics a blurred RR seen in the wild.
scroggo@google.com7b056592013-11-05 15:57:21 +000022class BlurRoundRectGM : public skiagm::GM {
23public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000024 BlurRoundRectGM(int width, int height)
herbb10fe492016-01-08 13:48:43 -080025 : fName("blurroundrect"), fWidth(width), fHeight(height) {
scroggoef470f12014-06-03 06:50:26 -070026 fName.appendf("-WH-%ix%i-unevenCorners", width, height);
scroggo@google.com7b056592013-11-05 15:57:21 +000027 }
28
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
scroggo@google.com7b056592013-11-05 15:57:21 +000030 return fName;
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
herbb10fe492016-01-08 13:48:43 -080034 return SkISize::Make(fWidth, fHeight);
35 }
36
37 void onOnceBeforeDraw() override {
38 SkVector radii[4];
39 radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
40 radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
41 radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
42 radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
43 SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight));
44 fRRect.setRectRadii(r, radii);
scroggo@google.com7b056592013-11-05 15:57:21 +000045 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000048 SkLayerDrawLooper::Builder looperBuilder;
scroggo@google.com7b056592013-11-05 15:57:21 +000049 {
50 SkLayerDrawLooper::LayerInfo info;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000051 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
52 | SkLayerDrawLooper::kColorFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -040053 info.fColorMode = SkBlendMode::kSrc;
scroggo@google.com7b056592013-11-05 15:57:21 +000054 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
55 info.fPostTranslate = false;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000056 SkPaint* paint = looperBuilder.addLayerOnTop(info);
Mike Reed1be1f8d2018-03-14 13:01:17 -040057 paint->setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000058 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040059 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf)));
reedd053ce92016-03-22 10:17:23 -070060 paint->setColorFilter(SkColorFilter::MakeModeFilter(
Mike Kleind46dce32018-08-16 10:17:03 -040061 SK_ColorLTGRAY,
Mike Reed7d954ad2016-10-28 15:42:34 -040062 SkBlendMode::kSrcIn));
Mike Kleind46dce32018-08-16 10:17:03 -040063 paint->setColor(SK_ColorGRAY);
scroggo@google.com7b056592013-11-05 15:57:21 +000064 }
65 {
66 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000067 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000068 }
69 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000070 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000071
reed7b380d02016-03-21 13:25:16 -070072 paint.setLooper(looperBuilder.detach());
scroggo@google.com5fdef982013-11-05 16:28:38 +000073 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000074 paint.setAntiAlias(true);
75
scroggo@google.com8610d2c2013-11-05 21:10:58 +000076 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000077 }
78
79private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000080 SkString fName;
81 SkRRect fRRect;
herbb10fe492016-01-08 13:48:43 -080082 int fWidth, fHeight;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000083
84 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000085};
86
joshualitt341400e2014-12-18 11:54:13 -080087#include "SkGradientShader.h"
88/*
89 * Spits out a dummy gradient to test blur with shader on paint
90 */
reed2ad1aa62016-03-09 09:50:50 -080091static sk_sp<SkShader> MakeRadial() {
joshualitt341400e2014-12-18 11:54:13 -080092 SkPoint pts[2] = {
93 { 0, 0 },
94 { SkIntToScalar(100), SkIntToScalar(100) }
95 };
96 SkShader::TileMode tm = SkShader::kClamp_TileMode;
97 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
98 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
99 SkMatrix scale;
100 scale.setScale(0.5f, 0.5f);
101 scale.postTranslate(5.f, 5.f);
102 SkPoint center0, center1;
103 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
104 SkScalarAve(pts[0].fY, pts[1].fY));
105 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
106 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed2ad1aa62016-03-09 09:50:50 -0800107 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
108 center0, (pts[1].fX - pts[0].fX) / 2,
109 colors, pos, SK_ARRAY_COUNT(colors), tm,
110 0, &scale);
joshualitt341400e2014-12-18 11:54:13 -0800111}
112
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000113// Simpler blurred RR test cases where all the radii are the same.
114class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +0000115public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000116 SimpleBlurRoundRectGM()
117 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000118 }
119
120protected:
joshualitt341400e2014-12-18 11:54:13 -0800121
mtklein36352bf2015-03-25 18:17:31 -0700122 SkString onShortName() override {
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000123 return fName;
124 }
125
mtklein36352bf2015-03-25 18:17:31 -0700126 SkISize onISize() override {
joshualitt341400e2014-12-18 11:54:13 -0800127 return SkISize::Make(1000, 500);
scroggo@google.com7b056592013-11-05 15:57:21 +0000128 }
129
mtklein36352bf2015-03-25 18:17:31 -0700130 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000131 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000132 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000133
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000134 const float blurRadii[] = { 1,5,10,20 };
135 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -0800136 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000137 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
138 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000139 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000140 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -0800141 for (int k = 0; k <= 1; k++) {
joshualitt341400e2014-12-18 11:54:13 -0800142 SkPaint paint;
143 paint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400144 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
145 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000146
joshualitt341400e2014-12-18 11:54:13 -0800147 bool useRadial = SkToBool(k);
148 if (useRadial) {
reed2ad1aa62016-03-09 09:50:50 -0800149 paint.setShader(MakeRadial());
joshualitt341400e2014-12-18 11:54:13 -0800150 }
151
152 SkRRect rrect;
153 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
154 SkIntToScalar(cornerRadii[j]));
155 canvas->drawRRect(rrect, paint);
156 canvas->translate(r.width() + SkIntToScalar(50), 0);
157 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000158 }
159 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000160 }
161private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000162 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000163
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000164 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000165};
166
167// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000168//
169// TODO(scroggo): Disabled in an attempt to rememdy
170// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
171// ran wrong number of tests')
172//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
173
scroggo@google.com7b056592013-11-05 15:57:21 +0000174// Rounded rect with two opposite corners with large radii, the other two
175// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000176DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000177
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000178DEF_GM(return new SimpleBlurRoundRectGM();)