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 | |
| 10 | #include "gm.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | |
| 14 | #include "GrContext.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 15 | #include "GrDefaultGeoProcFactory.h" |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 16 | #include "GrDrawContextPriv.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 17 | #include "GrPathUtils.h" |
| 18 | #include "GrTest.h" |
| 19 | #include "SkColorPriv.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 20 | #include "SkGeometry.h" |
| 21 | #include "SkTLList.h" |
| 22 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 23 | #include "batches/GrTestBatch.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 24 | #include "batches/GrVertexBatch.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 25 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 26 | #include "effects/GrConvexPolyEffect.h" |
| 27 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 28 | /** outset rendered rect to visualize anti-aliased poly edges */ |
| 29 | static SkRect outset(const SkRect& unsorted) { |
| 30 | SkRect r = unsorted; |
| 31 | r.outset(5.f, 5.f); |
| 32 | return r; |
| 33 | } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 34 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 35 | /** sorts a rect */ |
| 36 | static SkRect sorted_rect(const SkRect& unsorted) { |
| 37 | SkRect r = unsorted; |
| 38 | r.sort(); |
| 39 | return r; |
| 40 | } |
| 41 | |
| 42 | namespace skiagm { |
| 43 | class PolyBoundsBatch : public GrTestBatch { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 44 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 45 | DEFINE_BATCH_CLASS_ID |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 46 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 47 | const char* name() const override { return "PolyBoundsBatch"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 48 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 49 | PolyBoundsBatch(const SkRect& rect, GrColor color) |
| 50 | : INHERITED(ClassID(), outset(sorted_rect(rect)), color) |
| 51 | , fRect(outset(rect)) { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private: |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 55 | void onPrepareDraws(Target* target) const override { |
| 56 | using namespace GrDefaultGeoProcFactory; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 57 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 58 | Color color(this->color()); |
| 59 | Coverage coverage(Coverage::kSolid_Type); |
| 60 | LocalCoords localCoords(LocalCoords::kUnused_Type); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 61 | sk_sp<GrGeometryProcessor> gp( |
| 62 | GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I())); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 63 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 64 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 65 | SkASSERT(vertexStride == sizeof(SkPoint)); |
| 66 | QuadHelper helper; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 67 | SkPoint* verts = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 68 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 72 | fRect.toQuad(verts); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 73 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 74 | helper.recordDraw(target, gp.get()); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 75 | } |
| 76 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 77 | SkRect fRect; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 78 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 79 | typedef GrTestBatch INHERITED; |
| 80 | }; |
| 81 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 82 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 83 | * This GM directly exercises a GrProcessor that draws convex polygons. |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 84 | */ |
| 85 | class ConvexPolyEffect : public GM { |
| 86 | public: |
| 87 | ConvexPolyEffect() { |
| 88 | this->setBGColor(0xFFFFFFFF); |
| 89 | } |
| 90 | |
| 91 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | SkString onShortName() override { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 93 | return SkString("convex_poly_effect"); |
| 94 | } |
| 95 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 97 | return SkISize::Make(720, 800); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 98 | } |
| 99 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 100 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 101 | SkPath tri; |
| 102 | tri.moveTo(5.f, 5.f); |
| 103 | tri.lineTo(100.f, 20.f); |
| 104 | tri.lineTo(15.f, 100.f); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 105 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 106 | fPaths.addToTail(tri); |
| 107 | fPaths.addToTail(SkPath())->reverseAddPath(tri); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 109 | tri.close(); |
| 110 | fPaths.addToTail(tri); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 112 | SkPath ngon; |
| 113 | static const SkScalar kRadius = 50.f; |
| 114 | const SkPoint center = { kRadius, kRadius }; |
| 115 | for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) { |
| 116 | SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges; |
| 117 | SkPoint point; |
| 118 | point.fY = SkScalarSinCos(angle, &point.fX); |
| 119 | point.scale(kRadius); |
| 120 | point = center + point; |
| 121 | if (0 == i) { |
| 122 | ngon.moveTo(point); |
| 123 | } else { |
| 124 | ngon.lineTo(point); |
| 125 | } |
| 126 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 127 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 128 | fPaths.addToTail(ngon); |
| 129 | SkMatrix scaleM; |
| 130 | scaleM.setScale(1.1f, 0.4f); |
| 131 | ngon.transform(scaleM); |
| 132 | fPaths.addToTail(ngon); |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 133 | |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 134 | SkPath linePath; |
| 135 | linePath.moveTo(5.f, 5.f); |
| 136 | linePath.lineTo(6.f, 6.f); |
| 137 | fPaths.addToTail(linePath); |
| 138 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 139 | // integer edges |
| 140 | fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f)); |
| 141 | // half-integer edges |
| 142 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f)); |
| 143 | // vertically/horizontally thin rects that cover pixel centers |
| 144 | fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f)); |
| 145 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f)); |
| 146 | // vertically/horizontally thin rects that don't cover pixel centers |
| 147 | fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f)); |
| 148 | fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f)); |
| 149 | // small in x and y |
| 150 | fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f)); |
| 151 | // inverted in x and y |
| 152 | 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] | 153 | } |
| 154 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 155 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 156 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 157 | if (!drawContext) { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 158 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 162 | SkScalar y = 0; |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 163 | static const SkScalar kDX = 12.f; |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 164 | for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 165 | iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 166 | iter.next()) { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 167 | const SkPath* path = iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 168 | SkScalar x = 0; |
| 169 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 170 | for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 171 | const SkMatrix m = SkMatrix::MakeTrans(x, y); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 172 | SkPath p; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 173 | path->transform(m, &p); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 174 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 175 | GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 176 | sk_sp<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, p)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 178 | continue; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 179 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 180 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 181 | GrPaint grPaint; |
| 182 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
| 183 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 184 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 185 | SkAutoTUnref<GrDrawBatch> batch(new PolyBoundsBatch(p.getBounds(), 0xff000000)); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 186 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 187 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 188 | |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 189 | x += SkScalarCeilToScalar(path->getBounds().width() + kDX); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 190 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 191 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 192 | // Draw AA and non AA paths using normal API for reference. |
| 193 | canvas->save(); |
| 194 | canvas->translate(x, y); |
| 195 | SkPaint paint; |
| 196 | canvas->drawPath(*path, paint); |
| 197 | canvas->translate(path->getBounds().width() + 10.f, 0); |
| 198 | paint.setAntiAlias(true); |
| 199 | canvas->drawPath(*path, paint); |
| 200 | canvas->restore(); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 201 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 202 | y += SkScalarCeilToScalar(path->getBounds().height() + 20.f); |
| 203 | } |
| 204 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 205 | for (RectList::Iter iter(fRects, RectList::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 206 | iter.get(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 207 | iter.next()) { |
| 208 | |
| 209 | SkScalar x = 0; |
| 210 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 211 | for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 212 | SkRect rect = *iter.get(); |
| 213 | rect.offset(x, y); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 214 | GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 215 | sk_sp<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, rect)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 216 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 217 | continue; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 218 | } |
| 219 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 220 | GrPaint grPaint; |
| 221 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
| 222 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 223 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 224 | SkAutoTUnref<GrDrawBatch> batch(new PolyBoundsBatch(rect, 0xff000000)); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 225 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 226 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 227 | |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 228 | x += SkScalarCeilToScalar(rect.width() + kDX); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 229 | } |
| 230 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 231 | // 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] | 232 | canvas->save(); |
| 233 | canvas->translate(x, y); |
| 234 | SkPaint paint; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 235 | canvas->drawRect(*iter.get(), paint); |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 236 | x += SkScalarCeilToScalar(iter.get()->width() + kDX); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 237 | paint.setAntiAlias(true); |
| 238 | canvas->drawRect(*iter.get(), paint); |
| 239 | canvas->restore(); |
| 240 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 241 | y += SkScalarCeilToScalar(iter.get()->height() + 20.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | private: |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 246 | typedef SkTLList<SkPath, 1> PathList; |
| 247 | typedef SkTLList<SkRect, 1> RectList; |
| 248 | PathList fPaths; |
| 249 | RectList fRects; |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 250 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 251 | typedef GM INHERITED; |
| 252 | }; |
| 253 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 254 | DEF_GM(return new ConvexPolyEffect;) |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | #endif |