1/*
2 * Copyright 2011 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 "sk_tool_utils.h"
10#include "Resources.h"
11#include "SkBlurMask.h"
12#include "SkBlurMaskFilter.h"
13#include "SkImage.h"
14#include "SkPath.h"
15
16DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, sk_tool_utils::color_to_565(0xFFDDDDDD)) {
17        SkBlurStyle NONE = SkBlurStyle(-999);
18        const struct {
19            SkBlurStyle fStyle;
20            int         fCx, fCy;
21        } gRecs[] = {
22            { NONE,                 0,  0 },
23            { kInner_SkBlurStyle,  -1,  0 },
24            { kNormal_SkBlurStyle,  0,  1 },
25            { kSolid_SkBlurStyle,   0, -1 },
26            { kOuter_SkBlurStyle,   1,  0 },
27        };
28
29        SkPaint paint;
30        paint.setAntiAlias(true);
31        sk_tool_utils::set_portable_typeface(&paint);
32        paint.setTextSize(SkIntToScalar(25));
33        canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
34
35        SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag;
36        for (int j = 0; j < 2; j++) {
37            canvas->save();
38            paint.setColor(SK_ColorBLUE);
39            for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
40                if (gRecs[i].fStyle != NONE) {
41                    paint.setMaskFilter(SkBlurMaskFilter::Make(gRecs[i].fStyle,
42                                           SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
43                                           flags));
44                } else {
45                    paint.setMaskFilter(nullptr);
46                }
47                canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
48                                   SkIntToScalar(200 + gRecs[i].fCy*100),
49                                   SkIntToScalar(50),
50                                   paint);
51            }
52            // draw text
53            {
54                paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
55                                           SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
56                                           flags));
57                SkScalar x = SkIntToScalar(70);
58                SkScalar y = SkIntToScalar(400);
59                paint.setColor(SK_ColorBLACK);
60                canvas->drawString("Hamburgefons Style", x, y, paint);
61                canvas->drawString("Hamburgefons Style",
62                                 x, y + SkIntToScalar(50), paint);
63                paint.setMaskFilter(nullptr);
64                paint.setColor(SK_ColorWHITE);
65                x -= SkIntToScalar(2);
66                y -= SkIntToScalar(2);
67                canvas->drawString("Hamburgefons Style", x, y, paint);
68            }
69            canvas->restore();
70            flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
71            canvas->translate(SkIntToScalar(350), SkIntToScalar(0));
72        }
73}
74
75//////////////////////////////////////////////////////////////////////////////////////////////
76
77// exercise a special-case of blurs, which is two nested rects. These are drawn specially,
78// and possibly cached.
79//
80// in particular, we want to notice that the 2nd rect draws slightly differently, since it
81// is translated a fractional amount.
82//
83DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) {
84        SkPaint paint;
85
86        paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 2.3f));
87
88        SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
89        SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
90        SkPath path;
91        path.addRect(outer, SkPath::kCW_Direction);
92        path.addRect(inner, SkPath::kCCW_Direction);
93
94        canvas->drawPath(path, paint);
95        // important to translate by a factional amount to exercise a different "phase"
96        // of the same path w.r.t. the pixel grid
97        SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
98        canvas->translate(dx, 0);
99        canvas->drawPath(path, paint);
100}
101
102DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) {
103        SkPaint paint;
104        paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 4.3f));
105
106        SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
107        SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
108        SkPath path;
109        path.addRect(outer, SkPath::kCW_Direction);
110        path.addRect(inner, SkPath::kCW_Direction);
111        canvas->drawPath(path, paint);
112
113        SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
114        canvas->translate(dx, 0);
115        canvas->drawPath(path, paint);
116
117        // Translate to outside of clip bounds.
118        canvas->translate(-dx, 0);
119        canvas->translate(-30, -150);
120        canvas->drawPath(path, paint);
121}
122
123DEF_SIMPLE_GM(BlurDrawImage, canvas, 256, 256) {
124    SkPaint paint;
125    paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 10, 0));
126    canvas->clear(0xFF88FF88);
127    if (auto image = GetResourceAsImage("images/mandrill_512_q075.jpg")) {
128        canvas->scale(0.25, 0.25);
129        canvas->drawImage(image, 256, 256, &paint);
130    }
131}
132