blob: 9267b7c795cfd7f731cecbfac2706b2912ed00b2 [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/SkClipOpPriv.h"
25#include "src/core/SkMask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "tools/ToolUtils.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) {
32 SkMask mask;
33 SkBitmap bm;
34
35 clip.copyToMask(&mask);
36
37 SkAutoMaskFreeImage amfi(mask.fImage);
38
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
46 canvas->drawBitmap(bm2,
47 SK_Scalar1 * mask.fBounds.fLeft,
48 SK_Scalar1 * mask.fBounds.fTop,
49 &paint);
50}
51
52//////////////////////////////////////////////////////////////////////////////
53/*
rmistry@google.comd6176b02012-08-23 18:14:13 +000054 * This GM tests anti aliased single operation booleans with SkAAClips,
robertphillips@google.com67febd92012-05-22 12:14:50 +000055 * SkRect and SkPaths.
56 */
57class SimpleClipGM : public GM {
58public:
59 enum SkGeomTypes {
60 kRect_GeomType,
61 kPath_GeomType,
62 kAAClip_GeomType
63 };
64
65 SimpleClipGM(SkGeomTypes geomType)
66 : fGeomType(geomType) {
caryclark63c684a2015-02-25 09:04:04 -080067 }
robertphillips@google.com67febd92012-05-22 12:14:50 +000068
caryclark63c684a2015-02-25 09:04:04 -080069protected:
mtklein36352bf2015-03-25 18:17:31 -070070 void onOnceBeforeDraw() override {
robertphillips@google.com67febd92012-05-22 12:14:50 +000071 // offset the rects a bit so we get anti-aliasing in the rect case
Mike Reed92b33352019-08-24 19:39:13 -040072 fBase.setLTRB(100.65f,
73 100.65f,
74 150.65f,
75 150.65f);
robertphillips@google.com67febd92012-05-22 12:14:50 +000076 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));
Mike Kleind46dce32018-08-16 10:17:03 -040082 INHERITED::setBGColor(0xFFDDDDDD);
robertphillips@google.com67febd92012-05-22 12:14:50 +000083 }
84
Mike Reedc1f77742016-12-09 09:00:50 -050085 void buildRgn(SkAAClip* clip, SkClipOp op) {
halcanary96fcdcc2015-08-27 07:41:13 -070086 clip->setPath(fBasePath, nullptr, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +000087
88 SkAAClip clip2;
halcanary96fcdcc2015-08-27 07:41:13 -070089 clip2.setPath(fRectPath, nullptr, true);
reed73603f32016-09-20 08:42:38 -070090 clip->op(clip2, (SkRegion::Op)op);
robertphillips@google.com67febd92012-05-22 12:14:50 +000091 }
92
93 void drawOrig(SkCanvas* canvas) {
94 SkPaint paint;
rmistry@google.comd6176b02012-08-23 18:14:13 +000095
robertphillips@google.com67febd92012-05-22 12:14:50 +000096 paint.setStyle(SkPaint::kStroke_Style);
97 paint.setColor(SK_ColorBLACK);
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
robertphillips@google.com67febd92012-05-22 12:14:50 +000099 canvas->drawRect(fBase, paint);
100 canvas->drawRect(fRect, paint);
101 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
Mike Reedc1f77742016-12-09 09:00:50 -0500103 void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000104
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
Mike Reedc1f77742016-12-09 09:00:50 -0500115 void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000116
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
reed66998382016-09-21 11:15:07 -0700124 canvas->clipPath(fBasePath, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000125 canvas->clipPath(fRectPath, op, true);
126 } else {
127 // rect-based case
reed66998382016-09-21 11:15:07 -0700128 canvas->clipRect(fBase, true);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000129 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
mtkleinf0599002015-07-13 06:18:39 -0700144 SkString onShortName() override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000145 SkString str;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000146 str.printf("simpleaaclip_%s",
robertphillips@google.com67febd92012-05-22 12:14:50 +0000147 kRect_GeomType == fGeomType ? "rect" :
rmistry@google.comd6176b02012-08-23 18:14:13 +0000148 (kPath_GeomType == fGeomType ? "path" :
robertphillips@google.com67febd92012-05-22 12:14:50 +0000149 "aaclip"));
150 return str;
151 }
152
mtkleinf0599002015-07-13 06:18:39 -0700153 SkISize onISize() override {
Michael Ludwig9689e392020-05-05 10:56:42 -0400154 return SkISize::Make(500, 240);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000155 }
156
mtkleinf0599002015-07-13 06:18:39 -0700157 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000158
mtkleindbfd7ab2016-09-01 11:24:54 -0700159 const struct {
robertphillips@google.com67febd92012-05-22 12:14:50 +0000160 SkColor fColor;
161 const char* fName;
Mike Reedc1f77742016-12-09 09:00:50 -0500162 SkClipOp fOp;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000163 } gOps[] = {
Mike Kleinea3f0142019-03-20 11:12:10 -0500164 {SK_ColorBLACK, "Difference", kDifference_SkClipOp},
165 {SK_ColorRED, "Intersect", kIntersect_SkClipOp},
robertphillips@google.com67febd92012-05-22 12:14:50 +0000166 };
167
168 SkPaint textPaint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500169 SkFont font(ToolUtils::create_portable_typeface(), 24);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000170 int xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000171
172 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
Hal Canarydf2d27e2019-01-08 09:38:02 -0500173 canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint);
robertphillips@google.com67febd92012-05-22 12:14:50 +0000174
175 if (kAAClip_GeomType == fGeomType) {
176 this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
177 } else {
178 this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
179 }
180
robertphillips@google.com2a021292012-07-17 15:37:15 +0000181 if (xOff >= 400) {
182 canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
183 xOff = 0;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000184 } else {
185 canvas->translate(SkIntToScalar(200), 0);
robertphillips@google.com2a021292012-07-17 15:37:15 +0000186 xOff += 200;
robertphillips@google.com67febd92012-05-22 12:14:50 +0000187 }
188 }
189 }
190private:
191
192 SkGeomTypes fGeomType;
193
194 SkRect fBase;
195 SkRect fRect;
196
197 SkPath fBasePath; // fBase as a round rect
198 SkPath fRectPath; // fRect as a round rect
199
200 typedef GM INHERITED;
201};
202
203//////////////////////////////////////////////////////////////////////////////
204
205// rects
reed@google.comde1fc472012-12-14 12:59:07 +0000206DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
207DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
208DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +0000209
robertphillips@google.com67febd92012-05-22 12:14:50 +0000210}