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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 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" |
| 16 | #include "SkUtils.h" |
| 17 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkColorPriv.h" |
| 19 | #include "SkColorFilter.h" |
| 20 | #include "SkTime.h" |
| 21 | #include "SkTypeface.h" |
| 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); |
| 33 | bm.lockPixels(); |
| 34 | uint32_t* pixels = (uint32_t*) bm.getPixels(); |
| 35 | pixels[0] = pixels[2] = color0; |
| 36 | pixels[1] = pixels[3] = color1; |
| 37 | bm.unlockPixels(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 38 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 39 | return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 42 | static sk_sp<SkShader> make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 43 | SkPoint pts[] = { { 0, 0 }, |
| 44 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 46 | return SkGradientShader::MakeLinear(pts, colors, nullptr, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 47 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 50 | class VerticesView : public SampleView { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 51 | sk_sp<SkShader> fShader0; |
| 52 | sk_sp<SkShader> fShader1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 54 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | VerticesView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | SkIPoint size; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | fShader0 = make_shader0(&size); |
| 59 | fShader1 = make_shader1(size); |
| 60 | |
| 61 | make_strip(&fRecs[0], size.fX, size.fY); |
| 62 | make_fan(&fRecs[1], size.fX, size.fY); |
| 63 | make_tris(&fRecs[2]); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | fScale = SK_Scalar1; |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 66 | |
| 67 | this->setBGColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | protected: |
| 71 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | bool onQuery(SkEvent* evt) override { |
tfarina@chromium.org | 45369a3 | 2012-09-30 11:30:01 +0000 | [diff] [blame] | 73 | if (SampleCode::TitleQ(*evt)) { |
| 74 | SampleCode::TitleR(evt, "Vertices"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | return this->INHERITED::onQuery(evt); |
| 78 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | SkScalar fScale; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 81 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 82 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | SkPaint paint; |
| 84 | paint.setDither(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 85 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 86 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 87 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 92 | fRecs[i].fVerts, fRecs[i].fTexs, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 93 | nullptr, nullptr, 0, paint); |
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 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | paint.setShader(fShader0); |
| 98 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 99 | fRecs[i].fVerts, fRecs[i].fTexs, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 100 | nullptr, nullptr, 0, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | |
| 102 | canvas->translate(SkIntToScalar(250), 0); |
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 | paint.setShader(fShader1); |
| 105 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 106 | fRecs[i].fVerts, fRecs[i].fTexs, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 107 | nullptr, nullptr, 0, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | canvas->restore(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | canvas->translate(0, SkIntToScalar(250)); |
| 111 | } |
| 112 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 113 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 114 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | return new Click(this); |
| 116 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 117 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 118 | bool onClick(Click* click) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | // fCurrX = click->fICurr.fX; |
| 120 | // fCurrY = click->fICurr.fY; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 121 | this->inval(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 124 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | private: |
| 126 | struct Rec { |
| 127 | SkCanvas::VertexMode fMode; |
| 128 | int fCount; |
| 129 | SkPoint* fVerts; |
| 130 | SkPoint* fTexs; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 131 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 132 | Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 134 | }; |
| 135 | |
| 136 | void make_tris(Rec* rec) { |
| 137 | int n = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 138 | SkRandom rand; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | rec->fMode = SkCanvas::kTriangles_VertexMode; |
| 141 | rec->fCount = n * 3; |
| 142 | rec->fVerts = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 143 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | for (int i = 0; i < n; i++) { |
| 145 | SkPoint* v = &rec->fVerts[i*3]; |
| 146 | for (int j = 0; j < 3; j++) { |
| 147 | v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); |
| 148 | } |
| 149 | } |
| 150 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | void make_fan(Rec* rec, int texWidth, int texHeight) { |
| 153 | const SkScalar tx = SkIntToScalar(texWidth); |
| 154 | const SkScalar ty = SkIntToScalar(texHeight); |
| 155 | const int n = 24; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 156 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | rec->fMode = SkCanvas::kTriangleFan_VertexMode; |
| 158 | rec->fCount = n + 2; |
| 159 | rec->fVerts = new SkPoint[rec->fCount]; |
| 160 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 161 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | SkPoint* v = rec->fVerts; |
| 163 | SkPoint* t = rec->fTexs; |
| 164 | |
| 165 | v[0].set(0, 0); |
| 166 | t[0].set(0, 0); |
| 167 | for (int i = 0; i < n; i++) { |
| 168 | SkScalar cos; |
| 169 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 170 | v[i+1].set(cos, sin); |
| 171 | t[i+1].set(i*tx/n, ty); |
| 172 | } |
| 173 | v[n+1] = v[1]; |
| 174 | t[n+1].set(tx, ty); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 175 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | SkMatrix m; |
| 177 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 178 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 179 | m.mapPoints(v, rec->fCount); |
| 180 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 181 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | void make_strip(Rec* rec, int texWidth, int texHeight) { |
| 183 | const SkScalar tx = SkIntToScalar(texWidth); |
| 184 | const SkScalar ty = SkIntToScalar(texHeight); |
| 185 | const int n = 24; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 186 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | rec->fMode = SkCanvas::kTriangleStrip_VertexMode; |
| 188 | rec->fCount = 2 * (n + 1); |
| 189 | rec->fVerts = new SkPoint[rec->fCount]; |
| 190 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 191 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | SkPoint* v = rec->fVerts; |
| 193 | SkPoint* t = rec->fTexs; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 194 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | for (int i = 0; i < n; i++) { |
| 196 | SkScalar cos; |
| 197 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 198 | v[i*2 + 0].set(cos/2, sin/2); |
| 199 | v[i*2 + 1].set(cos, sin); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 200 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | t[i*2 + 0].set(tx * i / n, ty); |
| 202 | t[i*2 + 1].set(tx * i / n, 0); |
| 203 | } |
| 204 | v[2*n + 0] = v[0]; |
| 205 | v[2*n + 1] = v[1]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 206 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | t[2*n + 0].set(tx, ty); |
| 208 | t[2*n + 1].set(tx, 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 209 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | SkMatrix m; |
| 211 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 212 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 213 | m.mapPoints(v, rec->fCount); |
| 214 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 215 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | Rec fRecs[3]; |
| 217 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 218 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | ////////////////////////////////////////////////////////////////////////////// |
| 222 | |
| 223 | static SkView* MyFactory() { return new VerticesView; } |
| 224 | static SkViewRegister reg(MyFactory); |