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" |
| 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkGradientShader.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkPath.h" |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 13 | #include "SkTLList.h" |
| 14 | |
| 15 | static SkBitmap make_bmp(int w, int h) { |
| 16 | SkBitmap bmp; |
| 17 | bmp.allocN32Pixels(w, h, true); |
| 18 | |
| 19 | SkCanvas canvas(bmp); |
| 20 | SkScalar wScalar = SkIntToScalar(w); |
| 21 | SkScalar hScalar = SkIntToScalar(h); |
| 22 | |
| 23 | SkPoint pt = { wScalar / 2, hScalar / 2 }; |
| 24 | |
| 25 | SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar); |
| 26 | |
caryclark | 2e09660 | 2015-07-13 05:39:47 -0700 | [diff] [blame] | 27 | SkColor colors[] = { sk_tool_utils::color_to_565(SK_ColorDKGRAY), |
| 28 | sk_tool_utils::color_to_565(0xFF222255), |
| 29 | sk_tool_utils::color_to_565(0xFF331133), |
| 30 | sk_tool_utils::color_to_565(0xFF884422), |
| 31 | sk_tool_utils::color_to_565(0xFF000022), SK_ColorWHITE, |
| 32 | sk_tool_utils::color_to_565(0xFFAABBCC) }; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 33 | |
| 34 | SkScalar pos[] = {0, |
| 35 | SK_Scalar1 / 6, |
| 36 | 2 * SK_Scalar1 / 6, |
| 37 | 3 * SK_Scalar1 / 6, |
| 38 | 4 * SK_Scalar1 / 6, |
| 39 | 5 * SK_Scalar1 / 6, |
| 40 | SK_Scalar1}; |
| 41 | |
| 42 | SkPaint paint; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 43 | SkRect rect = SkRect::MakeWH(wScalar, hScalar); |
| 44 | SkMatrix mat = SkMatrix::I(); |
| 45 | for (int i = 0; i < 4; ++i) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 46 | paint.setShader(SkGradientShader::MakeRadial( |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 47 | pt, radius, |
| 48 | colors, pos, |
| 49 | SK_ARRAY_COUNT(colors), |
| 50 | SkShader::kRepeat_TileMode, |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 51 | 0, &mat)); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 52 | canvas.drawRect(rect, paint); |
| 53 | rect.inset(wScalar / 8, hScalar / 8); |
| 54 | mat.preTranslate(6 * wScalar, 6 * hScalar); |
| 55 | mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3); |
| 56 | } |
| 57 | |
| 58 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 59 | sk_tool_utils::set_portable_typeface(&paint); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 60 | paint.setTextSize(wScalar / 2.2f); |
| 61 | paint.setShader(0); |
caryclark | 2e09660 | 2015-07-13 05:39:47 -0700 | [diff] [blame] | 62 | paint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY)); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 63 | constexpr char kTxt[] = "Skia"; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 64 | SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f }; |
| 65 | canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint); |
| 66 | paint.setColor(SK_ColorBLACK); |
| 67 | paint.setStyle(SkPaint::kStroke_Style); |
| 68 | paint.setStrokeWidth(SK_Scalar1); |
| 69 | canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint); |
| 70 | return bmp; |
| 71 | } |
| 72 | |
| 73 | namespace skiagm { |
| 74 | /** |
| 75 | * This GM tests convex polygon clips. |
| 76 | */ |
| 77 | class ConvexPolyClip : public GM { |
| 78 | public: |
| 79 | ConvexPolyClip() { |
| 80 | this->setBGColor(0xFFFFFFFF); |
| 81 | } |
| 82 | |
| 83 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | SkString onShortName() override { |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 85 | return SkString("convex_poly_clip"); |
| 86 | } |
| 87 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | SkISize onISize() override { |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 89 | // When benchmarking the saveLayer set of draws is skipped. |
| 90 | int w = 435; |
| 91 | if (kBench_Mode != this->getMode()) { |
| 92 | w *= 2; |
| 93 | } |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 94 | return SkISize::Make(w, 540); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 95 | } |
| 96 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 98 | SkPath tri; |
| 99 | tri.moveTo(5.f, 5.f); |
| 100 | tri.lineTo(100.f, 20.f); |
| 101 | tri.lineTo(15.f, 100.f); |
| 102 | |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 103 | fClips.addToTail()->setPath(tri); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 104 | |
| 105 | SkPath hexagon; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 106 | constexpr SkScalar kRadius = 45.f; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 107 | const SkPoint center = { kRadius, kRadius }; |
| 108 | for (int i = 0; i < 6; ++i) { |
| 109 | SkScalar angle = 2 * SK_ScalarPI * i / 6; |
| 110 | SkPoint point; |
| 111 | point.fY = SkScalarSinCos(angle, &point.fX); |
| 112 | point.scale(kRadius); |
| 113 | point = center + point; |
| 114 | if (0 == i) { |
| 115 | hexagon.moveTo(point); |
| 116 | } else { |
| 117 | hexagon.lineTo(point); |
| 118 | } |
| 119 | } |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 120 | fClips.addToTail()->setPath(hexagon); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 121 | |
| 122 | SkMatrix scaleM; |
| 123 | scaleM.setScale(1.1f, 0.4f, kRadius, kRadius); |
| 124 | hexagon.transform(scaleM); |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 125 | fClips.addToTail()->setPath(hexagon); |
| 126 | |
| 127 | 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] | 128 | |
| 129 | SkPath rotRect; |
| 130 | SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f); |
| 131 | rotRect.addRect(rect); |
| 132 | SkMatrix rotM; |
| 133 | rotM.setRotate(23.f, rect.centerX(), rect.centerY()); |
| 134 | rotRect.transform(rotM); |
commit-bot@chromium.org | b511be5 | 2014-02-04 15:09:16 +0000 | [diff] [blame] | 135 | fClips.addToTail()->setPath(rotRect); |
skia.committer@gmail.com | 1dab403 | 2014-02-05 03:01:48 +0000 | [diff] [blame] | 136 | |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 137 | fBmp = make_bmp(100, 100); |
| 138 | } |
| 139 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 140 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 141 | SkScalar y = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 142 | constexpr SkScalar kMargin = 10.f; |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 143 | |
| 144 | SkPaint bgPaint; |
| 145 | bgPaint.setAlpha(0x15); |
| 146 | SkISize size = canvas->getDeviceSize(); |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 147 | canvas->drawBitmapRect(fBmp, SkRect::MakeIWH(size.fWidth, size.fHeight), &bgPaint); |
commit-bot@chromium.org | a343c84 | 2014-01-31 14:48:58 +0000 | [diff] [blame] | 148 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 149 | constexpr char kTxt[] = "Clip Me!"; |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 150 | SkPaint txtPaint; |
| 151 | txtPaint.setTextSize(23.f); |
| 152 | txtPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 153 | sk_tool_utils::set_portable_typeface(&txtPaint); |
caryclark | 2e09660 | 2015-07-13 05:39:47 -0700 | [diff] [blame] | 154 | txtPaint.setColor(sk_tool_utils::color_to_565(SK_ColorDKGRAY)); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 155 | SkScalar textW = txtPaint.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1); |
| 156 | |
| 157 | SkScalar startX = 0; |
| 158 | int testLayers = kBench_Mode != this->getMode(); |
| 159 | for (int doLayer = 0; doLayer <= testLayers; ++doLayer) { |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 160 | for (ClipList::Iter iter(fClips, ClipList::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 161 | iter.get(); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 162 | iter.next()) { |
| 163 | const Clip* clip = iter.get(); |
| 164 | SkScalar x = startX; |
| 165 | for (int aa = 0; aa < 2; ++aa) { |
| 166 | if (doLayer) { |
| 167 | SkRect bounds; |
| 168 | clip->getBounds(&bounds); |
| 169 | bounds.outset(2, 2); |
| 170 | bounds.offset(x, y); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 171 | canvas->saveLayer(&bounds, nullptr); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 172 | } else { |
| 173 | canvas->save(); |
| 174 | } |
| 175 | canvas->translate(x, y); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 176 | clip->setOnCanvas(canvas, SkCanvas::kIntersect_Op, SkToBool(aa)); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 177 | canvas->drawBitmap(fBmp, 0, 0); |
| 178 | canvas->restore(); |
| 179 | x += fBmp.width() + kMargin; |
| 180 | } |
| 181 | for (int aa = 0; aa < 2; ++aa) { |
| 182 | |
| 183 | SkPaint clipOutlinePaint; |
| 184 | clipOutlinePaint.setAntiAlias(true); |
| 185 | clipOutlinePaint.setColor(0x50505050); |
| 186 | clipOutlinePaint.setStyle(SkPaint::kStroke_Style); |
| 187 | clipOutlinePaint.setStrokeWidth(0); |
| 188 | |
| 189 | if (doLayer) { |
| 190 | SkRect bounds; |
| 191 | clip->getBounds(&bounds); |
| 192 | bounds.outset(2, 2); |
| 193 | bounds.offset(x, y); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | canvas->saveLayer(&bounds, nullptr); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 195 | } else { |
| 196 | canvas->save(); |
| 197 | } |
| 198 | canvas->translate(x, y); |
| 199 | SkPath closedClipPath; |
| 200 | clip->asClosedPath(&closedClipPath); |
| 201 | canvas->drawPath(closedClipPath, clipOutlinePaint); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 202 | clip->setOnCanvas(canvas, SkCanvas::kIntersect_Op, SkToBool(aa)); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 203 | canvas->scale(1.f, 1.8f); |
| 204 | canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, |
| 205 | 0, 1.5f * txtPaint.getTextSize(), |
| 206 | txtPaint); |
| 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 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 230 | void setOnCanvas(SkCanvas* canvas, SkCanvas::ClipOp 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 | } |