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