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