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 | |
| 21 | #include "SkGeometry.h" // private include :( |
| 22 | |
| 23 | static void drawtriangle(SkCanvas* canvas, const SkPaint& paint, |
| 24 | const SkPoint pts[3]) { |
| 25 | SkPath path; |
| 26 | |
| 27 | path.moveTo(pts[0]); |
| 28 | path.lineTo(pts[1]); |
| 29 | path.lineTo(pts[2]); |
| 30 | |
| 31 | canvas->drawPath(path, paint); |
| 32 | } |
| 33 | |
| 34 | static SkShader* make_shader0(SkIPoint* size) { |
| 35 | SkBitmap bm; |
| 36 | |
| 37 | // SkImageDecoder::DecodeFile("/skimages/progressivejpg.jpg", &bm); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 38 | SkImageDecoder::DecodeFile("/skimages/logo.png", &bm); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | size->set(bm.width(), bm.height()); |
| 40 | return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode, |
| 41 | SkShader::kClamp_TileMode); |
| 42 | } |
| 43 | |
| 44 | static SkShader* make_shader1(const SkIPoint& size) { |
| 45 | SkPoint pts[] = { 0, 0, SkIntToScalar(size.fX), SkIntToScalar(size.fY) }; |
| 46 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
| 47 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
| 48 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL); |
| 49 | } |
| 50 | |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
| 53 | class Patch { |
| 54 | public: |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 55 | Patch() { sk_bzero(fPts, sizeof(fPts)); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | ~Patch() {} |
| 57 | |
| 58 | void setPatch(const SkPoint pts[12]) { |
| 59 | memcpy(fPts, pts, 12 * sizeof(SkPoint)); |
| 60 | fPts[12] = pts[0]; // the last shall be first |
| 61 | } |
| 62 | void setBounds(int w, int h) { fW = w; fH = h; } |
| 63 | |
| 64 | void draw(SkCanvas*, const SkPaint&, int segsU, int segsV, |
| 65 | bool doTextures, bool doColors); |
| 66 | |
| 67 | private: |
| 68 | SkPoint fPts[13]; |
| 69 | int fW, fH; |
| 70 | }; |
| 71 | |
| 72 | static void eval_patch_edge(const SkPoint cubic[], SkPoint samples[], int segs) { |
| 73 | SkScalar t = 0; |
| 74 | SkScalar dt = SK_Scalar1 / segs; |
| 75 | |
| 76 | samples[0] = cubic[0]; |
| 77 | for (int i = 1; i < segs; i++) { |
| 78 | t += dt; |
| 79 | SkEvalCubicAt(cubic, t, &samples[i], NULL, NULL); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv, |
| 84 | SkPoint* pt) { |
| 85 | const int TL = 0; |
| 86 | const int TR = nu; |
| 87 | const int BR = TR + nv; |
| 88 | const int BL = BR + nu; |
| 89 | |
| 90 | SkScalar u = SkIntToScalar(iu) / nu; |
| 91 | SkScalar v = SkIntToScalar(iv) / nv; |
| 92 | |
| 93 | SkScalar uv = SkScalarMul(u, v); |
| 94 | SkScalar Uv = SkScalarMul(SK_Scalar1 - u, v); |
| 95 | SkScalar uV = SkScalarMul(u, SK_Scalar1 - v); |
| 96 | SkScalar UV = SkScalarMul(SK_Scalar1 - u, SK_Scalar1 - v); |
| 97 | |
| 98 | SkScalar x0 = SkScalarMul(UV, edge[TL].fX) + SkScalarMul(uV, edge[TR].fX) + |
| 99 | SkScalarMul(Uv, edge[BL].fX) + SkScalarMul(uv, edge[BR].fX); |
| 100 | SkScalar y0 = SkScalarMul(UV, edge[TL].fY) + SkScalarMul(uV, edge[TR].fY) + |
| 101 | SkScalarMul(Uv, edge[BL].fY) + SkScalarMul(uv, edge[BR].fY); |
| 102 | |
| 103 | SkScalar x = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fX) + |
| 104 | SkScalarMul(u, edge[TR+iv].fX) + |
| 105 | SkScalarMul(v, edge[BR+nu-iu].fX) + |
| 106 | SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fX) - x0; |
| 107 | SkScalar y = SkScalarMul(SK_Scalar1 - v, edge[TL+iu].fY) + |
| 108 | SkScalarMul(u, edge[TR+iv].fY) + |
| 109 | SkScalarMul(v, edge[BR+nu-iu].fY) + |
| 110 | SkScalarMul(SK_Scalar1 - u, edge[BL+nv-iv].fY) - y0; |
| 111 | pt->set(x, y); |
| 112 | } |
| 113 | |
| 114 | static int ScalarTo255(SkScalar v) { |
| 115 | int scale = SkScalarToFixed(v) >> 8; |
| 116 | if (scale < 0) { |
| 117 | scale = 0; |
| 118 | } else if (scale > 255) { |
| 119 | scale = 255; |
| 120 | } |
| 121 | return scale; |
| 122 | } |
| 123 | |
| 124 | static SkColor make_color(SkScalar s, SkScalar t) { |
| 125 | int cs = ScalarTo255(s); |
| 126 | int ct = ScalarTo255(t); |
| 127 | return SkColorSetARGB(0xFF, cs, 0, 0) + SkColorSetARGB(0, 0, ct, 0); |
| 128 | } |
| 129 | |
| 130 | void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv, |
| 131 | bool doTextures, bool doColors) { |
| 132 | if (nu < 1 || nv < 1) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | int i, npts = (nu + nv) * 2; |
| 137 | SkAutoSTMalloc<16, SkPoint> storage(npts + 1); |
| 138 | SkPoint* edge0 = storage.get(); |
| 139 | SkPoint* edge1 = edge0 + nu; |
| 140 | SkPoint* edge2 = edge1 + nv; |
| 141 | SkPoint* edge3 = edge2 + nu; |
| 142 | |
| 143 | // evaluate the edge points |
| 144 | eval_patch_edge(fPts + 0, edge0, nu); |
| 145 | eval_patch_edge(fPts + 3, edge1, nv); |
| 146 | eval_patch_edge(fPts + 6, edge2, nu); |
| 147 | eval_patch_edge(fPts + 9, edge3, nv); |
| 148 | edge3[nv] = edge0[0]; // the last shall be first |
| 149 | |
| 150 | for (i = 0; i < npts; i++) { |
| 151 | // canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint); |
| 152 | } |
| 153 | |
| 154 | int row, vertCount = (nu + 1) * (nv + 1); |
| 155 | SkAutoTMalloc<SkPoint> vertStorage(vertCount); |
| 156 | SkPoint* verts = vertStorage.get(); |
| 157 | |
| 158 | // first row |
| 159 | memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint)); |
| 160 | // rows |
| 161 | SkPoint* r = verts; |
| 162 | for (row = 1; row < nv; row++) { |
| 163 | r += nu + 1; |
| 164 | r[0] = edge3[nv - row]; |
| 165 | for (int col = 1; col < nu; col++) { |
| 166 | eval_sheet(edge0, nu, nv, col, row, &r[col]); |
| 167 | } |
| 168 | r[nu] = edge1[row]; |
| 169 | } |
| 170 | // last row |
| 171 | SkPoint* last = verts + nv * (nu + 1); |
| 172 | for (i = 0; i <= nu; i++) { |
| 173 | last[i] = edge2[nu - i]; |
| 174 | } |
| 175 | |
| 176 | // canvas->drawPoints(verts, vertCount, paint); |
| 177 | |
| 178 | int stripCount = (nu + 1) * 2; |
| 179 | SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2); |
| 180 | SkAutoTMalloc<SkColor> colorStorage(stripCount); |
| 181 | SkPoint* strip = stripStorage.get(); |
| 182 | SkPoint* tex = strip + stripCount; |
| 183 | SkColor* colors = colorStorage.get(); |
| 184 | SkScalar t = 0; |
| 185 | const SkScalar ds = SK_Scalar1 * fW / nu; |
| 186 | const SkScalar dt = SK_Scalar1 * fH / nv; |
| 187 | r = verts; |
| 188 | for (row = 0; row < nv; row++) { |
| 189 | SkPoint* upper = r; |
| 190 | SkPoint* lower = r + nu + 1; |
| 191 | r = lower; |
| 192 | SkScalar s = 0; |
| 193 | for (i = 0; i <= nu; i++) { |
| 194 | strip[i*2 + 0] = *upper++; |
| 195 | strip[i*2 + 1] = *lower++; |
| 196 | tex[i*2 + 0].set(s, t); |
| 197 | tex[i*2 + 1].set(s, t + dt); |
| 198 | colors[i*2 + 0] = make_color(s/fW, t/fH); |
| 199 | colors[i*2 + 1] = make_color(s/fW, (t + dt)/fH); |
| 200 | s += ds; |
| 201 | } |
| 202 | t += dt; |
| 203 | canvas->drawVertices(SkCanvas::kTriangleStrip_VertexMode, stripCount, |
| 204 | strip, doTextures ? tex : NULL, |
| 205 | doColors ? colors : NULL, NULL, |
| 206 | NULL, 0, paint); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static void drawpatches(SkCanvas* canvas, const SkPaint& paint, int nu, int nv, |
| 211 | Patch* patch) { |
| 212 | |
| 213 | SkAutoCanvasRestore ar(canvas, true); |
| 214 | |
| 215 | patch->draw(canvas, paint, 10, 10, false, false); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 216 | canvas->translate(SkIntToScalar(180), 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | patch->draw(canvas, paint, 10, 10, true, false); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 218 | canvas->translate(SkIntToScalar(180), 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | patch->draw(canvas, paint, 10, 10, false, true); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 220 | canvas->translate(SkIntToScalar(180), 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | patch->draw(canvas, paint, 10, 10, true, true); |
| 222 | } |
| 223 | |
| 224 | class PatchView : public SkView { |
| 225 | SkShader* fShader0; |
| 226 | SkShader* fShader1; |
| 227 | SkIPoint fSize0, fSize1; |
| 228 | SkPoint fPts[12]; |
| 229 | |
| 230 | public: |
| 231 | PatchView() { |
| 232 | fShader0 = make_shader0(&fSize0); |
| 233 | fSize1 = fSize0; |
| 234 | if (fSize0.fX == 0 || fSize0.fY == 0) { |
| 235 | fSize1.set(2, 2); |
| 236 | } |
| 237 | fShader1 = make_shader1(fSize1); |
| 238 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 239 | const SkScalar S = SkIntToScalar(50); |
| 240 | const SkScalar T = SkIntToScalar(40); |
| 241 | fPts[0].set(S*0, T); |
| 242 | fPts[1].set(S*1, T); |
| 243 | fPts[2].set(S*2, T); |
| 244 | fPts[3].set(S*3, T); |
| 245 | fPts[4].set(S*3, T*2); |
| 246 | fPts[5].set(S*3, T*3); |
| 247 | fPts[6].set(S*3, T*4); |
| 248 | fPts[7].set(S*2, T*4); |
| 249 | fPts[8].set(S*1, T*4); |
| 250 | fPts[9].set(S*0, T*4); |
| 251 | fPts[10].set(S*0, T*3); |
| 252 | fPts[11].set(S*0, T*2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | virtual ~PatchView() { |
| 256 | fShader0->safeUnref(); |
| 257 | fShader1->safeUnref(); |
| 258 | } |
| 259 | |
| 260 | protected: |
| 261 | // overrides from SkEventSink |
| 262 | virtual bool onQuery(SkEvent* evt) { |
| 263 | if (SampleCode::TitleQ(*evt)) |
| 264 | { |
| 265 | SkString str("Patch"); |
| 266 | SampleCode::TitleR(evt, str.c_str()); |
| 267 | return true; |
| 268 | } |
| 269 | return this->INHERITED::onQuery(evt); |
| 270 | } |
| 271 | |
| 272 | void drawBG(SkCanvas* canvas) { |
| 273 | canvas->drawColor(SK_ColorGRAY); |
| 274 | } |
| 275 | |
| 276 | virtual void onDraw(SkCanvas* canvas) { |
| 277 | this->drawBG(canvas); |
| 278 | |
| 279 | SkPaint paint; |
| 280 | paint.setDither(true); |
| 281 | paint.setFilterBitmap(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 283 | canvas->translate(SkIntToScalar(20), 0); |
| 284 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 285 | Patch patch; |
| 286 | |
| 287 | paint.setShader(fShader0); |
| 288 | if (fSize0.fX == 0) { |
| 289 | fSize0.fX = 1; |
| 290 | } |
| 291 | if (fSize0.fY == 0) { |
| 292 | fSize0.fY = 1; |
| 293 | } |
| 294 | patch.setBounds(fSize0.fX, fSize0.fY); |
| 295 | |
| 296 | patch.setPatch(fPts); |
| 297 | drawpatches(canvas, paint, 10, 10, &patch); |
| 298 | |
| 299 | paint.setShader(NULL); |
| 300 | paint.setAntiAlias(true); |
| 301 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 302 | canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | |
| 304 | canvas->translate(0, SkIntToScalar(300)); |
| 305 | |
| 306 | paint.setAntiAlias(false); |
| 307 | paint.setShader(fShader1); |
| 308 | patch.setBounds(fSize1.fX, fSize1.fY); |
| 309 | drawpatches(canvas, paint, 10, 10, &patch); |
| 310 | } |
| 311 | |
| 312 | class PtClick : public Click { |
| 313 | public: |
| 314 | int fIndex; |
| 315 | PtClick(SkView* view, int index) : Click(view), fIndex(index) {} |
| 316 | }; |
| 317 | |
| 318 | static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) { |
| 319 | return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5); |
| 320 | } |
| 321 | |
| 322 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 323 | for (int i = 0; i < SK_ARRAY_COUNT(fPts); i++) { |
| 324 | if (hittest(fPts[i], x, y)) { |
| 325 | return new PtClick(this, i); |
| 326 | } |
| 327 | } |
| 328 | return this->INHERITED::onFindClickHandler(x, y); |
| 329 | } |
| 330 | |
| 331 | virtual bool onClick(Click* click) { |
| 332 | fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX, click->fCurr.fY); |
| 333 | this->inval(NULL); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | typedef SkView INHERITED; |
| 339 | }; |
| 340 | |
| 341 | ////////////////////////////////////////////////////////////////////////////// |
| 342 | |
| 343 | static SkView* MyFactory() { return new PatchView; } |
| 344 | static SkViewRegister reg(MyFactory); |
| 345 | |