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