1 /* 2 * Copyright 2013 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 8 #include "gm.h" 9 #include "SkBitmap.h" 10 #include "SkPath.h" 11 #include "SkPathOps.h" 12 #include "SkRect.h" 13 14 namespace skiagm { 15 16 class PathOpsInverseGM : public GM { 17 public: 18 PathOpsInverseGM() { 19 } 20 21 protected: 22 virtual void onOnceBeforeDraw() SK_OVERRIDE { 23 const unsigned oneColor = 0xFF8080FF; 24 const unsigned twoColor = 0x807F1f1f; 25 SkColor blendColor = blend(oneColor, twoColor); 26 makePaint(&fOnePaint, oneColor); 27 makePaint(&fTwoPaint, twoColor); 28 makePaint(&fOpPaint[kDifference_PathOp], oneColor); 29 makePaint(&fOpPaint[kIntersect_PathOp], blendColor); 30 makePaint(&fOpPaint[kUnion_PathOp], 0xFFc0FFc0); 31 makePaint(&fOpPaint[kReverseDifference_PathOp], twoColor); 32 makePaint(&fOpPaint[kXOR_PathOp], 0xFFa0FFe0); 33 makePaint(&fOutlinePaint, 0xFF000000); 34 fOutlinePaint.setStyle(SkPaint::kStroke_Style); 35 } 36 37 SkColor blend(SkColor one, SkColor two) { 38 SkBitmap temp; 39 temp.allocN32Pixels(1, 1); 40 SkCanvas canvas(temp); 41 canvas.drawColor(one); 42 canvas.drawColor(two); 43 void* pixels = temp.getPixels(); 44 return *(SkColor*) pixels; 45 } 46 47 void makePaint(SkPaint* paint, SkColor color) { 48 paint->setAntiAlias(true); 49 paint->setStyle(SkPaint::kFill_Style); 50 paint->setColor(color); 51 } 52 53 virtual uint32_t onGetFlags() const SK_OVERRIDE { 54 return kSkipTiled_Flag; // Only for 565. 8888 is fine. 55 } 56 57 virtual SkString onShortName() SK_OVERRIDE { 58 return SkString("pathopsinverse"); 59 } 60 61 virtual SkISize onISize() SK_OVERRIDE { 62 return SkISize::Make(1200, 900); 63 } 64 65 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 66 SkPath one, two; 67 int yPos = 0; 68 for (int oneFill = 0; oneFill <= 1; ++oneFill) { 69 SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType 70 : SkPath::kEvenOdd_FillType; 71 for (int twoFill = 0; twoFill <= 1; ++twoFill) { 72 SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillType 73 : SkPath::kEvenOdd_FillType; 74 one.reset(); 75 one.setFillType(oneF); 76 one.addRect(10, 10, 70, 70); 77 two.reset(); 78 two.setFillType(twoF); 79 two.addRect(40, 40, 100, 100); 80 canvas->save(); 81 canvas->translate(0, SkIntToScalar(yPos)); 82 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true); 83 canvas->drawPath(one, fOnePaint); 84 canvas->drawPath(one, fOutlinePaint); 85 canvas->drawPath(two, fTwoPaint); 86 canvas->drawPath(two, fOutlinePaint); 87 canvas->restore(); 88 int xPos = 150; 89 for (int op = kDifference_PathOp; op <= kReverseDifference_PathOp; ++op) { 90 SkPath result; 91 Op(one, two, (SkPathOp) op, &result); 92 canvas->save(); 93 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); 94 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true); 95 canvas->drawPath(result, fOpPaint[op]); 96 canvas->drawPath(result, fOutlinePaint); 97 canvas->restore(); 98 xPos += 150; 99 } 100 yPos += 150; 101 } 102 } 103 } 104 105 private: 106 SkPaint fOnePaint; 107 SkPaint fTwoPaint; 108 SkPaint fOutlinePaint; 109 SkPaint fOpPaint[kReverseDifference_PathOp - kDifference_PathOp + 1]; 110 typedef GM INHERITED; 111 }; 112 113 ////////////////////////////////////////////////////////////////////////////// 114 115 static GM* MyFactory(void*) { return new PathOpsInverseGM; } 116 static GMRegistry reg(MyFactory); 117 118 } 119