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 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 10 | #include "SkAAClip.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkPath.h" |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip, |
| 17 | const SkPaint& paint) { |
| 18 | SkMask mask; |
| 19 | SkBitmap bm; |
| 20 | |
| 21 | clip.copyToMask(&mask); |
| 22 | |
| 23 | SkAutoMaskFreeImage amfi(mask.fImage); |
| 24 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 25 | bm.installMaskPixels(mask); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 26 | |
| 27 | // need to copy for deferred drawing test to work |
| 28 | SkBitmap bm2; |
| 29 | |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 30 | sk_tool_utils::copy_to(&bm2, bm.colorType(), bm); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 31 | |
| 32 | canvas->drawBitmap(bm2, |
| 33 | SK_Scalar1 * mask.fBounds.fLeft, |
| 34 | SK_Scalar1 * mask.fBounds.fTop, |
| 35 | &paint); |
| 36 | } |
| 37 | |
| 38 | ////////////////////////////////////////////////////////////////////////////// |
| 39 | /* |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 40 | * This GM tests anti aliased single operation booleans with SkAAClips, |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 41 | * SkRect and SkPaths. |
| 42 | */ |
| 43 | class SimpleClipGM : public GM { |
| 44 | public: |
| 45 | enum SkGeomTypes { |
| 46 | kRect_GeomType, |
| 47 | kPath_GeomType, |
| 48 | kAAClip_GeomType |
| 49 | }; |
| 50 | |
| 51 | SimpleClipGM(SkGeomTypes geomType) |
| 52 | : fGeomType(geomType) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 53 | } |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 54 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 55 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | void onOnceBeforeDraw() override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 57 | // 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] | 58 | fBase.set(100.65f, |
| 59 | 100.65f, |
| 60 | 150.65f, |
| 61 | 150.65f); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 62 | fRect = fBase; |
| 63 | fRect.inset(5, 5); |
| 64 | fRect.offset(25, 25); |
| 65 | |
| 66 | fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5)); |
| 67 | fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5)); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 68 | INHERITED::setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 71 | void buildRgn(SkAAClip* clip, SkClipOp op) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | clip->setPath(fBasePath, nullptr, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 73 | |
| 74 | SkAAClip clip2; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | clip2.setPath(fRectPath, nullptr, true); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 76 | clip->op(clip2, (SkRegion::Op)op); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void drawOrig(SkCanvas* canvas) { |
| 80 | SkPaint paint; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 81 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 82 | paint.setStyle(SkPaint::kStroke_Style); |
| 83 | paint.setColor(SK_ColorBLACK); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 84 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 85 | canvas->drawRect(fBase, paint); |
| 86 | canvas->drawRect(fRect, paint); |
| 87 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 88 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 89 | void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 90 | |
| 91 | SkAAClip clip; |
| 92 | |
| 93 | this->buildRgn(&clip, op); |
| 94 | this->drawOrig(canvas); |
| 95 | |
| 96 | SkPaint paint; |
| 97 | paint.setColor(color); |
| 98 | paint_rgn(canvas, clip, paint); |
| 99 | } |
| 100 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 101 | void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 102 | |
| 103 | this->drawOrig(canvas); |
| 104 | |
| 105 | canvas->save(); |
| 106 | |
| 107 | // create the clip mask with the supplied boolean op |
| 108 | if (kPath_GeomType == fGeomType) { |
| 109 | // path-based case |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 110 | canvas->clipPath(fBasePath, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 111 | canvas->clipPath(fRectPath, op, true); |
| 112 | } else { |
| 113 | // rect-based case |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 114 | canvas->clipRect(fBase, true); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 115 | canvas->clipRect(fRect, op, true); |
| 116 | } |
| 117 | |
| 118 | // draw a rect that will entirely cover the clip mask area |
| 119 | SkPaint paint; |
| 120 | paint.setColor(color); |
| 121 | |
| 122 | SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90), |
| 123 | SkIntToScalar(180), SkIntToScalar(180)); |
| 124 | |
| 125 | canvas->drawRect(r, paint); |
| 126 | |
| 127 | canvas->restore(); |
| 128 | } |
| 129 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 130 | SkString onShortName() override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 131 | SkString str; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 132 | str.printf("simpleaaclip_%s", |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 133 | kRect_GeomType == fGeomType ? "rect" : |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 134 | (kPath_GeomType == fGeomType ? "path" : |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 135 | "aaclip")); |
| 136 | return str; |
| 137 | } |
| 138 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 139 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 140 | return SkISize::Make(640, 480); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 141 | } |
| 142 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 143 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 144 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 145 | const struct { |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 146 | SkColor fColor; |
| 147 | const char* fName; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 148 | SkClipOp fOp; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 149 | } gOps[] = { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 150 | { SK_ColorBLACK, "Difference", kDifference_SkClipOp }, |
| 151 | { SK_ColorRED, "Intersect", kIntersect_SkClipOp }, |
| 152 | { sk_tool_utils::color_to_565(0xFF008800), "Union", kUnion_SkClipOp }, |
| 153 | { SK_ColorGREEN, "Rev Diff", kReverseDifference_SkClipOp }, |
| 154 | { SK_ColorYELLOW, "Replace", kReplace_SkClipOp }, |
| 155 | { SK_ColorBLUE, "XOR", kXOR_SkClipOp }, |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | SkPaint textPaint; |
| 159 | textPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 160 | sk_tool_utils::set_portable_typeface(&textPaint); |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 161 | textPaint.setTextSize(SK_Scalar1*24); |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 162 | int xOff = 0; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 163 | |
| 164 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 165 | canvas->drawString(gOps[op].fName, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 166 | SkIntToScalar(75), SkIntToScalar(50), |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 167 | textPaint); |
| 168 | |
| 169 | if (kAAClip_GeomType == fGeomType) { |
| 170 | this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 171 | } else { |
| 172 | this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 173 | } |
| 174 | |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 175 | if (xOff >= 400) { |
| 176 | canvas->translate(SkIntToScalar(-400), SkIntToScalar(250)); |
| 177 | xOff = 0; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 178 | } else { |
| 179 | canvas->translate(SkIntToScalar(200), 0); |
robertphillips@google.com | 2a02129 | 2012-07-17 15:37:15 +0000 | [diff] [blame] | 180 | xOff += 200; |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | private: |
| 185 | |
| 186 | SkGeomTypes fGeomType; |
| 187 | |
| 188 | SkRect fBase; |
| 189 | SkRect fRect; |
| 190 | |
| 191 | SkPath fBasePath; // fBase as a round rect |
| 192 | SkPath fRectPath; // fRect as a round rect |
| 193 | |
| 194 | typedef GM INHERITED; |
| 195 | }; |
| 196 | |
| 197 | ////////////////////////////////////////////////////////////////////////////// |
| 198 | |
| 199 | // rects |
reed@google.com | de1fc47 | 2012-12-14 12:59:07 +0000 | [diff] [blame] | 200 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); ) |
| 201 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); ) |
| 202 | DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); ) |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 203 | |
robertphillips@google.com | 67febd9 | 2012-05-22 12:14:50 +0000 | [diff] [blame] | 204 | } |