1 /*
2 * Copyright 2012 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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkClipOp.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPath.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRegion.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "src/core/SkAAClip.h"
24 #include "src/core/SkClipOpPriv.h"
25 #include "src/core/SkMask.h"
26 #include "tools/ToolUtils.h"
27
28 namespace skiagm {
29
paint_rgn(SkCanvas * canvas,const SkAAClip & clip,const SkPaint & paint)30 static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
31 const SkPaint& paint) {
32 SkMask mask;
33 SkBitmap bm;
34
35 clip.copyToMask(&mask);
36
37 SkAutoMaskFreeImage amfi(mask.fImage);
38
39 bm.installMaskPixels(mask);
40
41 // need to copy for deferred drawing test to work
42 SkBitmap bm2;
43
44 ToolUtils::copy_to(&bm2, bm.colorType(), bm);
45
46 canvas->drawBitmap(bm2,
47 SK_Scalar1 * mask.fBounds.fLeft,
48 SK_Scalar1 * mask.fBounds.fTop,
49 &paint);
50 }
51
52 //////////////////////////////////////////////////////////////////////////////
53 /*
54 * This GM tests anti aliased single operation booleans with SkAAClips,
55 * SkRect and SkPaths.
56 */
57 class SimpleClipGM : public GM {
58 public:
59 enum SkGeomTypes {
60 kRect_GeomType,
61 kPath_GeomType,
62 kAAClip_GeomType
63 };
64
SimpleClipGM(SkGeomTypes geomType)65 SimpleClipGM(SkGeomTypes geomType)
66 : fGeomType(geomType) {
67 }
68
69 protected:
onOnceBeforeDraw()70 void onOnceBeforeDraw() override {
71 // offset the rects a bit so we get anti-aliasing in the rect case
72 fBase.set(100.65f,
73 100.65f,
74 150.65f,
75 150.65f);
76 fRect = fBase;
77 fRect.inset(5, 5);
78 fRect.offset(25, 25);
79
80 fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
81 fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
82 INHERITED::setBGColor(0xFFDDDDDD);
83 }
84
buildRgn(SkAAClip * clip,SkClipOp op)85 void buildRgn(SkAAClip* clip, SkClipOp op) {
86 clip->setPath(fBasePath, nullptr, true);
87
88 SkAAClip clip2;
89 clip2.setPath(fRectPath, nullptr, true);
90 clip->op(clip2, (SkRegion::Op)op);
91 }
92
drawOrig(SkCanvas * canvas)93 void drawOrig(SkCanvas* canvas) {
94 SkPaint paint;
95
96 paint.setStyle(SkPaint::kStroke_Style);
97 paint.setColor(SK_ColorBLACK);
98
99 canvas->drawRect(fBase, paint);
100 canvas->drawRect(fRect, paint);
101 }
102
drawRgnOped(SkCanvas * canvas,SkClipOp op,SkColor color)103 void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
104
105 SkAAClip clip;
106
107 this->buildRgn(&clip, op);
108 this->drawOrig(canvas);
109
110 SkPaint paint;
111 paint.setColor(color);
112 paint_rgn(canvas, clip, paint);
113 }
114
drawPathsOped(SkCanvas * canvas,SkClipOp op,SkColor color)115 void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
116
117 this->drawOrig(canvas);
118
119 canvas->save();
120
121 // create the clip mask with the supplied boolean op
122 if (kPath_GeomType == fGeomType) {
123 // path-based case
124 canvas->clipPath(fBasePath, true);
125 canvas->clipPath(fRectPath, op, true);
126 } else {
127 // rect-based case
128 canvas->clipRect(fBase, true);
129 canvas->clipRect(fRect, op, true);
130 }
131
132 // draw a rect that will entirely cover the clip mask area
133 SkPaint paint;
134 paint.setColor(color);
135
136 SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90),
137 SkIntToScalar(180), SkIntToScalar(180));
138
139 canvas->drawRect(r, paint);
140
141 canvas->restore();
142 }
143
onShortName()144 SkString onShortName() override {
145 SkString str;
146 str.printf("simpleaaclip_%s",
147 kRect_GeomType == fGeomType ? "rect" :
148 (kPath_GeomType == fGeomType ? "path" :
149 "aaclip"));
150 return str;
151 }
152
onISize()153 SkISize onISize() override {
154 return SkISize::Make(640, 480);
155 }
156
onDraw(SkCanvas * canvas)157 void onDraw(SkCanvas* canvas) override {
158
159 const struct {
160 SkColor fColor;
161 const char* fName;
162 SkClipOp fOp;
163 } gOps[] = {
164 {SK_ColorBLACK, "Difference", kDifference_SkClipOp},
165 {SK_ColorRED, "Intersect", kIntersect_SkClipOp},
166 {ToolUtils::color_to_565(0xFF008800), "Union", kUnion_SkClipOp},
167 {SK_ColorGREEN, "Rev Diff", kReverseDifference_SkClipOp},
168 {SK_ColorYELLOW, "Replace", kReplace_SkClipOp},
169 {SK_ColorBLUE, "XOR", kXOR_SkClipOp},
170 };
171
172 SkPaint textPaint;
173 SkFont font(ToolUtils::create_portable_typeface(), 24);
174 int xOff = 0;
175
176 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
177 canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint);
178
179 if (kAAClip_GeomType == fGeomType) {
180 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
181 } else {
182 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
183 }
184
185 if (xOff >= 400) {
186 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
187 xOff = 0;
188 } else {
189 canvas->translate(SkIntToScalar(200), 0);
190 xOff += 200;
191 }
192 }
193 }
194 private:
195
196 SkGeomTypes fGeomType;
197
198 SkRect fBase;
199 SkRect fRect;
200
201 SkPath fBasePath; // fBase as a round rect
202 SkPath fRectPath; // fRect as a round rect
203
204 typedef GM INHERITED;
205 };
206
207 //////////////////////////////////////////////////////////////////////////////
208
209 // rects
210 DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
211 DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
212 DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
213
214 }
215