blob: 9d3e6015ee8b4209c2acb17d154337388985911e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
11#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkRandom.h"
14#include "SkRegion.h"
15#include "SkShader.h"
16#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkColorPriv.h"
18#include "SkColorFilter.h"
19#include "SkTime.h"
20#include "SkTypeface.h"
Mike Reed887cdf12017-04-03 11:11:09 -040021#include "SkVertices.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkOSFile.h"
24#include "SkStream.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
reed8a21c9f2016-03-08 18:50:00 -080026static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 SkBitmap bm;
bsalomon@google.com8295dc12011-05-02 12:53:34 +000028 size->set(2, 2);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000029 SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80);
30 SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff);
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000031 bm.allocN32Pixels(size->fX, size->fY);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000032 bm.eraseColor(color0);
33 bm.lockPixels();
34 uint32_t* pixels = (uint32_t*) bm.getPixels();
35 pixels[0] = pixels[2] = color0;
36 pixels[1] = pixels[3] = color1;
37 bm.unlockPixels();
reed@google.com82065d62011-02-07 15:30:46 +000038
reed8a21c9f2016-03-08 18:50:00 -080039 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040}
41
reed8a21c9f2016-03-08 18:50:00 -080042static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000043 SkPoint pts[] = { { 0, 0 },
44 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -080046 return SkGradientShader::MakeLinear(pts, colors, nullptr,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000047 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048}
49
reed@google.comf2183392011-04-22 14:10:48 +000050class VerticesView : public SampleView {
reed8a21c9f2016-03-08 18:50:00 -080051 sk_sp<SkShader> fShader0;
52 sk_sp<SkShader> fShader1;
reed@android.com8a1c16f2008-12-17 15:59:43 +000053
reed@google.com82065d62011-02-07 15:30:46 +000054public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000055 VerticesView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 SkIPoint size;
reed@google.com82065d62011-02-07 15:30:46 +000057
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 fShader0 = make_shader0(&size);
59 fShader1 = make_shader1(size);
60
61 make_strip(&fRecs[0], size.fX, size.fY);
62 make_fan(&fRecs[1], size.fX, size.fY);
63 make_tris(&fRecs[2]);
reed@google.com82065d62011-02-07 15:30:46 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 fScale = SK_Scalar1;
reed@google.comf2183392011-04-22 14:10:48 +000066
67 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 }
reed@google.com82065d62011-02-07 15:30:46 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070protected:
71 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070072 bool onQuery(SkEvent* evt) override {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000073 if (SampleCode::TitleQ(*evt)) {
74 SampleCode::TitleR(evt, "Vertices");
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 return true;
76 }
77 return this->INHERITED::onQuery(evt);
78 }
reed@google.com82065d62011-02-07 15:30:46 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkScalar fScale;
reed@google.com82065d62011-02-07 15:30:46 +000081
mtklein36352bf2015-03-25 18:17:31 -070082 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 SkPaint paint;
84 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -070085 paint.setFilterQuality(kLow_SkFilterQuality);
reed@google.com82065d62011-02-07 15:30:46 +000086
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000087 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
Mike Reed887cdf12017-04-03 11:11:09 -040088 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
89 fRecs[i].fVerts, fRecs[i].fTexs,
90 nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 canvas->save();
reed@google.com82065d62011-02-07 15:30:46 +000092
halcanary96fcdcc2015-08-27 07:41:13 -070093 paint.setShader(nullptr);
Mike Reed887cdf12017-04-03 11:11:09 -040094 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@google.com82065d62011-02-07 15:30:46 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 paint.setShader(fShader0);
Mike Reed887cdf12017-04-03 11:11:09 -040099 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100
101 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 paint.setShader(fShader1);
Mike Reed887cdf12017-04-03 11:11:09 -0400104 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 canvas->restore();
reed@google.com82065d62011-02-07 15:30:46 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 canvas->translate(0, SkIntToScalar(250));
108 }
109 }
reed@google.com82065d62011-02-07 15:30:46 +0000110
mtklein36352bf2015-03-25 18:17:31 -0700111 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 return new Click(this);
113 }
reed@google.com82065d62011-02-07 15:30:46 +0000114
mtklein36352bf2015-03-25 18:17:31 -0700115 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 // fCurrX = click->fICurr.fX;
117 // fCurrY = click->fICurr.fY;
halcanary96fcdcc2015-08-27 07:41:13 -0700118 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 return true;
120 }
reed@google.com82065d62011-02-07 15:30:46 +0000121
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122private:
123 struct Rec {
Mike Reed887cdf12017-04-03 11:11:09 -0400124 SkVertices::VertexMode fMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 int fCount;
126 SkPoint* fVerts;
127 SkPoint* fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000128
halcanary96fcdcc2015-08-27 07:41:13 -0700129 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 ~Rec() { delete[] fVerts; delete[] fTexs; }
131 };
132
133 void make_tris(Rec* rec) {
134 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000135 SkRandom rand;
reed@google.com82065d62011-02-07 15:30:46 +0000136
Mike Reed887cdf12017-04-03 11:11:09 -0400137 rec->fMode = SkVertices::kTriangles_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 rec->fCount = n * 3;
139 rec->fVerts = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 for (int i = 0; i < n; i++) {
142 SkPoint* v = &rec->fVerts[i*3];
143 for (int j = 0; j < 3; j++) {
144 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
145 }
146 }
147 }
reed@google.com82065d62011-02-07 15:30:46 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 void make_fan(Rec* rec, int texWidth, int texHeight) {
150 const SkScalar tx = SkIntToScalar(texWidth);
151 const SkScalar ty = SkIntToScalar(texHeight);
152 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000153
Mike Reed887cdf12017-04-03 11:11:09 -0400154 rec->fMode = SkVertices::kTriangleFan_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 rec->fCount = n + 2;
156 rec->fVerts = new SkPoint[rec->fCount];
157 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 SkPoint* v = rec->fVerts;
160 SkPoint* t = rec->fTexs;
161
162 v[0].set(0, 0);
163 t[0].set(0, 0);
164 for (int i = 0; i < n; i++) {
165 SkScalar cos;
166 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
167 v[i+1].set(cos, sin);
168 t[i+1].set(i*tx/n, ty);
169 }
170 v[n+1] = v[1];
171 t[n+1].set(tx, ty);
reed@google.com82065d62011-02-07 15:30:46 +0000172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 SkMatrix m;
174 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
175 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
176 m.mapPoints(v, rec->fCount);
177 }
reed@google.com82065d62011-02-07 15:30:46 +0000178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 void make_strip(Rec* rec, int texWidth, int texHeight) {
180 const SkScalar tx = SkIntToScalar(texWidth);
181 const SkScalar ty = SkIntToScalar(texHeight);
182 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000183
Mike Reed887cdf12017-04-03 11:11:09 -0400184 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 rec->fCount = 2 * (n + 1);
186 rec->fVerts = new SkPoint[rec->fCount];
187 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000188
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 SkPoint* v = rec->fVerts;
190 SkPoint* t = rec->fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000191
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 for (int i = 0; i < n; i++) {
193 SkScalar cos;
194 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
195 v[i*2 + 0].set(cos/2, sin/2);
196 v[i*2 + 1].set(cos, sin);
reed@google.com82065d62011-02-07 15:30:46 +0000197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 t[i*2 + 0].set(tx * i / n, ty);
199 t[i*2 + 1].set(tx * i / n, 0);
200 }
201 v[2*n + 0] = v[0];
202 v[2*n + 1] = v[1];
reed@google.com82065d62011-02-07 15:30:46 +0000203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 t[2*n + 0].set(tx, ty);
205 t[2*n + 1].set(tx, 0);
reed@google.com82065d62011-02-07 15:30:46 +0000206
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 SkMatrix m;
208 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
209 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
210 m.mapPoints(v, rec->fCount);
211 }
reed@google.com82065d62011-02-07 15:30:46 +0000212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 Rec fRecs[3];
214
reed@google.comf2183392011-04-22 14:10:48 +0000215 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216};
217
218//////////////////////////////////////////////////////////////////////////////
219
220static SkView* MyFactory() { return new VerticesView; }
221static SkViewRegister reg(MyFactory);