reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | #include "SkGraphics.h" |
| 6 | #include "SkImageDecoder.h" |
| 7 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkRandom.h" |
| 9 | #include "SkRegion.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkUtils.h" |
| 12 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
| 14 | #include "SkColorFilter.h" |
| 15 | #include "SkTime.h" |
| 16 | #include "SkTypeface.h" |
| 17 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkOSFile.h" |
| 19 | #include "SkStream.h" |
| 20 | #include "SkNinePatch.h" |
| 21 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | static SkShader* make_shader0(SkIPoint* size) { |
| 23 | SkBitmap bm; |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 24 | size->set(2, 2); |
| 25 | bm.setConfig(SkBitmap::kARGB_8888_Config, size->fX, size->fY); |
| 26 | SkPMColor color0 = SkPreMultiplyARGB(0x80, 0x80, 0xff, 0x80); |
| 27 | SkPMColor color1 = SkPreMultiplyARGB(0x40, 0xff, 0x00, 0xff); |
| 28 | bm.allocPixels(); |
| 29 | bm.eraseColor(color0); |
| 30 | bm.lockPixels(); |
| 31 | uint32_t* pixels = (uint32_t*) bm.getPixels(); |
| 32 | pixels[0] = pixels[2] = color0; |
| 33 | pixels[1] = pixels[3] = color1; |
| 34 | bm.unlockPixels(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 35 | |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 36 | return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 37 | SkShader::kRepeat_TileMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static 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 }; |
| 44 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
| 45 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL); |
| 46 | } |
| 47 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 48 | class VerticesView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | SkShader* fShader0; |
| 50 | SkShader* fShader1; |
| 51 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 52 | public: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | VerticesView() { |
| 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 | virtual ~VerticesView() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 69 | SkSafeUnref(fShader0); |
| 70 | SkSafeUnref(fShader1); |
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 | protected: |
| 74 | // overrides from SkEventSink |
| 75 | virtual bool onQuery(SkEvent* evt) { |
| 76 | if (SampleCode::TitleQ(*evt)) |
| 77 | { |
| 78 | SkString str("Vertices"); |
| 79 | SampleCode::TitleR(evt, str.c_str()); |
| 80 | return true; |
| 81 | } |
| 82 | return this->INHERITED::onQuery(evt); |
| 83 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | SkScalar fScale; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 86 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 87 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | SkPaint paint; |
| 89 | paint.setDither(true); |
| 90 | paint.setFilterBitmap(true); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 91 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 92 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | canvas->save(); |
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(NULL); |
| 96 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 97 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 98 | NULL, NULL, NULL, 0, paint); |
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 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | paint.setShader(fShader0); |
| 103 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 104 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 105 | NULL, NULL, NULL, 0, paint); |
| 106 | |
| 107 | canvas->translate(SkIntToScalar(250), 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 108 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | paint.setShader(fShader1); |
| 110 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 111 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 112 | NULL, NULL, NULL, 0, paint); |
| 113 | canvas->restore(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | canvas->translate(0, SkIntToScalar(250)); |
| 116 | } |
| 117 | } |
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 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 120 | return new Click(this); |
| 121 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | virtual bool onClick(Click* click) { |
| 124 | // fCurrX = click->fICurr.fX; |
| 125 | // fCurrY = click->fICurr.fY; |
| 126 | this->inval(NULL); |
| 127 | return true; |
| 128 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 129 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | private: |
| 131 | struct Rec { |
| 132 | SkCanvas::VertexMode fMode; |
| 133 | int fCount; |
| 134 | SkPoint* fVerts; |
| 135 | SkPoint* fTexs; |
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 | Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {} |
| 138 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 139 | }; |
| 140 | |
| 141 | void make_tris(Rec* rec) { |
| 142 | int n = 10; |
| 143 | SkRandom rand; |
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 | rec->fMode = SkCanvas::kTriangles_VertexMode; |
| 146 | rec->fCount = n * 3; |
| 147 | rec->fVerts = 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 | for (int i = 0; i < n; i++) { |
| 150 | SkPoint* v = &rec->fVerts[i*3]; |
| 151 | for (int j = 0; j < 3; j++) { |
| 152 | v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); |
| 153 | } |
| 154 | } |
| 155 | } |
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 | void make_fan(Rec* rec, int texWidth, int texHeight) { |
| 158 | const SkScalar tx = SkIntToScalar(texWidth); |
| 159 | const SkScalar ty = SkIntToScalar(texHeight); |
| 160 | const int n = 24; |
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 | rec->fMode = SkCanvas::kTriangleFan_VertexMode; |
| 163 | rec->fCount = n + 2; |
| 164 | rec->fVerts = new SkPoint[rec->fCount]; |
| 165 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 166 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | SkPoint* v = rec->fVerts; |
| 168 | SkPoint* t = rec->fTexs; |
| 169 | |
| 170 | v[0].set(0, 0); |
| 171 | t[0].set(0, 0); |
| 172 | for (int i = 0; i < n; i++) { |
| 173 | SkScalar cos; |
| 174 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 175 | v[i+1].set(cos, sin); |
| 176 | t[i+1].set(i*tx/n, ty); |
| 177 | } |
| 178 | v[n+1] = v[1]; |
| 179 | t[n+1].set(tx, ty); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 180 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | SkMatrix m; |
| 182 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 183 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 184 | m.mapPoints(v, rec->fCount); |
| 185 | } |
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 | void make_strip(Rec* rec, int texWidth, int texHeight) { |
| 188 | const SkScalar tx = SkIntToScalar(texWidth); |
| 189 | const SkScalar ty = SkIntToScalar(texHeight); |
| 190 | const int n = 24; |
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 | rec->fMode = SkCanvas::kTriangleStrip_VertexMode; |
| 193 | rec->fCount = 2 * (n + 1); |
| 194 | rec->fVerts = new SkPoint[rec->fCount]; |
| 195 | rec->fTexs = new SkPoint[rec->fCount]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | SkPoint* v = rec->fVerts; |
| 198 | SkPoint* t = rec->fTexs; |
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 | for (int i = 0; i < n; i++) { |
| 201 | SkScalar cos; |
| 202 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 203 | v[i*2 + 0].set(cos/2, sin/2); |
| 204 | v[i*2 + 1].set(cos, sin); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 205 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | t[i*2 + 0].set(tx * i / n, ty); |
| 207 | t[i*2 + 1].set(tx * i / n, 0); |
| 208 | } |
| 209 | v[2*n + 0] = v[0]; |
| 210 | v[2*n + 1] = v[1]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 211 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | t[2*n + 0].set(tx, ty); |
| 213 | t[2*n + 1].set(tx, 0); |
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 | SkMatrix m; |
| 216 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 217 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 218 | m.mapPoints(v, rec->fCount); |
| 219 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 220 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | Rec fRecs[3]; |
| 222 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 223 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | ////////////////////////////////////////////////////////////////////////////// |
| 227 | |
| 228 | static SkView* MyFactory() { return new VerticesView; } |
| 229 | static SkViewRegister reg(MyFactory); |
| 230 | |