blob: 1e568f55689b96bf6a37d22b5d01c373919fbab0 [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);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000031 SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80);
32 SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff);
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000033 bm.allocN32Pixels(size->fX, size->fY);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000034 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();
reed@google.com82065d62011-02-07 15:30:46 +000040
bsalomon@google.com8295dc12011-05-02 12:53:34 +000041 return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
42 SkShader::kRepeat_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043}
44
45static SkShader* make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000046 SkPoint pts[] = { { 0, 0 },
47 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
49 return SkGradientShader::CreateLinear(pts, colors, NULL,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000050 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051}
52
reed@google.comf2183392011-04-22 14:10:48 +000053class VerticesView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkShader* fShader0;
55 SkShader* fShader1;
56
reed@google.com82065d62011-02-07 15:30:46 +000057public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000058 VerticesView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 SkIPoint size;
reed@google.com82065d62011-02-07 15:30:46 +000060
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 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]);
reed@google.com82065d62011-02-07 15:30:46 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 fScale = SK_Scalar1;
reed@google.comf2183392011-04-22 14:10:48 +000069
70 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 }
reed@google.com82065d62011-02-07 15:30:46 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 virtual ~VerticesView() {
reed@google.com82065d62011-02-07 15:30:46 +000074 SkSafeUnref(fShader0);
75 SkSafeUnref(fShader1);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 }
reed@google.com82065d62011-02-07 15:30:46 +000077
reed@android.com8a1c16f2008-12-17 15:59:43 +000078protected:
79 // overrides from SkEventSink
80 virtual bool onQuery(SkEvent* evt) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000081 if (SampleCode::TitleQ(*evt)) {
82 SampleCode::TitleR(evt, "Vertices");
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 return true;
84 }
85 return this->INHERITED::onQuery(evt);
86 }
reed@google.com82065d62011-02-07 15:30:46 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 SkScalar fScale;
reed@google.com82065d62011-02-07 15:30:46 +000089
reed@google.comf2183392011-04-22 14:10:48 +000090 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 SkPaint paint;
92 paint.setDither(true);
reed@google.com44699382013-10-31 17:28:30 +000093 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
reed@google.com82065d62011-02-07 15:30:46 +000094
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000095 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 canvas->save();
reed@google.com82065d62011-02-07 15:30:46 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 paint.setShader(NULL);
99 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
100 fRecs[i].fVerts, fRecs[i].fTexs,
101 NULL, NULL, NULL, 0, paint);
reed@google.com82065d62011-02-07 15:30:46 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 paint.setShader(fShader0);
106 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
107 fRecs[i].fVerts, fRecs[i].fTexs,
108 NULL, NULL, NULL, 0, paint);
109
110 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +0000111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 paint.setShader(fShader1);
113 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
114 fRecs[i].fVerts, fRecs[i].fTexs,
115 NULL, NULL, NULL, 0, paint);
116 canvas->restore();
reed@google.com82065d62011-02-07 15:30:46 +0000117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 canvas->translate(0, SkIntToScalar(250));
119 }
120 }
reed@google.com82065d62011-02-07 15:30:46 +0000121
sugoi@google.com9c55f802013-03-07 20:52:59 +0000122 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 return new Click(this);
124 }
reed@google.com82065d62011-02-07 15:30:46 +0000125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 virtual bool onClick(Click* click) {
127 // fCurrX = click->fICurr.fX;
128 // fCurrY = click->fICurr.fY;
129 this->inval(NULL);
130 return true;
131 }
reed@google.com82065d62011-02-07 15:30:46 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133private:
134 struct Rec {
135 SkCanvas::VertexMode fMode;
136 int fCount;
137 SkPoint* fVerts;
138 SkPoint* fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
141 ~Rec() { delete[] fVerts; delete[] fTexs; }
142 };
143
144 void make_tris(Rec* rec) {
145 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000146 SkRandom rand;
reed@google.com82065d62011-02-07 15:30:46 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 rec->fMode = SkCanvas::kTriangles_VertexMode;
149 rec->fCount = n * 3;
150 rec->fVerts = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 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 }
reed@google.com82065d62011-02-07 15:30:46 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 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;
reed@google.com82065d62011-02-07 15:30:46 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
166 rec->fCount = n + 2;
167 rec->fVerts = new SkPoint[rec->fCount];
168 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 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);
reed@google.com82065d62011-02-07 15:30:46 +0000183
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 SkMatrix m;
185 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
186 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
187 m.mapPoints(v, rec->fCount);
188 }
reed@google.com82065d62011-02-07 15:30:46 +0000189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 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;
reed@google.com82065d62011-02-07 15:30:46 +0000194
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 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];
reed@google.com82065d62011-02-07 15:30:46 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 SkPoint* v = rec->fVerts;
201 SkPoint* t = rec->fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000202
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 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);
reed@google.com82065d62011-02-07 15:30:46 +0000208
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 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];
reed@google.com82065d62011-02-07 15:30:46 +0000214
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 t[2*n + 0].set(tx, ty);
216 t[2*n + 1].set(tx, 0);
reed@google.com82065d62011-02-07 15:30:46 +0000217
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 SkMatrix m;
219 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
220 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
221 m.mapPoints(v, rec->fCount);
222 }
reed@google.com82065d62011-02-07 15:30:46 +0000223
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224 Rec fRecs[3];
225
reed@google.comf2183392011-04-22 14:10:48 +0000226 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227};
228
229//////////////////////////////////////////////////////////////////////////////
230
231static SkView* MyFactory() { return new VerticesView; }
232static SkViewRegister reg(MyFactory);