OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "gm.h" | |
9 #include "SkBitmap.h" | |
10 #include "SkPath.h" | |
11 #include "SkPathOps.h" | |
12 #include "SkRect.h" | |
13 | |
14 namespace skiagm { | |
15 | |
16 class PathOpsInverseGM : public GM { | |
17 public: | |
18 PathOpsInverseGM() { | |
19 this->makePaints(); | |
bsalomon
2013/04/22 13:13:57
There's a virtual on GM called "onOnceBeforeDraw".
| |
20 } | |
21 | |
22 protected: | |
23 void makePaints() { | |
24 const unsigned oneColor = 0xFF8080FF; | |
25 const unsigned twoColor = 0x807F1f1f; | |
26 SkColor blendColor = blend(oneColor, twoColor); | |
27 makePaint(&onePaint, oneColor); | |
28 makePaint(&twoPaint, twoColor); | |
29 makePaint(&opPaint[kDifference_PathOp], oneColor); | |
30 makePaint(&opPaint[kIntersect_PathOp], blendColor); | |
31 makePaint(&opPaint[kUnion_PathOp], 0xFFc0FFc0); | |
32 makePaint(&opPaint[kReverseDifference_PathOp], twoColor); | |
33 makePaint(&opPaint[kXOR_PathOp], 0xFFa0FFe0); | |
34 makePaint(&outlinePaint, 0xFF000000); | |
35 outlinePaint.setStyle(SkPaint::kStroke_Style); | |
36 } | |
37 | |
38 SkColor blend(SkColor one, SkColor two) { | |
39 SkBitmap temp; | |
40 temp.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); | |
41 temp.allocPixels(); | |
42 SkCanvas canvas(temp); | |
43 canvas.drawColor(one); | |
44 canvas.drawColor(two); | |
45 void* pixels = temp.getPixels(); | |
46 return *(SkColor*) pixels; | |
47 } | |
48 | |
49 void makePaint(SkPaint* paint, SkColor color) { | |
50 paint->setAntiAlias(true); | |
51 paint->setStyle(SkPaint::kFill_Style); | |
52 paint->setColor(color); | |
53 } | |
54 | |
55 virtual SkString onShortName() SK_OVERRIDE { | |
56 return SkString("pathopsinverse"); | |
57 } | |
58 | |
59 virtual SkISize onISize() SK_OVERRIDE { | |
60 return make_isize(1200, 900); | |
61 } | |
62 | |
63 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
64 SkPath one, two; | |
65 int yPos = 0; | |
66 for (int oneFill = 0; oneFill <= 1; ++oneFill) { | |
67 SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType | |
68 : SkPath::kEvenOdd_FillType; | |
69 for (int twoFill = 0; twoFill <= 1; ++twoFill) { | |
70 SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillTy pe | |
71 : SkPath::kEvenOdd_FillType; | |
72 one.reset(); | |
73 one.setFillType(oneF); | |
74 one.addRect(10, 10, 70, 70); | |
75 two.reset(); | |
76 two.setFillType(twoF); | |
77 two.addRect(40, 40, 100, 100); | |
78 canvas->save(); | |
79 canvas->translate(0, SkIntToScalar(yPos)); | |
80 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_ Op, true); | |
81 canvas->drawPath(one, onePaint); | |
82 canvas->drawPath(one, outlinePaint); | |
83 canvas->drawPath(two, twoPaint); | |
84 canvas->drawPath(two, outlinePaint); | |
85 canvas->restore(); | |
86 int xPos = 150; | |
87 for (int op = kDifference_PathOp; op <= kReverseDifference_PathO p; ++op) { | |
88 SkPath result; | |
89 Op(one, two, (SkPathOp) op, &result); | |
90 canvas->save(); | |
91 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); | |
92 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kInters ect_Op, true); | |
93 canvas->drawPath(result, opPaint[op]); | |
94 canvas->drawPath(result, outlinePaint); | |
95 canvas->restore(); | |
96 xPos += 150; | |
97 } | |
98 yPos += 150; | |
99 } | |
100 } | |
101 } | |
102 | |
103 private: | |
104 SkPaint onePaint; | |
bsalomon
2013/04/22 13:13:57
fOnePaint, etc?
| |
105 SkPaint twoPaint; | |
106 SkPaint outlinePaint; | |
107 SkPaint opPaint[kReverseDifference_PathOp - kDifference_PathOp + 1]; | |
108 typedef GM INHERITED; | |
109 }; | |
110 | |
111 ////////////////////////////////////////////////////////////////////////////// | |
112 | |
113 static GM* MyFactory(void*) { return new PathOpsInverseGM; } | |
114 static GMRegistry reg(MyFactory); | |
115 | |
116 } | |
OLD | NEW |