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