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/SkClipOpPriv.h" |
| 25 | #include "src/core/SkMask.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "tools/ToolUtils.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) { |
| 32 | SkMask mask; |
| 33 | SkBitmap bm; |
| 34 | |
| 35 | clip.copyToMask(&mask); |
| 36 | |
| 37 | SkAutoMaskFreeImage amfi(mask.fImage); |
| 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 | |
| 46 | canvas->drawBitmap(bm2, |
| 47 | SK_Scalar1 * mask.fBounds.fLeft, |
| 48 | SK_Scalar1 * mask.fBounds.fTop, |
| 49 | &paint); |
| 50 | } |
| 51 | |
| 52 | ////////////////////////////////////////////////////////////////////////////// |
| 53 | /* |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 54 | * This GM tests anti aliased single operation booleans with SkAAClips, |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 55 | * SkRect and SkPaths. |
| 56 | */ |
| 57 | class SimpleClipGM : public GM { |
| 58 | public: |
| 59 | enum SkGeomTypes { |
| 60 | kRect_GeomType, |
| 61 | kPath_GeomType, |
| 62 | kAAClip_GeomType |
| 63 | }; |
| 64 | |
| 65 | SimpleClipGM(SkGeomTypes geomType) |
| 66 | : fGeomType(geomType) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 67 | } |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 68 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 69 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 70 | void onOnceBeforeDraw() override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 71 | // offset the rects a bit so we get anti-aliasing in the rect case |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 72 | fBase.set(100.65f, |
| 73 | 100.65f, |
| 74 | 150.65f, |
| 75 | 150.65f); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 76 | 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 Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 82 | INHERITED::setBGColor(0xFFDDDDDD); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 85 | void buildRgn(SkAAClip* clip, SkClipOp op) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | clip->setPath(fBasePath, nullptr, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 87 | |
| 88 | SkAAClip clip2; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 89 | clip2.setPath(fRectPath, nullptr, true); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 90 | clip->op(clip2, (SkRegion::Op)op); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void drawOrig(SkCanvas* canvas) { |
| 94 | SkPaint paint; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 96 | paint.setStyle(SkPaint::kStroke_Style); |
| 97 | paint.setColor(SK_ColorBLACK); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 99 | canvas->drawRect(fBase, paint); |
| 100 | canvas->drawRect(fRect, paint); |
| 101 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 102 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 103 | void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 104 | |
| 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 Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 115 | void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 116 | |
| 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 |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 124 | canvas->clipPath(fBasePath, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 125 | canvas->clipPath(fRectPath, op, true); |
| 126 | } else { |
| 127 | // rect-based case |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 128 | canvas->clipRect(fBase, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 129 | 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 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 144 | SkString onShortName() override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 145 | SkString str; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 146 | str.printf("simpleaaclip_%s", |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 147 | kRect_GeomType == fGeomType ? "rect" : |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 148 | (kPath_GeomType == fGeomType ? "path" : |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 149 | "aaclip")); |
| 150 | return str; |
| 151 | } |
| 152 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 153 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 154 | return SkISize::Make(640, 480); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 157 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 158 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 159 | const struct { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 160 | SkColor fColor; |
| 161 | const char* fName; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 162 | SkClipOp fOp; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 163 | } gOps[] = { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 164 | {SK_ColorBLACK, "Difference", kDifference_SkClipOp}, |
| 165 | {SK_ColorRED, "Intersect", kIntersect_SkClipOp}, |
| 166 | {ToolUtils::color_to_565(0xFF008800), "Union", kUnion_SkClipOp}, |
| 167 | {SK_ColorGREEN, "Rev Diff", kReverseDifference_SkClipOp}, |
| 168 | {SK_ColorYELLOW, "Replace", kReplace_SkClipOp}, |
| 169 | {SK_ColorBLUE, "XOR", kXOR_SkClipOp}, |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | SkPaint textPaint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 173 | SkFont font(ToolUtils::create_portable_typeface(), 24); |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 174 | int xOff = 0; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 175 | |
| 176 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) { |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 177 | canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 178 | |
| 179 | if (kAAClip_GeomType == fGeomType) { |
| 180 | this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 181 | } else { |
| 182 | this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 183 | } |
| 184 | |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 185 | if (xOff >= 400) { |
| 186 | canvas->translate(SkIntToScalar(-400), SkIntToScalar(250)); |
| 187 | xOff = 0; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 188 | } else { |
| 189 | canvas->translate(SkIntToScalar(200), 0); |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 190 | xOff += 200; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | private: |
| 195 | |
| 196 | SkGeomTypes fGeomType; |
| 197 | |
| 198 | SkRect fBase; |
| 199 | SkRect fRect; |
| 200 | |
| 201 | SkPath fBasePath; // fBase as a round rect |
| 202 | SkPath fRectPath; // fRect as a round rect |
| 203 | |
| 204 | typedef GM INHERITED; |
| 205 | }; |
| 206 | |
| 207 | ////////////////////////////////////////////////////////////////////////////// |
| 208 | |
| 209 | // rects |
reed@google.com | de1fc47 | 2012-12-14 12:59:07 +0000 | [diff] [blame] | 210 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); ) |
| 211 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); ) |
| 212 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); ) |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 213 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 214 | } |