blob: 2fbc8f74bf445d8a257fa0cf62bed2a0c63a5bfc [file] [log] [blame]
caryclark@google.com7dfbb072013-04-22 14:37:05 +00001/*
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
14namespace skiagm {
15
16class PathOpsInverseGM : public GM {
17public:
18 PathOpsInverseGM() {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000019 }
20
21protected:
caryclark@google.com4eedae62013-04-22 20:12:47 +000022 virtual void onOnceBeforeDraw() SK_OVERRIDE {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000023 const unsigned oneColor = 0xFF8080FF;
24 const unsigned twoColor = 0x807F1f1f;
25 SkColor blendColor = blend(oneColor, twoColor);
caryclark@google.com4eedae62013-04-22 20:12:47 +000026 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);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000035 }
36
37 SkColor blend(SkColor one, SkColor two) {
38 SkBitmap temp;
39 temp.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
40 temp.allocPixels();
41 SkCanvas canvas(temp);
42 canvas.drawColor(one);
43 canvas.drawColor(two);
44 void* pixels = temp.getPixels();
45 return *(SkColor*) pixels;
46 }
47
48 void makePaint(SkPaint* paint, SkColor color) {
49 paint->setAntiAlias(true);
50 paint->setStyle(SkPaint::kFill_Style);
51 paint->setColor(color);
52 }
53
54 virtual SkString onShortName() SK_OVERRIDE {
55 return SkString("pathopsinverse");
56 }
57
58 virtual SkISize onISize() SK_OVERRIDE {
59 return make_isize(1200, 900);
60 }
61
62 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
63 SkPath one, two;
64 int yPos = 0;
65 for (int oneFill = 0; oneFill <= 1; ++oneFill) {
66 SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType
67 : SkPath::kEvenOdd_FillType;
68 for (int twoFill = 0; twoFill <= 1; ++twoFill) {
69 SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillType
70 : SkPath::kEvenOdd_FillType;
71 one.reset();
72 one.setFillType(oneF);
73 one.addRect(10, 10, 70, 70);
74 two.reset();
75 two.setFillType(twoF);
76 two.addRect(40, 40, 100, 100);
77 canvas->save();
78 canvas->translate(0, SkIntToScalar(yPos));
79 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true);
caryclark@google.com4eedae62013-04-22 20:12:47 +000080 canvas->drawPath(one, fOnePaint);
81 canvas->drawPath(one, fOutlinePaint);
82 canvas->drawPath(two, fTwoPaint);
83 canvas->drawPath(two, fOutlinePaint);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000084 canvas->restore();
85 int xPos = 150;
86 for (int op = kDifference_PathOp; op <= kReverseDifference_PathOp; ++op) {
87 SkPath result;
88 Op(one, two, (SkPathOp) op, &result);
89 canvas->save();
90 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
91 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true);
caryclark@google.com4eedae62013-04-22 20:12:47 +000092 canvas->drawPath(result, fOpPaint[op]);
93 canvas->drawPath(result, fOutlinePaint);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000094 canvas->restore();
95 xPos += 150;
96 }
97 yPos += 150;
98 }
99 }
100 }
101
102private:
caryclark@google.com4eedae62013-04-22 20:12:47 +0000103 SkPaint fOnePaint;
104 SkPaint fTwoPaint;
105 SkPaint fOutlinePaint;
106 SkPaint fOpPaint[kReverseDifference_PathOp - kDifference_PathOp + 1];
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000107 typedef GM INHERITED;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
112static GM* MyFactory(void*) { return new PathOpsInverseGM; }
113static GMRegistry reg(MyFactory);
114
115}