blob: 9a8fe61eaa0c1e8756b6caaef20045f7df7efbfa [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"
reedfb8c1fc2015-08-04 18:44:56 -07009#include "SkAAClip.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkCanvas.h"
11#include "SkPath.h"
robertphillips@google.com67febd92012-05-22 12:14:50 +000012
13namespace skiagm {
14
15static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
16 const SkPaint& paint) {
17 SkMask mask;
18 SkBitmap bm;
19
20 clip.copyToMask(&mask);
21
22 SkAutoMaskFreeImage amfi(mask.fImage);
23
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000024 bm.installMaskPixels(mask);
robertphillips@google.com67febd92012-05-22 12:14:50 +000025
26 // need to copy for deferred drawing test to work
27 SkBitmap bm2;
28
commit-bot@chromium.org6285f4f2014-02-20 19:08:07 +000029 bm.deepCopyTo(&bm2);
robertphillips@google.com67febd92012-05-22 12:14:50 +000030
31 canvas->drawBitmap(bm2,
32 SK_Scalar1 * mask.fBounds.fLeft,
33 SK_Scalar1 * mask.fBounds.fTop,
34 &paint);
35}
36
37//////////////////////////////////////////////////////////////////////////////
38/*
rmistry@google.comd6176b02012-08-23 18:14:13 +000039 * This GM tests anti aliased single operation booleans with SkAAClips,
robertphillips@google.com67febd92012-05-22 12:14:50 +000040 * SkRect and SkPaths.
41 */
42class SimpleClipGM : public GM {
43public:
44 enum SkGeomTypes {
45 kRect_GeomType,
46 kPath_GeomType,
47 kAAClip_GeomType
48 };
49
50 SimpleClipGM(SkGeomTypes geomType)
51 : fGeomType(geomType) {
caryclark63c684a2015-02-25 09:04:04 -080052 }
robertphillips@google.com67febd92012-05-22 12:14:50 +000053
caryclark63c684a2015-02-25 09:04:04 -080054protected:
mtklein36352bf2015-03-25 18:17:31 -070055 void onOnceBeforeDraw() override {
robertphillips@google.com67febd92012-05-22 12:14:50 +000056 // offset the rects a bit so we get anti-aliasing in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000057 fBase.set(100.65f,
58 100.65f,
59 150.65f,
60 150.65f);
robertphillips@google.com67febd92012-05-22 12:14:50 +000061 fRect = fBase;
62 fRect.inset(5, 5);
63 fRect.offset(25, 25);
64
65 fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
66 fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
caryclark65cdba62015-06-15 06:51:08 -070067 INHERITED::setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
robertphillips@google.com67febd92012-05-22 12:14:50 +000068 }
69
reed73603f32016-09-20 08:42:38 -070070 void buildRgn(SkAAClip* clip, SkCanvas::ClipOp op) {
halcanary96fcdcc2015-08-27 07:41:13 -070071 clip->setPath(fBasePath, nullptr, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +000072
73 SkAAClip clip2;
halcanary96fcdcc2015-08-27 07:41:13 -070074 clip2.setPath(fRectPath, nullptr, true);
reed73603f32016-09-20 08:42:38 -070075 clip->op(clip2, (SkRegion::Op)op);
robertphillips@google.com67febd92012-05-22 12:14:50 +000076 }
77
78 void drawOrig(SkCanvas* canvas) {
79 SkPaint paint;
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
robertphillips@google.com67febd92012-05-22 12:14:50 +000081 paint.setStyle(SkPaint::kStroke_Style);
82 paint.setColor(SK_ColorBLACK);
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
robertphillips@google.com67febd92012-05-22 12:14:50 +000084 canvas->drawRect(fBase, paint);
85 canvas->drawRect(fRect, paint);
86 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
reed73603f32016-09-20 08:42:38 -070088 void drawRgnOped(SkCanvas* canvas, SkCanvas::ClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +000089
90 SkAAClip clip;
91
92 this->buildRgn(&clip, op);
93 this->drawOrig(canvas);
94
95 SkPaint paint;
96 paint.setColor(color);
97 paint_rgn(canvas, clip, paint);
98 }
99
reed73603f32016-09-20 08:42:38 -0700100 void drawPathsOped(SkCanvas* canvas, SkCanvas::ClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000101
102 this->drawOrig(canvas);
103
104 canvas->save();
105
106 // create the clip mask with the supplied boolean op
107 if (kPath_GeomType == fGeomType) {
108 // path-based case
reed66998382016-09-21 11:15:07 -0700109 canvas->clipPath(fBasePath, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000110 canvas->clipPath(fRectPath, op, true);
111 } else {
112 // rect-based case
reed66998382016-09-21 11:15:07 -0700113 canvas->clipRect(fBase, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000114 canvas->clipRect(fRect, op, true);
115 }
116
117 // draw a rect that will entirely cover the clip mask area
118 SkPaint paint;
119 paint.setColor(color);
120
121 SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90),
122 SkIntToScalar(180), SkIntToScalar(180));
123
124 canvas->drawRect(r, paint);
125
126 canvas->restore();
127 }
128
mtkleinf0599002015-07-13 06:18:39 -0700129 SkString onShortName() override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000130 SkString str;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000131 str.printf("simpleaaclip_%s",
robertphillips@google.com67febd92012-05-22 12:14:50 +0000132 kRect_GeomType == fGeomType ? "rect" :
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133 (kPath_GeomType == fGeomType ? "path" :
robertphillips@google.com67febd92012-05-22 12:14:50 +0000134 "aaclip"));
135 return str;
136 }
137
mtkleinf0599002015-07-13 06:18:39 -0700138 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700139 return SkISize::Make(640, 480);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000140 }
141
mtkleinf0599002015-07-13 06:18:39 -0700142 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000143
mtkleindbfd7ab2016-09-01 11:24:54 -0700144 const struct {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000145 SkColor fColor;
146 const char* fName;
reed73603f32016-09-20 08:42:38 -0700147 SkCanvas::ClipOp fOp;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000148 } gOps[] = {
reed73603f32016-09-20 08:42:38 -0700149 { SK_ColorBLACK, "Difference", SkCanvas::kDifference_Op },
150 { SK_ColorRED, "Intersect", SkCanvas::kIntersect_Op },
151 { sk_tool_utils::color_to_565(0xFF008800), "Union", SkCanvas::kUnion_Op },
152 { SK_ColorGREEN, "Rev Diff", SkCanvas::kReverseDifference_Op },
153 { SK_ColorYELLOW, "Replace", SkCanvas::kReplace_Op },
154 { SK_ColorBLUE, "XOR", SkCanvas::kXOR_Op },
robertphillips@google.com67febd92012-05-22 12:14:50 +0000155 };
156
157 SkPaint textPaint;
158 textPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700159 sk_tool_utils::set_portable_typeface(&textPaint);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000160 textPaint.setTextSize(SK_Scalar1*24);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000161 int xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000162
163 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
164 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000165 SkIntToScalar(75), SkIntToScalar(50),
robertphillips@google.com67febd92012-05-22 12:14:50 +0000166 textPaint);
167
168 if (kAAClip_GeomType == fGeomType) {
169 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
170 } else {
171 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
172 }
173
robertphillips@google.com2a021292012-07-17 15:37:15 +0000174 if (xOff >= 400) {
175 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
176 xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000177 } else {
178 canvas->translate(SkIntToScalar(200), 0);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000179 xOff += 200;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000180 }
181 }
182 }
183private:
184
185 SkGeomTypes fGeomType;
186
187 SkRect fBase;
188 SkRect fRect;
189
190 SkPath fBasePath; // fBase as a round rect
191 SkPath fRectPath; // fRect as a round rect
192
193 typedef GM INHERITED;
194};
195
196//////////////////////////////////////////////////////////////////////////////
197
198// rects
reed@google.comde1fc472012-12-14 12:59:07 +0000199DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
200DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
201DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000202
robertphillips@google.com67febd92012-05-22 12:14:50 +0000203}