egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkClipOp.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkPaint.h" |
Mike Reed | cfb130c | 2020-08-03 11:02:20 -0400 | [diff] [blame] | 14 | #include "include/core/SkPathBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkScalar.h" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkTypeface.h" |
| 20 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "tools/ToolUtils.h" |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 22 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 23 | #include <utility> |
| 24 | |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 25 | namespace skiagm { |
| 26 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 27 | constexpr SkColor gPathColor = SK_ColorYELLOW; |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 28 | |
| 29 | class ComplexClip3GM : public GM { |
| 30 | public: |
| 31 | ComplexClip3GM(bool doSimpleClipFirst) |
| 32 | : fDoSimpleClipFirst(doSimpleClipFirst) { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 33 | this->setBGColor(0xFFDDDDDD); |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | protected: |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 37 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 38 | SkString onShortName() override { |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 39 | SkString str; |
| 40 | str.printf("complexclip3_%s", fDoSimpleClipFirst ? "simple" : "complex"); |
| 41 | return str; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 42 | } |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 43 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 44 | SkISize onISize() override { return SkISize::Make(400, 950); } |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 45 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 46 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | cfb130c | 2020-08-03 11:02:20 -0400 | [diff] [blame] | 47 | SkPath clipSimple = SkPath::Circle(70, 50, 20); |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 48 | |
| 49 | SkRect r1 = { 10, 20, 70, 80 }; |
Mike Reed | cfb130c | 2020-08-03 11:02:20 -0400 | [diff] [blame] | 50 | SkPath clipComplex = SkPathBuilder().moveTo(40, 50) |
| 51 | .arcTo(r1, 30, 300, false) |
| 52 | .close() |
| 53 | .detach(); |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 54 | |
| 55 | SkPath* firstClip = &clipSimple; |
| 56 | SkPath* secondClip = &clipComplex; |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 57 | if (!fDoSimpleClipFirst) { |
Mike Reed | cfb130c | 2020-08-03 11:02:20 -0400 | [diff] [blame] | 58 | std::swap(firstClip, secondClip); |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | SkPaint paint; |
| 62 | paint.setAntiAlias(true); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 63 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 64 | SkFont font(ToolUtils::create_portable_typeface(), 20); |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 65 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 66 | constexpr struct { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 67 | SkClipOp fOp; |
| 68 | const char* fName; |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 69 | } gOps[] = { |
Michael Ludwig | 2f6e2f8 | 2021-08-03 13:08:50 -0400 | [diff] [blame] | 70 | {SkClipOp::kIntersect, "I"}, |
| 71 | {SkClipOp::kDifference, "D" }, |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 75 | canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4); |
| 76 | |
| 77 | SkPaint pathPaint; |
| 78 | pathPaint.setAntiAlias(true); |
| 79 | pathPaint.setColor(gPathColor); |
| 80 | |
| 81 | for (int invA = 0; invA < 2; ++invA) { |
| 82 | for (int aaBits = 0; aaBits < 4; ++aaBits) { |
| 83 | canvas->save(); |
| 84 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
| 85 | for (int invB = 0; invB < 2; ++invB) { |
| 86 | bool doAAA = SkToBool(aaBits & 1); |
| 87 | bool doAAB = SkToBool(aaBits & 2); |
| 88 | bool doInvA = SkToBool(invA); |
| 89 | bool doInvB = SkToBool(invB); |
| 90 | canvas->save(); |
| 91 | // set clip |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 92 | firstClip->setFillType(doInvA ? SkPathFillType::kInverseEvenOdd : |
| 93 | SkPathFillType::kEvenOdd); |
| 94 | secondClip->setFillType(doInvB ? SkPathFillType::kInverseEvenOdd : |
| 95 | SkPathFillType::kEvenOdd); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 96 | canvas->clipPath(*firstClip, doAAA); |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 97 | canvas->clipPath(*secondClip, gOps[op].fOp, doAAB); |
| 98 | |
| 99 | // draw rect clipped |
| 100 | SkRect r = { 0, 0, 100, 100 }; |
| 101 | canvas->drawRect(r, pathPaint); |
| 102 | canvas->restore(); |
| 103 | |
| 104 | |
| 105 | SkScalar txtX = SkIntToScalar(10); |
| 106 | paint.setColor(SK_ColorBLACK); |
| 107 | SkString str; |
| 108 | str.printf("%s%s %s %s%s", doAAA ? "A" : "B", |
| 109 | doInvA ? "I" : "N", |
| 110 | gOps[op].fName, |
| 111 | doAAB ? "A" : "B", |
| 112 | doInvB ? "I" : "N"); |
| 113 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 114 | canvas->drawString(str.c_str(), txtX, SkIntToScalar(130), font, paint); |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 115 | if (doInvB) { |
| 116 | canvas->translate(SkIntToScalar(150),0); |
| 117 | } else { |
| 118 | canvas->translate(SkIntToScalar(120),0); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | canvas->restore(); |
| 123 | canvas->translate(0, SkIntToScalar(150)); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | bool fDoSimpleClipFirst; |
| 130 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 131 | using INHERITED = GM; |
egdaniel | df79503 | 2014-12-17 11:22:37 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | ////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | // Simple clip first |
| 137 | DEF_GM( return new ComplexClip3GM(true); ) |
| 138 | // Complex clip first |
| 139 | DEF_GM( return new ComplexClip3GM(false); ) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 140 | } // namespace skiagm |