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 bm.setConfig(SkBitmap::kARGB_8888_Config, size->fX, size->fY);
32 SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80);
33 SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff);
34 bm.allocPixels();
35 bm.eraseColor(color0);
36 bm.lockPixels();
37 uint32_t* pixels = (uint32_t*) bm.getPixels();
38 pixels[0] = pixels[2] = color0;
39 pixels[1] = pixels[3] = color1;
40 bm.unlockPixels();
41
42 return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
43 SkShader::kRepeat_TileMode);
44 }
45
make_shader1(const SkIPoint & size)46 static SkShader* make_shader1(const SkIPoint& size) {
47 SkPoint pts[] = { { 0, 0 },
48 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
49 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
50 return SkGradientShader::CreateLinear(pts, colors, NULL,
51 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL);
52 }
53
54 class VerticesView : public SampleView {
55 SkShader* fShader0;
56 SkShader* fShader1;
57
58 public:
VerticesView()59 VerticesView() {
60 SkIPoint size;
61
62 fShader0 = make_shader0(&size);
63 fShader1 = make_shader1(size);
64
65 make_strip(&fRecs[0], size.fX, size.fY);
66 make_fan(&fRecs[1], size.fX, size.fY);
67 make_tris(&fRecs[2]);
68
69 fScale = SK_Scalar1;
70
71 this->setBGColor(SK_ColorGRAY);
72 }
73
~VerticesView()74 virtual ~VerticesView() {
75 SkSafeUnref(fShader0);
76 SkSafeUnref(fShader1);
77 }
78
79 protected:
80 // overrides from SkEventSink
onQuery(SkEvent * evt)81 virtual bool onQuery(SkEvent* evt) {
82 if (SampleCode::TitleQ(*evt))
83 {
84 SkString str("Vertices");
85 SampleCode::TitleR(evt, str.c_str());
86 return true;
87 }
88 return this->INHERITED::onQuery(evt);
89 }
90
91 SkScalar fScale;
92
onDrawContent(SkCanvas * canvas)93 virtual void onDrawContent(SkCanvas* canvas) {
94 SkPaint paint;
95 paint.setDither(true);
96 paint.setFilterBitmap(true);
97
98 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
99 canvas->save();
100
101 paint.setShader(NULL);
102 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
103 fRecs[i].fVerts, fRecs[i].fTexs,
104 NULL, NULL, NULL, 0, paint);
105
106 canvas->translate(SkIntToScalar(250), 0);
107
108 paint.setShader(fShader0);
109 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
110 fRecs[i].fVerts, fRecs[i].fTexs,
111 NULL, NULL, NULL, 0, paint);
112
113 canvas->translate(SkIntToScalar(250), 0);
114
115 paint.setShader(fShader1);
116 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
117 fRecs[i].fVerts, fRecs[i].fTexs,
118 NULL, NULL, NULL, 0, paint);
119 canvas->restore();
120
121 canvas->translate(0, SkIntToScalar(250));
122 }
123 }
124
onFindClickHandler(SkScalar x,SkScalar y)125 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
126 return new Click(this);
127 }
128
onClick(Click * click)129 virtual bool onClick(Click* click) {
130 // fCurrX = click->fICurr.fX;
131 // fCurrY = click->fICurr.fY;
132 this->inval(NULL);
133 return true;
134 }
135
136 private:
137 struct Rec {
138 SkCanvas::VertexMode fMode;
139 int fCount;
140 SkPoint* fVerts;
141 SkPoint* fTexs;
142
RecVerticesView::Rec143 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
~RecVerticesView::Rec144 ~Rec() { delete[] fVerts; delete[] fTexs; }
145 };
146
make_tris(Rec * rec)147 void make_tris(Rec* rec) {
148 int n = 10;
149 SkRandom rand;
150
151 rec->fMode = SkCanvas::kTriangles_VertexMode;
152 rec->fCount = n * 3;
153 rec->fVerts = new SkPoint[rec->fCount];
154
155 for (int i = 0; i < n; i++) {
156 SkPoint* v = &rec->fVerts[i*3];
157 for (int j = 0; j < 3; j++) {
158 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
159 }
160 }
161 }
162
make_fan(Rec * rec,int texWidth,int texHeight)163 void make_fan(Rec* rec, int texWidth, int texHeight) {
164 const SkScalar tx = SkIntToScalar(texWidth);
165 const SkScalar ty = SkIntToScalar(texHeight);
166 const int n = 24;
167
168 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
169 rec->fCount = n + 2;
170 rec->fVerts = new SkPoint[rec->fCount];
171 rec->fTexs = new SkPoint[rec->fCount];
172
173 SkPoint* v = rec->fVerts;
174 SkPoint* t = rec->fTexs;
175
176 v[0].set(0, 0);
177 t[0].set(0, 0);
178 for (int i = 0; i < n; i++) {
179 SkScalar cos;
180 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
181 v[i+1].set(cos, sin);
182 t[i+1].set(i*tx/n, ty);
183 }
184 v[n+1] = v[1];
185 t[n+1].set(tx, ty);
186
187 SkMatrix m;
188 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
189 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
190 m.mapPoints(v, rec->fCount);
191 }
192
make_strip(Rec * rec,int texWidth,int texHeight)193 void make_strip(Rec* rec, int texWidth, int texHeight) {
194 const SkScalar tx = SkIntToScalar(texWidth);
195 const SkScalar ty = SkIntToScalar(texHeight);
196 const int n = 24;
197
198 rec->fMode = SkCanvas::kTriangleStrip_VertexMode;
199 rec->fCount = 2 * (n + 1);
200 rec->fVerts = new SkPoint[rec->fCount];
201 rec->fTexs = new SkPoint[rec->fCount];
202
203 SkPoint* v = rec->fVerts;
204 SkPoint* t = rec->fTexs;
205
206 for (int i = 0; i < n; i++) {
207 SkScalar cos;
208 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
209 v[i*2 + 0].set(cos/2, sin/2);
210 v[i*2 + 1].set(cos, sin);
211
212 t[i*2 + 0].set(tx * i / n, ty);
213 t[i*2 + 1].set(tx * i / n, 0);
214 }
215 v[2*n + 0] = v[0];
216 v[2*n + 1] = v[1];
217
218 t[2*n + 0].set(tx, ty);
219 t[2*n + 1].set(tx, 0);
220
221 SkMatrix m;
222 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
223 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
224 m.mapPoints(v, rec->fCount);
225 }
226
227 Rec fRecs[3];
228
229 typedef SampleView INHERITED;
230 };
231
232 //////////////////////////////////////////////////////////////////////////////
233
MyFactory()234 static SkView* MyFactory() { return new VerticesView; }
235 static SkViewRegister reg(MyFactory);
236
237