blob: 3a3d970534925f7087a9b07eeb7a5eb0e5b9c307 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
caryclark@google.com7dfbb072013-04-22 14:37:05 +000010#include "SkBitmap.h"
11#include "SkPath.h"
12#include "SkPathOps.h"
13#include "SkRect.h"
14
15namespace skiagm {
16
17class PathOpsInverseGM : public GM {
18public:
19 PathOpsInverseGM() {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000020 }
21
22protected:
mtklein36352bf2015-03-25 18:17:31 -070023 void onOnceBeforeDraw() override {
caryclark12596012015-07-29 05:27:47 -070024 const unsigned oneColor = sk_tool_utils::color_to_565(0xFF8080FF);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000025 const unsigned twoColor = 0x807F1f1f;
26 SkColor blendColor = blend(oneColor, twoColor);
caryclark@google.com4eedae62013-04-22 20:12:47 +000027 makePaint(&fOnePaint, oneColor);
28 makePaint(&fTwoPaint, twoColor);
caryclark54359292015-03-26 07:52:43 -070029 makePaint(&fOpPaint[kDifference_SkPathOp], oneColor);
30 makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor);
caryclark12596012015-07-29 05:27:47 -070031 makePaint(&fOpPaint[kUnion_SkPathOp], sk_tool_utils::color_to_565(0xFFc0FFc0));
caryclark54359292015-03-26 07:52:43 -070032 makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor);
caryclark12596012015-07-29 05:27:47 -070033 makePaint(&fOpPaint[kXOR_SkPathOp], sk_tool_utils::color_to_565(0xFFa0FFe0));
caryclark@google.com4eedae62013-04-22 20:12:47 +000034 makePaint(&fOutlinePaint, 0xFF000000);
35 fOutlinePaint.setStyle(SkPaint::kStroke_Style);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000036 }
37
38 SkColor blend(SkColor one, SkColor two) {
39 SkBitmap temp;
reed@google.comeb9a46c2014-01-25 16:46:20 +000040 temp.allocN32Pixels(1, 1);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000041 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
mtklein36352bf2015-03-25 18:17:31 -070054 SkString onShortName() override {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000055 return SkString("pathopsinverse");
56 }
57
mtklein36352bf2015-03-25 18:17:31 -070058 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070059 return SkISize::Make(1200, 900);
caryclark@google.com7dfbb072013-04-22 14:37:05 +000060 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 void onDraw(SkCanvas* canvas) override {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000063 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));
reed66998382016-09-21 11:15:07 -070079 canvas->clipRect(SkRect::MakeWH(110, 110), 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;
caryclark54359292015-03-26 07:52:43 -070086 for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
caryclark@google.com7dfbb072013-04-22 14:37:05 +000087 SkPath result;
88 Op(one, two, (SkPathOp) op, &result);
89 canvas->save();
90 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
reed66998382016-09-21 11:15:07 -070091 canvas->clipRect(SkRect::MakeWH(110, 110), 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;
caryclark54359292015-03-26 07:52:43 -0700106 SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 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}