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 { |
| 21 | public: |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 22 | ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw) |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 23 | : fDoAAClip(aaclip) |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 24 | , fDoSaveLayer(saveLayer) |
| 25 | , fInvertDraw(invertDraw) { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 26 | this->setBGColor(0xFFDDDDDD); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 31 | |
| 32 | SkString onShortName() { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 33 | SkString str; |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 34 | str.printf("complexclip_%s%s%s", |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 35 | fDoAAClip ? "aa" : "bw", |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 36 | fDoSaveLayer ? "_layer" : "", |
| 37 | fInvertDraw ? "_invert" : ""); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 38 | return str; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 39 | } |
| 40 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 41 | SkISize onISize() { return SkISize::Make(970, 780); } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 43 | virtual void onDraw(SkCanvas* canvas) { |
| 44 | SkPath path; |
| 45 | path.moveTo(SkIntToScalar(0), SkIntToScalar(50)); |
| 46 | path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0)); |
| 47 | path.lineTo(SkIntToScalar(175), SkIntToScalar(0)); |
| 48 | path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25)); |
| 49 | path.lineTo(SkIntToScalar(200), SkIntToScalar(150)); |
| 50 | path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200)); |
| 51 | path.lineTo(SkIntToScalar(0), SkIntToScalar(200)); |
| 52 | path.close(); |
| 53 | path.moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 54 | path.lineTo(SkIntToScalar(150), SkIntToScalar(50)); |
| 55 | path.lineTo(SkIntToScalar(150), SkIntToScalar(125)); |
| 56 | path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150)); |
| 57 | path.lineTo(SkIntToScalar(50), SkIntToScalar(150)); |
| 58 | path.close(); |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 59 | if (fInvertDraw) { |
| 60 | path.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 61 | } else { |
| 62 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 63 | } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 64 | SkPaint pathPaint; |
| 65 | pathPaint.setAntiAlias(true); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 66 | pathPaint.setColor(gPathColor); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 67 | |
| 68 | SkPath clipA; |
| 69 | clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20)); |
| 70 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22)); |
| 71 | clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105)); |
| 72 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177)); |
| 73 | clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180)); |
| 74 | clipA.close(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 75 | |
| 76 | SkPath clipB; |
| 77 | clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10)); |
| 78 | clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15)); |
| 79 | clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190)); |
| 80 | clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185)); |
| 81 | clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100)); |
| 82 | clipB.close(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 84 | SkPaint paint; |
| 85 | paint.setAntiAlias(true); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 86 | sk_tool_utils::set_portable_typeface(&paint); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 87 | paint.setTextSize(SkIntToScalar(20)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 88 | |
| 89 | static const struct { |
| 90 | SkRegion::Op fOp; |
| 91 | const char* fName; |
| 92 | } gOps[] = { //extra spaces in names for measureText |
| 93 | {SkRegion::kIntersect_Op, "Isect "}, |
| 94 | {SkRegion::kDifference_Op, "Diff " }, |
| 95 | {SkRegion::kUnion_Op, "Union "}, |
| 96 | {SkRegion::kXOR_Op, "Xor " }, |
| 97 | {SkRegion::kReverseDifference_Op, "RDiff "} |
| 98 | }; |
| 99 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 100 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 101 | canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 102 | |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 103 | if (fDoSaveLayer) { |
| 104 | // We want the layer to appear symmetric relative to actual |
| 105 | // device boundaries so we need to "undo" the effect of the |
| 106 | // scale and translate |
| 107 | SkRect bounds = SkRect::MakeLTRB( |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 108 | 4.0f/3.0f * -20, |
| 109 | 4.0f/3.0f * -20, |
| 110 | 4.0f/3.0f * (this->getISize().fWidth - 20), |
| 111 | 4.0f/3.0f * (this->getISize().fHeight - 20)); |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 112 | |
| 113 | bounds.inset(SkIntToScalar(100), SkIntToScalar(100)); |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 114 | SkPaint boundPaint; |
| 115 | boundPaint.setColor(SK_ColorRED); |
| 116 | boundPaint.setStyle(SkPaint::kStroke_Style); |
| 117 | canvas->drawRect(bounds, boundPaint); |
| 118 | canvas->saveLayer(&bounds, NULL); |
| 119 | } |
| 120 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 121 | for (int invBits = 0; invBits < 4; ++invBits) { |
| 122 | canvas->save(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 123 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 124 | this->drawHairlines(canvas, path, clipA, clipB); |
| 125 | |
| 126 | bool doInvA = SkToBool(invBits & 1); |
| 127 | bool doInvB = SkToBool(invBits & 2); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 128 | canvas->save(); |
| 129 | // set clip |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 130 | clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType : |
| 131 | SkPath::kEvenOdd_FillType); |
| 132 | clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType : |
| 133 | SkPath::kEvenOdd_FillType); |
| 134 | canvas->clipPath(clipA, SkRegion::kIntersect_Op, fDoAAClip); |
| 135 | canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 136 | |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 137 | // In the inverse case we need to prevent the draw from covering the whole |
| 138 | // canvas. |
| 139 | if (fInvertDraw) { |
| 140 | SkRect rectClip = clipA.getBounds(); |
| 141 | rectClip.join(path.getBounds()); |
| 142 | rectClip.join(path.getBounds()); |
| 143 | rectClip.outset(5, 5); |
| 144 | canvas->clipRect(rectClip); |
| 145 | } |
| 146 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 147 | // draw path clipped |
| 148 | canvas->drawPath(path, pathPaint); |
| 149 | canvas->restore(); |
| 150 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 151 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 152 | SkScalar txtX = SkIntToScalar(45); |
| 153 | paint.setColor(gClipAColor); |
| 154 | const char* aTxt = doInvA ? "InvA " : "A "; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 155 | canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint); |
| 156 | txtX += paint.measureText(aTxt, strlen(aTxt)); |
| 157 | paint.setColor(SK_ColorBLACK); |
| 158 | canvas->drawText(gOps[op].fName, strlen(gOps[op].fName), |
| 159 | txtX, SkIntToScalar(220), paint); |
| 160 | txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName)); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 161 | paint.setColor(gClipBColor); |
| 162 | const char* bTxt = doInvB ? "InvB " : "B "; |
| 163 | canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 164 | |
| 165 | canvas->translate(SkIntToScalar(250),0); |
| 166 | } |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 167 | canvas->restore(); |
| 168 | canvas->translate(0, SkIntToScalar(250)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 169 | } |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 170 | |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 171 | if (fDoSaveLayer) { |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 172 | canvas->restore(); |
| 173 | } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 174 | } |
| 175 | private: |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 176 | void drawHairlines(SkCanvas* canvas, const SkPath& path, |
| 177 | const SkPath& clipA, const SkPath& clipB) { |
| 178 | SkPaint paint; |
| 179 | paint.setAntiAlias(true); |
| 180 | paint.setStyle(SkPaint::kStroke_Style); |
| 181 | const SkAlpha fade = 0x33; |
| 182 | |
| 183 | // draw path in hairline |
| 184 | paint.setColor(gPathColor); paint.setAlpha(fade); |
| 185 | canvas->drawPath(path, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 186 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 187 | // draw clips in hair line |
| 188 | paint.setColor(gClipAColor); paint.setAlpha(fade); |
| 189 | canvas->drawPath(clipA, paint); |
| 190 | paint.setColor(gClipBColor); paint.setAlpha(fade); |
| 191 | canvas->drawPath(clipB, paint); |
| 192 | } |
| 193 | |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 194 | bool fDoAAClip; |
| 195 | bool fDoSaveLayer; |
| 196 | bool fInvertDraw; |
| 197 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 198 | typedef GM INHERITED; |
| 199 | }; |
| 200 | |
| 201 | ////////////////////////////////////////////////////////////////////////////// |
| 202 | |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 203 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, false, false)); ) |
| 204 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, false, true)); ) |
| 205 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, true, false)); ) |
| 206 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, true, true)); ) |
| 207 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, false, false)); ) |
| 208 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, false, true)); ) |
| 209 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, true, false)); ) |
| 210 | DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, true, true)); ) |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 211 | |
| 212 | } |