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