blob: 48e394c8576baa1c78c5188c81e6cac59690da3f [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"
14#include "include/core/SkMaskFilter.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040015#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkPoint.h"
18#include "include/core/SkRRect.h"
19#include "include/core/SkRect.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040020#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
22#include "include/core/SkShader.h"
23#include "include/core/SkSize.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkString.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040025#include "include/core/SkTileMode.h"
26#include "include/core/SkTypes.h"
27#include "include/effects/SkGradientShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkBlurMask.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000029
joshualitt341400e2014-12-18 11:54:13 -080030/*
Kevin Lubickbe03ef12021-06-16 15:28:00 -040031 * Spits out an arbitrary gradient to test blur with shader on paint
joshualitt341400e2014-12-18 11:54:13 -080032 */
reed2ad1aa62016-03-09 09:50:50 -080033static sk_sp<SkShader> MakeRadial() {
joshualitt341400e2014-12-18 11:54:13 -080034 SkPoint pts[2] = {
35 { 0, 0 },
36 { SkIntToScalar(100), SkIntToScalar(100) }
37 };
Mike Reedfae8fce2019-04-03 10:27:45 -040038 SkTileMode tm = SkTileMode::kClamp;
joshualitt341400e2014-12-18 11:54:13 -080039 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
40 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
41 SkMatrix scale;
42 scale.setScale(0.5f, 0.5f);
43 scale.postTranslate(5.f, 5.f);
44 SkPoint center0, center1;
45 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
46 SkScalarAve(pts[0].fY, pts[1].fY));
47 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
48 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed2ad1aa62016-03-09 09:50:50 -080049 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
50 center0, (pts[1].fX - pts[0].fX) / 2,
51 colors, pos, SK_ARRAY_COUNT(colors), tm,
52 0, &scale);
joshualitt341400e2014-12-18 11:54:13 -080053}
54
scroggo@google.com8610d2c2013-11-05 21:10:58 +000055// Simpler blurred RR test cases where all the radii are the same.
56class SimpleBlurRoundRectGM : public skiagm::GM {
Hal Canary594fe852019-07-18 13:35:49 -040057 SkString onShortName() override { return SkString("simpleblurroundrect"); }
scroggo@google.com7b056592013-11-05 15:57:21 +000058
Hal Canary594fe852019-07-18 13:35:49 -040059 SkISize onISize() override { return {1000, 500}; }
scroggo@google.com7b056592013-11-05 15:57:21 +000060
John Stilesa2d46a12020-06-11 14:12:44 -040061 bool runAsBench() const override { return true; }
62
mtklein36352bf2015-03-25 18:17:31 -070063 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000064 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +000065 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +000066
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +000067 const float blurRadii[] = { 1,5,10,20 };
68 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -080069 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +000070 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
71 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +000072 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +000073 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -080074 for (int k = 0; k <= 1; k++) {
joshualitt341400e2014-12-18 11:54:13 -080075 SkPaint paint;
76 paint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -040077 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
78 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
scroggo@google.com8610d2c2013-11-05 21:10:58 +000079
joshualitt341400e2014-12-18 11:54:13 -080080 bool useRadial = SkToBool(k);
81 if (useRadial) {
reed2ad1aa62016-03-09 09:50:50 -080082 paint.setShader(MakeRadial());
joshualitt341400e2014-12-18 11:54:13 -080083 }
84
85 SkRRect rrect;
86 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
87 SkIntToScalar(cornerRadii[j]));
88 canvas->drawRRect(rrect, paint);
89 canvas->translate(r.width() + SkIntToScalar(50), 0);
90 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +000091 }
92 }
scroggo@google.com7b056592013-11-05 15:57:21 +000093 }
scroggo@google.com7b056592013-11-05 15:57:21 +000094};
95
96// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +000097//
98// TODO(scroggo): Disabled in an attempt to rememdy
99// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
100// ran wrong number of tests')
101//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
102
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000103DEF_GM(return new SimpleBlurRoundRectGM();)
Brian Salomon5e961b02020-10-21 14:28:47 -0400104
105// From crbug.com/1138810
106DEF_SIMPLE_GM(blur_large_rrects, canvas, 300, 300) {
107 SkPaint paint;
108 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 20.f));
109
110 auto rect = SkRect::MakeLTRB(5.f, -20000.f, 240.f, 25.f);
111 SkRRect rrect = SkRRect::MakeRectXY(rect, 40.f, 40.f);
112 for (int i = 0; i < 4; ++i) {
113 SkColor4f color{(i & 1) ? 1.f : 0.f,
114 (i & 2) ? 1.f : 0.f,
115 (i < 2) ? 1.f : 0.f,
116 1.f};
117 paint.setColor(color);
118 canvas->drawRRect(rrect, paint);
119 canvas->rotate(90.f, 150.f, 150.f);
120 }
121}