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 | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkPaint.h" |
| 13 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 14 | #define BG_COLOR 0xFFDDDDDD |
| 15 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 16 | typedef void (*SlideProc)(SkCanvas*); |
| 17 | |
| 18 | /////////////////////////////////////////////////////////////////////////////// |
| 19 | |
| 20 | #include "Sk1DPathEffect.h" |
| 21 | #include "Sk2DPathEffect.h" |
| 22 | #include "SkCornerPathEffect.h" |
| 23 | #include "SkDashPathEffect.h" |
| 24 | #include "SkDiscretePathEffect.h" |
| 25 | |
| 26 | static void compose_pe(SkPaint* paint) { |
| 27 | SkPathEffect* pe = paint->getPathEffect(); |
| 28 | SkPathEffect* corner = new SkCornerPathEffect(25); |
| 29 | SkPathEffect* compose; |
| 30 | if (pe) { |
| 31 | compose = new SkComposePathEffect(pe, corner); |
| 32 | corner->unref(); |
| 33 | } else { |
| 34 | compose = corner; |
| 35 | } |
| 36 | paint->setPathEffect(compose)->unref(); |
| 37 | } |
| 38 | |
| 39 | static void hair_pe(SkPaint* paint) { |
| 40 | paint->setStrokeWidth(0); |
| 41 | } |
| 42 | |
| 43 | static void hair2_pe(SkPaint* paint) { |
| 44 | paint->setStrokeWidth(0); |
| 45 | compose_pe(paint); |
| 46 | } |
| 47 | |
| 48 | static void stroke_pe(SkPaint* paint) { |
| 49 | paint->setStrokeWidth(12); |
| 50 | compose_pe(paint); |
| 51 | } |
| 52 | |
| 53 | static void dash_pe(SkPaint* paint) { |
| 54 | SkScalar inter[] = { 20, 10, 10, 10 }; |
| 55 | paint->setStrokeWidth(12); |
| 56 | paint->setPathEffect(new SkDashPathEffect(inter, SK_ARRAY_COUNT(inter), |
| 57 | 0))->unref(); |
| 58 | compose_pe(paint); |
| 59 | } |
| 60 | |
| 61 | static const int gXY[] = { |
| 62 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 63 | }; |
| 64 | |
| 65 | static void scale(SkPath* path, SkScalar scale) { |
| 66 | SkMatrix m; |
| 67 | m.setScale(scale, scale); |
| 68 | path->transform(m); |
| 69 | } |
| 70 | |
| 71 | static void one_d_pe(SkPaint* paint) { |
| 72 | SkPath path; |
| 73 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 74 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 75 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 76 | path.close(); |
| 77 | path.offset(SkIntToScalar(-6), 0); |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 78 | scale(&path, SkFloatToScalar(1.5f)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 79 | |
| 80 | paint->setPathEffect(new SkPath1DPathEffect(path, SkIntToScalar(21), 0, |
| 81 | SkPath1DPathEffect::kRotate_Style))->unref(); |
| 82 | compose_pe(paint); |
| 83 | } |
| 84 | |
| 85 | typedef void (*PE_Proc)(SkPaint*); |
| 86 | static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe }; |
| 87 | |
| 88 | static void fill_pe(SkPaint* paint) { |
| 89 | paint->setStyle(SkPaint::kFill_Style); |
| 90 | paint->setPathEffect(NULL); |
| 91 | } |
| 92 | |
| 93 | static void discrete_pe(SkPaint* paint) { |
| 94 | paint->setPathEffect(new SkDiscretePathEffect(10, 4))->unref(); |
| 95 | } |
| 96 | |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 97 | static SkPathEffect* MakeTileEffect() { |
| 98 | SkMatrix m; |
| 99 | m.setScale(SkIntToScalar(12), SkIntToScalar(12)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 100 | |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 101 | SkPath path; |
| 102 | path.addCircle(0, 0, SkIntToScalar(5)); |
| 103 | |
| 104 | return new SkPath2DPathEffect(m, path); |
| 105 | } |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 106 | |
| 107 | static void tile_pe(SkPaint* paint) { |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 108 | paint->setPathEffect(MakeTileEffect())->unref(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe }; |
| 112 | |
| 113 | static void patheffect_slide(SkCanvas* canvas) { |
| 114 | SkPaint paint; |
| 115 | paint.setAntiAlias(true); |
| 116 | paint.setStyle(SkPaint::kStroke_Style); |
| 117 | |
| 118 | SkPath path; |
| 119 | path.moveTo(20, 20); |
| 120 | path.lineTo(70, 120); |
| 121 | path.lineTo(120, 30); |
| 122 | path.lineTo(170, 80); |
| 123 | path.lineTo(240, 50); |
| 124 | |
| 125 | size_t i; |
| 126 | canvas->save(); |
| 127 | for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) { |
| 128 | gPE[i](&paint); |
| 129 | canvas->drawPath(path, paint); |
| 130 | canvas->translate(0, 75); |
| 131 | } |
| 132 | canvas->restore(); |
| 133 | |
| 134 | path.reset(); |
| 135 | SkRect r = { 0, 0, 250, 120 }; |
| 136 | path.addOval(r, SkPath::kCW_Direction); |
| 137 | r.inset(50, 50); |
| 138 | path.addRect(r, SkPath::kCCW_Direction); |
| 139 | |
| 140 | canvas->translate(320, 20); |
| 141 | for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) { |
| 142 | gPE2[i](&paint); |
| 143 | canvas->drawPath(path, paint); |
| 144 | canvas->translate(0, 160); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | #include "SkGradientShader.h" |
| 151 | |
| 152 | struct GradData { |
| 153 | int fCount; |
| 154 | const SkColor* fColors; |
| 155 | const SkScalar* fPos; |
| 156 | }; |
| 157 | |
| 158 | static const SkColor gColors[] = { |
| 159 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 160 | }; |
| 161 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 162 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 163 | static const SkScalar gPos2[] = { |
| 164 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 165 | }; |
| 166 | |
| 167 | static const GradData gGradData[] = { |
| 168 | { 2, gColors, NULL }, |
| 169 | { 2, gColors, gPos0 }, |
| 170 | { 2, gColors, gPos1 }, |
| 171 | { 5, gColors, NULL }, |
| 172 | { 5, gColors, gPos2 } |
| 173 | }; |
| 174 | |
| 175 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
| 176 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 177 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 178 | data.fCount, tm, mapper); |
| 179 | } |
| 180 | |
| 181 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 182 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 183 | SkPoint center; |
| 184 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 185 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 186 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
| 187 | data.fPos, data.fCount, tm, mapper); |
| 188 | } |
| 189 | |
| 190 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
| 191 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 192 | SkPoint center; |
| 193 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 194 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 195 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 196 | data.fPos, data.fCount, mapper); |
| 197 | } |
| 198 | |
| 199 | static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
| 200 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 201 | SkPoint center0, center1; |
| 202 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 203 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 204 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 205 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 206 | return SkGradientShader::CreateTwoPointRadial( |
| 207 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 208 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 209 | data.fColors, data.fPos, data.fCount, tm, mapper); |
| 210 | } |
| 211 | |
| 212 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 213 | SkShader::TileMode tm, SkUnitMapper* mapper); |
| 214 | static const GradMaker gGradMakers[] = { |
| 215 | MakeLinear, MakeRadial, MakeSweep, Make2Radial |
| 216 | }; |
| 217 | |
| 218 | static void gradient_slide(SkCanvas* canvas) { |
| 219 | SkPoint pts[2] = { |
| 220 | { 0, 0 }, |
| 221 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 222 | }; |
| 223 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 224 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 225 | SkPaint paint; |
| 226 | paint.setAntiAlias(true); |
| 227 | paint.setDither(true); |
| 228 | |
| 229 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
| 230 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 231 | canvas->save(); |
| 232 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
| 233 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); |
| 234 | paint.setShader(shader); |
| 235 | canvas->drawRect(r, paint); |
| 236 | shader->unref(); |
| 237 | canvas->translate(0, SkIntToScalar(120)); |
| 238 | } |
| 239 | canvas->restore(); |
| 240 | canvas->translate(SkIntToScalar(120), 0); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /////////////////////////////////////////////////////////////////////////////// |
| 245 | |
| 246 | #include "SkPathMeasure.h" |
| 247 | |
| 248 | static SkScalar getpathlen(const SkPath& path) { |
| 249 | SkPathMeasure meas(path, false); |
| 250 | return meas.getLength(); |
| 251 | } |
| 252 | |
| 253 | static void textonpath_slide(SkCanvas* canvas) { |
| 254 | const char* text = "Displacement"; |
| 255 | size_t len =strlen(text); |
| 256 | SkPath path; |
| 257 | path.moveTo(100, 300); |
| 258 | path.quadTo(300, 100, 500, 300); |
| 259 | path.offset(0, -100); |
| 260 | |
| 261 | SkPaint paint; |
| 262 | paint.setAntiAlias(true); |
| 263 | paint.setTextSize(40); |
| 264 | |
| 265 | paint.setStyle(SkPaint::kStroke_Style); |
| 266 | canvas->drawPath(path, paint); |
| 267 | paint.setStyle(SkPaint::kFill_Style); |
| 268 | |
| 269 | SkScalar x = 50; |
| 270 | paint.setColor(0xFF008800); |
| 271 | canvas->drawTextOnPathHV(text, len, path, |
| 272 | x, paint.getTextSize()*2/3, paint); |
| 273 | paint.setColor(SK_ColorRED); |
| 274 | canvas->drawTextOnPathHV(text, len, path, |
| 275 | x + 60, 0, paint); |
| 276 | paint.setColor(SK_ColorBLUE); |
| 277 | canvas->drawTextOnPathHV(text, len, path, |
| 278 | x + 120, -paint.getTextSize()*2/3, paint); |
| 279 | |
| 280 | path.offset(0, 200); |
| 281 | paint.setTextAlign(SkPaint::kRight_Align); |
| 282 | |
| 283 | text = "Matrices"; |
| 284 | len = strlen(text); |
| 285 | SkScalar pathLen = getpathlen(path); |
| 286 | SkMatrix matrix; |
| 287 | |
| 288 | paint.setColor(SK_ColorBLACK); |
| 289 | paint.setStyle(SkPaint::kStroke_Style); |
| 290 | canvas->drawPath(path, paint); |
| 291 | paint.setStyle(SkPaint::kFill_Style); |
| 292 | |
| 293 | paint.setTextSize(50); |
| 294 | canvas->drawTextOnPath(text, len, path, NULL, paint); |
| 295 | |
| 296 | paint.setColor(SK_ColorRED); |
| 297 | matrix.setScale(-SK_Scalar1, SK_Scalar1); |
| 298 | matrix.postTranslate(pathLen, 0); |
| 299 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 300 | |
| 301 | paint.setColor(SK_ColorBLUE); |
| 302 | matrix.setScale(SK_Scalar1, -SK_Scalar1); |
| 303 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 304 | |
| 305 | paint.setColor(0xFF008800); |
| 306 | matrix.setScale(-SK_Scalar1, -SK_Scalar1); |
| 307 | matrix.postTranslate(pathLen, 0); |
| 308 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 309 | } |
| 310 | |
| 311 | /////////////////////////////////////////////////////////////////////////////// |
| 312 | |
| 313 | #include "SkImageDecoder.h" |
| 314 | #include "SkOSFile.h" |
| 315 | #include "SkRandom.h" |
| 316 | #include "SkStream.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 317 | |
| 318 | static SkShader* make_shader0(SkIPoint* size) { |
| 319 | SkBitmap bm; |
| 320 | |
| 321 | SkImageDecoder::DecodeFile("/skimages/logo.gif", &bm); |
| 322 | size->set(bm.width(), bm.height()); |
| 323 | return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode, |
| 324 | SkShader::kClamp_TileMode); |
| 325 | } |
| 326 | |
| 327 | static SkShader* make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 328 | SkPoint pts[] = { { 0, 0 }, |
| 329 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 330 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
| 331 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
| 332 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL); |
| 333 | } |
| 334 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 335 | class Rec { |
| 336 | public: |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 337 | SkCanvas::VertexMode fMode; |
| 338 | int fCount; |
| 339 | SkPoint* fVerts; |
| 340 | SkPoint* fTexs; |
| 341 | |
| 342 | Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {} |
| 343 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 344 | }; |
| 345 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 346 | static void make_tris(Rec* rec) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 347 | int n = 10; |
| 348 | SkRandom rand; |
| 349 | |
| 350 | rec->fMode = SkCanvas::kTriangles_VertexMode; |
| 351 | rec->fCount = n * 3; |
| 352 | rec->fVerts = new SkPoint[rec->fCount]; |
| 353 | |
| 354 | for (int i = 0; i < n; i++) { |
| 355 | SkPoint* v = &rec->fVerts[i*3]; |
| 356 | for (int j = 0; j < 3; j++) { |
| 357 | v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 362 | static void make_fan(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 363 | const SkScalar tx = SkIntToScalar(texWidth); |
| 364 | const SkScalar ty = SkIntToScalar(texHeight); |
| 365 | const int n = 24; |
| 366 | |
| 367 | rec->fMode = SkCanvas::kTriangleFan_VertexMode; |
| 368 | rec->fCount = n + 2; |
| 369 | rec->fVerts = new SkPoint[rec->fCount]; |
| 370 | rec->fTexs = new SkPoint[rec->fCount]; |
| 371 | |
| 372 | SkPoint* v = rec->fVerts; |
| 373 | SkPoint* t = rec->fTexs; |
| 374 | |
| 375 | v[0].set(0, 0); |
| 376 | t[0].set(0, 0); |
| 377 | for (int i = 0; i < n; i++) { |
| 378 | SkScalar cos; |
| 379 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 380 | v[i+1].set(cos, sin); |
| 381 | t[i+1].set(i*tx/n, ty); |
| 382 | } |
| 383 | v[n+1] = v[1]; |
| 384 | t[n+1].set(tx, ty); |
| 385 | |
| 386 | SkMatrix m; |
| 387 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 388 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 389 | m.mapPoints(v, rec->fCount); |
| 390 | } |
| 391 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 392 | static void make_strip(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 393 | const SkScalar tx = SkIntToScalar(texWidth); |
| 394 | const SkScalar ty = SkIntToScalar(texHeight); |
| 395 | const int n = 24; |
| 396 | |
| 397 | rec->fMode = SkCanvas::kTriangleStrip_VertexMode; |
| 398 | rec->fCount = 2 * (n + 1); |
| 399 | rec->fVerts = new SkPoint[rec->fCount]; |
| 400 | rec->fTexs = new SkPoint[rec->fCount]; |
| 401 | |
| 402 | SkPoint* v = rec->fVerts; |
| 403 | SkPoint* t = rec->fTexs; |
| 404 | |
| 405 | for (int i = 0; i < n; i++) { |
| 406 | SkScalar cos; |
| 407 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 408 | v[i*2 + 0].set(cos/2, sin/2); |
| 409 | v[i*2 + 1].set(cos, sin); |
| 410 | |
| 411 | t[i*2 + 0].set(tx * i / n, ty); |
| 412 | t[i*2 + 1].set(tx * i / n, 0); |
| 413 | } |
| 414 | v[2*n + 0] = v[0]; |
| 415 | v[2*n + 1] = v[1]; |
| 416 | |
| 417 | t[2*n + 0].set(tx, ty); |
| 418 | t[2*n + 1].set(tx, 0); |
| 419 | |
| 420 | SkMatrix m; |
| 421 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 422 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 423 | m.mapPoints(v, rec->fCount); |
| 424 | } |
| 425 | |
| 426 | static void mesh_slide(SkCanvas* canvas) { |
| 427 | Rec fRecs[3]; |
| 428 | SkIPoint size; |
| 429 | |
| 430 | SkShader* fShader0 = make_shader0(&size); |
| 431 | SkShader* fShader1 = make_shader1(size); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 432 | |
| 433 | SkAutoUnref aur0(fShader0); |
| 434 | SkAutoUnref aur1(fShader1); |
| 435 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 436 | make_strip(&fRecs[0], size.fX, size.fY); |
| 437 | make_fan(&fRecs[1], size.fX, size.fY); |
| 438 | make_tris(&fRecs[2]); |
| 439 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 440 | SkPaint paint; |
| 441 | paint.setDither(true); |
| 442 | paint.setFilterBitmap(true); |
| 443 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 444 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 445 | canvas->save(); |
| 446 | |
| 447 | paint.setShader(NULL); |
| 448 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 449 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 450 | NULL, NULL, NULL, 0, paint); |
| 451 | |
| 452 | canvas->translate(SkIntToScalar(210), 0); |
| 453 | |
| 454 | paint.setShader(fShader0); |
| 455 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 456 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 457 | NULL, NULL, NULL, 0, paint); |
| 458 | |
| 459 | canvas->translate(SkIntToScalar(210), 0); |
| 460 | |
| 461 | paint.setShader(fShader1); |
| 462 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 463 | fRecs[i].fVerts, fRecs[i].fTexs, |
| 464 | NULL, NULL, NULL, 0, paint); |
| 465 | canvas->restore(); |
| 466 | |
| 467 | canvas->translate(0, SkIntToScalar(250)); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /////////////////////////////////////////////////////////////////////////////// |
| 472 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 473 | #include "SkGradientShader.h" |
| 474 | #include "SkLayerRasterizer.h" |
| 475 | #include "SkBlurMaskFilter.h" |
| 476 | |
| 477 | static void r0(SkLayerRasterizer* rast, SkPaint& p) |
| 478 | { |
| 479 | p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3), |
| 480 | SkBlurMaskFilter::kNormal_BlurStyle))->unref(); |
| 481 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
| 482 | |
| 483 | p.setMaskFilter(NULL); |
| 484 | p.setStyle(SkPaint::kStroke_Style); |
| 485 | p.setStrokeWidth(SK_Scalar1); |
| 486 | rast->addLayer(p); |
| 487 | |
| 488 | p.setAlpha(0x11); |
| 489 | p.setStyle(SkPaint::kFill_Style); |
| 490 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 491 | rast->addLayer(p); |
| 492 | } |
| 493 | |
| 494 | static void r1(SkLayerRasterizer* rast, SkPaint& p) |
| 495 | { |
| 496 | rast->addLayer(p); |
| 497 | |
| 498 | p.setAlpha(0x40); |
| 499 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 500 | p.setStyle(SkPaint::kStroke_Style); |
| 501 | p.setStrokeWidth(SK_Scalar1*2); |
| 502 | rast->addLayer(p); |
| 503 | } |
| 504 | |
| 505 | static void r2(SkLayerRasterizer* rast, SkPaint& p) |
| 506 | { |
| 507 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 508 | p.setStrokeWidth(SK_Scalar1*4); |
| 509 | rast->addLayer(p); |
| 510 | |
| 511 | p.setStyle(SkPaint::kStroke_Style); |
| 512 | p.setStrokeWidth(SK_Scalar1*3/2); |
| 513 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 514 | rast->addLayer(p); |
| 515 | } |
| 516 | |
| 517 | static void r3(SkLayerRasterizer* rast, SkPaint& p) |
| 518 | { |
| 519 | p.setStyle(SkPaint::kStroke_Style); |
| 520 | p.setStrokeWidth(SK_Scalar1*3); |
| 521 | rast->addLayer(p); |
| 522 | |
| 523 | p.setAlpha(0x20); |
| 524 | p.setStyle(SkPaint::kFill_Style); |
| 525 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 526 | rast->addLayer(p); |
| 527 | } |
| 528 | |
| 529 | static void r4(SkLayerRasterizer* rast, SkPaint& p) |
| 530 | { |
| 531 | p.setAlpha(0x60); |
| 532 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
| 533 | |
| 534 | p.setAlpha(0xFF); |
| 535 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 536 | rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); |
| 537 | |
| 538 | p.setXfermode(NULL); |
| 539 | rast->addLayer(p); |
| 540 | } |
| 541 | |
| 542 | #include "SkDiscretePathEffect.h" |
| 543 | |
| 544 | static void r5(SkLayerRasterizer* rast, SkPaint& p) |
| 545 | { |
| 546 | rast->addLayer(p); |
| 547 | |
| 548 | p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref(); |
| 549 | p.setXfermodeMode(SkXfermode::kSrcOut_Mode); |
| 550 | rast->addLayer(p); |
| 551 | } |
| 552 | |
| 553 | static void r6(SkLayerRasterizer* rast, SkPaint& p) |
| 554 | { |
| 555 | rast->addLayer(p); |
| 556 | |
| 557 | p.setAntiAlias(false); |
| 558 | SkLayerRasterizer* rast2 = new SkLayerRasterizer; |
| 559 | r5(rast2, p); |
| 560 | p.setRasterizer(rast2)->unref(); |
| 561 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 562 | rast->addLayer(p); |
| 563 | } |
| 564 | |
| 565 | #include "Sk2DPathEffect.h" |
| 566 | |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 567 | static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) { |
| 568 | SkPath path; |
| 569 | path.addCircle(0, 0, radius); |
| 570 | return new SkPath2DPathEffect(matrix, path); |
| 571 | } |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 572 | |
| 573 | static void r7(SkLayerRasterizer* rast, SkPaint& p) |
| 574 | { |
| 575 | SkMatrix lattice; |
| 576 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 577 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 578 | p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref(); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 579 | rast->addLayer(p); |
| 580 | } |
| 581 | |
| 582 | static void r8(SkLayerRasterizer* rast, SkPaint& p) |
| 583 | { |
| 584 | rast->addLayer(p); |
| 585 | |
| 586 | SkMatrix lattice; |
| 587 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 588 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 589 | p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 590 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 591 | rast->addLayer(p); |
| 592 | |
| 593 | p.setPathEffect(NULL); |
| 594 | p.setXfermode(NULL); |
| 595 | p.setStyle(SkPaint::kStroke_Style); |
| 596 | p.setStrokeWidth(SK_Scalar1); |
| 597 | rast->addLayer(p); |
| 598 | } |
| 599 | |
| 600 | class Line2DPathEffect : public Sk2DPathEffect { |
| 601 | public: |
| 602 | Line2DPathEffect(SkScalar width, const SkMatrix& matrix) |
| 603 | : Sk2DPathEffect(matrix), fWidth(width) {} |
| 604 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 605 | virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec) SK_OVERRIDE { |
| 606 | if (this->INHERITED::filterPath(dst, src, rec)) { |
| 607 | rec->setStrokeStyle(fWidth); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 608 | return true; |
| 609 | } |
| 610 | return false; |
| 611 | } |
| 612 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 613 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Line2DPathEffect) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 614 | protected: |
| 615 | virtual void nextSpan(int u, int v, int ucount, SkPath* dst) |
| 616 | { |
| 617 | if (ucount > 1) |
| 618 | { |
| 619 | SkPoint src[2], dstP[2]; |
| 620 | |
| 621 | src[0].set(SkIntToScalar(u) + SK_ScalarHalf, |
| 622 | SkIntToScalar(v) + SK_ScalarHalf); |
| 623 | src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, |
| 624 | SkIntToScalar(v) + SK_ScalarHalf); |
| 625 | this->getMatrix().mapPoints(dstP, src, 2); |
| 626 | |
| 627 | dst->moveTo(dstP[0]); |
| 628 | dst->lineTo(dstP[1]); |
| 629 | } |
| 630 | } |
| 631 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 632 | Line2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 633 | { |
| 634 | fWidth = buffer.readScalar(); |
| 635 | } |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 636 | virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE |
| 637 | { |
| 638 | this->INHERITED::flatten(buffer); |
| 639 | buffer.writeScalar(fWidth); |
| 640 | } |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 641 | |
| 642 | private: |
| 643 | SkScalar fWidth; |
| 644 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 645 | typedef Sk2DPathEffect INHERITED; |
| 646 | }; |
| 647 | |
| 648 | static void r9(SkLayerRasterizer* rast, SkPaint& p) |
| 649 | { |
| 650 | rast->addLayer(p); |
| 651 | |
| 652 | SkMatrix lattice; |
| 653 | lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); |
| 654 | lattice.postRotate(SkIntToScalar(30), 0, 0); |
| 655 | p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref(); |
| 656 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 657 | rast->addLayer(p); |
| 658 | |
| 659 | p.setPathEffect(NULL); |
| 660 | p.setXfermode(NULL); |
| 661 | p.setStyle(SkPaint::kStroke_Style); |
| 662 | p.setStrokeWidth(SK_Scalar1); |
| 663 | rast->addLayer(p); |
| 664 | } |
| 665 | |
| 666 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 667 | |
| 668 | static const raster_proc gRastProcs[] = { |
| 669 | r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 |
| 670 | }; |
| 671 | |
| 672 | static void apply_shader(SkPaint* paint, int index) { |
| 673 | raster_proc proc = gRastProcs[index]; |
| 674 | SkPaint p; |
| 675 | SkLayerRasterizer* rast = new SkLayerRasterizer; |
| 676 | |
| 677 | p.setAntiAlias(true); |
| 678 | proc(rast, p); |
| 679 | paint->setRasterizer(rast)->unref(); |
| 680 | paint->setColor(SK_ColorBLUE); |
| 681 | } |
| 682 | |
| 683 | #include "SkTypeface.h" |
| 684 | |
| 685 | static void texteffect_slide(SkCanvas* canvas) { |
| 686 | const char* str = "Google"; |
| 687 | size_t len = strlen(str); |
| 688 | SkScalar x = 20; |
| 689 | SkScalar y = 80; |
| 690 | SkPaint paint; |
| 691 | paint.setTypeface(SkTypeface::CreateFromName("Georgia", SkTypeface::kItalic)); |
| 692 | paint.setTextSize(75); |
| 693 | paint.setAntiAlias(true); |
| 694 | paint.setColor(SK_ColorBLUE); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 695 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) { |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 696 | apply_shader(&paint, i); |
| 697 | canvas->drawText(str, len, x, y, paint); |
| 698 | y += 80; |
| 699 | if (i == 4) { |
| 700 | x += 320; |
| 701 | y = 80; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | /////////////////////////////////////////////////////////////////////////////// |
| 707 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 708 | #include "SkImageEncoder.h" |
| 709 | |
| 710 | static const SlideProc gProc[] = { |
| 711 | patheffect_slide, |
| 712 | gradient_slide, |
| 713 | textonpath_slide, |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 714 | mesh_slide, |
| 715 | texteffect_slide |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 716 | }; |
| 717 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 718 | class SlideView : public SampleView { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 719 | int fIndex; |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 720 | bool fOnce; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 721 | public: |
| 722 | SlideView() { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 723 | fOnce = false; |
| 724 | } |
| 725 | |
| 726 | void init() { |
| 727 | if (fOnce) { |
| 728 | return; |
| 729 | } |
| 730 | fOnce = true; |
| 731 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 732 | fIndex = 0; |
| 733 | |
| 734 | SkBitmap bm; |
| 735 | bm.setConfig(SkBitmap::kARGB_8888_Config, 1024, 768); |
| 736 | bm.allocPixels(); |
| 737 | SkCanvas canvas(bm); |
| 738 | SkScalar s = SkIntToScalar(1024) / 640; |
| 739 | canvas.scale(s, s); |
| 740 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) { |
| 741 | canvas.save(); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 742 | canvas.drawColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 743 | gProc[i](&canvas); |
| 744 | canvas.restore(); |
| 745 | SkString str; |
| 746 | str.printf("/skimages/slide_%d.png", i); |
| 747 | SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100); |
| 748 | } |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 749 | this->setBGColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | protected: |
| 753 | // overrides from SkEventSink |
| 754 | virtual bool onQuery(SkEvent* evt) { |
| 755 | if (SampleCode::TitleQ(*evt)) { |
| 756 | SampleCode::TitleR(evt, "Slides"); |
| 757 | return true; |
| 758 | } |
| 759 | return this->INHERITED::onQuery(evt); |
| 760 | } |
| 761 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 762 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 763 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 764 | gProc[fIndex](canvas); |
| 765 | } |
| 766 | |
| 767 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 768 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 769 | fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc); |
| 770 | this->inval(NULL); |
| 771 | return NULL; |
| 772 | } |
| 773 | |
| 774 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 775 | typedef SampleView INHERITED; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 776 | }; |
| 777 | |
| 778 | ////////////////////////////////////////////////////////////////////////////// |
| 779 | |
| 780 | static SkView* MyFactory() { return new SlideView; } |
| 781 | static SkViewRegister reg(MyFactory); |
| 782 | |