commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +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 | // This test only works with the GPU backend. |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkBlendMode.h" |
| 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkMatrix.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPath.h" |
| 16 | #include "include/core/SkPoint.h" |
| 17 | #include "include/core/SkRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "include/private/GrSharedEnums.h" |
| 23 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/core/SkTLList.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include "src/gpu/GrFragmentProcessor.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "src/gpu/GrPaint.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 29 | #include "src/gpu/effects/GrConvexPolyEffect.h" |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 30 | #include "tools/gpu/TestOps.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | |
| 32 | #include <memory> |
| 33 | #include <utility> |
| 34 | |
| 35 | class GrAppliedClip; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 36 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 37 | namespace skiagm { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 38 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 39 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 40 | * This GM directly exercises a GrProcessor that draws convex polygons. |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 41 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 42 | class ConvexPolyEffect : public GpuGM { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 43 | public: |
| 44 | ConvexPolyEffect() { |
| 45 | this->setBGColor(0xFFFFFFFF); |
| 46 | } |
| 47 | |
| 48 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | SkString onShortName() override { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 50 | return SkString("convex_poly_effect"); |
| 51 | } |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 54 | return SkISize::Make(720, 800); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 58 | SkPath tri; |
| 59 | tri.moveTo(5.f, 5.f); |
| 60 | tri.lineTo(100.f, 20.f); |
| 61 | tri.lineTo(15.f, 100.f); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 62 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 63 | fPaths.addToTail(tri); |
| 64 | fPaths.addToTail(SkPath())->reverseAddPath(tri); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 65 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 66 | tri.close(); |
| 67 | fPaths.addToTail(tri); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 69 | SkPath ngon; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 70 | constexpr SkScalar kRadius = 50.f; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 71 | const SkPoint center = { kRadius, kRadius }; |
| 72 | for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) { |
| 73 | SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 74 | SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) }; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 75 | point.scale(kRadius); |
| 76 | point = center + point; |
| 77 | if (0 == i) { |
| 78 | ngon.moveTo(point); |
| 79 | } else { |
| 80 | ngon.lineTo(point); |
| 81 | } |
| 82 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 84 | fPaths.addToTail(ngon); |
| 85 | SkMatrix scaleM; |
| 86 | scaleM.setScale(1.1f, 0.4f); |
| 87 | ngon.transform(scaleM); |
| 88 | fPaths.addToTail(ngon); |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 89 | |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 90 | SkPath linePath; |
| 91 | linePath.moveTo(5.f, 5.f); |
| 92 | linePath.lineTo(6.f, 6.f); |
| 93 | fPaths.addToTail(linePath); |
| 94 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 95 | // integer edges |
| 96 | fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f)); |
| 97 | // half-integer edges |
| 98 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f)); |
| 99 | // vertically/horizontally thin rects that cover pixel centers |
| 100 | fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f)); |
| 101 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f)); |
| 102 | // vertically/horizontally thin rects that don't cover pixel centers |
| 103 | fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f)); |
| 104 | fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f)); |
| 105 | // small in x and y |
| 106 | fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f)); |
| 107 | // inverted in x and y |
| 108 | fRects.addToTail(SkRect::MakeLTRB(100.f, 50.5f, 5.f, 0.5f)); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 111 | void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 112 | SkCanvas* canvas) override { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 113 | SkScalar y = 0; |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 114 | static constexpr SkScalar kDX = 12.f; |
| 115 | static constexpr SkScalar kOutset = 5.f; |
| 116 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 117 | for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 118 | iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 119 | iter.next()) { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 120 | const SkPath* path = iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 121 | SkScalar x = 0; |
| 122 | |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 123 | for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 124 | const SkMatrix m = SkMatrix::Translate(x, y); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 125 | SkPath p; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 126 | path->transform(m, &p); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 127 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 128 | GrClipEdgeType edgeType = (GrClipEdgeType) et; |
John Stiles | ec9269b | 2020-06-15 16:05:14 -0400 | [diff] [blame] | 129 | std::unique_ptr<GrFragmentProcessor> fp = |
| 130 | GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 131 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 132 | continue; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 133 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 134 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 135 | GrPaint grPaint; |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 136 | grPaint.setColor4f({ 0, 0, 0, 1.f }); |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 137 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 138 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 139 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 140 | auto rect = p.getBounds().makeOutset(kOutset, kOutset); |
| 141 | auto op = sk_gpu_test::test_ops::MakeRect(context, std::move(grPaint), rect); |
Brian Salomon | 576eda1 | 2017-07-14 11:30:17 -0400 | [diff] [blame] | 142 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 143 | |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 144 | x += SkScalarCeilToScalar(path->getBounds().width() + kDX); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 145 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 146 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 147 | // Draw AA and non AA paths using normal API for reference. |
| 148 | canvas->save(); |
| 149 | canvas->translate(x, y); |
| 150 | SkPaint paint; |
| 151 | canvas->drawPath(*path, paint); |
| 152 | canvas->translate(path->getBounds().width() + 10.f, 0); |
| 153 | paint.setAntiAlias(true); |
| 154 | canvas->drawPath(*path, paint); |
| 155 | canvas->restore(); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 156 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 157 | y += SkScalarCeilToScalar(path->getBounds().height() + 20.f); |
| 158 | } |
| 159 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 160 | for (RectList::Iter iter(fRects, RectList::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 161 | iter.get(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 162 | iter.next()) { |
| 163 | |
| 164 | SkScalar x = 0; |
| 165 | |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 166 | for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 167 | SkRect rect = iter.get()->makeOffset(x, y); |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 168 | GrClipEdgeType edgeType = (GrClipEdgeType) et; |
John Stiles | ec9269b | 2020-06-15 16:05:14 -0400 | [diff] [blame] | 169 | std::unique_ptr<GrFragmentProcessor> fp = |
| 170 | GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, rect); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 171 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 172 | continue; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 173 | } |
| 174 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 175 | GrPaint grPaint; |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 176 | grPaint.setColor4f({ 0, 0, 0, 1.f }); |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 177 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 178 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 179 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 180 | auto drawRect = rect.makeOutset(kOutset, kOutset); |
| 181 | auto op = sk_gpu_test::test_ops::MakeRect(context, std::move(grPaint), drawRect); |
| 182 | |
Brian Salomon | 576eda1 | 2017-07-14 11:30:17 -0400 | [diff] [blame] | 183 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 184 | |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 185 | x += SkScalarCeilToScalar(rect.width() + kDX); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 186 | } |
| 187 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 188 | // Draw rect without and with AA using normal API for reference |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 189 | canvas->save(); |
| 190 | canvas->translate(x, y); |
| 191 | SkPaint paint; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 192 | canvas->drawRect(*iter.get(), paint); |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 193 | x += SkScalarCeilToScalar(iter.get()->width() + kDX); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 194 | paint.setAntiAlias(true); |
| 195 | canvas->drawRect(*iter.get(), paint); |
| 196 | canvas->restore(); |
| 197 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 198 | y += SkScalarCeilToScalar(iter.get()->height() + 20.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | private: |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 203 | typedef SkTLList<SkPath, 1> PathList; |
| 204 | typedef SkTLList<SkRect, 1> RectList; |
| 205 | PathList fPaths; |
| 206 | RectList fRects; |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 207 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 208 | typedef GM INHERITED; |
| 209 | }; |
| 210 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 211 | DEF_GM(return new ConvexPolyEffect;) |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 212 | } |