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

Side by Side Diff: samplecode/SampleFatBits.cpp

Issue 2350613002: add 'g' to fatbits (Closed)
Patch Set: dang I wish I saw these warnings on mac Created 4 years, 3 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
« no previous file with comments | « no previous file | 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 2012 Google Inc. 2 * Copyright 2012 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 "sk_tool_utils.h" 8 #include "sk_tool_utils.h"
9 #include "SampleCode.h" 9 #include "SampleCode.h"
10 #include "SkView.h" 10 #include "SkView.h"
(...skipping 25 matching lines...) Expand all
36 surface->getCanvas()->clear(SK_ColorTRANSPARENT); 36 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
37 } 37 }
38 38
39 class FatBits { 39 class FatBits {
40 public: 40 public:
41 FatBits() { 41 FatBits() {
42 fAA = false; 42 fAA = false;
43 fStyle = kHair_Style; 43 fStyle = kHair_Style;
44 fGrid = false; 44 fGrid = false;
45 fShowSkeleton = true; 45 fShowSkeleton = true;
46 fUseGPU = false;
47 fUseClip = false; 46 fUseClip = false;
48 fRectAsOval = false; 47 fRectAsOval = false;
49 fUseTriangle = false; 48 fUseTriangle = false;
50 fStrokeCap = SkPaint::kButt_Cap; 49 fStrokeCap = SkPaint::kButt_Cap;
51 50
52 fClipRect.set(2, 2, 11, 8 ); 51 fClipRect.set(2, 2, 11, 8 );
53 } 52 }
54 53
55 int getZoom() const { return fZoom; } 54 int getZoom() const { return fZoom; }
56 55
57 bool getAA() const { return fAA; } 56 bool getAA() const { return fAA; }
58 void setAA(bool aa) { fAA = aa; } 57 void setAA(bool aa) { fAA = aa; }
59 58
60 bool getGrid() const { return fGrid; } 59 bool getGrid() const { return fGrid; }
61 void setGrid(bool g) { fGrid = g; } 60 void setGrid(bool g) { fGrid = g; }
62 61
63 bool getShowSkeleton() const { return fShowSkeleton; } 62 bool getShowSkeleton() const { return fShowSkeleton; }
64 void setShowSkeleton(bool ss) { fShowSkeleton = ss; } 63 void setShowSkeleton(bool ss) { fShowSkeleton = ss; }
65 64
66 bool getUseGPU() const { return fUseGPU; }
67 void setUseGPU(bool ug) { fUseGPU = ug; }
68
69 bool getTriangle() const { return fUseTriangle; } 65 bool getTriangle() const { return fUseTriangle; }
70 void setTriangle(bool ut) { fUseTriangle = ut; } 66 void setTriangle(bool ut) { fUseTriangle = ut; }
71 67
72 void toggleRectAsOval() { fRectAsOval = !fRectAsOval; } 68 void toggleRectAsOval() { fRectAsOval = !fRectAsOval; }
73 69
70 void togglePixelColors() {
71 if (fShader == fShader0) {
72 fShader = fShader1;
73 } else {
74 fShader = fShader0;
75 }
76 }
77
74 bool getUseClip() const { return fUseClip; } 78 bool getUseClip() const { return fUseClip; }
75 void setUseClip(bool uc) { fUseClip = uc; } 79 void setUseClip(bool uc) { fUseClip = uc; }
76 80
77 enum Style { 81 enum Style {
78 kHair_Style, 82 kHair_Style,
79 kStroke_Style, 83 kStroke_Style,
80 }; 84 };
81 Style getStyle() const { return fStyle; } 85 Style getStyle() const { return fStyle; }
82 void setStyle(Style s) { fStyle = s; } 86 void setStyle(Style s) { fStyle = s; }
83 87
84 void setWHZ(int width, int height, int zoom) { 88 void setWHZ(int width, int height, int zoom) {
85 fW = width; 89 fW = width;
86 fH = height; 90 fH = height;
87 fZoom = zoom; 91 fZoom = zoom;
88 fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zo om)); 92 fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zo om));
89 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom)); 93 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
90 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom); 94 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
91 fShader = sk_tool_utils::create_checkerboard_shader(0xFFCCCCCC, 0xFFFFFF FF, zoom); 95 fShader0 = sk_tool_utils::create_checkerboard_shader(0xFFDDDDDD, 0xFFFFF FFF, zoom);
96 fShader1 = SkShader::MakeColorShader(SK_ColorWHITE);
97 fShader = fShader0;
92 98
93 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); 99 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
94 fMinSurface = SkSurface::MakeRaster(info); 100 fMinSurface = SkSurface::MakeRaster(info);
95 info = info.makeWH(width * zoom, height * zoom); 101 info = info.makeWH(width * zoom, height * zoom);
96 fMaxSurface = SkSurface::MakeRaster(info); 102 fMaxSurface = SkSurface::MakeRaster(info);
97 } 103 }
98 104
99 void drawBG(SkCanvas*); 105 void drawBG(SkCanvas*);
100 void drawFG(SkCanvas*); 106 void drawFG(SkCanvas*);
101 void drawLine(SkCanvas*, SkPoint pts[2]); 107 void drawLine(SkCanvas*, SkPoint pts[2]);
102 void drawRect(SkCanvas* canvas, SkPoint pts[2]); 108 void drawRect(SkCanvas* canvas, SkPoint pts[2]);
103 void drawTriangle(SkCanvas* canvas, SkPoint pts[3]); 109 void drawTriangle(SkCanvas* canvas, SkPoint pts[3]);
104 110
105 SkPaint::Cap fStrokeCap; 111 SkPaint::Cap fStrokeCap;
106 112
107 private: 113 private:
108 bool fAA, fGrid, fShowSkeleton, fUseGPU, fUseClip, fRectAsOval, fUseTriangle ; 114 bool fAA, fGrid, fShowSkeleton, fUseClip, fRectAsOval, fUseTriangle;
109 Style fStyle; 115 Style fStyle;
110 int fW, fH, fZoom; 116 int fW, fH, fZoom;
111 SkMatrix fMatrix, fInverse; 117 SkMatrix fMatrix, fInverse;
112 SkRect fBounds, fClipRect; 118 SkRect fBounds, fClipRect;
113 sk_sp<SkShader> fShader; 119 sk_sp<SkShader> fShader0;
114 sk_sp<SkSurface> fMinSurface; 120 sk_sp<SkShader> fShader1;
115 sk_sp<SkSurface> fMaxSurface; 121 sk_sp<SkShader> fShader;
122 sk_sp<SkSurface> fMinSurface;
123 sk_sp<SkSurface> fMaxSurface;
116 124
117 void setupPaint(SkPaint* paint) { 125 void setupPaint(SkPaint* paint) {
118 bool aa = this->getAA(); 126 bool aa = this->getAA();
119 paint->setStrokeCap(fStrokeCap); 127 paint->setStrokeCap(fStrokeCap);
120 switch (fStyle) { 128 switch (fStyle) {
121 case kHair_Style: 129 case kHair_Style:
122 paint->setStrokeWidth(0); 130 paint->setStrokeWidth(0);
123 break; 131 break;
124 case kStroke_Style: 132 case kStroke_Style:
125 paint->setStrokeWidth(SK_Scalar1); 133 paint->setStrokeWidth(SK_Scalar1);
126 break; 134 break;
127 } 135 }
128 paint->setAntiAlias(aa); 136 paint->setAntiAlias(aa);
129 } 137 }
130 138
131 void setupSkeletonPaint(SkPaint* paint) { 139 void setupSkeletonPaint(SkPaint* paint) {
132 paint->setStyle(SkPaint::kStroke_Style); 140 paint->setStyle(SkPaint::kStroke_Style);
133 paint->setStrokeWidth(WIRE_FRAME_SIZE); 141 paint->setStrokeWidth(WIRE_FRAME_SIZE);
134 paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0); 142 paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0);
135 paint->setAntiAlias(true); 143 paint->setAntiAlias(true);
136 } 144 }
137 145
138 void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]); 146 void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]);
139 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]); 147 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
140 void drawRectSkeleton(SkCanvas* max, const SkRect& r) { 148 void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
141 SkPaint paint; 149 SkPaint paint;
142 this->setupSkeletonPaint(&paint); 150 this->setupSkeletonPaint(&paint);
143 SkPath path; 151 SkPath path;
144 152
145 if (fUseGPU && fAA) { 153 fRectAsOval ? path.addOval(r) : path.addRect(r);
146 SkRect rr = r;
147 rr.inset(SkIntToScalar(fZoom)/2, SkIntToScalar(fZoom)/2);
148 path.addRect(rr);
149 path.moveTo(rr.fLeft, rr.fTop);
150 path.lineTo(rr.fRight, rr.fBottom);
151 rr = r;
152 rr.inset(-SkIntToScalar(fZoom)/2, -SkIntToScalar(fZoom)/2);
153 path.addRect(rr);
154 } else {
155 fRectAsOval ? path.addOval(r) : path.addRect(r);
156 if (fUseGPU) {
157 path.moveTo(r.fLeft, r.fTop);
158 path.lineTo(r.fRight, r.fBottom);
159 }
160 }
161 max->drawPath(path, paint); 154 max->drawPath(path, paint);
162 } 155 }
163 156
164 void copyMinToMax() { 157 void copyMinToMax() {
165 erase(fMaxSurface.get()); 158 erase(fMaxSurface.get());
166 SkCanvas* canvas = fMaxSurface->getCanvas(); 159 SkCanvas* canvas = fMaxSurface->getCanvas();
167 canvas->save(); 160 canvas->save();
168 canvas->concat(fMatrix); 161 canvas->concat(fMatrix);
169 fMinSurface->draw(canvas, 0, 0, nullptr); 162 fMinSurface->draw(canvas, 0, 0, nullptr);
170 canvas->restore(); 163 canvas->restore();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 220 }
228 221
229 void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) { 222 void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
230 SkPaint paint; 223 SkPaint paint;
231 this->setupSkeletonPaint(&paint); 224 this->setupSkeletonPaint(&paint);
232 225
233 SkPath path; 226 SkPath path;
234 path.moveTo(pts[0]); 227 path.moveTo(pts[0]);
235 path.lineTo(pts[1]); 228 path.lineTo(pts[1]);
236 229
237 switch (fStyle) { 230 if (fStyle == kStroke_Style) {
238 case kHair_Style: 231 SkPaint p;
239 if (fUseGPU) { 232 p.setStyle(SkPaint::kStroke_Style);
240 SkPaint p; 233 p.setStrokeWidth(SK_Scalar1 * fZoom);
241 p.setStyle(SkPaint::kStroke_Style); 234 p.setStrokeCap(fStrokeCap);
242 p.setStrokeWidth(SK_Scalar1 * fZoom); 235 SkPath dst;
243 SkPath dst; 236 p.getFillPath(path, &dst);
244 p.getFillPath(path, &dst); 237 path = dst;
245 path.addPath(dst);
246 }
247 break;
248 case kStroke_Style: {
249 SkPaint p;
250 p.setStyle(SkPaint::kStroke_Style);
251 p.setStrokeWidth(SK_Scalar1 * fZoom);
252 p.setStrokeCap(fStrokeCap);
253 SkPath dst;
254 p.getFillPath(path, &dst);
255 path = dst;
256 238
257 path.moveTo(pts[0]); 239 path.moveTo(pts[0]);
258 path.lineTo(pts[1]); 240 path.lineTo(pts[1]);
259
260 if (fUseGPU) {
261 path.moveTo(dst.getPoint(0));
262 path.lineTo(dst.getPoint(2));
263 }
264 } break;
265 } 241 }
266 max->drawPath(path, paint); 242 max->drawPath(path, paint);
267 } 243 }
268 244
269 void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) { 245 void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
270 SkPaint paint; 246 SkPaint paint;
271 247
272 fInverse.mapPoints(pts, 2); 248 fInverse.mapPoints(pts, 2);
273 249
274 if (fGrid) { 250 if (fGrid) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 int fIndex; 352 int fIndex;
377 public: 353 public:
378 IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {} 354 IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
379 355
380 static int GetIndex(SkView::Click* click) { 356 static int GetIndex(SkView::Click* click) {
381 return ((IndexClick*)click)->fIndex; 357 return ((IndexClick*)click)->fIndex;
382 } 358 }
383 }; 359 };
384 360
385 class DrawLineView : public SampleView { 361 class DrawLineView : public SampleView {
386 enum {
387 kZoom = 96
388 };
389
390 FatBits fFB; 362 FatBits fFB;
391 SkPoint fPts[3]; 363 SkPoint fPts[3];
392 bool fIsRect; 364 bool fIsRect;
365 int fZoom = 64;
393 public: 366 public:
394 DrawLineView() { 367 DrawLineView() {
395 fFB.setWHZ(24, 16, kZoom); 368 fFB.setWHZ(24*2, 16*2, fZoom);
396 fPts[0].set(1, 1); 369 fPts[0].set(1, 1);
397 fPts[1].set(5, 4); 370 fPts[1].set(5, 4);
398 fPts[2].set(2, 6); 371 fPts[2].set(2, 6);
399 SkMatrix::MakeScale(kZoom, kZoom).mapPoints(fPts, 3); 372 SkMatrix::MakeScale(SkIntToScalar(fZoom)).mapPoints(fPts, 3);
400 fIsRect = false; 373 fIsRect = false;
401 } 374 }
402 375
403 void setStyle(FatBits::Style s) { 376 void setStyle(FatBits::Style s) {
404 fFB.setStyle(s); 377 fFB.setStyle(s);
405 this->inval(nullptr); 378 this->inval(nullptr);
406 } 379 }
407 380
408 protected: 381 protected:
409 bool onQuery(SkEvent* evt) override { 382 bool onQuery(SkEvent* evt) override {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 } break; 420 } break;
448 case 'a': 421 case 'a':
449 fFB.setAA(!fFB.getAA()); 422 fFB.setAA(!fFB.getAA());
450 this->inval(nullptr); 423 this->inval(nullptr);
451 return true; 424 return true;
452 case 'w': 425 case 'w':
453 fFB.setShowSkeleton(!fFB.getShowSkeleton()); 426 fFB.setShowSkeleton(!fFB.getShowSkeleton());
454 this->inval(nullptr); 427 this->inval(nullptr);
455 return true; 428 return true;
456 case 'g': 429 case 'g':
457 fFB.setUseGPU(!fFB.getUseGPU()); 430 fFB.togglePixelColors();
458 this->inval(nullptr); 431 this->inval(nullptr);
459 return true; 432 return true;
460 case 't': 433 case 't':
461 fFB.setTriangle(!fFB.getTriangle()); 434 fFB.setTriangle(!fFB.getTriangle());
462 this->inval(nullptr); 435 this->inval(nullptr);
463 return true; 436 return true;
464 } 437 }
465 } 438 }
466 return this->INHERITED::onQuery(evt); 439 return this->INHERITED::onQuery(evt);
467 } 440 }
468 441
469 void onDrawContent(SkCanvas* canvas) override { 442 void onDrawContent(SkCanvas* canvas) override {
470 fFB.drawBG(canvas); 443 fFB.drawBG(canvas);
471 if (fFB.getTriangle()) { 444 if (fFB.getTriangle()) {
472 fFB.drawTriangle(canvas, fPts); 445 fFB.drawTriangle(canvas, fPts);
473 } 446 }
474 else if (fIsRect) { 447 else if (fIsRect) {
475 fFB.drawRect(canvas, fPts); 448 fFB.drawRect(canvas, fPts);
476 } else { 449 } else {
477 fFB.drawLine(canvas, fPts); 450 fFB.drawLine(canvas, fPts);
478 } 451 }
479 fFB.drawFG(canvas); 452 fFB.drawFG(canvas);
480 453
481 { 454 {
482 SkString str; 455 SkString str;
483 str.printf("%s %s %s %s", 456 str.printf("%s %s %s",
484 fFB.getAA() ? "AA" : "BW", 457 fFB.getAA() ? "AA" : "BW",
485 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke ", 458 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke ",
486 fFB.getUseGPU() ? "GPU" : "CPU",
487 fFB.getUseClip() ? "clip" : "noclip"); 459 fFB.getUseClip() ? "clip" : "noclip");
488 SkPaint paint; 460 SkPaint paint;
489 paint.setAntiAlias(true); 461 paint.setAntiAlias(true);
490 paint.setTextSize(16); 462 paint.setTextSize(16);
491 paint.setColor(SK_ColorBLUE); 463 paint.setColor(SK_ColorBLUE);
492 canvas->drawText(str.c_str(), str.size(), 10, 16, paint); 464 canvas->drawText(str.c_str(), str.size(), 10, 16, paint);
493 } 465 }
494 } 466 }
495 467
496 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { 468 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride {
(...skipping 28 matching lines...) Expand all
525 497
526 private: 498 private:
527 499
528 typedef SampleView INHERITED; 500 typedef SampleView INHERITED;
529 }; 501 };
530 502
531 ////////////////////////////////////////////////////////////////////////////// 503 //////////////////////////////////////////////////////////////////////////////
532 504
533 static SkView* MyFactory() { return new DrawLineView; } 505 static SkView* MyFactory() { return new DrawLineView; }
534 static SkViewRegister reg(MyFactory); 506 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698