blob: 7bbf4058492bb3be7b9e4b45f906889eb1220a48 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkClipOp.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkAAClip.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "src/core/SkMask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040026#include "tools/fonts/FontToolUtils.h"
robertphillips@google.com67febd92012-05-22 12:14:50 +000027
28namespace skiagm {
29
30static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
31 const SkPaint& paint) {
Ben Wagner6cf423f2023-08-01 16:05:08 -040032 SkMaskBuilder mask;
robertphillips@google.com67febd92012-05-22 12:14:50 +000033 SkBitmap bm;
34
35 clip.copyToMask(&mask);
36
Ben Wagner6cf423f2023-08-01 16:05:08 -040037 SkAutoMaskFreeImage amfi(mask.image());
robertphillips@google.com67febd92012-05-22 12:14:50 +000038
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000039 bm.installMaskPixels(mask);
robertphillips@google.com67febd92012-05-22 12:14:50 +000040
41 // need to copy for deferred drawing test to work
42 SkBitmap bm2;
43
Mike Kleinea3f0142019-03-20 11:12:10 -050044 ToolUtils::copy_to(&bm2, bm.colorType(), bm);
robertphillips@google.com67febd92012-05-22 12:14:50 +000045
Mike Reed607a3822021-01-24 19:49:21 -050046 canvas->drawImage(bm2.asImage(),
47 SK_Scalar1 * mask.fBounds.fLeft,
48 SK_Scalar1 * mask.fBounds.fTop,
49 SkSamplingOptions(),
50 &paint);
robertphillips@google.com67febd92012-05-22 12:14:50 +000051}
52
53//////////////////////////////////////////////////////////////////////////////
54/*
rmistry@google.comd6176b02012-08-23 18:14:13 +000055 * This GM tests anti aliased single operation booleans with SkAAClips,
robertphillips@google.com67febd92012-05-22 12:14:50 +000056 * SkRect and SkPaths.
57 */
58class SimpleClipGM : public GM {
59public:
60 enum SkGeomTypes {
61 kRect_GeomType,
62 kPath_GeomType,
63 kAAClip_GeomType
64 };
65
66 SimpleClipGM(SkGeomTypes geomType)
67 : fGeomType(geomType) {
caryclark63c684a2015-02-25 09:04:04 -080068 }
robertphillips@google.com67febd92012-05-22 12:14:50 +000069
caryclark63c684a2015-02-25 09:04:04 -080070protected:
mtklein36352bf2015-03-25 18:17:31 -070071 void onOnceBeforeDraw() override {
robertphillips@google.com67febd92012-05-22 12:14:50 +000072 // offset the rects a bit so we get anti-aliasing in the rect case
Mike Reed92b33352019-08-24 19:39:13 -040073 fBase.setLTRB(100.65f,
74 100.65f,
75 150.65f,
76 150.65f);
robertphillips@google.com67febd92012-05-22 12:14:50 +000077 fRect = fBase;
78 fRect.inset(5, 5);
79 fRect.offset(25, 25);
80
81 fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
82 fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
Mike Kleind46dce32018-08-16 10:17:03 -040083 INHERITED::setBGColor(0xFFDDDDDD);
robertphillips@google.com67febd92012-05-22 12:14:50 +000084 }
85
Mike Reedc1f77742016-12-09 09:00:50 -050086 void buildRgn(SkAAClip* clip, SkClipOp op) {
Michael Ludwig8c9c1852021-09-14 13:16:42 -040087 clip->setPath(fBasePath, fBasePath.getBounds().roundOut(), true);
robertphillips@google.com67febd92012-05-22 12:14:50 +000088
89 SkAAClip clip2;
Michael Ludwig8c9c1852021-09-14 13:16:42 -040090 clip2.setPath(fRectPath, fRectPath.getBounds().roundOut(), true);
91 clip->op(clip2, op);
robertphillips@google.com67febd92012-05-22 12:14:50 +000092 }
93
94 void drawOrig(SkCanvas* canvas) {
95 SkPaint paint;
rmistry@google.comd6176b02012-08-23 18:14:13 +000096
robertphillips@google.com67febd92012-05-22 12:14:50 +000097 paint.setStyle(SkPaint::kStroke_Style);
98 paint.setColor(SK_ColorBLACK);
rmistry@google.comd6176b02012-08-23 18:14:13 +000099
robertphillips@google.com67febd92012-05-22 12:14:50 +0000100 canvas->drawRect(fBase, paint);
101 canvas->drawRect(fRect, paint);
102 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
Mike Reedc1f77742016-12-09 09:00:50 -0500104 void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000105
106 SkAAClip clip;
107
108 this->buildRgn(&clip, op);
109 this->drawOrig(canvas);
110
111 SkPaint paint;
112 paint.setColor(color);
113 paint_rgn(canvas, clip, paint);
114 }
115
Mike Reedc1f77742016-12-09 09:00:50 -0500116 void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000117
118 this->drawOrig(canvas);
119
120 canvas->save();
121
122 // create the clip mask with the supplied boolean op
123 if (kPath_GeomType == fGeomType) {
124 // path-based case
reed66998382016-09-21 11:15:07 -0700125 canvas->clipPath(fBasePath, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000126 canvas->clipPath(fRectPath, op, true);
127 } else {
128 // rect-based case
reed66998382016-09-21 11:15:07 -0700129 canvas->clipRect(fBase, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000130 canvas->clipRect(fRect, op, true);
131 }
132
133 // draw a rect that will entirely cover the clip mask area
134 SkPaint paint;
135 paint.setColor(color);
136
137 SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90),
138 SkIntToScalar(180), SkIntToScalar(180));
139
140 canvas->drawRect(r, paint);
141
142 canvas->restore();
143 }
144
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000145 SkString getName() const override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000146 SkString str;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147 str.printf("simpleaaclip_%s",
robertphillips@google.com67febd92012-05-22 12:14:50 +0000148 kRect_GeomType == fGeomType ? "rect" :
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149 (kPath_GeomType == fGeomType ? "path" :
robertphillips@google.com67febd92012-05-22 12:14:50 +0000150 "aaclip"));
151 return str;
152 }
153
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000154 SkISize getISize() override { return SkISize::Make(500, 240); }
robertphillips@google.com67febd92012-05-22 12:14:50 +0000155
mtkleinf0599002015-07-13 06:18:39 -0700156 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000157
mtkleindbfd7ab2016-09-01 11:24:54 -0700158 const struct {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000159 SkColor fColor;
160 const char* fName;
Mike Reedc1f77742016-12-09 09:00:50 -0500161 SkClipOp fOp;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000162 } gOps[] = {
Michael Ludwig2f6e2f82021-08-03 13:08:50 -0400163 {SK_ColorBLACK, "Difference", SkClipOp::kDifference},
164 {SK_ColorRED, "Intersect", SkClipOp::kIntersect},
robertphillips@google.com67febd92012-05-22 12:14:50 +0000165 };
166
167 SkPaint textPaint;
Kevin Lubicke836c3a2023-10-20 06:55:35 -0400168 SkFont font(ToolUtils::DefaultPortableTypeface(), 24);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000169 int xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000170
Herb Derbyc37b3862022-06-21 09:49:17 -0400171 for (size_t op = 0; op < std::size(gOps); op++) {
Hal Canarydf2d27e2019-01-08 09:38:02 -0500172 canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000173
174 if (kAAClip_GeomType == fGeomType) {
175 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
176 } else {
177 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
178 }
179
robertphillips@google.com2a021292012-07-17 15:37:15 +0000180 if (xOff >= 400) {
181 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
182 xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000183 } else {
184 canvas->translate(SkIntToScalar(200), 0);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000185 xOff += 200;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000186 }
187 }
188 }
189private:
190
191 SkGeomTypes fGeomType;
192
193 SkRect fBase;
194 SkRect fRect;
195
196 SkPath fBasePath; // fBase as a round rect
197 SkPath fRectPath; // fRect as a round rect
198
John Stiles7571f9e2020-09-02 22:42:33 -0400199 using INHERITED = GM;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000200};
201
202//////////////////////////////////////////////////////////////////////////////
203
204// rects
reed@google.comde1fc472012-12-14 12:59:07 +0000205DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
206DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
207DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000208
John Stilesa6841be2020-08-06 14:11:56 -0400209} // namespace skiagm