xref: /aosp_15_r20/external/skia/gm/shaderpath.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPath.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTileMode.h"
23 #include "include/core/SkTypes.h"
24 #include "include/effects/SkGradientShader.h"
25 #include "tools/ToolUtils.h"
26 
27 #include <string.h>
28 
29 namespace skiagm {
30 
makebm(SkBitmap * bm,int w,int h)31 static void makebm(SkBitmap* bm, int w, int h) {
32     bm->allocN32Pixels(w, h);
33     bm->eraseColor(SK_ColorTRANSPARENT);
34 
35     SkCanvas    canvas(*bm);
36     SkScalar    s = SkIntToScalar(std::min(w, h));
37     const SkPoint     kPts0[] = { { 0, 0 }, { s, s } };
38     const SkPoint     kPts1[] = { { s/2, 0 }, { s/2, s } };
39     const SkScalar    kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
40     const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
41     const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
42 
43 
44     SkPaint     paint;
45 
46     paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
47                     std::size(kColors0), SkTileMode::kClamp));
48     canvas.drawPaint(paint);
49     paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
50                     std::size(kColors1), SkTileMode::kClamp));
51     canvas.drawPaint(paint);
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////
55 
56 struct LabeledMatrix {
57     SkMatrix    fMatrix;
58     const char* fLabel;
59 };
60 
61 constexpr int kPointSize = 300;
62 
63 class ShaderPathGM : public GM {
64 public:
ShaderPathGM()65     ShaderPathGM() {
66         this->setBGColor(0xFFDDDDDD);
67     }
68 
69 protected:
getName() const70     SkString getName() const override { return SkString("shaderpath"); }
71 
getISize()72     SkISize getISize() override { return SkISize::Make(820, 930); }
73 
onOnceBeforeDraw()74     void onOnceBeforeDraw() override {
75         makebm(&fBmp, kPointSize / 4, kPointSize / 4);
76     }
77 
onDraw(SkCanvas * canvas)78     void onDraw(SkCanvas* canvas) override {
79 
80         SkPaint bmpPaint;
81         bmpPaint.setAntiAlias(true);
82         bmpPaint.setAlphaf(0.5f);
83         SkSamplingOptions sampling(SkFilterMode::kLinear);
84 
85         canvas->drawImage(fBmp.asImage(), 5.f, 5.f, sampling, &bmpPaint);
86 
87         SkPaint outlinePaint;
88         outlinePaint.setStyle(SkPaint::kStroke_Style);
89         outlinePaint.setStrokeWidth(0.f);
90 
91         canvas->translate(15.f, 15.f);
92         canvas->scale(2.f, 2.f);
93 
94         constexpr SkTileMode kTileModes[] = {
95             SkTileMode::kRepeat,
96             SkTileMode::kMirror,
97         };
98 
99         // position the baseline of the first path
100         canvas->translate(0.f, 2.25);
101 
102         SkPath path;
103         path.moveTo(0, 40).cubicTo(10, 70, 20, 10, 30, 40);
104 
105         canvas->save();
106         int i = 0;
107         for (size_t tm0 = 0; tm0 < std::size(kTileModes); ++tm0) {
108             for (size_t tm1 = 0; tm1 < std::size(kTileModes); ++tm1) {
109                 SkMatrix localM;
110                 localM.setTranslate(5.f, 5.f);
111                 localM.postRotate(20);
112                 localM.postScale(1.15f, .85f);
113 
114                 SkPaint fillPaint;
115                 fillPaint.setAntiAlias(true);
116                 fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1],
117                                                     sampling, localM));
118 
119                 canvas->drawPath(path, fillPaint);
120                 canvas->drawPath(path, outlinePaint);
121                 canvas->translate(50.f, 0.f);
122                 ++i;
123                 if (!(i % 2)) {
124                     canvas->restore();
125                     canvas->translate(0, 22.5f);
126                     canvas->save();
127                 }
128             }
129         }
130         canvas->restore();
131     }
132 
133 private:
134     SkBitmap fBmp;
135     using INHERITED = GM;
136 };
137 
138 ///////////////////////////////////////////////////////////////////////////////
139 
140 DEF_GM( return new ShaderPathGM; )
141 }  // namespace skiagm
142