commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 8 | #include "ToolUtils.h" |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 9 | #include "gm.h" |
| 10 | |
| 11 | #include "SkBitmap.h" |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 12 | #include "SkFont.h" |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 14 | #include "SkPath.h" |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 15 | #include "SkTLList.h" |
| 16 | |
| 17 | static SkBitmap make_bmp(int w, int h) { |
| 18 | SkBitmap bmp; |
| 19 | bmp.allocN32Pixels(w, h, true); |
| 20 | |
| 21 | SkCanvas canvas(bmp); |
| 22 | SkScalar wScalar = SkIntToScalar(w); |
| 23 | SkScalar hScalar = SkIntToScalar(h); |
| 24 | |
| 25 | SkPoint pt = { wScalar / 2, hScalar / 2 }; |
| 26 | |
| 27 | SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar); |
| 28 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 29 | SkColor colors[] = {SK_ColorDKGRAY, |
| 30 | ToolUtils::color_to_565(0xFF222255), |
| 31 | ToolUtils::color_to_565(0xFF331133), |
| 32 | ToolUtils::color_to_565(0xFF884422), |
| 33 | ToolUtils::color_to_565(0xFF000022), |
| 34 | SK_ColorWHITE, |
| 35 | ToolUtils::color_to_565(0xFFAABBCC)}; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 36 | |
| 37 | SkScalar pos[] = {0, |
| 38 | SK_Scalar1 / 6, |
| 39 | 2 * SK_Scalar1 / 6, |
| 40 | 3 * SK_Scalar1 / 6, |
| 41 | 4 * SK_Scalar1 / 6, |
| 42 | 5 * SK_Scalar1 / 6, |
| 43 | SK_Scalar1}; |
| 44 | |
| 45 | SkPaint paint; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 46 | SkRect rect = SkRect::MakeWH(wScalar, hScalar); |
| 47 | SkMatrix mat = SkMatrix::I(); |
| 48 | for (int i = 0; i < 4; ++i) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 49 | paint.setShader(SkGradientShader::MakeRadial( |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 50 | pt, radius, |
| 51 | colors, pos, |
| 52 | SK_ARRAY_COUNT(colors), |
| 53 | SkShader::kRepeat_TileMode, |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 54 | 0, &mat)); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 55 | canvas.drawRect(rect, paint); |
| 56 | rect.inset(wScalar / 8, hScalar / 8); |
| 57 | mat.preTranslate(6 * wScalar, 6 * hScalar); |
| 58 | mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3); |
| 59 | } |
| 60 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 61 | SkFont font(ToolUtils::create_portable_typeface(), wScalar / 2.2f); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 62 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 63 | paint.setShader(nullptr); |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 64 | paint.setColor(SK_ColorLTGRAY); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 65 | constexpr char kTxt[] = "Skia"; |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 66 | SkPoint texPos = { wScalar / 17, hScalar / 2 + font.getSize() / 2.5f }; |
| 67 | canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding, |
| 68 | texPos.fX, texPos.fY, font, paint); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 69 | paint.setColor(SK_ColorBLACK); |
| 70 | paint.setStyle(SkPaint::kStroke_Style); |
| 71 | paint.setStrokeWidth(SK_Scalar1); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 72 | canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding, |
| 73 | texPos.fX, texPos.fY, font, paint); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 74 | return bmp; |
| 75 | } |
| 76 | |
| 77 | namespace skiagm { |
| 78 | /** |
| 79 | * This GM tests convex polygon clips. |
| 80 | */ |
| 81 | class ConvexPolyClip : public GM { |
| 82 | public: |
| 83 | ConvexPolyClip() { |
| 84 | this->setBGColor(0xFFFFFFFF); |
| 85 | } |
| 86 | |
| 87 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | SkString onShortName() override { |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 89 | return SkString("convex_poly_clip"); |
| 90 | } |
| 91 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | SkISize onISize() override { |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 93 | // When benchmarking the saveLayer set of draws is skipped. |
| 94 | int w = 435; |
| 95 | if (kBench_Mode != this->getMode()) { |
| 96 | w *= 2; |
| 97 | } |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 98 | return SkISize::Make(w, 540); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 99 | } |
| 100 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 101 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 102 | SkPath tri; |
| 103 | tri.moveTo(5.f, 5.f); |
| 104 | tri.lineTo(100.f, 20.f); |
| 105 | tri.lineTo(15.f, 100.f); |
| 106 | |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 107 | fClips.addToTail()->setPath(tri); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 108 | |
| 109 | SkPath hexagon; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 110 | constexpr SkScalar kRadius = 45.f; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 111 | const SkPoint center = { kRadius, kRadius }; |
| 112 | for (int i = 0; i < 6; ++i) { |
| 113 | SkScalar angle = 2 * SK_ScalarPI * i / 6; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame^] | 114 | SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) }; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 115 | point.scale(kRadius); |
| 116 | point = center + point; |
| 117 | if (0 == i) { |
| 118 | hexagon.moveTo(point); |
| 119 | } else { |
| 120 | hexagon.lineTo(point); |
| 121 | } |
| 122 | } |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 123 | fClips.addToTail()->setPath(hexagon); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 124 | |
| 125 | SkMatrix scaleM; |
| 126 | scaleM.setScale(1.1f, 0.4f, kRadius, kRadius); |
| 127 | hexagon.transform(scaleM); |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 128 | fClips.addToTail()->setPath(hexagon); |
| 129 | |
| 130 | fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f)); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 131 | |
| 132 | SkPath rotRect; |
| 133 | SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f); |
| 134 | rotRect.addRect(rect); |
| 135 | SkMatrix rotM; |
| 136 | rotM.setRotate(23.f, rect.centerX(), rect.centerY()); |
| 137 | rotRect.transform(rotM); |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 138 | fClips.addToTail()->setPath(rotRect); |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 139 | |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 140 | fBmp = make_bmp(100, 100); |
| 141 | } |
| 142 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 143 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 144 | SkScalar y = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 145 | constexpr SkScalar kMargin = 10.f; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 146 | |
| 147 | SkPaint bgPaint; |
| 148 | bgPaint.setAlpha(0x15); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 149 | SkISize size = canvas->getBaseLayerSize(); |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 150 | canvas->drawBitmapRect(fBmp, SkRect::MakeIWH(size.fWidth, size.fHeight), &bgPaint); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 151 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 152 | constexpr char kTxt[] = "Clip Me!"; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 153 | SkFont font(ToolUtils::create_portable_typeface(), 23); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 154 | SkScalar textW = font.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 155 | SkPaint txtPaint; |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 156 | txtPaint.setColor(SK_ColorDKGRAY); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 157 | |
| 158 | SkScalar startX = 0; |
| 159 | int testLayers = kBench_Mode != this->getMode(); |
| 160 | for (int doLayer = 0; doLayer <= testLayers; ++doLayer) { |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 161 | for (ClipList::Iter iter(fClips, ClipList::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 162 | iter.get(); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 163 | iter.next()) { |
| 164 | const Clip* clip = iter.get(); |
| 165 | SkScalar x = startX; |
| 166 | for (int aa = 0; aa < 2; ++aa) { |
| 167 | if (doLayer) { |
| 168 | SkRect bounds; |
| 169 | clip->getBounds(&bounds); |
| 170 | bounds.outset(2, 2); |
| 171 | bounds.offset(x, y); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | canvas->saveLayer(&bounds, nullptr); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 173 | } else { |
| 174 | canvas->save(); |
| 175 | } |
| 176 | canvas->translate(x, y); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 177 | clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa)); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 178 | canvas->drawBitmap(fBmp, 0, 0); |
| 179 | canvas->restore(); |
| 180 | x += fBmp.width() + kMargin; |
| 181 | } |
| 182 | for (int aa = 0; aa < 2; ++aa) { |
| 183 | |
| 184 | SkPaint clipOutlinePaint; |
| 185 | clipOutlinePaint.setAntiAlias(true); |
| 186 | clipOutlinePaint.setColor(0x50505050); |
| 187 | clipOutlinePaint.setStyle(SkPaint::kStroke_Style); |
| 188 | clipOutlinePaint.setStrokeWidth(0); |
| 189 | |
| 190 | if (doLayer) { |
| 191 | SkRect bounds; |
| 192 | clip->getBounds(&bounds); |
| 193 | bounds.outset(2, 2); |
| 194 | bounds.offset(x, y); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 195 | canvas->saveLayer(&bounds, nullptr); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 196 | } else { |
| 197 | canvas->save(); |
| 198 | } |
| 199 | canvas->translate(x, y); |
| 200 | SkPath closedClipPath; |
| 201 | clip->asClosedPath(&closedClipPath); |
| 202 | canvas->drawPath(closedClipPath, clipOutlinePaint); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 203 | clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa)); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 204 | canvas->scale(1.f, 1.8f); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 205 | canvas->drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding, |
| 206 | 0, 1.5f * font.getSize(), font, txtPaint); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 207 | canvas->restore(); |
| 208 | x += textW + 2 * kMargin; |
| 209 | } |
| 210 | y += fBmp.height() + kMargin; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 211 | } |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 212 | y = 0; |
| 213 | startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 217 | bool runAsBench() const override { return true; } |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 218 | |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 219 | private: |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 220 | class Clip { |
| 221 | public: |
| 222 | enum ClipType { |
| 223 | kNone_ClipType, |
| 224 | kPath_ClipType, |
| 225 | kRect_ClipType |
| 226 | }; |
| 227 | |
| 228 | Clip () : fClipType(kNone_ClipType) {} |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 229 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 230 | void setOnCanvas(SkCanvas* canvas, SkClipOp op, bool aa) const { |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 231 | switch (fClipType) { |
| 232 | case kPath_ClipType: |
| 233 | canvas->clipPath(fPath, op, aa); |
| 234 | break; |
| 235 | case kRect_ClipType: |
| 236 | canvas->clipRect(fRect, op, aa); |
| 237 | break; |
| 238 | case kNone_ClipType: |
| 239 | SkDEBUGFAIL("Uninitialized Clip."); |
| 240 | break; |
| 241 | } |
| 242 | } |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 243 | |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 244 | void asClosedPath(SkPath* path) const { |
| 245 | switch (fClipType) { |
| 246 | case kPath_ClipType: |
| 247 | *path = fPath; |
| 248 | path->close(); |
| 249 | break; |
| 250 | case kRect_ClipType: |
| 251 | path->reset(); |
| 252 | path->addRect(fRect); |
| 253 | break; |
| 254 | case kNone_ClipType: |
| 255 | SkDEBUGFAIL("Uninitialized Clip."); |
| 256 | break; |
| 257 | } |
| 258 | } |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 259 | |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 260 | void setPath(const SkPath& path) { |
| 261 | fClipType = kPath_ClipType; |
| 262 | fPath = path; |
| 263 | } |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 264 | |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 265 | void setRect(const SkRect& rect) { |
| 266 | fClipType = kRect_ClipType; |
| 267 | fRect = rect; |
| 268 | fPath.reset(); |
| 269 | } |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 270 | |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 271 | ClipType getType() const { return fClipType; } |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 272 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 273 | void getBounds(SkRect* bounds) const { |
| 274 | switch (fClipType) { |
| 275 | case kPath_ClipType: |
| 276 | *bounds = fPath.getBounds(); |
| 277 | break; |
| 278 | case kRect_ClipType: |
| 279 | *bounds = fRect; |
| 280 | break; |
| 281 | case kNone_ClipType: |
| 282 | SkDEBUGFAIL("Uninitialized Clip."); |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 287 | private: |
| 288 | ClipType fClipType; |
| 289 | SkPath fPath; |
| 290 | SkRect fRect; |
| 291 | }; |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 292 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 293 | typedef SkTLList<Clip, 1> ClipList; |
| 294 | ClipList fClips; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 295 | SkBitmap fBmp; |
| 296 | |
| 297 | typedef GM INHERITED; |
| 298 | }; |
| 299 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 300 | DEF_GM(return new ConvexPolyClip;) |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 301 | } |