epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/core/SkStream.h" |
| 24 | #include "src/core/SkOSFile.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 26 | static sk_sp<SkShader> make_shader0(SkIPoint* size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | SkBitmap bm; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 28 | size->set(2, 2); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 29 | SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80); |
| 30 | SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff); |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 31 | bm.allocN32Pixels(size->fX, size->fY); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 32 | bm.eraseColor(color0); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 33 | uint32_t* pixels = (uint32_t*) bm.getPixels(); |
| 34 | pixels[0] = pixels[2] = color0; |
| 35 | pixels[1] = pixels[3] = color1; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 36 | |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 37 | return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | } |
| 39 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 40 | static sk_sp<SkShader> make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 41 | SkPoint pts[] = { { 0, 0 }, |
| 42 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 44 | return SkGradientShader::MakeLinear(pts, colors, nullptr, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 45 | SK_ARRAY_COUNT(colors), SkTileMode::kMirror); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 48 | class VerticesView : public Sample { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 49 | sk_sp<SkShader> fShader0; |
| 50 | sk_sp<SkShader> fShader1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 52 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | VerticesView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkIPoint size; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 62 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | fScale = SK_Scalar1; |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 64 | |
| 65 | this->setBGColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 69 | SkString name() override { return SkString("Vertices"); } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 70 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | SkScalar fScale; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | SkPaint paint; |
| 75 | paint.setDither(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 76 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 77 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 78 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 79 | auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount, |
| 80 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 81 | nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | canvas->save(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 83 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 84 | paint.setShader(nullptr); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 85 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 86 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | paint.setShader(fShader0); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 90 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | |
| 92 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | paint.setShader(fShader1); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 95 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | canvas->restore(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | canvas->translate(0, SkIntToScalar(250)); |
| 99 | } |
| 100 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 101 | |
Hal Canary | 3a85ed1 | 2019-07-08 16:07:57 -0400 | [diff] [blame] | 102 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey) override { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame^] | 103 | return new Click(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 105 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 106 | bool onClick(Click* click) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | // fCurrX = click->fICurr.fX; |
| 108 | // fCurrY = click->fICurr.fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 111 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | private: |
| 113 | struct Rec { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 114 | SkVertices::VertexMode fMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | int fCount; |
| 116 | SkPoint* fVerts; |
| 117 | SkPoint* fTexs; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 118 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 119 | Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 121 | }; |
| 122 | |
| 123 | void make_tris(Rec* rec) { |
| 124 | int n = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 125 | SkRandom rand; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 126 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 127 | rec->fMode = SkVertices::kTriangles_VertexMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | rec->fCount = n * 3; |
| 129 | rec->fVerts = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 143 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 144 | rec->fMode = SkVertices::kTriangleFan_VertexMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | rec->fCount = n + 2; |
| 146 | rec->fVerts = new SkPoint[rec->fCount]; |
| 147 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | 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 Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 155 | SkScalar r = SK_ScalarPI * 2 * i / n, |
| 156 | sin = SkScalarSin(r), |
| 157 | cos = SkScalarCos(r); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 163 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 169 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 174 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 175 | rec->fMode = SkVertices::kTriangleStrip_VertexMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | rec->fCount = 2 * (n + 1); |
| 177 | rec->fVerts = new SkPoint[rec->fCount]; |
| 178 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 179 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | SkPoint* v = rec->fVerts; |
| 181 | SkPoint* t = rec->fTexs; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 182 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | for (int i = 0; i < n; i++) { |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 184 | SkScalar r = SK_ScalarPI * 2 * i / n, |
| 185 | sin = SkScalarSin(r), |
| 186 | cos = SkScalarCos(r); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | v[i*2 + 0].set(cos/2, sin/2); |
| 188 | v[i*2 + 1].set(cos, sin); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 189 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 195 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | t[2*n + 0].set(tx, ty); |
| 197 | t[2*n + 1].set(tx, 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 198 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 204 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | Rec fRecs[3]; |
| 206 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 207 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | ////////////////////////////////////////////////////////////////////////////// |
| 211 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 212 | DEF_SAMPLE( return new VerticesView(); ) |