2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkPathOps.h"
16 class PathOpsInverseGM : public GM {
22 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);
37 SkColor blend(SkColor one, SkColor two) {
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;
47 void makePaint(SkPaint* paint, SkColor color) {
48 paint->setAntiAlias(true);
49 paint->setStyle(SkPaint::kFill_Style);
50 paint->setColor(color);
53 SkString onShortName() SK_OVERRIDE {
54 return SkString("pathopsinverse");
57 SkISize onISize() SK_OVERRIDE {
58 return SkISize::Make(1200, 900);
61 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
64 for (int oneFill = 0; oneFill <= 1; ++oneFill) {
65 SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType
66 : SkPath::kEvenOdd_FillType;
67 for (int twoFill = 0; twoFill <= 1; ++twoFill) {
68 SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillType
69 : SkPath::kEvenOdd_FillType;
71 one.setFillType(oneF);
72 one.addRect(10, 10, 70, 70);
74 two.setFillType(twoF);
75 two.addRect(40, 40, 100, 100);
77 canvas->translate(0, SkIntToScalar(yPos));
78 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true);
79 canvas->drawPath(one, fOnePaint);
80 canvas->drawPath(one, fOutlinePaint);
81 canvas->drawPath(two, fTwoPaint);
82 canvas->drawPath(two, fOutlinePaint);
85 for (int op = kDifference_PathOp; op <= kReverseDifference_PathOp; ++op) {
87 Op(one, two, (SkPathOp) op, &result);
89 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
90 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true);
91 canvas->drawPath(result, fOpPaint[op]);
92 canvas->drawPath(result, fOutlinePaint);
104 SkPaint fOutlinePaint;
105 SkPaint fOpPaint[kReverseDifference_PathOp - kDifference_PathOp + 1];
106 typedef GM INHERITED;
109 //////////////////////////////////////////////////////////////////////////////
111 static GM* MyFactory(void*) { return new PathOpsInverseGM; }
112 static GMRegistry reg(MyFactory);