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

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: plumb pictlist down to layer hoister 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
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 draw_root(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 virtual bool onQuery(SkEvent* evt) {
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
robertphillips 2014/11/24 15:42:02 DrawRectWithLines ?
reed1 2014/11/24 17:10:48 Done.
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
robertphillips 2014/11/24 15:42:02 DrawLabel ?
reed1 2014/11/24 17:10:48 Done.
119 static void draw_label(SkCanvas* canvas, const SkRect& rect, 125 static void draw_label(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
robertphillips 2014/11/24 15:42:02 DrawArcs ?
reed1 2014/11/24 17:10:48 Done.
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
(...skipping 22 matching lines...) Expand all
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 draw_label(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
robertphillips 2014/11/24 15:42:02 drawRoot due to fRect usage ?
reed1 2014/11/24 17:10:48 Done.
178 virtual void onDrawContent(SkCanvas* canvas) { 184 void draw_root(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
robertphillips 2014/11/24 15:42:02 SK_OVERRIDE ?
reed1 2014/11/24 17:10:48 Done.
197 virtual void onDrawContent(SkCanvas* canvas) {
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 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
196 unsigned modi) { 204 unsigned modi) {
197 // fSweep += SK_Scalar1; 205 // fSweep += SK_Scalar1;
198 this->inval(NULL); 206 this->inval(NULL);
199 return this->INHERITED::onFindClickHandler(x, y, modi); 207 return this->INHERITED::onFindClickHandler(x, y, modi);
200 } 208 }
201 209
202 virtual bool onClick(Click* click) { 210 virtual bool onClick(Click* click) {
203 return this->INHERITED::onClick(click); 211 return this->INHERITED::onClick(click);
204 } 212 }
205 213
206 private: 214 private:
207 SkScalar fSweep; 215 SkScalar fSweep;
208 216
209 typedef SampleView INHERITED; 217 typedef SampleView INHERITED;
210 }; 218 };
211 219
212 ////////////////////////////////////////////////////////////////////////////// 220 //////////////////////////////////////////////////////////////////////////////
213 221
214 static SkView* MyFactory() { return new ArcsView; } 222 static SkView* MyFactory() { return new ArcsView; }
215 static SkViewRegister reg(MyFactory); 223 static SkViewRegister reg(MyFactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698