epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | //#include "SkParsePath.h" |
| 11 | #include "SkPath.h" |
| 12 | //#include "SkRandom.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 16 | static const SkColor gPathColor = SK_ColorBLACK; |
| 17 | static const SkColor gClipAColor = SK_ColorBLUE; |
| 18 | static const SkColor gClipBColor = SK_ColorRED; |
| 19 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 20 | class ComplexClipGM : public GM { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 21 | bool fDoAAClip; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 22 | public: |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 23 | ComplexClipGM(bool aaclip) : fDoAAClip(aaclip) { |
| 24 | this->setBGColor(0xFFDDDDDD); |
| 25 | // this->setBGColor(SkColorSetRGB(0xB0,0xDD,0xB0)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | protected: |
| 29 | |
| 30 | SkString onShortName() { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 31 | SkString str; |
| 32 | str.printf("complexclip_%s", fDoAAClip ? "aa" : "bw"); |
| 33 | return str; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 34 | } |
| 35 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 36 | SkISize onISize() { return make_isize(970, 780); } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 38 | virtual void onDraw(SkCanvas* canvas) { |
| 39 | SkPath path; |
| 40 | path.moveTo(SkIntToScalar(0), SkIntToScalar(50)); |
| 41 | path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0)); |
| 42 | path.lineTo(SkIntToScalar(175), SkIntToScalar(0)); |
| 43 | path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25)); |
| 44 | path.lineTo(SkIntToScalar(200), SkIntToScalar(150)); |
| 45 | path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200)); |
| 46 | path.lineTo(SkIntToScalar(0), SkIntToScalar(200)); |
| 47 | path.close(); |
| 48 | path.moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 49 | path.lineTo(SkIntToScalar(150), SkIntToScalar(50)); |
| 50 | path.lineTo(SkIntToScalar(150), SkIntToScalar(125)); |
| 51 | path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150)); |
| 52 | path.lineTo(SkIntToScalar(50), SkIntToScalar(150)); |
| 53 | path.close(); |
| 54 | path.setFillType(SkPath::kEvenOdd_FillType); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 55 | SkPaint pathPaint; |
| 56 | pathPaint.setAntiAlias(true); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 57 | pathPaint.setColor(gPathColor); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 58 | |
| 59 | SkPath clipA; |
| 60 | clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20)); |
| 61 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22)); |
| 62 | clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105)); |
| 63 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177)); |
| 64 | clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180)); |
| 65 | clipA.close(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 66 | |
| 67 | SkPath clipB; |
| 68 | clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10)); |
| 69 | clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15)); |
| 70 | clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190)); |
| 71 | clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185)); |
| 72 | clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100)); |
| 73 | clipB.close(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 75 | SkPaint paint; |
| 76 | paint.setAntiAlias(true); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 77 | paint.setTextSize(SkIntToScalar(20)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 78 | |
| 79 | static const struct { |
| 80 | SkRegion::Op fOp; |
| 81 | const char* fName; |
| 82 | } gOps[] = { //extra spaces in names for measureText |
| 83 | {SkRegion::kIntersect_Op, "Isect "}, |
| 84 | {SkRegion::kDifference_Op, "Diff " }, |
| 85 | {SkRegion::kUnion_Op, "Union "}, |
| 86 | {SkRegion::kXOR_Op, "Xor " }, |
| 87 | {SkRegion::kReverseDifference_Op, "RDiff "} |
| 88 | }; |
| 89 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 90 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 91 | canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 92 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 93 | for (int invBits = 0; invBits < 4; ++invBits) { |
| 94 | canvas->save(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 95 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 96 | this->drawHairlines(canvas, path, clipA, clipB); |
| 97 | |
| 98 | bool doInvA = SkToBool(invBits & 1); |
| 99 | bool doInvB = SkToBool(invBits & 2); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 100 | canvas->save(); |
| 101 | // set clip |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 102 | clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType : |
| 103 | SkPath::kEvenOdd_FillType); |
| 104 | clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType : |
| 105 | SkPath::kEvenOdd_FillType); |
| 106 | canvas->clipPath(clipA, SkRegion::kIntersect_Op, fDoAAClip); |
| 107 | canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 108 | |
| 109 | // draw path clipped |
| 110 | canvas->drawPath(path, pathPaint); |
| 111 | canvas->restore(); |
| 112 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 113 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 114 | SkScalar txtX = SkIntToScalar(45); |
| 115 | paint.setColor(gClipAColor); |
| 116 | const char* aTxt = doInvA ? "InvA " : "A "; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 117 | canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint); |
| 118 | txtX += paint.measureText(aTxt, strlen(aTxt)); |
| 119 | paint.setColor(SK_ColorBLACK); |
| 120 | canvas->drawText(gOps[op].fName, strlen(gOps[op].fName), |
| 121 | txtX, SkIntToScalar(220), paint); |
| 122 | txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName)); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 123 | paint.setColor(gClipBColor); |
| 124 | const char* bTxt = doInvB ? "InvB " : "B "; |
| 125 | canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 126 | |
| 127 | canvas->translate(SkIntToScalar(250),0); |
| 128 | } |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 129 | canvas->restore(); |
| 130 | canvas->translate(0, SkIntToScalar(250)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 131 | } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 132 | } |
| 133 | private: |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 134 | void drawHairlines(SkCanvas* canvas, const SkPath& path, |
| 135 | const SkPath& clipA, const SkPath& clipB) { |
| 136 | SkPaint paint; |
| 137 | paint.setAntiAlias(true); |
| 138 | paint.setStyle(SkPaint::kStroke_Style); |
| 139 | const SkAlpha fade = 0x33; |
| 140 | |
| 141 | // draw path in hairline |
| 142 | paint.setColor(gPathColor); paint.setAlpha(fade); |
| 143 | canvas->drawPath(path, paint); |
| 144 | |
| 145 | // draw clips in hair line |
| 146 | paint.setColor(gClipAColor); paint.setAlpha(fade); |
| 147 | canvas->drawPath(clipA, paint); |
| 148 | paint.setColor(gClipBColor); paint.setAlpha(fade); |
| 149 | canvas->drawPath(clipB, paint); |
| 150 | } |
| 151 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 152 | typedef GM INHERITED; |
| 153 | }; |
| 154 | |
| 155 | ////////////////////////////////////////////////////////////////////////////// |
| 156 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 157 | static GM* gFact0(void*) { return new ComplexClipGM(false); } |
| 158 | static GM* gFact1(void*) { return new ComplexClipGM(true); } |
| 159 | |
| 160 | static GMRegistry gReg0(gFact0); |
| 161 | static GMRegistry gReg1(gFact1); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 162 | |
| 163 | } |