epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkGraphics.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
| 16 | #include "SkRegion.h" |
| 17 | #include "SkShader.h" |
| 18 | #include "SkUtils.h" |
| 19 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
| 22 | #include "SkTime.h" |
| 23 | #include "SkTypeface.h" |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkOSFile.h" |
| 26 | #include "SkStream.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | static SkShader* make_shader0(SkIPoint* size) { |
| 29 | SkBitmap bm; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 30 | size->set(2, 2); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 31 | SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80); |
| 32 | SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff); |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 33 | bm.allocN32Pixels(size->fX, size->fY); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 34 | bm.eraseColor(color0); |
| 35 | bm.lockPixels(); |
| 36 | uint32_t* pixels = (uint32_t*) bm.getPixels(); |
| 37 | pixels[0] = pixels[2] = color0; |
| 38 | pixels[1] = pixels[3] = color1; |
| 39 | bm.unlockPixels(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 41 | return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 42 | SkShader::kRepeat_TileMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static SkShader* make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 46 | SkPoint pts[] = { { 0, 0 }, |
| 47 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
| 49 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
commit-bot@chromium.org | e2b193c | 2014-05-16 13:49:08 +0000 | [diff] [blame] | 50 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 53 | class VerticesView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkShader* fShader0; |
| 55 | SkShader* fShader1; |
| 56 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 57 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | VerticesView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | SkIPoint size; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 60 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | fShader0 = make_shader0(&size); |
| 62 | fShader1 = make_shader1(size); |
| 63 | |
| 64 | make_strip(&fRecs[0], size.fX, size.fY); |
| 65 | make_fan(&fRecs[1], size.fX, size.fY); |
| 66 | make_tris(&fRecs[2]); |
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 | fScale = SK_Scalar1; |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 69 | |
| 70 | this->setBGColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | virtual ~VerticesView() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 74 | SkSafeUnref(fShader0); |
| 75 | SkSafeUnref(fShader1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | protected: |
| 79 | // overrides from SkEventSink |
| 80 | virtual bool onQuery(SkEvent* evt) { |
tfarina@chromium.org | 45369a3 | 2012-09-30 11:30:01 +0000 | [diff] [blame] | 81 | if (SampleCode::TitleQ(*evt)) { |
| 82 | SampleCode::TitleR(evt, "Vertices"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | return this->INHERITED::onQuery(evt); |
| 86 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | SkScalar fScale; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 89 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 90 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | SkPaint paint; |
| 92 | paint.setDither(true); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 93 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 94 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 95 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | canvas->save(); |
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 | paint.setShader(NULL); |
| 99 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 100 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 101 | NULL, NULL, NULL, 0, paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | paint.setShader(fShader0); |
| 106 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 107 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 108 | NULL, NULL, NULL, 0, paint); |
| 109 | |
| 110 | canvas->translate(SkIntToScalar(250), 0); |
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 | paint.setShader(fShader1); |
| 113 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 114 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 115 | NULL, NULL, NULL, 0, paint); |
| 116 | canvas->restore(); |
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 | canvas->translate(0, SkIntToScalar(250)); |
| 119 | } |
| 120 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 121 | |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 122 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | return new Click(this); |
| 124 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 125 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | virtual bool onClick(Click* click) { |
| 127 | // fCurrX = click->fICurr.fX; |
| 128 | // fCurrY = click->fICurr.fY; |
| 129 | this->inval(NULL); |
| 130 | return true; |
| 131 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 132 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | private: |
| 134 | struct Rec { |
| 135 | SkCanvas::VertexMode fMode; |
| 136 | int fCount; |
| 137 | SkPoint* fVerts; |
| 138 | SkPoint* fTexs; |
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() : fCount(0), fVerts(NULL), fTexs(NULL) {} |
| 141 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 142 | }; |
| 143 | |
| 144 | void make_tris(Rec* rec) { |
| 145 | int n = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 146 | SkRandom rand; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 147 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | rec->fMode = SkCanvas::kTriangles_VertexMode; |
| 149 | rec->fCount = n * 3; |
| 150 | rec->fVerts = new SkPoint[rec->fCount]; |
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 | for (int i = 0; i < n; i++) { |
| 153 | SkPoint* v = &rec->fVerts[i*3]; |
| 154 | for (int j = 0; j < 3; j++) { |
| 155 | v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); |
| 156 | } |
| 157 | } |
| 158 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | void make_fan(Rec* rec, int texWidth, int texHeight) { |
| 161 | const SkScalar tx = SkIntToScalar(texWidth); |
| 162 | const SkScalar ty = SkIntToScalar(texHeight); |
| 163 | const int n = 24; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 164 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | rec->fMode = SkCanvas::kTriangleFan_VertexMode; |
| 166 | rec->fCount = n + 2; |
| 167 | rec->fVerts = new SkPoint[rec->fCount]; |
| 168 | rec->fTexs = new SkPoint[rec->fCount]; |
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 | SkPoint* v = rec->fVerts; |
| 171 | SkPoint* t = rec->fTexs; |
| 172 | |
| 173 | v[0].set(0, 0); |
| 174 | t[0].set(0, 0); |
| 175 | for (int i = 0; i < n; i++) { |
| 176 | SkScalar cos; |
| 177 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 178 | v[i+1].set(cos, sin); |
| 179 | t[i+1].set(i*tx/n, ty); |
| 180 | } |
| 181 | v[n+1] = v[1]; |
| 182 | t[n+1].set(tx, ty); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 183 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | SkMatrix m; |
| 185 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 186 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 187 | m.mapPoints(v, rec->fCount); |
| 188 | } |
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 | void make_strip(Rec* rec, int texWidth, int texHeight) { |
| 191 | const SkScalar tx = SkIntToScalar(texWidth); |
| 192 | const SkScalar ty = SkIntToScalar(texHeight); |
| 193 | const int n = 24; |
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 | rec->fMode = SkCanvas::kTriangleStrip_VertexMode; |
| 196 | rec->fCount = 2 * (n + 1); |
| 197 | rec->fVerts = new SkPoint[rec->fCount]; |
| 198 | rec->fTexs = new SkPoint[rec->fCount]; |
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 | SkPoint* v = rec->fVerts; |
| 201 | SkPoint* t = rec->fTexs; |
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 | for (int i = 0; i < n; i++) { |
| 204 | SkScalar cos; |
| 205 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 206 | v[i*2 + 0].set(cos/2, sin/2); |
| 207 | v[i*2 + 1].set(cos, sin); |
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 | t[i*2 + 0].set(tx * i / n, ty); |
| 210 | t[i*2 + 1].set(tx * i / n, 0); |
| 211 | } |
| 212 | v[2*n + 0] = v[0]; |
| 213 | v[2*n + 1] = v[1]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | t[2*n + 0].set(tx, ty); |
| 216 | t[2*n + 1].set(tx, 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 217 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | SkMatrix m; |
| 219 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 220 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 221 | m.mapPoints(v, rec->fCount); |
| 222 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 223 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | Rec fRecs[3]; |
| 225 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 226 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | ////////////////////////////////////////////////////////////////////////////// |
| 230 | |
| 231 | static SkView* MyFactory() { return new VerticesView; } |
| 232 | static SkViewRegister reg(MyFactory); |