Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: samplecode/SamplePathUtils.cpp

Issue 18552005: Animated the PathUtils Sample to show path contours (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Added function pointers to sample to reduce code in OnDrawContent method. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« gm/nate_gradients.cpp ('K') | « gyp/gmslides.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16
13 #include "SkRandom.h" 17 typedef void (*BitsToPath)(SkPath*, const char*, int, int, int);
14 //#include "SkTime.h" 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
15 36
16 class samplePathUtils : public SampleView { 37 class samplePathUtils : public SampleView {
17 public: 38 public:
18 samplePathUtils() { 39 samplePathUtils() {
19 bmp_paint.setAntiAlias(true); // Black paint for bitmap 40 bmp_paint.setAntiAlias(true); // Black paint for bitmap
20 bmp_paint.setStyle(SkPaint::kFill_Style); 41 bmp_paint.setStyle(SkPaint::kFill_Style);
21 bmp_paint.setColor(SK_ColorBLACK); 42
43 outline_paint.setAntiAlias(true); // Black paint for bitmap
44 outline_paint.setStyle(SkPaint::kStroke_Style);
45
46 gPhase = 0.0f;
22 } 47 }
23 48
24 protected: 49 protected:
scroggo 2013/07/08 19:11:31 nit: Need the member variables be protected?
25 static const int numModes = 3; 50 static const int numBits = 3;
26 static const int h=8, w=12, stride=2; // stride is in bytes 51 static const int h=8, w=12, stride=2; // stride is in bytes
scroggo 2013/07/08 19:11:31 nit: Typically byte counts are represented as size
27 static const int numChars = h * stride; // number of chars in entire array 52 static const int numChars = h * stride; // number of chars in entire array
28 53
29 SkPaint bmp_paint; 54 SkPaint bmp_paint;
scroggo 2013/07/08 19:11:31 nit: In Skia we name member variables in camelCase
30 55 SkPaint outline_paint;
scroggo 2013/07/08 19:11:31 nit: fOutlinePaint
56 SkScalar gPhase;
scroggo 2013/07/08 19:11:31 nit: Unless this is global, it should be fPhase.
57
31 // overrides from SkEventSink 58 // overrides from SkEventSink
32 virtual bool onQuery(SkEvent* evt) { 59 virtual bool onQuery(SkEvent* evt) {
33 if (SampleCode::TitleQ(*evt)) { 60 if (SampleCode::TitleQ(*evt)) {
34 SampleCode::TitleR(evt, "PathUtils"); 61 SampleCode::TitleR(evt, "PathUtils");
35 return true; 62 return true;
36 } 63 }
37 return this->INHERITED::onQuery(evt); 64 return this->INHERITED::onQuery(evt);
38 } 65 }
39 66
40 ///////////////////////////////////////////////////////////// 67 /////////////////////////////////////////////////////////////
41 68
42 virtual void onDrawContent(SkCanvas* canvas) { 69 virtual void onDrawContent(SkCanvas* canvas) {
scroggo 2013/07/08 19:11:31 I love SamplePathUtils's images :)
43 // bitmap definitions 70 // creating animated path effect composition (avoiding memory leaks)
44 const uint8_t bits[numModes][numChars] = { 71 SkScalar intervals[8] = {.5, .3, .5, .3, .5, .3, .5, .3};
45 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00, 72 SkAutoTUnref<SkDashPathEffect> dash(new SkDashPathEffect(intervals, 2, g Phase) );
46 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 }, 73 SkAutoTUnref<SkCornerPathEffect> corner(new SkCornerPathEffect(.25f) );
74 outline_paint.setPathEffect(new SkComposePathEffect(dash, corner) )->unr ef();
47 75
48 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0, 76 canvas->scale(10.0f, 10.0f); // scales up each bitmap
49 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 }, 77 for (int i = 0; i < numBits; ++i) {
50 78
51 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70, 79 canvas->save();
52 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 } 80 for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) {
53 }; 81 SkPath path;
82 gBitsToPath_fns[j](&path, (char*) &gBits[i], h, w, stride);
scroggo 2013/07/08 19:11:31 Most of our API supplies width as a parameter befo
54 83
55 static const SkScalar kScale = 10.0f; 84 //draw skPath and outline
85 canvas->drawPath(path, bmp_paint);
86 canvas->translate(1.5f * w, 0); // translates past previous bitm ap
87 canvas->drawPath(path, outline_paint);
88 canvas->translate(1.5f * w, 0); // translates past previous bitm ap
89 }
90 canvas->restore();
91 canvas->translate(0, 1.5f * h); //translate to next row
92 }
56 93
57 for (int i = 0; i < numModes; ++i) { 94 // for animated pathEffect
58 SkPath path; // generate and simplify each path 95 gPhase += .01;
59 SkPathUtils::BitsToPath_Path(&path, (char*) &bits[i], h, w, stride); 96 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 } 97 }
79 98
80 private: 99 private:
81 typedef SkView INHERITED; 100 typedef SkView INHERITED;
82 }; 101 };
83 102
84 ////////////////////////////////////////////////////////////////////////////// 103 //////////////////////////////////////////////////////////////////////////////
85 104
86 static SkView* MyFactory() { return new samplePathUtils; } 105 static SkView* MyFactory() { return new samplePathUtils; }
87 static SkViewRegister reg(MyFactory) 106 static SkViewRegister reg(MyFactory)
88 ; 107 ;
OLDNEW
« gm/nate_gradients.cpp ('K') | « gyp/gmslides.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698