blob: f3c90e2a883bd6cfc4380b6bceb4a5941b3cd8b7 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkBitmap.h"
8#include "include/core/SkCanvas.h"
9#include "include/core/SkColorFilter.h"
10#include "include/core/SkColorPriv.h"
11#include "include/core/SkGraphics.h"
12#include "include/core/SkPath.h"
13#include "include/core/SkRegion.h"
14#include "include/core/SkShader.h"
15#include "include/core/SkTime.h"
16#include "include/core/SkTypeface.h"
17#include "include/core/SkVertices.h"
18#include "include/effects/SkGradientShader.h"
19#include "include/utils/SkRandom.h"
20#include "samplecode/Sample.h"
21#include "src/utils/SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkStream.h"
24#include "src/core/SkOSFile.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);
bsalomon@google.com8295dc12011-05-02 12:53:34 +000033 uint32_t* pixels = (uint32_t*) bm.getPixels();
34 pixels[0] = pixels[2] = color0;
35 pixels[1] = pixels[3] = color1;
reed@google.com82065d62011-02-07 15:30:46 +000036
Mike Reed50acf8f2019-04-08 13:20:23 -040037 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038}
39
reed8a21c9f2016-03-08 18:50:00 -080040static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000041 SkPoint pts[] = { { 0, 0 },
42 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -080044 return SkGradientShader::MakeLinear(pts, colors, nullptr,
Mike Reedfae8fce2019-04-03 10:27:45 -040045 SK_ARRAY_COUNT(colors), SkTileMode::kMirror);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046}
47
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040048class VerticesView : public Sample {
reed8a21c9f2016-03-08 18:50:00 -080049 sk_sp<SkShader> fShader0;
50 sk_sp<SkShader> fShader1;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051
reed@google.com82065d62011-02-07 15:30:46 +000052public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000053 VerticesView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkIPoint size;
reed@google.com82065d62011-02-07 15:30:46 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 fShader0 = make_shader0(&size);
57 fShader1 = make_shader1(size);
58
59 make_strip(&fRecs[0], size.fX, size.fY);
60 make_fan(&fRecs[1], size.fX, size.fY);
61 make_tris(&fRecs[2]);
reed@google.com82065d62011-02-07 15:30:46 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 fScale = SK_Scalar1;
reed@google.comf2183392011-04-22 14:10:48 +000064
65 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 }
reed@google.com82065d62011-02-07 15:30:46 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040069 bool onQuery(Sample::Event* evt) override {
70 if (Sample::TitleQ(*evt)) {
71 Sample::TitleR(evt, "Vertices");
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 return true;
73 }
74 return this->INHERITED::onQuery(evt);
75 }
reed@google.com82065d62011-02-07 15:30:46 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 SkScalar fScale;
reed@google.com82065d62011-02-07 15:30:46 +000078
mtklein36352bf2015-03-25 18:17:31 -070079 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SkPaint paint;
81 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -070082 paint.setFilterQuality(kLow_SkFilterQuality);
reed@google.com82065d62011-02-07 15:30:46 +000083
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000084 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
Mike Reed887cdf12017-04-03 11:11:09 -040085 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
86 fRecs[i].fVerts, fRecs[i].fTexs,
87 nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 canvas->save();
reed@google.com82065d62011-02-07 15:30:46 +000089
halcanary96fcdcc2015-08-27 07:41:13 -070090 paint.setShader(nullptr);
Mike Reed887cdf12017-04-03 11:11:09 -040091 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@google.com82065d62011-02-07 15:30:46 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 paint.setShader(fShader0);
Mike Reed887cdf12017-04-03 11:11:09 -040096 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 paint.setShader(fShader1);
Mike Reed887cdf12017-04-03 11:11:09 -0400101 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 canvas->restore();
reed@google.com82065d62011-02-07 15:30:46 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 canvas->translate(0, SkIntToScalar(250));
105 }
106 }
reed@google.com82065d62011-02-07 15:30:46 +0000107
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 return new Click(this);
110 }
reed@google.com82065d62011-02-07 15:30:46 +0000111
mtklein36352bf2015-03-25 18:17:31 -0700112 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 // fCurrX = click->fICurr.fX;
114 // fCurrY = click->fICurr.fY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 return true;
116 }
reed@google.com82065d62011-02-07 15:30:46 +0000117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118private:
119 struct Rec {
Mike Reed887cdf12017-04-03 11:11:09 -0400120 SkVertices::VertexMode fMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 int fCount;
122 SkPoint* fVerts;
123 SkPoint* fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000124
halcanary96fcdcc2015-08-27 07:41:13 -0700125 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 ~Rec() { delete[] fVerts; delete[] fTexs; }
127 };
128
129 void make_tris(Rec* rec) {
130 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000131 SkRandom rand;
reed@google.com82065d62011-02-07 15:30:46 +0000132
Mike Reed887cdf12017-04-03 11:11:09 -0400133 rec->fMode = SkVertices::kTriangles_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 rec->fCount = n * 3;
135 rec->fVerts = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 for (int i = 0; i < n; i++) {
138 SkPoint* v = &rec->fVerts[i*3];
139 for (int j = 0; j < 3; j++) {
140 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
141 }
142 }
143 }
reed@google.com82065d62011-02-07 15:30:46 +0000144
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 void make_fan(Rec* rec, int texWidth, int texHeight) {
146 const SkScalar tx = SkIntToScalar(texWidth);
147 const SkScalar ty = SkIntToScalar(texHeight);
148 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000149
Mike Reed887cdf12017-04-03 11:11:09 -0400150 rec->fMode = SkVertices::kTriangleFan_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 rec->fCount = n + 2;
152 rec->fVerts = new SkPoint[rec->fCount];
153 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 SkPoint* v = rec->fVerts;
156 SkPoint* t = rec->fTexs;
157
158 v[0].set(0, 0);
159 t[0].set(0, 0);
160 for (int i = 0; i < n; i++) {
Brian Osman4428f2c2019-04-02 10:59:28 -0400161 SkScalar r = SK_ScalarPI * 2 * i / n,
162 sin = SkScalarSin(r),
163 cos = SkScalarCos(r);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 v[i+1].set(cos, sin);
165 t[i+1].set(i*tx/n, ty);
166 }
167 v[n+1] = v[1];
168 t[n+1].set(tx, ty);
reed@google.com82065d62011-02-07 15:30:46 +0000169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 SkMatrix m;
171 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
172 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
173 m.mapPoints(v, rec->fCount);
174 }
reed@google.com82065d62011-02-07 15:30:46 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 void make_strip(Rec* rec, int texWidth, int texHeight) {
177 const SkScalar tx = SkIntToScalar(texWidth);
178 const SkScalar ty = SkIntToScalar(texHeight);
179 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000180
Mike Reed887cdf12017-04-03 11:11:09 -0400181 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 rec->fCount = 2 * (n + 1);
183 rec->fVerts = new SkPoint[rec->fCount];
184 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 SkPoint* v = rec->fVerts;
187 SkPoint* t = rec->fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000188
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 for (int i = 0; i < n; i++) {
Brian Osman4428f2c2019-04-02 10:59:28 -0400190 SkScalar r = SK_ScalarPI * 2 * i / n,
191 sin = SkScalarSin(r),
192 cos = SkScalarCos(r);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400213 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214};
215
216//////////////////////////////////////////////////////////////////////////////
217
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400218DEF_SAMPLE( return new VerticesView(); )