OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 | |
9 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkCornerPathEffect.h" | |
12 #include "SkDashPathEffect.h" | |
10 #include "SkPathUtils.h" | 13 #include "SkPathUtils.h" |
14 #include "SkRandom.h" | |
11 #include "SkView.h" | 15 #include "SkView.h" |
12 //#include "SkPathOps.h" // loads fine here, won't in PathUtils src files | |
13 #include "SkRandom.h" | |
14 //#include "SkTime.h" | |
15 | 16 |
16 class samplePathUtils : public SampleView { | 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 }, | |
28 | |
29 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, | |
30 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 }, | |
31 | |
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 { | |
17 public: | 38 public: |
18 samplePathUtils() { | 39 static const int fNumBits = 3; |
19 bmp_paint.setAntiAlias(true); // Black paint for bitmap | 40 static const int fH = 8, fW = 12; |
20 bmp_paint.setStyle(SkPaint::kFill_Style); | 41 static const size_t fRowBytes = 2; |
21 bmp_paint.setColor(SK_ColorBLACK); | 42 static const int fNumChars = fH * fRowBytes; |
43 | |
44 SkPaint fBmpPaint; | |
45 SkScalar fPhase; | |
46 | |
47 SamplePathUtils() { | |
48 fBmpPaint.setAntiAlias(true); // Black paint for bitmap | |
49 fBmpPaint.setStyle(SkPaint::kFill_Style); | |
50 | |
51 | |
52 fPhase = 0.0f; | |
22 } | 53 } |
23 | 54 |
24 protected: | 55 protected: |
25 static const int numModes = 3; | |
26 static const int h=8, w=12, stride=2; // stride is in bytes | |
27 static const int numChars = h * stride; // number of chars in entire array | |
28 | |
29 SkPaint bmp_paint; | |
30 | |
31 // overrides from SkEventSink | 56 // overrides from SkEventSink |
32 virtual bool onQuery(SkEvent* evt) { | 57 virtual bool onQuery(SkEvent* evt) { |
33 if (SampleCode::TitleQ(*evt)) { | 58 if (SampleCode::TitleQ(*evt)) { |
34 SampleCode::TitleR(evt, "PathUtils"); | 59 SampleCode::TitleR(evt, "PathUtils"); |
35 return true; | 60 return true; |
36 } | 61 } |
37 return this->INHERITED::onQuery(evt); | 62 return this->INHERITED::onQuery(evt); |
38 } | 63 } |
39 | 64 |
40 ///////////////////////////////////////////////////////////// | 65 ///////////////////////////////////////////////////////////// |
41 | 66 |
42 virtual void onDrawContent(SkCanvas* canvas) { | 67 virtual void onDrawContent(SkCanvas* canvas) { |
43 // bitmap definitions | 68 // creating animated path effect composition (avoiding memory leaks) |
44 const uint8_t bits[numModes][numChars] = { | 69 SkScalar intervals[8] = {.5, .3, .5, .3, .5, .3, .5, .3}; |
45 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00, | 70 SkPaint OutlinePaint; |
46 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 }, | 71 OutlinePaint.setAntiAlias(true); // Black paint for bitmap |
72 OutlinePaint.setStyle(SkPaint::kStroke_Style); | |
73 OutlinePaint.setPathEffect(new SkComposePathEffect( | |
74 new SkDashPathEffect(intervals, 2, fPhase ), | |
75 new SkCornerPathEffect(.25f) | |
djsollen
2013/07/09 14:12:36
I'm pretty sure this will leak the dash and corner
dierk
2013/07/09 15:11:27
Done.
| |
76 ) )->unref(); | |
47 | 77 |
48 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, | 78 canvas->scale(10.0f, 10.0f); // scales up |
49 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 }, | |
50 | 79 |
51 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70, | 80 for (int i = 0; i < fNumBits; ++i) { |
52 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 } | 81 canvas->save(); |
53 }; | 82 for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) { |
83 SkPath path; | |
84 gBitsToPath_fns[j](&path, (char*) &gBits[i], fW, fH, fRowBytes); | |
54 | 85 |
55 static const SkScalar kScale = 10.0f; | 86 //draw skPath and outline |
87 canvas->drawPath(path, fBmpPaint); | |
88 canvas->translate(1.5f * fW, 0); // translates past previous bit map | |
89 canvas->drawPath(path, OutlinePaint); | |
90 canvas->translate(1.5f * fW, 0); // translates past previous bit map | |
91 } | |
92 canvas->restore(); | |
93 canvas->translate(0, 1.5f * fH); //translate to next row | |
94 } | |
56 | 95 |
57 for (int i = 0; i < numModes; ++i) { | 96 // for animated pathEffect |
58 SkPath path; // generate and simplify each path | 97 fPhase += .01; |
59 SkPathUtils::BitsToPath_Path(&path, (char*) &bits[i], h, w, stride); | 98 this->inval(NULL); |
60 | |
61 canvas->save(); // DRAWING | |
62 canvas->scale(kScale, kScale); // scales up each bitmap | |
63 canvas->translate(0, 1.5f * h * i); | |
64 canvas->drawPath(path, bmp_paint); // draw bitmap | |
65 canvas->restore(); | |
66 | |
67 // use the SkRegion method | |
68 SkPath pathR; | |
69 SkPathUtils::BitsToPath_Region(&pathR, (char*) &bits[i], h, w, strid e); | |
70 | |
71 canvas->save(); | |
72 | |
73 canvas->scale(kScale, kScale); // scales up each bitmap | |
74 canvas->translate(1.5f * w, 1.5f * h * i); // translates past previo us bitmap | |
75 canvas->drawPath(pathR, bmp_paint); // draw bitmap | |
76 canvas->restore(); | |
77 } | |
78 } | 99 } |
79 | 100 |
80 private: | 101 private: |
81 typedef SkView INHERITED; | 102 typedef SkView INHERITED; |
82 }; | 103 }; |
83 | 104 |
84 ////////////////////////////////////////////////////////////////////////////// | 105 ////////////////////////////////////////////////////////////////////////////// |
85 | 106 |
86 static SkView* MyFactory() { return new samplePathUtils; } | 107 static SkView* MyFactory() { return new SamplePathUtils; } |
87 static SkViewRegister reg(MyFactory) | 108 static SkViewRegister reg(MyFactory) |
88 ; | 109 ; |
OLD | NEW |