commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| 8 | #include "SampleCode.h" |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 9 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkCornerPathEffect.h" |
| 12 | #include "SkDashPathEffect.h" |
| 13 | #include "SkPathUtils.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkView.h" |
| 16 | |
| 17 | typedef void (*BitsToPath)(SkPath*, const char*, int, int, int); |
| 18 | |
| 19 | static const BitsToPath gBitsToPath_fns[] = { |
| 20 | SkPathUtils::BitsToPath_Path, |
| 21 | SkPathUtils::BitsToPath_Region, |
| 22 | }; |
| 23 | |
| 24 | // hardcoded bitmap patterns |
| 25 | static const uint8_t gBits[][16] = { |
| 26 | { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00, |
| 27 | 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 }, |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 28 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 29 | { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, |
| 30 | 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 }, |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 32 | { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70, |
| 33 | 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 } |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | class SamplePathUtils : public SampleView { |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 38 | public: |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 39 | static const int fNumBits = 3; |
| 40 | static const int fH = 8, fW = 12; |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 41 | static const size_t fRowBytes = 2; |
| 42 | static const int fNumChars = fH * fRowBytes; |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 43 | |
| 44 | SkPaint fBmpPaint; |
| 45 | SkScalar fPhase; |
| 46 | |
| 47 | SamplePathUtils() { |
| 48 | fBmpPaint.setAntiAlias(true); // Black paint for bitmap |
| 49 | fBmpPaint.setStyle(SkPaint::kFill_Style); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 50 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 51 | fPhase = 0.0f; // to animate the dashed path |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | protected: |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 55 | // overrides from SkEventSink |
| 56 | virtual bool onQuery(SkEvent* evt) { |
| 57 | if (SampleCode::TitleQ(*evt)) { |
| 58 | SampleCode::TitleR(evt, "PathUtils"); |
| 59 | return true; |
| 60 | } |
| 61 | return this->INHERITED::onQuery(evt); |
| 62 | } |
| 63 | |
| 64 | ///////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 65 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 66 | virtual void onDrawContent(SkCanvas* canvas) { |
robertphillips@google.com | 64527e9 | 2013-07-09 16:57:01 +0000 | [diff] [blame] | 67 | SkScalar intervals[8] = { .5f, .3f, .5f, .3f, .5f, .3f, .5f, .3f }; |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 68 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, fPhase)); |
| 69 | SkAutoTUnref<SkCornerPathEffect> corner(SkCornerPathEffect::Create(.25f)); |
| 70 | SkAutoTUnref<SkComposePathEffect> compose(SkComposePathEffect::Create(dash, corner)); |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 72 | SkPaint outlinePaint; |
| 73 | outlinePaint.setAntiAlias(true); // dashed paint for bitmap |
| 74 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 75 | outlinePaint.setPathEffect(compose); |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 77 | canvas->scale(10.0f, 10.0f); // scales up |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 78 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 79 | for (int i = 0; i < fNumBits; ++i) { |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 80 | canvas->save(); |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 81 | for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) { |
| 82 | SkPath path; |
| 83 | gBitsToPath_fns[j](&path, (char*) &gBits[i], fW, fH, fRowBytes); |
robertphillips@google.com | 95faedb | 2013-07-01 23:15:57 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 85 | //draw skPath and outline |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 86 | canvas->drawPath(path, fBmpPaint); |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 87 | canvas->translate(1.5f * fW, 0); // translates past previous bitmap |
| 88 | canvas->drawPath(path, outlinePaint); |
| 89 | canvas->translate(1.5f * fW, 0); // translates past previous bitmap |
| 90 | } |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 91 | canvas->restore(); |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 92 | canvas->translate(0, 1.5f * fH); //translate to next row |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 93 | } |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 94 | |
| 95 | // for animated pathEffect |
robertphillips@google.com | 64527e9 | 2013-07-09 16:57:01 +0000 | [diff] [blame] | 96 | fPhase += .01f; |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 97 | this->inval(NULL); |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 98 | } |
skia.committer@gmail.com | 0d55dd7 | 2013-07-02 07:00:59 +0000 | [diff] [blame] | 99 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 100 | private: |
| 101 | typedef SkView INHERITED; |
| 102 | }; |
| 103 | |
| 104 | ////////////////////////////////////////////////////////////////////////////// |
| 105 | |
commit-bot@chromium.org | d43f644 | 2013-07-09 16:30:38 +0000 | [diff] [blame] | 106 | static SkView* MyFactory() { return new SamplePathUtils; } |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 107 | static SkViewRegister reg(MyFactory) |
| 108 | ; |