Debian Codegraph @

gm/shaderpath.cpp

raw
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/SkPathBuilder.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkGradient.h"
26 #include "src/core/SkColorPriv.h"
27 #include "tools/ToolUtils.h"
28
29 #include <string.h>
30
31 namespace skiagm {
32
33 static void makebm(SkBitmap* bm, int w, int h) {
34 bm->allocN32Pixels(w, h);
35 bm->eraseColor(SK_ColorTRANSPARENT);
36
37 SkCanvas canvas(*bm);
38 SkScalar s = SkIntToScalar(std::min(w, h));
39 const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
40 const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
41 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
42
43 SkPaint paint;
44 SkColorConverter conv0({0x80F00080, 0xF0F08000, 0x800080F0});
45 paint.setShader(SkShaders::LinearGradient(kPts0,
46 {{conv0.colors4f(), kPos, SkTileMode::kClamp}, {}}));
47 canvas.drawPaint(paint);
48 SkColorConverter conv1({0xF08000F0, 0x8080F000, 0xF000F080});
49 paint.setShader(SkShaders::LinearGradient(kPts1,
50 {{conv1.colors4f(), kPos, 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:
65 ShaderPathGM() {
66 this->setBGColor(0xFFDDDDDD);
67 }
68
69 protected:
70 SkString getName() const override { return SkString("shaderpath"); }
71
72 SkISize getISize() override { return SkISize::Make(820, 930); }
73
74 void onOnceBeforeDraw() override {
75 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
76 }
77
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 = SkPathBuilder()
103 .moveTo(0, 40)
104 .cubicTo(10, 70, 20, 10, 30, 40)
105 .detach();
106
107 canvas->save();
108 int i = 0;
109 for (size_t tm0 = 0; tm0 < std::size(kTileModes); ++tm0) {
110 for (size_t tm1 = 0; tm1 < std::size(kTileModes); ++tm1) {
111 SkMatrix localM;
112 localM.setTranslate(5.f, 5.f);
113 localM.postRotate(20);
114 localM.postScale(1.15f, .85f);
115
116 SkPaint fillPaint;
117 fillPaint.setAntiAlias(true);
118 fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1],
119 sampling, localM));
120
121 canvas->drawPath(path, fillPaint);
122 canvas->drawPath(path, outlinePaint);
123 canvas->translate(50.f, 0.f);
124 ++i;
125 if (!(i % 2)) {
126 canvas->restore();
127 canvas->translate(0, 22.5f);
128 canvas->save();
129 }
130 }
131 }
132 canvas->restore();
133 }
134
135 private:
136 SkBitmap fBmp;
137 using INHERITED = GM;
138 };
139
140 ///////////////////////////////////////////////////////////////////////////////
141
142 DEF_GM( return new ShaderPathGM; )
143 } // namespace skiagm