xref: /third_party/skia/gm/blurroundrect.cpp (revision cb93a386)
1/*
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/gm.h"
9#include "include/core/SkBlendMode.h"
10#include "include/core/SkBlurTypes.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkColorFilter.h"
14#include "include/core/SkMaskFilter.h"
15#include "include/core/SkMatrix.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRRect.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
22#include "include/core/SkShader.h"
23#include "include/core/SkSize.h"
24#include "include/core/SkString.h"
25#include "include/core/SkTileMode.h"
26#include "include/core/SkTypes.h"
27#include "include/effects/SkGradientShader.h"
28#include "src/core/SkBlurMask.h"
29
30/*
31 * Spits out an arbitrary gradient to test blur with shader on paint
32 */
33static sk_sp<SkShader> MakeRadial() {
34    SkPoint pts[2] = {
35        { 0, 0 },
36        { SkIntToScalar(100), SkIntToScalar(100) }
37    };
38    SkTileMode tm = SkTileMode::kClamp;
39    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));
49    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);
53}
54
55// Simpler blurred RR test cases where all the radii are the same.
56class SimpleBlurRoundRectGM : public skiagm::GM {
57    SkString onShortName() override { return SkString("simpleblurroundrect"); }
58
59    SkISize onISize() override { return {1000, 500}; }
60
61    bool runAsBench() const override { return true; }
62
63    void onDraw(SkCanvas* canvas) override {
64        canvas->scale(1.5f, 1.5f);
65        canvas->translate(50,50);
66
67        const float blurRadii[] = { 1,5,10,20 };
68        const int cornerRadii[] = { 1,5,10,20 };
69        const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
70        for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
71            SkAutoCanvasRestore autoRestore(canvas, true);
72            canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
73            for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
74                for (int k = 0; k <= 1; k++) {
75                    SkPaint paint;
76                    paint.setColor(SK_ColorBLACK);
77                    paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
78                                   SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
79
80                    bool useRadial = SkToBool(k);
81                    if (useRadial) {
82                        paint.setShader(MakeRadial());
83                    }
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                }
91            }
92        }
93    }
94};
95
96// Create one with dimensions/rounded corners based on the skp
97//
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
103DEF_GM(return new SimpleBlurRoundRectGM();)
104
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}
122