2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
16 static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
17 const SkPaint& paint) {
21 clip.copyToMask(&mask);
23 SkAutoMaskFreeImage amfi(mask.fImage);
25 bm.installMaskPixels(mask);
27 // need to copy for deferred drawing test to work
30 sk_tool_utils::copy_to(&bm2, bm.colorType(), bm);
32 canvas->drawBitmap(bm2,
33 SK_Scalar1 * mask.fBounds.fLeft,
34 SK_Scalar1 * mask.fBounds.fTop,
38 //////////////////////////////////////////////////////////////////////////////
40 * This GM tests anti aliased single operation booleans with SkAAClips,
43 class SimpleClipGM : public GM {
51 SimpleClipGM(SkGeomTypes geomType)
52 : fGeomType(geomType) {
56 void onOnceBeforeDraw() override {
57 // offset the rects a bit so we get anti-aliasing in the rect case
66 fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
67 fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
68 INHERITED::setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
71 void buildRgn(SkAAClip* clip, SkClipOp op) {
72 clip->setPath(fBasePath, nullptr, true);
75 clip2.setPath(fRectPath, nullptr, true);
76 clip->op(clip2, (SkRegion::Op)op);
79 void drawOrig(SkCanvas* canvas) {
82 paint.setStyle(SkPaint::kStroke_Style);
83 paint.setColor(SK_ColorBLACK);
85 canvas->drawRect(fBase, paint);
86 canvas->drawRect(fRect, paint);
89 void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
93 this->buildRgn(&clip, op);
94 this->drawOrig(canvas);
97 paint.setColor(color);
98 paint_rgn(canvas, clip, paint);
101 void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
103 this->drawOrig(canvas);
107 // create the clip mask with the supplied boolean op
108 if (kPath_GeomType == fGeomType) {
110 canvas->clipPath(fBasePath, true);
111 canvas->clipPath(fRectPath, op, true);
114 canvas->clipRect(fBase, true);
115 canvas->clipRect(fRect, op, true);
118 // draw a rect that will entirely cover the clip mask area
120 paint.setColor(color);
122 SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90),
123 SkIntToScalar(180), SkIntToScalar(180));
125 canvas->drawRect(r, paint);
130 SkString onShortName() override {
132 str.printf("simpleaaclip_%s",
133 kRect_GeomType == fGeomType ? "rect" :
134 (kPath_GeomType == fGeomType ? "path" :
139 SkISize onISize() override {
140 return SkISize::Make(640, 480);
143 void onDraw(SkCanvas* canvas) override {
150 { SK_ColorBLACK, "Difference", kDifference_SkClipOp },
151 { SK_ColorRED, "Intersect", kIntersect_SkClipOp },
152 { sk_tool_utils::color_to_565(0xFF008800), "Union", kUnion_SkClipOp },
153 { SK_ColorGREEN, "Rev Diff", kReverseDifference_SkClipOp },
154 { SK_ColorYELLOW, "Replace", kReplace_SkClipOp },
155 { SK_ColorBLUE, "XOR", kXOR_SkClipOp },
159 textPaint.setAntiAlias(true);
160 sk_tool_utils::set_portable_typeface(&textPaint);
161 textPaint.setTextSize(SK_Scalar1*24);
164 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
165 canvas->drawString(gOps[op].fName,
166 SkIntToScalar(75), SkIntToScalar(50),
169 if (kAAClip_GeomType == fGeomType) {
170 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
172 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
176 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
179 canvas->translate(SkIntToScalar(200), 0);
186 SkGeomTypes fGeomType;
191 SkPath fBasePath; // fBase as a round rect
192 SkPath fRectPath; // fRect as a round rect
194 typedef GM INHERITED;
197 //////////////////////////////////////////////////////////////////////////////
200 DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
201 DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
202 DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )