blob: 53db2ea3fa8f2cb521563eaecb887c98d949998e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRandom.h"
16#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
19#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkColorPriv.h"
21#include "SkColorFilter.h"
22#include "SkTime.h"
23#include "SkTypeface.h"
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkOSFile.h"
26#include "SkStream.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028static SkShader* make_shader0(SkIPoint* size) {
29 SkBitmap bm;
bsalomon@google.com8295dc12011-05-02 12:53:34 +000030 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();
reed@google.com82065d62011-02-07 15:30:46 +000041
bsalomon@google.com8295dc12011-05-02 12:53:34 +000042 return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
43 SkShader::kRepeat_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044}
45
46static SkShader* make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000047 SkPoint pts[] = { { 0, 0 },
48 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 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
reed@google.comf2183392011-04-22 14:10:48 +000054class VerticesView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 SkShader* fShader0;
56 SkShader* fShader1;
57
reed@google.com82065d62011-02-07 15:30:46 +000058public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000059 VerticesView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 SkIPoint size;
reed@google.com82065d62011-02-07 15:30:46 +000061
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 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]);
reed@google.com82065d62011-02-07 15:30:46 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 fScale = SK_Scalar1;
reed@google.comf2183392011-04-22 14:10:48 +000070
71 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 }
reed@google.com82065d62011-02-07 15:30:46 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 virtual ~VerticesView() {
reed@google.com82065d62011-02-07 15:30:46 +000075 SkSafeUnref(fShader0);
76 SkSafeUnref(fShader1);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 }
reed@google.com82065d62011-02-07 15:30:46 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079protected:
80 // overrides from SkEventSink
81 virtual bool onQuery(SkEvent* evt) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000082 if (SampleCode::TitleQ(*evt)) {
83 SampleCode::TitleR(evt, "Vertices");
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 return true;
85 }
86 return this->INHERITED::onQuery(evt);
87 }
reed@google.com82065d62011-02-07 15:30:46 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 SkScalar fScale;
reed@google.com82065d62011-02-07 15:30:46 +000090
reed@google.comf2183392011-04-22 14:10:48 +000091 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 SkPaint paint;
93 paint.setDither(true);
94 paint.setFilterBitmap(true);
reed@google.com82065d62011-02-07 15:30:46 +000095
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000096 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 canvas->save();
reed@google.com82065d62011-02-07 15:30:46 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 paint.setShader(NULL);
100 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
101 fRecs[i].fVerts, fRecs[i].fTexs,
102 NULL, NULL, NULL, 0, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 paint.setShader(fShader0);
107 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
108 fRecs[i].fVerts, fRecs[i].fTexs,
109 NULL, NULL, NULL, 0, paint);
110
111 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 paint.setShader(fShader1);
114 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
115 fRecs[i].fVerts, fRecs[i].fTexs,
116 NULL, NULL, NULL, 0, paint);
117 canvas->restore();
reed@google.com82065d62011-02-07 15:30:46 +0000118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 canvas->translate(0, SkIntToScalar(250));
120 }
121 }
reed@google.com82065d62011-02-07 15:30:46 +0000122
sugoi@google.com9c55f802013-03-07 20:52:59 +0000123 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 return new Click(this);
125 }
reed@google.com82065d62011-02-07 15:30:46 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 virtual bool onClick(Click* click) {
128 // fCurrX = click->fICurr.fX;
129 // fCurrY = click->fICurr.fY;
130 this->inval(NULL);
131 return true;
132 }
reed@google.com82065d62011-02-07 15:30:46 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134private:
135 struct Rec {
136 SkCanvas::VertexMode fMode;
137 int fCount;
138 SkPoint* fVerts;
139 SkPoint* fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
142 ~Rec() { delete[] fVerts; delete[] fTexs; }
143 };
144
145 void make_tris(Rec* rec) {
146 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000147 SkRandom rand;
reed@google.com82065d62011-02-07 15:30:46 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 rec->fMode = SkCanvas::kTriangles_VertexMode;
150 rec->fCount = n * 3;
151 rec->fVerts = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 for (int i = 0; i < n; i++) {
154 SkPoint* v = &rec->fVerts[i*3];
155 for (int j = 0; j < 3; j++) {
156 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
157 }
158 }
159 }
reed@google.com82065d62011-02-07 15:30:46 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 void make_fan(Rec* rec, int texWidth, int texHeight) {
162 const SkScalar tx = SkIntToScalar(texWidth);
163 const SkScalar ty = SkIntToScalar(texHeight);
164 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
167 rec->fCount = n + 2;
168 rec->fVerts = new SkPoint[rec->fCount];
169 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 SkPoint* v = rec->fVerts;
172 SkPoint* t = rec->fTexs;
173
174 v[0].set(0, 0);
175 t[0].set(0, 0);
176 for (int i = 0; i < n; i++) {
177 SkScalar cos;
178 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
179 v[i+1].set(cos, sin);
180 t[i+1].set(i*tx/n, ty);
181 }
182 v[n+1] = v[1];
183 t[n+1].set(tx, ty);
reed@google.com82065d62011-02-07 15:30:46 +0000184
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 SkMatrix m;
186 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
187 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
188 m.mapPoints(v, rec->fCount);
189 }
reed@google.com82065d62011-02-07 15:30:46 +0000190
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 void make_strip(Rec* rec, int texWidth, int texHeight) {
192 const SkScalar tx = SkIntToScalar(texWidth);
193 const SkScalar ty = SkIntToScalar(texHeight);
194 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 rec->fMode = SkCanvas::kTriangleStrip_VertexMode;
197 rec->fCount = 2 * (n + 1);
198 rec->fVerts = new SkPoint[rec->fCount];
199 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000200
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 SkPoint* v = rec->fVerts;
202 SkPoint* t = rec->fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 for (int i = 0; i < n; i++) {
205 SkScalar cos;
206 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
207 v[i*2 + 0].set(cos/2, sin/2);
208 v[i*2 + 1].set(cos, sin);
reed@google.com82065d62011-02-07 15:30:46 +0000209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 t[i*2 + 0].set(tx * i / n, ty);
211 t[i*2 + 1].set(tx * i / n, 0);
212 }
213 v[2*n + 0] = v[0];
214 v[2*n + 1] = v[1];
reed@google.com82065d62011-02-07 15:30:46 +0000215
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 t[2*n + 0].set(tx, ty);
217 t[2*n + 1].set(tx, 0);
reed@google.com82065d62011-02-07 15:30:46 +0000218
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 SkMatrix m;
220 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
221 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
222 m.mapPoints(v, rec->fCount);
223 }
reed@google.com82065d62011-02-07 15:30:46 +0000224
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 Rec fRecs[3];
226
reed@google.comf2183392011-04-22 14:10:48 +0000227 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228};
229
230//////////////////////////////////////////////////////////////////////////////
231
232static SkView* MyFactory() { return new VerticesView; }
233static SkViewRegister reg(MyFactory);