reed@google.com | 603dbed | 2012-11-20 19:00:28 +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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkPoint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkScalar.h" |
| 16 | #include "include/core/SkSize.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/core/SkTypes.h" |
| 19 | #include "include/private/SkTemplates.h" |
| 20 | |
| 21 | #include <float.h> |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 22 | |
| 23 | #define STROKE_WIDTH SkIntToScalar(20) |
| 24 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 25 | static void draw_path(SkCanvas* canvas, const SkPath& path, const SkRect& rect, |
| 26 | SkPaint::Join join, int doFill) { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 27 | SkPaint paint; |
| 28 | paint.setAntiAlias(true); |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 29 | paint.setStyle(doFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 30 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 31 | paint.setColor(SK_ColorGRAY); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 32 | paint.setStrokeWidth(STROKE_WIDTH); |
| 33 | paint.setStrokeJoin(join); |
| 34 | canvas->drawRect(rect, paint); |
| 35 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 36 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 37 | paint.setStrokeWidth(0); |
| 38 | paint.setColor(SK_ColorRED); |
| 39 | canvas->drawPath(path, paint); |
| 40 | |
| 41 | paint.setStrokeWidth(3); |
| 42 | paint.setStrokeJoin(SkPaint::kMiter_Join); |
| 43 | int n = path.countPoints(); |
| 44 | SkAutoTArray<SkPoint> points(n); |
| 45 | path.getPoints(points.get(), n); |
| 46 | canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), paint); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Test calling SkStroker for rectangles. Cases to cover: |
| 51 | * |
| 52 | * geometry: normal, small (smaller than stroke-width), empty, inverted |
| 53 | * joint-type for the corners |
| 54 | */ |
| 55 | class StrokeRectGM : public skiagm::GM { |
| 56 | public: |
| 57 | StrokeRectGM() {} |
| 58 | |
| 59 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | SkString onShortName() override { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 62 | return SkString("strokerect"); |
| 63 | } |
| 64 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 65 | SkISize onISize() override { |
bsalomon | 9c67381 | 2016-07-06 06:30:36 -0700 | [diff] [blame] | 66 | return SkISize::Make(1400, 740); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 67 | } |
| 68 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 70 | canvas->drawColor(SK_ColorWHITE); |
| 71 | canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2); |
| 72 | |
| 73 | SkPaint paint; |
| 74 | paint.setStyle(SkPaint::kStroke_Style); |
| 75 | paint.setStrokeWidth(STROKE_WIDTH); |
| 76 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 77 | constexpr SkPaint::Join gJoins[] = { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 78 | SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join |
| 79 | }; |
| 80 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 81 | constexpr SkScalar W = 80; |
| 82 | constexpr SkScalar H = 80; |
| 83 | constexpr SkRect gRects[] = { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 84 | { 0, 0, W, H }, |
| 85 | { W, 0, 0, H }, |
| 86 | { 0, H, W, 0 }, |
| 87 | { 0, 0, STROKE_WIDTH, H }, |
| 88 | { 0, 0, W, STROKE_WIDTH }, |
| 89 | { 0, 0, STROKE_WIDTH/2, STROKE_WIDTH/2 }, |
| 90 | { 0, 0, W, 0 }, |
| 91 | { 0, 0, 0, H }, |
| 92 | { 0, 0, 0, 0 }, |
caryclark | c53b82e | 2015-12-22 07:50:16 -0800 | [diff] [blame] | 93 | { 0, 0, W, FLT_EPSILON }, |
| 94 | { 0, 0, FLT_EPSILON, H }, |
| 95 | { 0, 0, FLT_EPSILON, FLT_EPSILON }, |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 96 | }; |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 97 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 98 | for (int doFill = 0; doFill <= 1; ++doFill) { |
| 99 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); ++i) { |
| 100 | SkPaint::Join join = gJoins[i]; |
| 101 | paint.setStrokeJoin(join); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 102 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 103 | SkAutoCanvasRestore acr(canvas, true); |
| 104 | for (size_t j = 0; j < SK_ARRAY_COUNT(gRects); ++j) { |
| 105 | const SkRect& r = gRects[j]; |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 106 | |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 107 | SkPath path, fillPath; |
| 108 | path.addRect(r); |
| 109 | paint.getFillPath(path, &fillPath); |
| 110 | draw_path(canvas, fillPath, r, join, doFill); |
| 111 | |
| 112 | canvas->translate(W + 2 * STROKE_WIDTH, 0); |
| 113 | } |
| 114 | acr.restore(); |
| 115 | canvas->translate(0, H + 2 * STROKE_WIDTH); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 116 | } |
reed@google.com | 9903395 | 2012-11-20 21:32:30 +0000 | [diff] [blame] | 117 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | typedef GM INHERITED; |
| 123 | }; |
reed | 8151103 | 2016-06-10 13:21:07 -0700 | [diff] [blame] | 124 | DEF_GM(return new StrokeRectGM;) |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 125 | |
| 126 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 127 | |
reed | 8151103 | 2016-06-10 13:21:07 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Exercise rect-stroking (which is specialized from paths) when the resulting stroke-width is |
| 130 | * non-square. See https://bugs.chromium.org/p/skia/issues/detail?id=5408 |
| 131 | */ |
| 132 | DEF_SIMPLE_GM(strokerect_anisotropic_5408, canvas, 200, 50) { |
| 133 | SkPaint p; |
| 134 | p.setStyle(SkPaint::kStroke_Style); |
| 135 | p.setStrokeWidth(6); |
| 136 | |
| 137 | canvas->scale(10, 1); |
| 138 | SkRect r = SkRect::MakeXYWH(5, 20, 10, 10); |
| 139 | canvas->drawRect(r, p); |
| 140 | } |