blob: d2c7fea54b74c644429186582152b5ea7ed53ffd [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 Reedb41bd152020-12-12 11:18:31 -050037 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
Mike Reed5ec22382021-01-14 21:59:01 -050038 SkSamplingOptions(SkFilterMode::kLinear));
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,
Mike Reedfae8fce2019-04-03 10:27:45 -040046 SK_ARRAY_COUNT(colors), SkTileMode::kMirror);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047}
48
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040049class VerticesView : public Sample {
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:
Hal Canary8a027312019-07-03 10:55:44 -040070 SkString name() override { return SkString("Vertices"); }
reed@google.com82065d62011-02-07 15:30:46 +000071
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkScalar fScale;
reed@google.com82065d62011-02-07 15:30:46 +000073
mtklein36352bf2015-03-25 18:17:31 -070074 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkPaint paint;
76 paint.setDither(true);
reed@google.com82065d62011-02-07 15:30:46 +000077
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000078 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
Mike Reed887cdf12017-04-03 11:11:09 -040079 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
80 fRecs[i].fVerts, fRecs[i].fTexs,
81 nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 canvas->save();
reed@google.com82065d62011-02-07 15:30:46 +000083
halcanary96fcdcc2015-08-27 07:41:13 -070084 paint.setShader(nullptr);
Mike Reed887cdf12017-04-03 11:11:09 -040085 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@google.com82065d62011-02-07 15:30:46 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 paint.setShader(fShader0);
Mike Reed887cdf12017-04-03 11:11:09 -040090 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091
92 canvas->translate(SkIntToScalar(250), 0);
reed@google.com82065d62011-02-07 15:30:46 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 paint.setShader(fShader1);
Mike Reed887cdf12017-04-03 11:11:09 -040095 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 canvas->restore();
reed@google.com82065d62011-02-07 15:30:46 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 canvas->translate(0, SkIntToScalar(250));
99 }
100 }
reed@google.com82065d62011-02-07 15:30:46 +0000101
Hal Canaryb1f411a2019-08-29 10:39:22 -0400102 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
Hal Canaryfcf63592019-07-12 11:32:43 -0400103 return new Click();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 }
reed@google.com82065d62011-02-07 15:30:46 +0000105
mtklein36352bf2015-03-25 18:17:31 -0700106 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 // fCurrX = click->fICurr.fX;
108 // fCurrY = click->fICurr.fY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 return true;
110 }
reed@google.com82065d62011-02-07 15:30:46 +0000111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112private:
113 struct Rec {
Mike Reed887cdf12017-04-03 11:11:09 -0400114 SkVertices::VertexMode fMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 int fCount;
116 SkPoint* fVerts;
117 SkPoint* fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000118
halcanary96fcdcc2015-08-27 07:41:13 -0700119 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 ~Rec() { delete[] fVerts; delete[] fTexs; }
121 };
122
123 void make_tris(Rec* rec) {
124 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000125 SkRandom rand;
reed@google.com82065d62011-02-07 15:30:46 +0000126
Mike Reed887cdf12017-04-03 11:11:09 -0400127 rec->fMode = SkVertices::kTriangles_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 rec->fCount = n * 3;
129 rec->fVerts = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 for (int i = 0; i < n; i++) {
132 SkPoint* v = &rec->fVerts[i*3];
133 for (int j = 0; j < 3; j++) {
134 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
135 }
136 }
137 }
reed@google.com82065d62011-02-07 15:30:46 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 void make_fan(Rec* rec, int texWidth, int texHeight) {
140 const SkScalar tx = SkIntToScalar(texWidth);
141 const SkScalar ty = SkIntToScalar(texHeight);
142 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000143
Mike Reed887cdf12017-04-03 11:11:09 -0400144 rec->fMode = SkVertices::kTriangleFan_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 rec->fCount = n + 2;
146 rec->fVerts = new SkPoint[rec->fCount];
147 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 SkPoint* v = rec->fVerts;
150 SkPoint* t = rec->fTexs;
151
152 v[0].set(0, 0);
153 t[0].set(0, 0);
154 for (int i = 0; i < n; i++) {
Brian Osman4428f2c2019-04-02 10:59:28 -0400155 SkScalar r = SK_ScalarPI * 2 * i / n,
156 sin = SkScalarSin(r),
157 cos = SkScalarCos(r);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 v[i+1].set(cos, sin);
159 t[i+1].set(i*tx/n, ty);
160 }
161 v[n+1] = v[1];
162 t[n+1].set(tx, ty);
reed@google.com82065d62011-02-07 15:30:46 +0000163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 SkMatrix m;
165 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
166 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
167 m.mapPoints(v, rec->fCount);
168 }
reed@google.com82065d62011-02-07 15:30:46 +0000169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 void make_strip(Rec* rec, int texWidth, int texHeight) {
171 const SkScalar tx = SkIntToScalar(texWidth);
172 const SkScalar ty = SkIntToScalar(texHeight);
173 const int n = 24;
reed@google.com82065d62011-02-07 15:30:46 +0000174
Mike Reed887cdf12017-04-03 11:11:09 -0400175 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 rec->fCount = 2 * (n + 1);
177 rec->fVerts = new SkPoint[rec->fCount];
178 rec->fTexs = new SkPoint[rec->fCount];
reed@google.com82065d62011-02-07 15:30:46 +0000179
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 SkPoint* v = rec->fVerts;
181 SkPoint* t = rec->fTexs;
reed@google.com82065d62011-02-07 15:30:46 +0000182
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 for (int i = 0; i < n; i++) {
Brian Osman4428f2c2019-04-02 10:59:28 -0400184 SkScalar r = SK_ScalarPI * 2 * i / n,
185 sin = SkScalarSin(r),
186 cos = SkScalarCos(r);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 v[i*2 + 0].set(cos/2, sin/2);
188 v[i*2 + 1].set(cos, sin);
reed@google.com82065d62011-02-07 15:30:46 +0000189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 t[i*2 + 0].set(tx * i / n, ty);
191 t[i*2 + 1].set(tx * i / n, 0);
192 }
193 v[2*n + 0] = v[0];
194 v[2*n + 1] = v[1];
reed@google.com82065d62011-02-07 15:30:46 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 t[2*n + 0].set(tx, ty);
197 t[2*n + 1].set(tx, 0);
reed@google.com82065d62011-02-07 15:30:46 +0000198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 SkMatrix m;
200 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
201 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
202 m.mapPoints(v, rec->fCount);
203 }
reed@google.com82065d62011-02-07 15:30:46 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 Rec fRecs[3];
206
John Stiles7571f9e2020-09-02 22:42:33 -0400207 using INHERITED = Sample;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208};
209
210//////////////////////////////////////////////////////////////////////////////
211
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400212DEF_SAMPLE( return new VerticesView(); )