caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkPathOps.h" |
| 13 | #include "SkRect.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class PathOpsInverseGM : public GM { |
| 18 | public: |
| 19 | PathOpsInverseGM() { |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | void onOnceBeforeDraw() override { |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 24 | const unsigned oneColor = sk_tool_utils::color_to_565(0xFF8080FF); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 25 | const unsigned twoColor = 0x807F1f1f; |
| 26 | SkColor blendColor = blend(oneColor, twoColor); |
caryclark@google.com | 4eedae6 | 2013-04-22 20:12:47 +0000 | [diff] [blame] | 27 | makePaint(&fOnePaint, oneColor); |
| 28 | makePaint(&fTwoPaint, twoColor); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 29 | makePaint(&fOpPaint[kDifference_SkPathOp], oneColor); |
| 30 | makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor); |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 31 | makePaint(&fOpPaint[kUnion_SkPathOp], sk_tool_utils::color_to_565(0xFFc0FFc0)); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 32 | makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor); |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 33 | makePaint(&fOpPaint[kXOR_SkPathOp], sk_tool_utils::color_to_565(0xFFa0FFe0)); |
caryclark@google.com | 4eedae6 | 2013-04-22 20:12:47 +0000 | [diff] [blame] | 34 | makePaint(&fOutlinePaint, 0xFF000000); |
| 35 | fOutlinePaint.setStyle(SkPaint::kStroke_Style); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | SkColor blend(SkColor one, SkColor two) { |
| 39 | SkBitmap temp; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 40 | temp.allocN32Pixels(1, 1); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 41 | SkCanvas canvas(temp); |
| 42 | canvas.drawColor(one); |
| 43 | canvas.drawColor(two); |
| 44 | void* pixels = temp.getPixels(); |
| 45 | return *(SkColor*) pixels; |
| 46 | } |
| 47 | |
| 48 | void makePaint(SkPaint* paint, SkColor color) { |
| 49 | paint->setAntiAlias(true); |
| 50 | paint->setStyle(SkPaint::kFill_Style); |
| 51 | paint->setColor(color); |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | SkString onShortName() override { |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 55 | return SkString("pathopsinverse"); |
| 56 | } |
| 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 59 | return SkISize::Make(1200, 900); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 60 | } |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | void onDraw(SkCanvas* canvas) override { |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 63 | SkPath one, two; |
| 64 | int yPos = 0; |
| 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; |
| 71 | one.reset(); |
| 72 | one.setFillType(oneF); |
| 73 | one.addRect(10, 10, 70, 70); |
| 74 | two.reset(); |
| 75 | two.setFillType(twoF); |
| 76 | two.addRect(40, 40, 100, 100); |
| 77 | canvas->save(); |
| 78 | canvas->translate(0, SkIntToScalar(yPos)); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 79 | canvas->clipRect(SkRect::MakeWH(110, 110), true); |
caryclark@google.com | 4eedae6 | 2013-04-22 20:12:47 +0000 | [diff] [blame] | 80 | canvas->drawPath(one, fOnePaint); |
| 81 | canvas->drawPath(one, fOutlinePaint); |
| 82 | canvas->drawPath(two, fTwoPaint); |
| 83 | canvas->drawPath(two, fOutlinePaint); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 84 | canvas->restore(); |
| 85 | int xPos = 150; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 86 | for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) { |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 87 | SkPath result; |
| 88 | Op(one, two, (SkPathOp) op, &result); |
| 89 | canvas->save(); |
| 90 | canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 91 | canvas->clipRect(SkRect::MakeWH(110, 110), true); |
caryclark@google.com | 4eedae6 | 2013-04-22 20:12:47 +0000 | [diff] [blame] | 92 | canvas->drawPath(result, fOpPaint[op]); |
| 93 | canvas->drawPath(result, fOutlinePaint); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 94 | canvas->restore(); |
| 95 | xPos += 150; |
| 96 | } |
| 97 | yPos += 150; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private: |
caryclark@google.com | 4eedae6 | 2013-04-22 20:12:47 +0000 | [diff] [blame] | 103 | SkPaint fOnePaint; |
| 104 | SkPaint fTwoPaint; |
| 105 | SkPaint fOutlinePaint; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 106 | SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1]; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 107 | typedef GM INHERITED; |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | static GM* MyFactory(void*) { return new PathOpsInverseGM; } |
| 113 | static GMRegistry reg(MyFactory); |
| 114 | |
| 115 | } |