blob: 55ded036b48402538f5e180e67fa16306c76f59a [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"
Mike Reed75ae4212018-01-23 11:24:08 -05009#include "SkBitmap.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkRandom.h"
15#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkColorPriv.h"
19#include "SkColorFilter.h"
20#include "SkTime.h"
21#include "SkTypeface.h"
Mike Reed887cdf12017-04-03 11:11:09 -040022#include "SkVertices.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkOSFile.h"
25#include "SkStream.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
reed8a21c9f2016-03-08 18:50:00 -080027static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 SkBitmap bm;
bsalomon@google.com8295dc12011-05-02 12:53:34 +000029 size->set(2, 2);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000030 SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80);
31 SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff);
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000032 bm.allocN32Pixels(size->fX, size->fY);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000033 bm.eraseColor(color0);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000034 uint32_t* pixels = (uint32_t*) bm.getPixels();
35 pixels[0] = pixels[2] = color0;
36 pixels[1] = pixels[3] = color1;
reed@google.com82065d62011-02-07 15:30:46 +000037
reed8a21c9f2016-03-08 18:50:00 -080038 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039}
40
reed8a21c9f2016-03-08 18:50:00 -080041static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000042 SkPoint pts[] = { { 0, 0 },
43 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -080045 return SkGradientShader::MakeLinear(pts, colors, nullptr,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000046 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047}
48
reed@google.comf2183392011-04-22 14:10:48 +000049class VerticesView : public SampleView {
reed8a21c9f2016-03-08 18:50:00 -080050 sk_sp<SkShader> fShader0;
51 sk_sp<SkShader> fShader1;
reed@android.com8a1c16f2008-12-17 15:59:43 +000052
reed@google.com82065d62011-02-07 15:30:46 +000053public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000054 VerticesView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 SkIPoint size;
reed@google.com82065d62011-02-07 15:30:46 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 fShader0 = make_shader0(&size);
58 fShader1 = make_shader1(size);
59
60 make_strip(&fRecs[0], size.fX, size.fY);
61 make_fan(&fRecs[1], size.fX, size.fY);
62 make_tris(&fRecs[2]);
reed@google.com82065d62011-02-07 15:30:46 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 fScale = SK_Scalar1;
reed@google.comf2183392011-04-22 14:10:48 +000065
66 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 }
reed@google.com82065d62011-02-07 15:30:46 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069protected:
70 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070071 bool onQuery(SkEvent* evt) override {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000072 if (SampleCode::TitleQ(*evt)) {
73 SampleCode::TitleR(evt, "Vertices");
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 return true;
75 }
76 return this->INHERITED::onQuery(evt);
77 }
reed@google.com82065d62011-02-07 15:30:46 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkScalar fScale;
reed@google.com82065d62011-02-07 15:30:46 +000080
mtklein36352bf2015-03-25 18:17:31 -070081 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 SkPaint paint;
83 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -070084 paint.setFilterQuality(kLow_SkFilterQuality);
reed@google.com82065d62011-02-07 15:30:46 +000085
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000086 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
Mike Reed887cdf12017-04-03 11:11:09 -040087 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
88 fRecs[i].fVerts, fRecs[i].fTexs,
89 nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 canvas->save();
reed@google.com82065d62011-02-07 15:30:46 +000091
halcanary96fcdcc2015-08-27 07:41:13 -070092 paint.setShader(nullptr);
Mike Reed887cdf12017-04-03 11:11:09 -040093 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@google.com82065d62011-02-07 15:30:46 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 paint.setShader(fShader0);
Mike Reed887cdf12017-04-03 11:11:09 -040098 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099
100 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 paint.setShader(fShader1);
Mike Reed887cdf12017-04-03 11:11:09 -0400103 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 canvas->restore();
reed@google.com82065d62011-02-07 15:30:46 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 canvas->translate(0, SkIntToScalar(250));
107 }
108 }
reed@google.com82065d62011-02-07 15:30:46 +0000109
mtklein36352bf2015-03-25 18:17:31 -0700110 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 return new Click(this);
112 }
reed@google.com82065d62011-02-07 15:30:46 +0000113
mtklein36352bf2015-03-25 18:17:31 -0700114 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 // fCurrX = click->fICurr.fX;
116 // fCurrY = click->fICurr.fY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 return true;
118 }
reed@google.com82065d62011-02-07 15:30:46 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120private:
121 struct Rec {
Mike Reed887cdf12017-04-03 11:11:09 -0400122 SkVertices::VertexMode fMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 int fCount;
124 SkPoint* fVerts;
125 SkPoint* fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000126
halcanary96fcdcc2015-08-27 07:41:13 -0700127 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 ~Rec() { delete[] fVerts; delete[] fTexs; }
129 };
130
131 void make_tris(Rec* rec) {
132 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000133 SkRandom rand;
reed@google.com82065d62011-02-07 15:30:46 +0000134
Mike Reed887cdf12017-04-03 11:11:09 -0400135 rec->fMode = SkVertices::kTriangles_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 rec->fCount = n * 3;
137 rec->fVerts = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 for (int i = 0; i < n; i++) {
140 SkPoint* v = &rec->fVerts[i*3];
141 for (int j = 0; j < 3; j++) {
142 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
143 }
144 }
145 }
reed@google.com82065d62011-02-07 15:30:46 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 void make_fan(Rec* rec, int texWidth, int texHeight) {
148 const SkScalar tx = SkIntToScalar(texWidth);
149 const SkScalar ty = SkIntToScalar(texHeight);
150 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000151
Mike Reed887cdf12017-04-03 11:11:09 -0400152 rec->fMode = SkVertices::kTriangleFan_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 rec->fCount = n + 2;
154 rec->fVerts = new SkPoint[rec->fCount];
155 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 SkPoint* v = rec->fVerts;
158 SkPoint* t = rec->fTexs;
159
160 v[0].set(0, 0);
161 t[0].set(0, 0);
162 for (int i = 0; i < n; i++) {
163 SkScalar cos;
164 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
165 v[i+1].set(cos, sin);
166 t[i+1].set(i*tx/n, ty);
167 }
168 v[n+1] = v[1];
169 t[n+1].set(tx, ty);
reed@google.com82065d62011-02-07 15:30:46 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 SkMatrix m;
172 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
173 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
174 m.mapPoints(v, rec->fCount);
175 }
reed@google.com82065d62011-02-07 15:30:46 +0000176
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 void make_strip(Rec* rec, int texWidth, int texHeight) {
178 const SkScalar tx = SkIntToScalar(texWidth);
179 const SkScalar ty = SkIntToScalar(texHeight);
180 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000181
Mike Reed887cdf12017-04-03 11:11:09 -0400182 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 rec->fCount = 2 * (n + 1);
184 rec->fVerts = new SkPoint[rec->fCount];
185 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 SkPoint* v = rec->fVerts;
188 SkPoint* t = rec->fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 for (int i = 0; i < n; i++) {
191 SkScalar cos;
192 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
193 v[i*2 + 0].set(cos/2, sin/2);
194 v[i*2 + 1].set(cos, sin);
reed@google.com82065d62011-02-07 15:30:46 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 t[i*2 + 0].set(tx * i / n, ty);
197 t[i*2 + 1].set(tx * i / n, 0);
198 }
199 v[2*n + 0] = v[0];
200 v[2*n + 1] = v[1];
reed@google.com82065d62011-02-07 15:30:46 +0000201
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 t[2*n + 0].set(tx, ty);
203 t[2*n + 1].set(tx, 0);
reed@google.com82065d62011-02-07 15:30:46 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 SkMatrix m;
206 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
207 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
208 m.mapPoints(v, rec->fCount);
209 }
reed@google.com82065d62011-02-07 15:30:46 +0000210
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211 Rec fRecs[3];
212
reed@google.comf2183392011-04-22 14:10:48 +0000213 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214};
215
216//////////////////////////////////////////////////////////////////////////////
217
218static SkView* MyFactory() { return new VerticesView; }
219static SkViewRegister reg(MyFactory);