1/* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7#include "SampleCode.h" 8#include "SkBlurMask.h" 9#include "SkBlurMaskFilter.h" 10#include "SkCanvas.h" 11#include "SkReadBuffer.h" 12#include "SkWriteBuffer.h" 13#include "SkGradientShader.h" 14#include "SkPaint.h" 15#include "SkVertices.h" 16#include "SkView.h" 17 18#include "sk_tool_utils.h" 19 20#define BG_COLOR 0xFFDDDDDD 21 22typedef void (*SlideProc)(SkCanvas*); 23 24/////////////////////////////////////////////////////////////////////////////// 25 26#include "Sk1DPathEffect.h" 27#include "Sk2DPathEffect.h" 28#include "SkCornerPathEffect.h" 29#include "SkDashPathEffect.h" 30#include "SkDiscretePathEffect.h" 31 32static void compose_pe(SkPaint* paint) { 33 SkPathEffect* pe = paint->getPathEffect(); 34 sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25); 35 sk_sp<SkPathEffect> compose; 36 if (pe) { 37 compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner); 38 } else { 39 compose = corner; 40 } 41 paint->setPathEffect(compose); 42} 43 44static void hair_pe(SkPaint* paint) { 45 paint->setStrokeWidth(0); 46} 47 48static void hair2_pe(SkPaint* paint) { 49 paint->setStrokeWidth(0); 50 compose_pe(paint); 51} 52 53static void stroke_pe(SkPaint* paint) { 54 paint->setStrokeWidth(12); 55 compose_pe(paint); 56} 57 58static void dash_pe(SkPaint* paint) { 59 SkScalar inter[] = { 20, 10, 10, 10 }; 60 paint->setStrokeWidth(12); 61 paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0)); 62 compose_pe(paint); 63} 64 65static const int gXY[] = { 664, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 67}; 68 69static void scale(SkPath* path, SkScalar scale) { 70 SkMatrix m; 71 m.setScale(scale, scale); 72 path->transform(m); 73} 74 75static void one_d_pe(SkPaint* paint) { 76 SkPath path; 77 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); 78 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) 79 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); 80 path.close(); 81 path.offset(SkIntToScalar(-6), 0); 82 scale(&path, 1.5f); 83 84 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0, 85 SkPath1DPathEffect::kRotate_Style)); 86 compose_pe(paint); 87} 88 89typedef void (*PE_Proc)(SkPaint*); 90static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe }; 91 92static void fill_pe(SkPaint* paint) { 93 paint->setStyle(SkPaint::kFill_Style); 94 paint->setPathEffect(nullptr); 95} 96 97static void discrete_pe(SkPaint* paint) { 98 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4)); 99} 100 101static sk_sp<SkPathEffect> MakeTileEffect() { 102 SkMatrix m; 103 m.setScale(SkIntToScalar(12), SkIntToScalar(12)); 104 105 SkPath path; 106 path.addCircle(0, 0, SkIntToScalar(5)); 107 108 return SkPath2DPathEffect::Make(m, path); 109} 110 111static void tile_pe(SkPaint* paint) { 112 paint->setPathEffect(MakeTileEffect()); 113} 114 115static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe }; 116 117static void patheffect_slide(SkCanvas* canvas) { 118 SkPaint paint; 119 paint.setAntiAlias(true); 120 paint.setStyle(SkPaint::kStroke_Style); 121 122 SkPath path; 123 path.moveTo(20, 20); 124 path.lineTo(70, 120); 125 path.lineTo(120, 30); 126 path.lineTo(170, 80); 127 path.lineTo(240, 50); 128 129 size_t i; 130 canvas->save(); 131 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) { 132 gPE[i](&paint); 133 canvas->drawPath(path, paint); 134 canvas->translate(0, 75); 135 } 136 canvas->restore(); 137 138 path.reset(); 139 SkRect r = { 0, 0, 250, 120 }; 140 path.addOval(r, SkPath::kCW_Direction); 141 r.inset(50, 50); 142 path.addRect(r, SkPath::kCCW_Direction); 143 144 canvas->translate(320, 20); 145 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) { 146 gPE2[i](&paint); 147 canvas->drawPath(path, paint); 148 canvas->translate(0, 160); 149 } 150} 151 152/////////////////////////////////////////////////////////////////////////////// 153 154#include "SkGradientShader.h" 155 156struct GradData { 157 int fCount; 158 const SkColor* fColors; 159 const SkScalar* fPos; 160}; 161 162static const SkColor gColors[] = { 163SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK 164}; 165static const SkScalar gPos0[] = { 0, SK_Scalar1 }; 166static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; 167static const SkScalar gPos2[] = { 1680, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 169}; 170 171static const GradData gGradData[] = { 172{ 2, gColors, nullptr }, 173{ 2, gColors, gPos0 }, 174{ 2, gColors, gPos1 }, 175{ 5, gColors, nullptr }, 176{ 5, gColors, gPos2 } 177}; 178 179static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { 180 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm); 181} 182 183static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { 184 SkPoint center; 185 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 186 SkScalarAve(pts[0].fY, pts[1].fY)); 187 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, 188 data.fPos, data.fCount, tm); 189} 190 191static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { 192 SkPoint center; 193 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 194 SkScalarAve(pts[0].fY, pts[1].fY)); 195 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); 196} 197 198static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { 199 SkPoint center0, center1; 200 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), 201 SkScalarAve(pts[0].fY, pts[1].fY)); 202 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), 203 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); 204 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, 205 center0, (pts[1].fX - pts[0].fX) / 2, 206 data.fColors, data.fPos, data.fCount, tm); 207} 208 209typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode); 210static const GradMaker gGradMakers[] = { 211 MakeLinear, MakeRadial, MakeSweep, Make2Conical 212}; 213 214static void gradient_slide(SkCanvas* canvas) { 215 SkPoint pts[2] = { 216 { 0, 0 }, 217 { SkIntToScalar(100), SkIntToScalar(100) } 218 }; 219 SkShader::TileMode tm = SkShader::kClamp_TileMode; 220 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; 221 SkPaint paint; 222 paint.setAntiAlias(true); 223 paint.setDither(true); 224 225 canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); 226 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { 227 canvas->save(); 228 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { 229 paint.setShader(gGradMakers[j](pts, gGradData[i], tm)); 230 canvas->drawRect(r, paint); 231 canvas->translate(0, SkIntToScalar(120)); 232 } 233 canvas->restore(); 234 canvas->translate(SkIntToScalar(120), 0); 235 } 236} 237 238/////////////////////////////////////////////////////////////////////////////// 239 240#include "SkPathMeasure.h" 241 242static SkScalar getpathlen(const SkPath& path) { 243 SkPathMeasure meas(path, false); 244 return meas.getLength(); 245} 246 247static void textonpath_slide(SkCanvas* canvas) { 248 const char* text = "Displacement"; 249 size_t len =strlen(text); 250 SkPath path; 251 path.moveTo(100, 300); 252 path.quadTo(300, 100, 500, 300); 253 path.offset(0, -100); 254 255 SkPaint paint; 256 paint.setAntiAlias(true); 257 paint.setTextSize(40); 258 259 paint.setStyle(SkPaint::kStroke_Style); 260 canvas->drawPath(path, paint); 261 paint.setStyle(SkPaint::kFill_Style); 262 263 SkScalar x = 50; 264 paint.setColor(0xFF008800); 265 canvas->drawTextOnPathHV(text, len, path, 266 x, paint.getTextSize()*2/3, paint); 267 paint.setColor(SK_ColorRED); 268 canvas->drawTextOnPathHV(text, len, path, 269 x + 60, 0, paint); 270 paint.setColor(SK_ColorBLUE); 271 canvas->drawTextOnPathHV(text, len, path, 272 x + 120, -paint.getTextSize()*2/3, paint); 273 274 path.offset(0, 200); 275 paint.setTextAlign(SkPaint::kRight_Align); 276 277 text = "Matrices"; 278 len = strlen(text); 279 SkScalar pathLen = getpathlen(path); 280 SkMatrix matrix; 281 282 paint.setColor(SK_ColorBLACK); 283 paint.setStyle(SkPaint::kStroke_Style); 284 canvas->drawPath(path, paint); 285 paint.setStyle(SkPaint::kFill_Style); 286 287 paint.setTextSize(50); 288 canvas->drawTextOnPath(text, len, path, nullptr, paint); 289 290 paint.setColor(SK_ColorRED); 291 matrix.setScale(-SK_Scalar1, SK_Scalar1); 292 matrix.postTranslate(pathLen, 0); 293 canvas->drawTextOnPath(text, len, path, &matrix, paint); 294 295 paint.setColor(SK_ColorBLUE); 296 matrix.setScale(SK_Scalar1, -SK_Scalar1); 297 canvas->drawTextOnPath(text, len, path, &matrix, paint); 298 299 paint.setColor(0xFF008800); 300 matrix.setScale(-SK_Scalar1, -SK_Scalar1); 301 matrix.postTranslate(pathLen, 0); 302 canvas->drawTextOnPath(text, len, path, &matrix, paint); 303} 304 305/////////////////////////////////////////////////////////////////////////////// 306 307#include "DecodeFile.h" 308#include "SkOSFile.h" 309#include "SkRandom.h" 310#include "SkStream.h" 311 312static sk_sp<SkShader> make_shader0(SkIPoint* size) { 313 SkBitmap bm; 314 315 decode_file("/skimages/logo.gif", &bm); 316 size->set(bm.width(), bm.height()); 317 return SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode, 318 SkShader::kClamp_TileMode); 319} 320 321static sk_sp<SkShader> make_shader1(const SkIPoint& size) { 322 SkPoint pts[] = { { 0, 0 }, 323 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; 324 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; 325 return SkGradientShader::MakeLinear(pts, colors, nullptr, 326 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode); 327} 328 329class Rec { 330public: 331 SkVertices::VertexMode fMode; 332 int fCount; 333 SkPoint* fVerts; 334 SkPoint* fTexs; 335 336 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {} 337 ~Rec() { delete[] fVerts; delete[] fTexs; } 338}; 339 340static void make_tris(Rec* rec) { 341 int n = 10; 342 SkRandom rand; 343 344 rec->fMode = SkVertices::kTriangles_VertexMode; 345 rec->fCount = n * 3; 346 rec->fVerts = new SkPoint[rec->fCount]; 347 348 for (int i = 0; i < n; i++) { 349 SkPoint* v = &rec->fVerts[i*3]; 350 for (int j = 0; j < 3; j++) { 351 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); 352 } 353 } 354} 355 356static void make_fan(Rec* rec, int texWidth, int texHeight) { 357 const SkScalar tx = SkIntToScalar(texWidth); 358 const SkScalar ty = SkIntToScalar(texHeight); 359 const int n = 24; 360 361 rec->fMode = SkVertices::kTriangleFan_VertexMode; 362 rec->fCount = n + 2; 363 rec->fVerts = new SkPoint[rec->fCount]; 364 rec->fTexs = new SkPoint[rec->fCount]; 365 366 SkPoint* v = rec->fVerts; 367 SkPoint* t = rec->fTexs; 368 369 v[0].set(0, 0); 370 t[0].set(0, 0); 371 for (int i = 0; i < n; i++) { 372 SkScalar cos; 373 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); 374 v[i+1].set(cos, sin); 375 t[i+1].set(i*tx/n, ty); 376 } 377 v[n+1] = v[1]; 378 t[n+1].set(tx, ty); 379 380 SkMatrix m; 381 m.setScale(SkIntToScalar(100), SkIntToScalar(100)); 382 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); 383 m.mapPoints(v, rec->fCount); 384} 385 386static void make_strip(Rec* rec, int texWidth, int texHeight) { 387 const SkScalar tx = SkIntToScalar(texWidth); 388 const SkScalar ty = SkIntToScalar(texHeight); 389 const int n = 24; 390 391 rec->fMode = SkVertices::kTriangleStrip_VertexMode; 392 rec->fCount = 2 * (n + 1); 393 rec->fVerts = new SkPoint[rec->fCount]; 394 rec->fTexs = new SkPoint[rec->fCount]; 395 396 SkPoint* v = rec->fVerts; 397 SkPoint* t = rec->fTexs; 398 399 for (int i = 0; i < n; i++) { 400 SkScalar cos; 401 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); 402 v[i*2 + 0].set(cos/2, sin/2); 403 v[i*2 + 1].set(cos, sin); 404 405 t[i*2 + 0].set(tx * i / n, ty); 406 t[i*2 + 1].set(tx * i / n, 0); 407 } 408 v[2*n + 0] = v[0]; 409 v[2*n + 1] = v[1]; 410 411 t[2*n + 0].set(tx, ty); 412 t[2*n + 1].set(tx, 0); 413 414 SkMatrix m; 415 m.setScale(SkIntToScalar(100), SkIntToScalar(100)); 416 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); 417 m.mapPoints(v, rec->fCount); 418} 419 420static void mesh_slide(SkCanvas* canvas) { 421 Rec fRecs[3]; 422 SkIPoint size; 423 424 auto fShader0 = make_shader0(&size); 425 auto fShader1 = make_shader1(size); 426 427 make_strip(&fRecs[0], size.fX, size.fY); 428 make_fan(&fRecs[1], size.fX, size.fY); 429 make_tris(&fRecs[2]); 430 431 SkPaint paint; 432 paint.setDither(true); 433 paint.setFilterQuality(kLow_SkFilterQuality); 434 435 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { 436 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount, 437 fRecs[i].fVerts, fRecs[i].fTexs, nullptr); 438 canvas->save(); 439 440 paint.setShader(nullptr); 441 canvas->drawVertices(verts, SkBlendMode::kModulate, paint); 442 443 canvas->translate(SkIntToScalar(210), 0); 444 445 paint.setShader(fShader0); 446 canvas->drawVertices(verts, SkBlendMode::kModulate, paint); 447 448 canvas->translate(SkIntToScalar(210), 0); 449 450 paint.setShader(fShader1); 451 canvas->drawVertices(verts, SkBlendMode::kModulate, paint); 452 canvas->restore(); 453 454 canvas->translate(0, SkIntToScalar(250)); 455 } 456} 457 458/////////////////////////////////////////////////////////////////////////////// 459 460#include "SkTypeface.h" 461 462/////////////////////////////////////////////////////////////////////////////// 463 464#include "SkImageEncoder.h" 465 466static const SlideProc gProc[] = { 467 patheffect_slide, 468 gradient_slide, 469 textonpath_slide, 470 mesh_slide, 471}; 472 473class SlideView : public SampleView { 474 int fIndex; 475 bool fOnce; 476public: 477 SlideView() { 478 fOnce = false; 479 } 480 481 void init() { 482 if (fOnce) { 483 return; 484 } 485 fOnce = true; 486 487 fIndex = 0; 488 489 SkBitmap bm; 490 bm.allocN32Pixels(1024, 768); 491 SkCanvas canvas(bm); 492 SkScalar s = SkIntToScalar(1024) / 640; 493 canvas.scale(s, s); 494 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) { 495 canvas.save(); 496 canvas.drawColor(BG_COLOR); 497 gProc[i](&canvas); 498 canvas.restore(); 499 SkString str; 500 str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i); 501 sk_tool_utils::EncodeImageToFile(str.c_str(), bm, SkEncodedImageFormat::kPNG, 100); 502 } 503 this->setBGColor(BG_COLOR); 504 } 505 506protected: 507 // overrides from SkEventSink 508 bool onQuery(SkEvent* evt) override { 509 if (SampleCode::TitleQ(*evt)) { 510 SampleCode::TitleR(evt, "Slides"); 511 return true; 512 } 513 return this->INHERITED::onQuery(evt); 514 } 515 516 void onDrawContent(SkCanvas* canvas) override { 517 this->init(); 518 gProc[fIndex](canvas); 519 } 520 521 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { 522 this->init(); 523 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc); 524 return nullptr; 525 } 526 527private: 528 typedef SampleView INHERITED; 529}; 530 531////////////////////////////////////////////////////////////////////////////// 532 533static SkView* MyFactory() { return new SlideView; } 534static SkViewRegister reg(MyFactory); 535