blob: 5e4067f966effd1fc318bec96ad53109bcb48fca [file] [log] [blame]
robertphillips@google.com67febd92012-05-22 12:14:50 +00001/*
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
robertphillips@google.com67febd92012-05-22 12:14:50 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reedfb8c1fc2015-08-04 18:44:56 -070010#include "SkAAClip.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkCanvas.h"
12#include "SkPath.h"
robertphillips@google.com67febd92012-05-22 12:14:50 +000013
14namespace skiagm {
15
16static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
17 const SkPaint& paint) {
18 SkMask mask;
19 SkBitmap bm;
20
21 clip.copyToMask(&mask);
22
23 SkAutoMaskFreeImage amfi(mask.fImage);
24
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000025 bm.installMaskPixels(mask);
robertphillips@google.com67febd92012-05-22 12:14:50 +000026
27 // need to copy for deferred drawing test to work
28 SkBitmap bm2;
29
Matt Sarett68b8e3d2017-04-28 11:15:22 -040030 sk_tool_utils::copy_to(&bm2, bm.colorType(), bm);
robertphillips@google.com67febd92012-05-22 12:14:50 +000031
32 canvas->drawBitmap(bm2,
33 SK_Scalar1 * mask.fBounds.fLeft,
34 SK_Scalar1 * mask.fBounds.fTop,
35 &paint);
36}
37
38//////////////////////////////////////////////////////////////////////////////
39/*
rmistry@google.comd6176b02012-08-23 18:14:13 +000040 * This GM tests anti aliased single operation booleans with SkAAClips,
robertphillips@google.com67febd92012-05-22 12:14:50 +000041 * SkRect and SkPaths.
42 */
43class SimpleClipGM : public GM {
44public:
45 enum SkGeomTypes {
46 kRect_GeomType,
47 kPath_GeomType,
48 kAAClip_GeomType
49 };
50
51 SimpleClipGM(SkGeomTypes geomType)
52 : fGeomType(geomType) {
caryclark63c684a2015-02-25 09:04:04 -080053 }
robertphillips@google.com67febd92012-05-22 12:14:50 +000054
caryclark63c684a2015-02-25 09:04:04 -080055protected:
mtklein36352bf2015-03-25 18:17:31 -070056 void onOnceBeforeDraw() override {
robertphillips@google.com67febd92012-05-22 12:14:50 +000057 // offset the rects a bit so we get anti-aliasing in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000058 fBase.set(100.65f,
59 100.65f,
60 150.65f,
61 150.65f);
robertphillips@google.com67febd92012-05-22 12:14:50 +000062 fRect = fBase;
63 fRect.inset(5, 5);
64 fRect.offset(25, 25);
65
66 fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
67 fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
caryclark65cdba62015-06-15 06:51:08 -070068 INHERITED::setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
robertphillips@google.com67febd92012-05-22 12:14:50 +000069 }
70
Mike Reedc1f77742016-12-09 09:00:50 -050071 void buildRgn(SkAAClip* clip, SkClipOp op) {
halcanary96fcdcc2015-08-27 07:41:13 -070072 clip->setPath(fBasePath, nullptr, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +000073
74 SkAAClip clip2;
halcanary96fcdcc2015-08-27 07:41:13 -070075 clip2.setPath(fRectPath, nullptr, true);
reed73603f32016-09-20 08:42:38 -070076 clip->op(clip2, (SkRegion::Op)op);
robertphillips@google.com67febd92012-05-22 12:14:50 +000077 }
78
79 void drawOrig(SkCanvas* canvas) {
80 SkPaint paint;
rmistry@google.comd6176b02012-08-23 18:14:13 +000081
robertphillips@google.com67febd92012-05-22 12:14:50 +000082 paint.setStyle(SkPaint::kStroke_Style);
83 paint.setColor(SK_ColorBLACK);
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
robertphillips@google.com67febd92012-05-22 12:14:50 +000085 canvas->drawRect(fBase, paint);
86 canvas->drawRect(fRect, paint);
87 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
Mike Reedc1f77742016-12-09 09:00:50 -050089 void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +000090
91 SkAAClip clip;
92
93 this->buildRgn(&clip, op);
94 this->drawOrig(canvas);
95
96 SkPaint paint;
97 paint.setColor(color);
98 paint_rgn(canvas, clip, paint);
99 }
100
Mike Reedc1f77742016-12-09 09:00:50 -0500101 void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000102
103 this->drawOrig(canvas);
104
105 canvas->save();
106
107 // create the clip mask with the supplied boolean op
108 if (kPath_GeomType == fGeomType) {
109 // path-based case
reed66998382016-09-21 11:15:07 -0700110 canvas->clipPath(fBasePath, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000111 canvas->clipPath(fRectPath, op, true);
112 } else {
113 // rect-based case
reed66998382016-09-21 11:15:07 -0700114 canvas->clipRect(fBase, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000115 canvas->clipRect(fRect, op, true);
116 }
117
118 // draw a rect that will entirely cover the clip mask area
119 SkPaint paint;
120 paint.setColor(color);
121
122 SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90),
123 SkIntToScalar(180), SkIntToScalar(180));
124
125 canvas->drawRect(r, paint);
126
127 canvas->restore();
128 }
129
mtkleinf0599002015-07-13 06:18:39 -0700130 SkString onShortName() override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000131 SkString str;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132 str.printf("simpleaaclip_%s",
robertphillips@google.com67febd92012-05-22 12:14:50 +0000133 kRect_GeomType == fGeomType ? "rect" :
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134 (kPath_GeomType == fGeomType ? "path" :
robertphillips@google.com67febd92012-05-22 12:14:50 +0000135 "aaclip"));
136 return str;
137 }
138
mtkleinf0599002015-07-13 06:18:39 -0700139 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700140 return SkISize::Make(640, 480);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000141 }
142
mtkleinf0599002015-07-13 06:18:39 -0700143 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000144
mtkleindbfd7ab2016-09-01 11:24:54 -0700145 const struct {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000146 SkColor fColor;
147 const char* fName;
Mike Reedc1f77742016-12-09 09:00:50 -0500148 SkClipOp fOp;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000149 } gOps[] = {
Mike Reedc1f77742016-12-09 09:00:50 -0500150 { 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 },
robertphillips@google.com67febd92012-05-22 12:14:50 +0000156 };
157
158 SkPaint textPaint;
159 textPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700160 sk_tool_utils::set_portable_typeface(&textPaint);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000161 textPaint.setTextSize(SK_Scalar1*24);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000162 int xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000163
164 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
Cary Clark2a475ea2017-04-28 15:35:12 -0400165 canvas->drawString(gOps[op].fName,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166 SkIntToScalar(75), SkIntToScalar(50),
robertphillips@google.com67febd92012-05-22 12:14:50 +0000167 textPaint);
168
169 if (kAAClip_GeomType == fGeomType) {
170 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
171 } else {
172 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
173 }
174
robertphillips@google.com2a021292012-07-17 15:37:15 +0000175 if (xOff >= 400) {
176 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
177 xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000178 } else {
179 canvas->translate(SkIntToScalar(200), 0);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000180 xOff += 200;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000181 }
182 }
183 }
184private:
185
186 SkGeomTypes fGeomType;
187
188 SkRect fBase;
189 SkRect fRect;
190
191 SkPath fBasePath; // fBase as a round rect
192 SkPath fRectPath; // fRect as a round rect
193
194 typedef GM INHERITED;
195};
196
197//////////////////////////////////////////////////////////////////////////////
198
199// rects
reed@google.comde1fc472012-12-14 12:59:07 +0000200DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
201DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
202DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000203
robertphillips@google.com67febd92012-05-22 12:14:50 +0000204}