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 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 7 | #include "Sample.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 8 | #include "SkBitmap.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkGraphics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkRandom.h" |
| 14 | #include "SkRegion.h" |
| 15 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 16 | #include "SkUTF.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkColorPriv.h" |
| 18 | #include "SkColorFilter.h" |
| 19 | #include "SkTime.h" |
| 20 | #include "SkTypeface.h" |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 21 | #include "SkVertices.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkOSFile.h" |
| 24 | #include "SkStream.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 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 37 | return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
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, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 45 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode); |
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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 69 | bool onQuery(Sample::Event* evt) override { |
| 70 | if (Sample::TitleQ(*evt)) { |
| 71 | Sample::TitleR(evt, "Vertices"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | return this->INHERITED::onQuery(evt); |
| 75 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 76 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | SkScalar fScale; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 78 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 79 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | SkPaint paint; |
| 81 | paint.setDither(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 82 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 83 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 84 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 85 | auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount, |
| 86 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 87 | nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | canvas->save(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 89 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 90 | paint.setShader(nullptr); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 91 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | paint.setShader(fShader0); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 96 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | |
| 98 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | paint.setShader(fShader1); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 101 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | canvas->restore(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | canvas->translate(0, SkIntToScalar(250)); |
| 105 | } |
| 106 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 107 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 108 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | return new Click(this); |
| 110 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 111 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 112 | bool onClick(Click* click) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | // fCurrX = click->fICurr.fX; |
| 114 | // fCurrY = click->fICurr.fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 117 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | private: |
| 119 | struct Rec { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 120 | SkVertices::VertexMode fMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | int fCount; |
| 122 | SkPoint* fVerts; |
| 123 | SkPoint* fTexs; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 124 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 127 | }; |
| 128 | |
| 129 | void make_tris(Rec* rec) { |
| 130 | int n = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 131 | SkRandom rand; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 132 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 133 | rec->fMode = SkVertices::kTriangles_VertexMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | rec->fCount = n * 3; |
| 135 | rec->fVerts = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 144 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 149 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 150 | rec->fMode = SkVertices::kTriangleFan_VertexMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | rec->fCount = n + 2; |
| 152 | rec->fVerts = new SkPoint[rec->fCount]; |
| 153 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 154 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | 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++) { |
| 161 | SkScalar cos; |
| 162 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 163 | v[i+1].set(cos, sin); |
| 164 | t[i+1].set(i*tx/n, ty); |
| 165 | } |
| 166 | v[n+1] = v[1]; |
| 167 | t[n+1].set(tx, ty); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 168 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | SkMatrix m; |
| 170 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 171 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 172 | m.mapPoints(v, rec->fCount); |
| 173 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 174 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | void make_strip(Rec* rec, int texWidth, int texHeight) { |
| 176 | const SkScalar tx = SkIntToScalar(texWidth); |
| 177 | const SkScalar ty = SkIntToScalar(texHeight); |
| 178 | const int n = 24; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 179 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 180 | rec->fMode = SkVertices::kTriangleStrip_VertexMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | rec->fCount = 2 * (n + 1); |
| 182 | rec->fVerts = new SkPoint[rec->fCount]; |
| 183 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 184 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | SkPoint* v = rec->fVerts; |
| 186 | SkPoint* t = rec->fTexs; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 187 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | for (int i = 0; i < n; i++) { |
| 189 | SkScalar cos; |
| 190 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 191 | v[i*2 + 0].set(cos/2, sin/2); |
| 192 | v[i*2 + 1].set(cos, sin); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | t[i*2 + 0].set(tx * i / n, ty); |
| 195 | t[i*2 + 1].set(tx * i / n, 0); |
| 196 | } |
| 197 | v[2*n + 0] = v[0]; |
| 198 | v[2*n + 1] = v[1]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | t[2*n + 0].set(tx, ty); |
| 201 | t[2*n + 1].set(tx, 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 202 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | SkMatrix m; |
| 204 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 205 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 206 | m.mapPoints(v, rec->fCount); |
| 207 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 208 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | Rec fRecs[3]; |
| 210 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 211 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | ////////////////////////////////////////////////////////////////////////////// |
| 215 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 216 | DEF_SAMPLE( return new VerticesView(); ) |