xref: /aosp_15_r20/external/skia/gm/emboss.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "tools/fonts/FontToolUtils.h"
26 
make_bm()27 static sk_sp<SkImage> make_bm() {
28     auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
29 
30     SkPaint paint;
31     paint.setAntiAlias(true);
32     surf->getCanvas()->drawCircle(50, 50, 50, paint);
33     return surf->makeImageSnapshot();
34 }
35 
36 class EmbossGM : public skiagm::GM {
37 public:
EmbossGM()38     EmbossGM() {
39     }
40 
41 protected:
getName() const42     SkString getName() const override { return SkString("emboss"); }
43 
getISize()44     SkISize getISize() override { return SkISize::Make(600, 120); }
45 
onDraw(SkCanvas * canvas)46     void onDraw(SkCanvas* canvas) override {
47         SkPaint paint;
48         auto img = make_bm();
49         canvas->drawImage(img, 10, 10);
50         canvas->translate(img->width() + SkIntToScalar(10), 0);
51 
52         paint.setMaskFilter(SkEmbossMaskFilter::Make(
53             SkBlurMask::ConvertRadiusToSigma(3),
54             { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
55         canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
56         canvas->translate(img->width() + SkIntToScalar(10), 0);
57 
58         // this combination of emboss+colorfilter used to crash -- so we exercise it to
59         // confirm that we have a fix.
60         paint.setColorFilter(SkColorFilters::Blend(0xFFFF0000, SkBlendMode::kSrcATop));
61         canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
62         canvas->translate(img->width() + SkIntToScalar(10), 0);
63 
64         paint.setAntiAlias(true);
65         paint.setStyle(SkPaint::kStroke_Style);
66         paint.setStrokeWidth(SkIntToScalar(10));
67         paint.setMaskFilter(SkEmbossMaskFilter::Make(
68             SkBlurMask::ConvertRadiusToSigma(4),
69             { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
70         paint.setColorFilter(nullptr);
71         paint.setShader(SkShaders::Color(SK_ColorBLUE));
72         paint.setDither(true);
73         canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
74                            SkIntToScalar(30), paint);
75         canvas->translate(SkIntToScalar(100), 0);
76 
77         SkFont font = SkFont(ToolUtils::DefaultPortableTypeface(), 50);
78         paint.setStyle(SkPaint::kFill_Style);
79         canvas->drawString("Hello", 0, 50, font, paint);
80 
81         paint.setShader(nullptr);
82         paint.setColor(SK_ColorGREEN);
83         canvas->drawString("World", 0, 100, font, paint);
84     }
85 
86 private:
87     using INHERITED = skiagm::GM;
88 };
89 
90 DEF_GM(return new EmbossGM;)
91