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