2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
12 #include "SkPathOps.h"
17 class PathOpsInverseGM : public GM {
23 void onOnceBeforeDraw() override {
24 const unsigned oneColor = sk_tool_utils::color_to_565(0xFF8080FF);
25 const unsigned twoColor = 0x807F1f1f;
26 SkColor blendColor = blend(oneColor, twoColor);
27 makePaint(&fOnePaint, oneColor);
28 makePaint(&fTwoPaint, twoColor);
29 makePaint(&fOpPaint[kDifference_SkPathOp], oneColor);
30 makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor);
31 makePaint(&fOpPaint[kUnion_SkPathOp], sk_tool_utils::color_to_565(0xFFc0FFc0));
32 makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor);
33 makePaint(&fOpPaint[kXOR_SkPathOp], sk_tool_utils::color_to_565(0xFFa0FFe0));
34 makePaint(&fOutlinePaint, 0xFF000000);
35 fOutlinePaint.setStyle(SkPaint::kStroke_Style);
38 SkColor blend(SkColor one, SkColor two) {
40 temp.allocN32Pixels(1, 1);
41 SkCanvas canvas(temp);
42 canvas.drawColor(one);
43 canvas.drawColor(two);
44 void* pixels = temp.getPixels();
45 return *(SkColor*) pixels;
48 void makePaint(SkPaint* paint, SkColor color) {
49 paint->setAntiAlias(true);
50 paint->setStyle(SkPaint::kFill_Style);
51 paint->setColor(color);
54 SkString onShortName() override {
55 return SkString("pathopsinverse");
58 SkISize onISize() override {
59 return SkISize::Make(1200, 900);
62 void onDraw(SkCanvas* canvas) override {
65 for (int oneFill = 0; oneFill <= 1; ++oneFill) {
66 SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType
67 : SkPath::kEvenOdd_FillType;
68 for (int twoFill = 0; twoFill <= 1; ++twoFill) {
69 SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillType
70 : SkPath::kEvenOdd_FillType;
72 one.setFillType(oneF);
73 one.addRect(10, 10, 70, 70);
75 two.setFillType(twoF);
76 two.addRect(40, 40, 100, 100);
78 canvas->translate(0, SkIntToScalar(yPos));
79 canvas->clipRect(SkRect::MakeWH(110, 110), true);
80 canvas->drawPath(one, fOnePaint);
81 canvas->drawPath(one, fOutlinePaint);
82 canvas->drawPath(two, fTwoPaint);
83 canvas->drawPath(two, fOutlinePaint);
86 for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
88 Op(one, two, (SkPathOp) op, &result);
90 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
91 canvas->clipRect(SkRect::MakeWH(110, 110), true);
92 canvas->drawPath(result, fOpPaint[op]);
93 canvas->drawPath(result, fOutlinePaint);
105 SkPaint fOutlinePaint;
106 SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1];
107 typedef GM INHERITED;
110 //////////////////////////////////////////////////////////////////////////////
112 static GM* MyFactory(void*) { return new PathOpsInverseGM; }
113 static GMRegistry reg(MyFactory);