blob: 67916caf760b794e1e60066e2bfcab391abb9341 [file] [log] [blame]
egdanieldf795032014-12-17 11:22:37 -08001/*
2 * Copyright 2014 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#include "gm.h"
8#include "SkCanvas.h"
9#include "SkPath.h"
10
11namespace skiagm {
12
13static const SkColor gPathColor = SK_ColorYELLOW;
14
15class ComplexClip3GM : public GM {
16public:
17 ComplexClip3GM(bool doSimpleClipFirst)
18 : fDoSimpleClipFirst(doSimpleClipFirst) {
caryclark65cdba62015-06-15 06:51:08 -070019 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
egdanieldf795032014-12-17 11:22:37 -080020 }
21
22protected:
egdanieldf795032014-12-17 11:22:37 -080023
24 SkString onShortName() {
25 SkString str;
26 str.printf("complexclip3_%s", fDoSimpleClipFirst ? "simple" : "complex");
27 return str;
halcanary9d524f22016-03-29 09:03:52 -070028 }
egdanieldf795032014-12-17 11:22:37 -080029
30 SkISize onISize() { return SkISize::Make(1000, 950); }
31
32 virtual void onDraw(SkCanvas* canvas) {
33 SkPath clipSimple;
34 clipSimple.addCircle(SkIntToScalar(70), SkIntToScalar(50), SkIntToScalar(20));
35
36 SkRect r1 = { 10, 20, 70, 80 };
37 SkPath clipComplex;
38 clipComplex.moveTo(SkIntToScalar(40), SkIntToScalar(50));
39 clipComplex.arcTo(r1, SkIntToScalar(30), SkIntToScalar(300), false);
40 clipComplex.close();
41
42 SkPath* firstClip = &clipSimple;
43 SkPath* secondClip = &clipComplex;
44
45 if (!fDoSimpleClipFirst) {
46 SkTSwap<SkPath*>(firstClip, secondClip);
47 }
48
49 SkPaint paint;
50 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070051 sk_tool_utils::set_portable_typeface(&paint);
egdanieldf795032014-12-17 11:22:37 -080052 paint.setTextSize(SkIntToScalar(20));
53
54 static const struct {
55 SkRegion::Op fOp;
56 const char* fName;
57 } gOps[] = {
58 {SkRegion::kIntersect_Op, "I"},
59 {SkRegion::kDifference_Op, "D" },
60 {SkRegion::kUnion_Op, "U"},
61 {SkRegion::kXOR_Op, "X" },
62 {SkRegion::kReverseDifference_Op, "R"}
63 };
64
65 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
66 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
67
68 SkPaint pathPaint;
69 pathPaint.setAntiAlias(true);
70 pathPaint.setColor(gPathColor);
71
72 for (int invA = 0; invA < 2; ++invA) {
73 for (int aaBits = 0; aaBits < 4; ++aaBits) {
74 canvas->save();
75 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
76 for (int invB = 0; invB < 2; ++invB) {
77 bool doAAA = SkToBool(aaBits & 1);
78 bool doAAB = SkToBool(aaBits & 2);
79 bool doInvA = SkToBool(invA);
80 bool doInvB = SkToBool(invB);
81 canvas->save();
82 // set clip
83 firstClip->setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
84 SkPath::kEvenOdd_FillType);
85 secondClip->setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
86 SkPath::kEvenOdd_FillType);
87 canvas->clipPath(*firstClip, SkRegion::kIntersect_Op, doAAA);
88 canvas->clipPath(*secondClip, gOps[op].fOp, doAAB);
89
90 // draw rect clipped
91 SkRect r = { 0, 0, 100, 100 };
92 canvas->drawRect(r, pathPaint);
93 canvas->restore();
94
95
96 SkScalar txtX = SkIntToScalar(10);
97 paint.setColor(SK_ColorBLACK);
98 SkString str;
99 str.printf("%s%s %s %s%s", doAAA ? "A" : "B",
100 doInvA ? "I" : "N",
101 gOps[op].fName,
102 doAAB ? "A" : "B",
103 doInvB ? "I" : "N");
104
105 canvas->drawText(str.c_str(), strlen(str.c_str()), txtX, SkIntToScalar(130),
106 paint);
107 if (doInvB) {
108 canvas->translate(SkIntToScalar(150),0);
109 } else {
110 canvas->translate(SkIntToScalar(120),0);
111 }
112 }
113 }
114 canvas->restore();
115 canvas->translate(0, SkIntToScalar(150));
116 }
117 }
118 }
119
120private:
121 bool fDoSimpleClipFirst;
122
123 typedef GM INHERITED;
124};
125
126//////////////////////////////////////////////////////////////////////////////
127
128// Simple clip first
129DEF_GM( return new ComplexClip3GM(true); )
130// Complex clip first
131DEF_GM( return new ComplexClip3GM(false); )
132}