robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkClipOp.h" |
| 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 16 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/core/SkAAClip.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include "src/core/SkMask.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 26 | #include "tools/fonts/FontToolUtils.h" |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 27 | |
| 28 | namespace skiagm { |
| 29 | |
| 30 | static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip, |
| 31 | const SkPaint& paint) { |
Ben Wagner | 6cf423f | 2023-08-01 16:05:08 -0400 | [diff] [blame] | 32 | SkMaskBuilder mask; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 33 | SkBitmap bm; |
| 34 | |
| 35 | clip.copyToMask(&mask); |
| 36 | |
Ben Wagner | 6cf423f | 2023-08-01 16:05:08 -0400 | [diff] [blame] | 37 | SkAutoMaskFreeImage amfi(mask.image()); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 39 | bm.installMaskPixels(mask); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 40 | |
| 41 | // need to copy for deferred drawing test to work |
| 42 | SkBitmap bm2; |
| 43 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 44 | ToolUtils::copy_to(&bm2, bm.colorType(), bm); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 45 | |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame] | 46 | canvas->drawImage(bm2.asImage(), |
| 47 | SK_Scalar1 * mask.fBounds.fLeft, |
| 48 | SK_Scalar1 * mask.fBounds.fTop, |
| 49 | SkSamplingOptions(), |
| 50 | &paint); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ////////////////////////////////////////////////////////////////////////////// |
| 54 | /* |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | * This GM tests anti aliased single operation booleans with SkAAClips, |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 56 | * SkRect and SkPaths. |
| 57 | */ |
| 58 | class SimpleClipGM : public GM { |
| 59 | public: |
| 60 | enum SkGeomTypes { |
| 61 | kRect_GeomType, |
| 62 | kPath_GeomType, |
| 63 | kAAClip_GeomType |
| 64 | }; |
| 65 | |
| 66 | SimpleClipGM(SkGeomTypes geomType) |
| 67 | : fGeomType(geomType) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 68 | } |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 69 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 70 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | void onOnceBeforeDraw() override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 72 | // offset the rects a bit so we get anti-aliasing in the rect case |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 73 | fBase.setLTRB(100.65f, |
| 74 | 100.65f, |
| 75 | 150.65f, |
| 76 | 150.65f); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 77 | 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 Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 83 | INHERITED::setBGColor(0xFFDDDDDD); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 86 | void buildRgn(SkAAClip* clip, SkClipOp op) { |
Michael Ludwig | 8c9c185 | 2021-09-14 13:16:42 -0400 | [diff] [blame] | 87 | clip->setPath(fBasePath, fBasePath.getBounds().roundOut(), true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 88 | |
| 89 | SkAAClip clip2; |
Michael Ludwig | 8c9c185 | 2021-09-14 13:16:42 -0400 | [diff] [blame] | 90 | clip2.setPath(fRectPath, fRectPath.getBounds().roundOut(), true); |
| 91 | clip->op(clip2, op); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void drawOrig(SkCanvas* canvas) { |
| 95 | SkPaint paint; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 96 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 97 | paint.setStyle(SkPaint::kStroke_Style); |
| 98 | paint.setColor(SK_ColorBLACK); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 99 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 100 | canvas->drawRect(fBase, paint); |
| 101 | canvas->drawRect(fRect, paint); |
| 102 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 103 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 104 | void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 105 | |
| 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 Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 116 | void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 117 | |
| 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 |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 125 | canvas->clipPath(fBasePath, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 126 | canvas->clipPath(fRectPath, op, true); |
| 127 | } else { |
| 128 | // rect-based case |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 129 | canvas->clipRect(fBase, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 130 | 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 Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 145 | SkString getName() const override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 146 | SkString str; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 147 | str.printf("simpleaaclip_%s", |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 148 | kRect_GeomType == fGeomType ? "rect" : |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 149 | (kPath_GeomType == fGeomType ? "path" : |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 150 | "aaclip")); |
| 151 | return str; |
| 152 | } |
| 153 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 154 | SkISize getISize() override { return SkISize::Make(500, 240); } |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 155 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 156 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 157 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 158 | const struct { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 159 | SkColor fColor; |
| 160 | const char* fName; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 161 | SkClipOp fOp; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 162 | } gOps[] = { |
Michael Ludwig | 2f6e2f8 | 2021-08-03 13:08:50 -0400 | [diff] [blame] | 163 | {SK_ColorBLACK, "Difference", SkClipOp::kDifference}, |
| 164 | {SK_ColorRED, "Intersect", SkClipOp::kIntersect}, |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | SkPaint textPaint; |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 168 | SkFont font(ToolUtils::DefaultPortableTypeface(), 24); |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 169 | int xOff = 0; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 170 | |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 171 | for (size_t op = 0; op < std::size(gOps); op++) { |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 172 | canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 173 | |
| 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.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 180 | if (xOff >= 400) { |
| 181 | canvas->translate(SkIntToScalar(-400), SkIntToScalar(250)); |
| 182 | xOff = 0; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 183 | } else { |
| 184 | canvas->translate(SkIntToScalar(200), 0); |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 185 | xOff += 200; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | private: |
| 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 Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 199 | using INHERITED = GM; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | ////////////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | // rects |
reed@google.com | de1fc47 | 2012-12-14 12:59:07 +0000 | [diff] [blame] | 205 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); ) |
| 206 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); ) |
| 207 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); ) |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 208 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 209 | } // namespace skiagm |