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

Side by Side Diff: samplecode/SampleArc.cpp

Issue 732653004: option to return drawable from recording (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years 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
« no previous file with comments | « include/core/SkPictureRecorder.h ('k') | src/core/SkCanvasDrawable.h » ('j') | 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 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkView.h" 9 #include "SkView.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkGradientShader.h" 11 #include "SkGradientShader.h"
12 #include "SkPath.h" 12 #include "SkPath.h"
13 #include "SkRegion.h" 13 #include "SkRegion.h"
14 #include "SkShader.h" 14 #include "SkShader.h"
15 #include "SkUtils.h" 15 #include "SkUtils.h"
16 #include "SkComposeShader.h" 16 #include "SkComposeShader.h"
17 #include "Sk1DPathEffect.h" 17 #include "Sk1DPathEffect.h"
18 #include "SkCornerPathEffect.h" 18 #include "SkCornerPathEffect.h"
19 #include "SkPathMeasure.h" 19 #include "SkPathMeasure.h"
20 #include "SkPictureRecorder.h"
20 #include "SkRandom.h" 21 #include "SkRandom.h"
21 #include "SkColorPriv.h" 22 #include "SkColorPriv.h"
22 #include "SkColorFilter.h" 23 #include "SkColorFilter.h"
23 #include "SkLayerRasterizer.h" 24 #include "SkLayerRasterizer.h"
24 25
25 #include "SkCanvasDrawable.h" 26 #include "SkCanvasDrawable.h"
26 27
27 #include "SkParsePath.h" 28 #include "SkParsePath.h"
28 static void testparse() { 29 static void testparse() {
29 SkRect r; 30 SkRect r;
30 r.set(0, 0, 10, 10.5f); 31 r.set(0, 0, 10, 10.5f);
31 SkPath p, p2; 32 SkPath p, p2;
32 SkString str, str2; 33 SkString str, str2;
33 34
34 p.addRect(r); 35 p.addRect(r);
35 SkParsePath::ToSVGString(p, &str); 36 SkParsePath::ToSVGString(p, &str);
36 SkParsePath::FromSVGString(str.c_str(), &p2); 37 SkParsePath::FromSVGString(str.c_str(), &p2);
37 SkParsePath::ToSVGString(p2, &str2); 38 SkParsePath::ToSVGString(p2, &str2);
38 } 39 }
39 40
40 class ArcsView : public SampleView { 41 class ArcsView : public SampleView {
41 class MyDrawable : public SkCanvasDrawable { 42 class MyDrawable : public SkCanvasDrawable {
42 SkRect fR; 43 SkRect fR;
43 SkScalar fSweep; 44 SkScalar fSweep;
44 public: 45 public:
45 MyDrawable(const SkRect& r) : fR(r), fSweep(0) { 46 MyDrawable(const SkRect& r) : fR(r), fSweep(0) {}
46 }
47 47
48 void setSweep(SkScalar sweep) { 48 void setSweep(SkScalar sweep) {
49 if (fSweep != sweep) { 49 if (fSweep != sweep) {
50 fSweep = sweep; 50 fSweep = sweep;
51 this->notifyDrawingChanged(); 51 this->notifyDrawingChanged();
52 } 52 }
53 } 53 }
54 54
55 void onDraw(SkCanvas* canvas) SK_OVERRIDE { 55 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
56 SkPaint paint; 56 SkPaint paint;
(...skipping 18 matching lines...) Expand all
75 75
76 SkRect onGetBounds() SK_OVERRIDE { 76 SkRect onGetBounds() SK_OVERRIDE {
77 SkRect r(fR); 77 SkRect r(fR);
78 r.outset(2, 2); 78 r.outset(2, 2);
79 return r; 79 return r;
80 } 80 }
81 }; 81 };
82 82
83 public: 83 public:
84 SkRect fRect; 84 SkRect fRect;
85 MyDrawable* fDrawable; 85 MyDrawable* fAnimatingDrawable;
86 SkCanvasDrawable* fRootDrawable;
86 87
87 ArcsView() { 88 ArcsView() {
88 testparse(); 89 testparse();
89 fSweep = SkIntToScalar(100); 90 fSweep = SkIntToScalar(100);
90 this->setBGColor(0xFFDDDDDD); 91 this->setBGColor(0xFFDDDDDD);
91 92
92 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200)); 93 fRect.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
93 fRect.offset(SkIntToScalar(20), SkIntToScalar(20)); 94 fRect.offset(SkIntToScalar(20), SkIntToScalar(20));
94 fDrawable = SkNEW_ARGS(MyDrawable, (fRect)); 95 fAnimatingDrawable = SkNEW_ARGS(MyDrawable, (fRect));
96
97 SkPictureRecorder recorder;
98 this->drawRoot(recorder.beginRecording(SkRect::MakeWH(800, 500)));
99 fRootDrawable = recorder.EXPERIMENTAL_endRecordingAsDrawable();
95 } 100 }
96 101
97 virtual ~ArcsView() SK_OVERRIDE { 102 virtual ~ArcsView() SK_OVERRIDE {
98 fDrawable->unref(); 103 fAnimatingDrawable->unref();
104 fRootDrawable->unref();
99 } 105 }
100 106
101 protected: 107 protected:
102 // overrides from SkEventSink 108 // overrides from SkEventSink
103 virtual bool onQuery(SkEvent* evt) { 109 bool onQuery(SkEvent* evt) SK_OVERRIDE {
104 if (SampleCode::TitleQ(*evt)) { 110 if (SampleCode::TitleQ(*evt)) {
105 SampleCode::TitleR(evt, "Arcs"); 111 SampleCode::TitleR(evt, "Arcs");
106 return true; 112 return true;
107 } 113 }
108 return this->INHERITED::onQuery(evt); 114 return this->INHERITED::onQuery(evt);
109 } 115 }
110 116
111 static void drawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPai nt& p) { 117 static void DrawRectWithLines(SkCanvas* canvas, const SkRect& r, const SkPai nt& p) {
112 canvas->drawRect(r, p); 118 canvas->drawRect(r, p);
113 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p); 119 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, p);
114 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p); 120 canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, p);
115 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p); 121 canvas->drawLine(r.fLeft, r.centerY(), r.fRight, r.centerY(), p);
116 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p); 122 canvas->drawLine(r.centerX(), r.fTop, r.centerX(), r.fBottom, p);
117 } 123 }
118 124
119 static void draw_label(SkCanvas* canvas, const SkRect& rect, 125 static void DrawLabel(SkCanvas* canvas, const SkRect& rect,
120 int start, int sweep) { 126 int start, int sweep) {
121 SkPaint paint; 127 SkPaint paint;
122 128
123 paint.setAntiAlias(true); 129 paint.setAntiAlias(true);
124 paint.setTextAlign(SkPaint::kCenter_Align); 130 paint.setTextAlign(SkPaint::kCenter_Align);
125 131
126 SkString str; 132 SkString str;
127 133
128 str.appendS32(start); 134 str.appendS32(start);
129 str.append(", "); 135 str.append(", ");
130 str.appendS32(sweep); 136 str.appendS32(sweep);
131 canvas->drawText(str.c_str(), str.size(), rect.centerX(), 137 canvas->drawText(str.c_str(), str.size(), rect.centerX(),
132 rect.fBottom + paint.getTextSize() * 5/4, paint); 138 rect.fBottom + paint.getTextSize() * 5/4, paint);
133 } 139 }
134 140
135 static void drawArcs(SkCanvas* canvas) { 141 static void DrawArcs(SkCanvas* canvas) {
136 SkPaint paint; 142 SkPaint paint;
137 SkRect r; 143 SkRect r;
138 SkScalar w = SkIntToScalar(75); 144 SkScalar w = SkIntToScalar(75);
139 SkScalar h = SkIntToScalar(50); 145 SkScalar h = SkIntToScalar(50);
140 146
141 r.set(0, 0, w, h); 147 r.set(0, 0, w, h);
142 paint.setAntiAlias(true); 148 paint.setAntiAlias(true);
143 paint.setStyle(SkPaint::kStroke_Style); 149 paint.setStyle(SkPaint::kStroke_Style);
144 150
145 canvas->save(); 151 canvas->save();
146 canvas->translate(SkIntToScalar(10), SkIntToScalar(300)); 152 canvas->translate(SkIntToScalar(10), SkIntToScalar(300));
147 153
148 paint.setStrokeWidth(SkIntToScalar(1)); 154 paint.setStrokeWidth(SkIntToScalar(1));
149 155
150 static const int gAngles[] = { 156 static const int gAngles[] = {
151 0, 360, 157 0, 360,
152 0, 45, 158 0, 45,
153 0, -45, 159 0, -45,
154 720, 135, 160 720, 135,
155 -90, 269, 161 -90, 269,
156 -90, 270, 162 -90, 270,
157 -90, 271, 163 -90, 271,
158 -180, -270, 164 -180, -270,
159 225, 90 165 225, 90
160 }; 166 };
161 167
162 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) { 168 for (size_t i = 0; i < SK_ARRAY_COUNT(gAngles); i += 2) {
163 paint.setColor(SK_ColorBLACK); 169 paint.setColor(SK_ColorBLACK);
164 drawRectWithLines(canvas, r, paint); 170 DrawRectWithLines(canvas, r, paint);
165 171
166 paint.setColor(SK_ColorRED); 172 paint.setColor(SK_ColorRED);
167 canvas->drawArc(r, SkIntToScalar(gAngles[i]), 173 canvas->drawArc(r, SkIntToScalar(gAngles[i]),
168 SkIntToScalar(gAngles[i+1]), false, paint); 174 SkIntToScalar(gAngles[i+1]), false, paint);
169 175
170 draw_label(canvas, r, gAngles[i], gAngles[i+1]); 176 DrawLabel(canvas, r, gAngles[i], gAngles[i+1]);
171 177
172 canvas->translate(w * 8 / 7, 0); 178 canvas->translate(w * 8 / 7, 0);
173 } 179 }
174 180
175 canvas->restore(); 181 canvas->restore();
176 } 182 }
177 183
178 virtual void onDrawContent(SkCanvas* canvas) { 184 void drawRoot(SkCanvas* canvas) {
179 fDrawable->setSweep(SampleCode::GetAnimScalar(SkIntToScalar(360)/24,
180 SkIntToScalar(360)));
181
182 SkPaint paint; 185 SkPaint paint;
183 paint.setAntiAlias(true); 186 paint.setAntiAlias(true);
184 paint.setStrokeWidth(SkIntToScalar(2)); 187 paint.setStrokeWidth(SkIntToScalar(2));
185 paint.setStyle(SkPaint::kStroke_Style); 188 paint.setStyle(SkPaint::kStroke_Style);
186 189
187 drawRectWithLines(canvas, fRect, paint); 190 DrawRectWithLines(canvas, fRect, paint);
188 191
189 canvas->EXPERIMENTAL_drawDrawable(fDrawable); 192 canvas->EXPERIMENTAL_drawDrawable(fAnimatingDrawable);
190 193
191 drawArcs(canvas); 194 DrawArcs(canvas);
195 }
196
197 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
198 fAnimatingDrawable->setSweep(SampleCode::GetAnimScalar(360/24, 360));
199 canvas->EXPERIMENTAL_drawDrawable(fRootDrawable);
192 this->inval(NULL); 200 this->inval(NULL);
193 } 201 }
194 202
195 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, 203 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_ OVERRIDE {
196 unsigned modi) {
197 // fSweep += SK_Scalar1; 204 // fSweep += SK_Scalar1;
198 this->inval(NULL); 205 this->inval(NULL);
199 return this->INHERITED::onFindClickHandler(x, y, modi); 206 return this->INHERITED::onFindClickHandler(x, y, modi);
200 } 207 }
201 208
202 virtual bool onClick(Click* click) {
203 return this->INHERITED::onClick(click);
204 }
205
206 private: 209 private:
207 SkScalar fSweep; 210 SkScalar fSweep;
208 211
209 typedef SampleView INHERITED; 212 typedef SampleView INHERITED;
210 }; 213 };
211 214
212 ////////////////////////////////////////////////////////////////////////////// 215 //////////////////////////////////////////////////////////////////////////////
213 216
214 static SkView* MyFactory() { return new ArcsView; } 217 static SkView* MyFactory() { return new ArcsView; }
215 static SkViewRegister reg(MyFactory); 218 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « include/core/SkPictureRecorder.h ('k') | src/core/SkCanvasDrawable.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698