blob: 0c88632d4c6dcd29ec64b1f7e9d79a943e7c9c28 [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:
mtklein36352bf2015-03-25 18:17:31 -070022 void onOnceBeforeDraw() override {
caryclark12596012015-07-29 05:27:47 -070023 const unsigned oneColor = sk_tool_utils::color_to_565(0xFF8080FF);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000024 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);
caryclark54359292015-03-26 07:52:43 -070028 makePaint(&fOpPaint[kDifference_SkPathOp], oneColor);
29 makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor);
caryclark12596012015-07-29 05:27:47 -070030 makePaint(&fOpPaint[kUnion_SkPathOp], sk_tool_utils::color_to_565(0xFFc0FFc0));
caryclark54359292015-03-26 07:52:43 -070031 makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor);
caryclark12596012015-07-29 05:27:47 -070032 makePaint(&fOpPaint[kXOR_SkPathOp], sk_tool_utils::color_to_565(0xFFa0FFe0));
caryclark@google.com4eedae62013-04-22 20:12:47 +000033 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;
reed@google.comeb9a46c2014-01-25 16:46:20 +000039 temp.allocN32Pixels(1, 1);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000040 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
mtklein36352bf2015-03-25 18:17:31 -070053 SkString onShortName() override {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000054 return SkString("pathopsinverse");
55 }
56
mtklein36352bf2015-03-25 18:17:31 -070057 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070058 return SkISize::Make(1200, 900);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000059 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 void onDraw(SkCanvas* canvas) override {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000062 SkPath one, two;
63 int yPos = 0;
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;
70 one.reset();
71 one.setFillType(oneF);
72 one.addRect(10, 10, 70, 70);
73 two.reset();
74 two.setFillType(twoF);
75 two.addRect(40, 40, 100, 100);
76 canvas->save();
77 canvas->translate(0, SkIntToScalar(yPos));
78 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true);
caryclark@google.com4eedae62013-04-22 20:12:47 +000079 canvas->drawPath(one, fOnePaint);
80 canvas->drawPath(one, fOutlinePaint);
81 canvas->drawPath(two, fTwoPaint);
82 canvas->drawPath(two, fOutlinePaint);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000083 canvas->restore();
84 int xPos = 150;
caryclark54359292015-03-26 07:52:43 -070085 for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000086 SkPath result;
87 Op(one, two, (SkPathOp) op, &result);
88 canvas->save();
89 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
90 canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, true);
caryclark@google.com4eedae62013-04-22 20:12:47 +000091 canvas->drawPath(result, fOpPaint[op]);
92 canvas->drawPath(result, fOutlinePaint);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000093 canvas->restore();
94 xPos += 150;
95 }
96 yPos += 150;
97 }
98 }
99 }
100
101private:
caryclark@google.com4eedae62013-04-22 20:12:47 +0000102 SkPaint fOnePaint;
103 SkPaint fTwoPaint;
104 SkPaint fOutlinePaint;
caryclark54359292015-03-26 07:52:43 -0700105 SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1];
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000106 typedef GM INHERITED;
107};
108
109//////////////////////////////////////////////////////////////////////////////
110
111static GM* MyFactory(void*) { return new PathOpsInverseGM; }
112static GMRegistry reg(MyFactory);
113
114}