| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2013 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 "include/core/SkTypes.h" | 
| halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 9 |  | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" | 
 | 11 | #include "include/core/SkCanvas.h" | 
 | 12 | #include "include/core/SkColor.h" | 
 | 13 | #include "include/core/SkPaint.h" | 
 | 14 | #include "include/core/SkPath.h" | 
 | 15 | #include "include/core/SkRRect.h" | 
 | 16 | #include "include/core/SkRect.h" | 
 | 17 | #include "include/core/SkSurface.h" | 
 | 18 | #include "include/effects/SkDashPathEffect.h" | 
 | 19 | #include "include/gpu/GrContext.h" | 
 | 20 | #include "src/gpu/GrPath.h" | 
| Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 21 | #include "src/gpu/geometry/GrShape.h" | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tests/Test.h" | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 23 |  | 
| kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 24 | #include <initializer_list> | 
 | 25 |  | 
| tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 26 | static void test_drawPathEmpty(skiatest::Reporter*, SkCanvas* canvas) { | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 27 |     // Filling an empty path should not crash. | 
 | 28 |     SkPaint paint; | 
| fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 29 |     SkRect emptyRect = SkRect::MakeEmpty(); | 
 | 30 |     canvas->drawRect(emptyRect, paint); | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 31 |     canvas->drawPath(SkPath(), paint); | 
| fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 32 |     canvas->drawOval(emptyRect, paint); | 
 | 33 |     canvas->drawRect(emptyRect, paint); | 
 | 34 |     canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint); | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 35 |  | 
 | 36 |     // Stroking an empty path should not crash. | 
 | 37 |     paint.setAntiAlias(true); | 
 | 38 |     paint.setStyle(SkPaint::kStroke_Style); | 
 | 39 |     paint.setColor(SK_ColorGRAY); | 
 | 40 |     paint.setStrokeWidth(SkIntToScalar(20)); | 
 | 41 |     paint.setStrokeJoin(SkPaint::kRound_Join); | 
| fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 42 |     canvas->drawRect(emptyRect, paint); | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 43 |     canvas->drawPath(SkPath(), paint); | 
| fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 44 |     canvas->drawOval(emptyRect, paint); | 
 | 45 |     canvas->drawRect(emptyRect, paint); | 
 | 46 |     canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint); | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 47 | } | 
 | 48 |  | 
| fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 49 | static void fill_and_stroke(SkCanvas* canvas, const SkPath& p1, const SkPath& p2, | 
| reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 50 |                             sk_sp<SkPathEffect> effect) { | 
| fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 51 |     SkPaint paint; | 
 | 52 |     paint.setAntiAlias(true); | 
 | 53 |     paint.setPathEffect(effect); | 
 | 54 |  | 
 | 55 |     canvas->drawPath(p1, paint); | 
 | 56 |     canvas->drawPath(p2, paint); | 
 | 57 |  | 
 | 58 |     paint.setStyle(SkPaint::kStroke_Style); | 
 | 59 |     canvas->drawPath(p1, paint); | 
 | 60 |     canvas->drawPath(p2, paint); | 
 | 61 | } | 
 | 62 |  | 
 | 63 | static void test_drawSameRectOvals(skiatest::Reporter*, SkCanvas* canvas) { | 
 | 64 |     // Drawing ovals with similar bounds but different points order should not crash. | 
 | 65 |  | 
 | 66 |     SkPath oval1, oval2; | 
 | 67 |     const SkRect rect = SkRect::MakeWH(100, 50); | 
 | 68 |     oval1.addOval(rect, SkPath::kCW_Direction); | 
 | 69 |     oval2.addOval(rect, SkPath::kCCW_Direction); | 
 | 70 |  | 
 | 71 |     fill_and_stroke(canvas, oval1, oval2, nullptr); | 
 | 72 |  | 
 | 73 |     const SkScalar intervals[] = { 1, 1 }; | 
| reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 74 |     fill_and_stroke(canvas, oval1, oval2, SkDashPathEffect::Make(intervals, 2, 0)); | 
| fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 75 | } | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 76 |  | 
| bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 77 | DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GpuDrawPath, reporter, ctxInfo) { | 
| kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 78 |     for (auto& test_func : { &test_drawPathEmpty, &test_drawSameRectOvals }) { | 
| Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 79 |         for (auto& sampleCount : {1, 4, 16}) { | 
| reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 80 |             SkImageInfo info = SkImageInfo::MakeN32Premul(255, 255); | 
| reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 81 |             auto surface( | 
| bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 82 |                 SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info, | 
 | 83 |                                             sampleCount, nullptr)); | 
| kkinnunen | c11a527 | 2015-11-19 09:37:02 -0800 | [diff] [blame] | 84 |             if (!surface) { | 
 | 85 |                 continue; | 
 | 86 |             } | 
| kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 87 |             test_func(reporter, surface->getCanvas()); | 
| commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 88 |         } | 
 | 89 |     } | 
 | 90 | } | 
 | 91 |  | 
| Brian Osman | 2caecd2 | 2019-09-25 14:02:26 -0400 | [diff] [blame] | 92 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrDrawCollapsedPath, reporter, ctxInfo) { | 
 | 93 |     // From https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=37330, it's possible for a convex | 
 | 94 |     // path to be accepted by AAConvexPathRenderer, then be transformed to something without a | 
 | 95 |     // computable first direction by a perspective matrix. | 
 | 96 |     SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); | 
 | 97 |     auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info)); | 
 | 98 |  | 
 | 99 |     SkPaint paint; | 
 | 100 |     paint.setAntiAlias(true); | 
 | 101 |  | 
 | 102 |     SkPath path; | 
 | 103 |     path.moveTo(0, 0); | 
 | 104 |     path.lineTo(50, 0); | 
 | 105 |     path.lineTo(0, 50); | 
 | 106 |     path.close(); | 
 | 107 |  | 
 | 108 |     SkMatrix m; | 
 | 109 |     m.setAll( 0.966006875f   , -0.125156224f  , 72.0899811f, | 
 | 110 |              -0.00885376986f , -0.112347461f  , 64.7121124f, | 
 | 111 |              -8.94321693e-06f, -0.00173384184f, 0.998692870f); | 
 | 112 |     surface->getCanvas()->setMatrix(m); | 
 | 113 |     surface->getCanvas()->drawPath(path, paint); | 
 | 114 |     surface->flush(); | 
 | 115 | } | 
 | 116 |  | 
| Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 117 | DEF_GPUTEST(GrPathKeys, reporter, /* options */) { | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 118 |     SkPaint strokePaint; | 
 | 119 |     strokePaint.setStyle(SkPaint::kStroke_Style); | 
 | 120 |     strokePaint.setStrokeWidth(10.f); | 
 | 121 |     GrStyle styles[] = { | 
 | 122 |         GrStyle::SimpleFill(), | 
 | 123 |         GrStyle::SimpleHairline(), | 
 | 124 |         GrStyle(strokePaint) | 
 | 125 |     }; | 
| fmalita | 3b44448 | 2015-11-19 07:28:50 -0800 | [diff] [blame] | 126 |  | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 127 |     for (const GrStyle& style : styles) { | 
 | 128 |         // Keys should not ignore conic weights. | 
 | 129 |         SkPath path1, path2; | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 130 |         SkPoint p0 = SkPoint::Make(100, 0); | 
 | 131 |         SkPoint p1 = SkPoint::Make(100, 100); | 
| fmalita | 3b44448 | 2015-11-19 07:28:50 -0800 | [diff] [blame] | 132 |  | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 133 |         path1.conicTo(p0, p1, .5f); | 
 | 134 |         path2.conicTo(p0, p1, .7f); | 
 | 135 |  | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 136 |         GrUniqueKey key1, key2; | 
| bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 137 |         // We expect these small paths to be keyed based on their data. | 
 | 138 |         bool isVolatile; | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 139 |         GrPath::ComputeKey(GrShape(path1, GrStyle::SimpleFill()), &key1, &isVolatile); | 
 | 140 |         REPORTER_ASSERT(reporter, !isVolatile); | 
 | 141 |         REPORTER_ASSERT(reporter, key1.isValid()); | 
 | 142 |         GrPath::ComputeKey(GrShape(path2, GrStyle::SimpleFill()), &key2, &isVolatile); | 
 | 143 |         REPORTER_ASSERT(reporter, !isVolatile); | 
 | 144 |         REPORTER_ASSERT(reporter, key1.isValid()); | 
 | 145 |         REPORTER_ASSERT(reporter, key1 != key2); | 
| bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 146 |         { | 
 | 147 |             GrUniqueKey tempKey; | 
 | 148 |             path1.setIsVolatile(true); | 
 | 149 |             GrPath::ComputeKey(GrShape(path1, style), &key1, &isVolatile); | 
 | 150 |             REPORTER_ASSERT(reporter, isVolatile); | 
 | 151 |             REPORTER_ASSERT(reporter, !tempKey.isValid()); | 
 | 152 |         } | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 153 |  | 
 | 154 |         // Ensure that recreating the GrShape doesn't change the key. | 
 | 155 |         { | 
 | 156 |             GrUniqueKey tempKey; | 
| bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 157 |             GrPath::ComputeKey(GrShape(path2, GrStyle::SimpleFill()), &tempKey, &isVolatile); | 
 | 158 |             REPORTER_ASSERT(reporter, key2 == tempKey); | 
| bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 159 |         } | 
 | 160 |  | 
 | 161 |         // Try a large path that is too big to be keyed off its data. | 
 | 162 |         SkPath path3; | 
 | 163 |         SkPath path4; | 
 | 164 |         for (int i = 0; i < 1000; ++i) { | 
 | 165 |             SkScalar s = SkIntToScalar(i); | 
 | 166 |             path3.conicTo(s, 3.f * s / 4, s + 1.f, s, 0.5f + s / 2000.f); | 
 | 167 |             path4.conicTo(s, 3.f * s / 4, s + 1.f, s, 0.3f + s / 2000.f); | 
 | 168 |         } | 
 | 169 |  | 
 | 170 |         GrUniqueKey key3, key4; | 
 | 171 |         // These aren't marked volatile and so should have keys | 
 | 172 |         GrPath::ComputeKey(GrShape(path3, style), &key3, &isVolatile); | 
 | 173 |         REPORTER_ASSERT(reporter, !isVolatile); | 
 | 174 |         REPORTER_ASSERT(reporter, key3.isValid()); | 
 | 175 |         GrPath::ComputeKey(GrShape(path4, style), &key4, &isVolatile); | 
 | 176 |         REPORTER_ASSERT(reporter, !isVolatile); | 
 | 177 |         REPORTER_ASSERT(reporter, key4.isValid()); | 
 | 178 |         REPORTER_ASSERT(reporter, key3 != key4); | 
 | 179 |  | 
 | 180 |         { | 
 | 181 |             GrUniqueKey tempKey; | 
 | 182 |             path3.setIsVolatile(true); | 
 | 183 |             GrPath::ComputeKey(GrShape(path3, style), &key1, &isVolatile); | 
 | 184 |             REPORTER_ASSERT(reporter, isVolatile); | 
 | 185 |             REPORTER_ASSERT(reporter, !tempKey.isValid()); | 
 | 186 |         } | 
 | 187 |     } | 
| fmalita | 3b44448 | 2015-11-19 07:28:50 -0800 | [diff] [blame] | 188 | } |