• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 a dummy gradient to test blur with shader on paint
32  */
MakeRadial()33 static 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.
56 class SimpleBlurRoundRectGM : public skiagm::GM {
onShortName()57     SkString onShortName() override { return SkString("simpleblurroundrect"); }
58 
onISize()59     SkISize onISize() override { return {1000, 500}; }
60 
onDraw(SkCanvas * canvas)61     void onDraw(SkCanvas* canvas) override {
62         canvas->scale(1.5f, 1.5f);
63         canvas->translate(50,50);
64 
65         const float blurRadii[] = { 1,5,10,20 };
66         const int cornerRadii[] = { 1,5,10,20 };
67         const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
68         for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
69             SkAutoCanvasRestore autoRestore(canvas, true);
70             canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
71             for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
72                 for (int k = 0; k <= 1; k++) {
73                     SkPaint paint;
74                     paint.setColor(SK_ColorBLACK);
75                     paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
76                                    SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
77 
78                     bool useRadial = SkToBool(k);
79                     if (useRadial) {
80                         paint.setShader(MakeRadial());
81                     }
82 
83                     SkRRect rrect;
84                     rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
85                                     SkIntToScalar(cornerRadii[j]));
86                     canvas->drawRRect(rrect, paint);
87                     canvas->translate(r.width() + SkIntToScalar(50), 0);
88                 }
89             }
90         }
91     }
92 };
93 
94 // Create one with dimensions/rounded corners based on the skp
95 //
96 // TODO(scroggo): Disabled in an attempt to rememdy
97 // https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
98 // ran wrong number of tests')
99 //DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
100 
101 DEF_GM(return new SimpleBlurRoundRectGM();)
102