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