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 #include "Sample.h"
8 #include "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkGradientShader.h"
11 #include "SkGraphics.h"
12 #include "SkPath.h"
13 #include "SkRandom.h"
14 #include "SkRegion.h"
15 #include "SkShader.h"
16 #include "SkUTF.h"
17 #include "SkColorPriv.h"
18 #include "SkColorFilter.h"
19 #include "SkTime.h"
20 #include "SkTypeface.h"
21 #include "SkVertices.h"
22
23 #include "SkOSFile.h"
24 #include "SkStream.h"
25
make_shader0(SkIPoint * size)26 static sk_sp<SkShader> make_shader0(SkIPoint* size) {
27 SkBitmap bm;
28 size->set(2, 2);
29 SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80);
30 SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff);
31 bm.allocN32Pixels(size->fX, size->fY);
32 bm.eraseColor(color0);
33 uint32_t* pixels = (uint32_t*) bm.getPixels();
34 pixels[0] = pixels[2] = color0;
35 pixels[1] = pixels[3] = color1;
36
37 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
38 }
39
make_shader1(const SkIPoint & size)40 static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
41 SkPoint pts[] = { { 0, 0 },
42 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
43 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
44 return SkGradientShader::MakeLinear(pts, colors, nullptr,
45 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
46 }
47
48 class VerticesView : public Sample {
49 sk_sp<SkShader> fShader0;
50 sk_sp<SkShader> fShader1;
51
52 public:
VerticesView()53 VerticesView() {
54 SkIPoint size;
55
56 fShader0 = make_shader0(&size);
57 fShader1 = make_shader1(size);
58
59 make_strip(&fRecs[0], size.fX, size.fY);
60 make_fan(&fRecs[1], size.fX, size.fY);
61 make_tris(&fRecs[2]);
62
63 fScale = SK_Scalar1;
64
65 this->setBGColor(SK_ColorGRAY);
66 }
67
68 protected:
onQuery(Sample::Event * evt)69 bool onQuery(Sample::Event* evt) override {
70 if (Sample::TitleQ(*evt)) {
71 Sample::TitleR(evt, "Vertices");
72 return true;
73 }
74 return this->INHERITED::onQuery(evt);
75 }
76
77 SkScalar fScale;
78
onDrawContent(SkCanvas * canvas)79 void onDrawContent(SkCanvas* canvas) override {
80 SkPaint paint;
81 paint.setDither(true);
82 paint.setFilterQuality(kLow_SkFilterQuality);
83
84 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
85 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
86 fRecs[i].fVerts, fRecs[i].fTexs,
87 nullptr);
88 canvas->save();
89
90 paint.setShader(nullptr);
91 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
92
93 canvas->translate(SkIntToScalar(250), 0);
94
95 paint.setShader(fShader0);
96 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
97
98 canvas->translate(SkIntToScalar(250), 0);
99
100 paint.setShader(fShader1);
101 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
102 canvas->restore();
103
104 canvas->translate(0, SkIntToScalar(250));
105 }
106 }
107
onFindClickHandler(SkScalar x,SkScalar y,unsigned)108 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
109 return new Click(this);
110 }
111
onClick(Click * click)112 bool onClick(Click* click) override {
113 // fCurrX = click->fICurr.fX;
114 // fCurrY = click->fICurr.fY;
115 return true;
116 }
117
118 private:
119 struct Rec {
120 SkVertices::VertexMode fMode;
121 int fCount;
122 SkPoint* fVerts;
123 SkPoint* fTexs;
124
RecVerticesView::Rec125 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
~RecVerticesView::Rec126 ~Rec() { delete[] fVerts; delete[] fTexs; }
127 };
128
make_tris(Rec * rec)129 void make_tris(Rec* rec) {
130 int n = 10;
131 SkRandom rand;
132
133 rec->fMode = SkVertices::kTriangles_VertexMode;
134 rec->fCount = n * 3;
135 rec->fVerts = new SkPoint[rec->fCount];
136
137 for (int i = 0; i < n; i++) {
138 SkPoint* v = &rec->fVerts[i*3];
139 for (int j = 0; j < 3; j++) {
140 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
141 }
142 }
143 }
144
make_fan(Rec * rec,int texWidth,int texHeight)145 void make_fan(Rec* rec, int texWidth, int texHeight) {
146 const SkScalar tx = SkIntToScalar(texWidth);
147 const SkScalar ty = SkIntToScalar(texHeight);
148 const int n = 24;
149
150 rec->fMode = SkVertices::kTriangleFan_VertexMode;
151 rec->fCount = n + 2;
152 rec->fVerts = new SkPoint[rec->fCount];
153 rec->fTexs = new SkPoint[rec->fCount];
154
155 SkPoint* v = rec->fVerts;
156 SkPoint* t = rec->fTexs;
157
158 v[0].set(0, 0);
159 t[0].set(0, 0);
160 for (int i = 0; i < n; i++) {
161 SkScalar cos;
162 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
163 v[i+1].set(cos, sin);
164 t[i+1].set(i*tx/n, ty);
165 }
166 v[n+1] = v[1];
167 t[n+1].set(tx, ty);
168
169 SkMatrix m;
170 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
171 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
172 m.mapPoints(v, rec->fCount);
173 }
174
make_strip(Rec * rec,int texWidth,int texHeight)175 void make_strip(Rec* rec, int texWidth, int texHeight) {
176 const SkScalar tx = SkIntToScalar(texWidth);
177 const SkScalar ty = SkIntToScalar(texHeight);
178 const int n = 24;
179
180 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
181 rec->fCount = 2 * (n + 1);
182 rec->fVerts = new SkPoint[rec->fCount];
183 rec->fTexs = new SkPoint[rec->fCount];
184
185 SkPoint* v = rec->fVerts;
186 SkPoint* t = rec->fTexs;
187
188 for (int i = 0; i < n; i++) {
189 SkScalar cos;
190 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
191 v[i*2 + 0].set(cos/2, sin/2);
192 v[i*2 + 1].set(cos, sin);
193
194 t[i*2 + 0].set(tx * i / n, ty);
195 t[i*2 + 1].set(tx * i / n, 0);
196 }
197 v[2*n + 0] = v[0];
198 v[2*n + 1] = v[1];
199
200 t[2*n + 0].set(tx, ty);
201 t[2*n + 1].set(tx, 0);
202
203 SkMatrix m;
204 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
205 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
206 m.mapPoints(v, rec->fCount);
207 }
208
209 Rec fRecs[3];
210
211 typedef Sample INHERITED;
212 };
213
214 //////////////////////////////////////////////////////////////////////////////
215
216 DEF_SAMPLE( return new VerticesView(); )
217