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