1 /*
2 * Copyright 2014 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/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkFont.h"
15 #include "include/core/SkMaskFilter.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkSurface.h"
22 #include "include/core/SkTypeface.h"
23 #include "src/core/SkBlurMask.h"
24 #include "src/effects/SkEmbossMaskFilter.h"
25
make_bm()26 static sk_sp<SkImage> make_bm() {
27 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
28
29 SkPaint paint;
30 paint.setAntiAlias(true);
31 surf->getCanvas()->drawCircle(50, 50, 50, paint);
32 return surf->makeImageSnapshot();
33 }
34
35 class EmbossGM : public skiagm::GM {
36 public:
EmbossGM()37 EmbossGM() {
38 }
39
40 protected:
onShortName()41 SkString onShortName() override {
42 return SkString("emboss");
43 }
44
onISize()45 SkISize onISize() override {
46 return SkISize::Make(600, 120);
47 }
48
onDraw(SkCanvas * canvas)49 void onDraw(SkCanvas* canvas) override {
50 SkPaint paint;
51 auto img = make_bm();
52 canvas->drawImage(img, 10, 10);
53 canvas->translate(img->width() + SkIntToScalar(10), 0);
54
55 paint.setMaskFilter(SkEmbossMaskFilter::Make(
56 SkBlurMask::ConvertRadiusToSigma(3),
57 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
58 canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
59 canvas->translate(img->width() + SkIntToScalar(10), 0);
60
61 // this combination of emboss+colorfilter used to crash -- so we exercise it to
62 // confirm that we have a fix.
63 paint.setColorFilter(SkColorFilters::Blend(0xFFFF0000, SkBlendMode::kSrcATop));
64 canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
65 canvas->translate(img->width() + SkIntToScalar(10), 0);
66
67 paint.setAntiAlias(true);
68 paint.setStyle(SkPaint::kStroke_Style);
69 paint.setStrokeWidth(SkIntToScalar(10));
70 paint.setMaskFilter(SkEmbossMaskFilter::Make(
71 SkBlurMask::ConvertRadiusToSigma(4),
72 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
73 paint.setColorFilter(nullptr);
74 paint.setShader(SkShaders::Color(SK_ColorBLUE));
75 paint.setDither(true);
76 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
77 SkIntToScalar(30), paint);
78 canvas->translate(SkIntToScalar(100), 0);
79
80 paint.setStyle(SkPaint::kFill_Style);
81 canvas->drawString("Hello", 0, 50, SkFont(nullptr, 50), paint);
82
83 paint.setShader(nullptr);
84 paint.setColor(SK_ColorGREEN);
85 canvas->drawString("World", 0, 100, SkFont(nullptr, 50), paint);
86 }
87
88 private:
89 using INHERITED = skiagm::GM;
90 };
91
92 DEF_GM(return new EmbossGM;)
93