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