commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | // This test only works with the GPU backend. |
| 10 | |
| 11 | #include "gm.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 15 | #include "GrBatchTarget.h" |
| 16 | #include "GrBufferAllocPool.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 17 | #include "GrContext.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 18 | #include "GrDefaultGeoProcFactory.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 19 | #include "GrPathUtils.h" |
| 20 | #include "GrTest.h" |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 21 | #include "GrTestBatch.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 22 | #include "SkColorPriv.h" |
| 23 | #include "SkDevice.h" |
| 24 | #include "SkGeometry.h" |
| 25 | #include "SkTLList.h" |
| 26 | |
| 27 | #include "effects/GrConvexPolyEffect.h" |
| 28 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 29 | namespace skiagm { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 30 | |
| 31 | class ConvexPolyTestBatch : public GrTestBatch { |
| 32 | public: |
| 33 | struct Geometry : public GrTestBatch::Geometry { |
| 34 | SkRect fBounds; |
| 35 | }; |
| 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | const char* name() const override { return "ConvexPolyTestBatch"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 38 | |
| 39 | static GrBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo) { |
| 40 | return SkNEW_ARGS(ConvexPolyTestBatch, (gp, geo)); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | ConvexPolyTestBatch(const GrGeometryProcessor* gp, const Geometry& geo) |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 45 | : INHERITED(gp, geo.fBounds) |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 46 | , fGeometry(geo) { |
| 47 | } |
| 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | Geometry* geoData(int index) override { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 50 | SkASSERT(0 == index); |
| 51 | return &fGeometry; |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 55 | size_t vertexStride = this->geometryProcessor()->getVertexStride(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 56 | SkASSERT(vertexStride == sizeof(SkPoint)); |
| 57 | QuadHelper helper; |
| 58 | SkPoint* verts = reinterpret_cast<SkPoint*>(helper.init(batchTarget, vertexStride, 1)); |
| 59 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 63 | // Make sure any artifacts around the exterior of path are visible by using overly |
| 64 | // conservative bounding geometry. |
| 65 | fGeometry.fBounds.outset(5.f, 5.f); |
| 66 | fGeometry.fBounds.toQuad(verts); |
| 67 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 68 | helper.issueDraws(batchTarget); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | Geometry fGeometry; |
| 72 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 73 | typedef GrTestBatch INHERITED; |
| 74 | }; |
| 75 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 76 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | * This GM directly exercises a GrProcessor that draws convex polygons. |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 78 | */ |
| 79 | class ConvexPolyEffect : public GM { |
| 80 | public: |
| 81 | ConvexPolyEffect() { |
| 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 | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 87 | return SkString("convex_poly_effect"); |
| 88 | } |
| 89 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 90 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 91 | return SkISize::Make(720, 800); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 92 | } |
| 93 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 94 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 95 | SkPath tri; |
| 96 | tri.moveTo(5.f, 5.f); |
| 97 | tri.lineTo(100.f, 20.f); |
| 98 | tri.lineTo(15.f, 100.f); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 99 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 100 | fPaths.addToTail(tri); |
| 101 | fPaths.addToTail(SkPath())->reverseAddPath(tri); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 103 | tri.close(); |
| 104 | fPaths.addToTail(tri); |
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 | SkPath ngon; |
| 107 | static const SkScalar kRadius = 50.f; |
| 108 | const SkPoint center = { kRadius, kRadius }; |
| 109 | for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) { |
| 110 | SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges; |
| 111 | SkPoint point; |
| 112 | point.fY = SkScalarSinCos(angle, &point.fX); |
| 113 | point.scale(kRadius); |
| 114 | point = center + point; |
| 115 | if (0 == i) { |
| 116 | ngon.moveTo(point); |
| 117 | } else { |
| 118 | ngon.lineTo(point); |
| 119 | } |
| 120 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 121 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 122 | fPaths.addToTail(ngon); |
| 123 | SkMatrix scaleM; |
| 124 | scaleM.setScale(1.1f, 0.4f); |
| 125 | ngon.transform(scaleM); |
| 126 | fPaths.addToTail(ngon); |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 127 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 128 | // integer edges |
| 129 | fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f)); |
| 130 | // half-integer edges |
| 131 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f)); |
| 132 | // vertically/horizontally thin rects that cover pixel centers |
| 133 | fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f)); |
| 134 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f)); |
| 135 | // vertically/horizontally thin rects that don't cover pixel centers |
| 136 | fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f)); |
| 137 | fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f)); |
| 138 | // small in x and y |
| 139 | fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f)); |
| 140 | // inverted in x and y |
| 141 | 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] | 142 | } |
| 143 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 144 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 145 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 146 | if (NULL == rt) { |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 147 | this->drawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | GrContext* context = rt->getContext(); |
| 151 | if (NULL == context) { |
| 152 | return; |
| 153 | } |
| 154 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 155 | SkAutoTUnref<const GrGeometryProcessor> gp( |
| 156 | GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, |
| 157 | 0xff000000)); |
| 158 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 159 | SkScalar y = 0; |
| 160 | for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 161 | iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 162 | iter.next()) { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 163 | const SkPath* path = iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 164 | SkScalar x = 0; |
| 165 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 166 | for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 167 | GrTestTarget tt; |
| 168 | context->getTestTarget(&tt); |
| 169 | if (NULL == tt.target()) { |
| 170 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 171 | return; |
| 172 | } |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 173 | const SkMatrix m = SkMatrix::MakeTrans(x, y); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 174 | SkPath p; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 175 | path->transform(m, &p); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 176 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
| 178 | SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, p)); |
| 179 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 180 | continue; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 181 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 182 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 183 | GrPipelineBuilder pipelineBuilder; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 184 | pipelineBuilder.addCoverageProcessor(fp); |
| 185 | pipelineBuilder.setRenderTarget(rt); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 186 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 187 | ConvexPolyTestBatch::Geometry geometry; |
| 188 | geometry.fColor = gp->color(); |
| 189 | geometry.fBounds = p.getBounds(); |
joshualitt | 50408ad | 2014-11-03 12:31:14 -0800 | [diff] [blame] | 190 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 191 | SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geometry)); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 192 | |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 193 | tt.target()->drawBatch(&pipelineBuilder, batch); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 194 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 195 | x += SkScalarCeilToScalar(path->getBounds().width() + 10.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 196 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 197 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 198 | // Draw AA and non AA paths using normal API for reference. |
| 199 | canvas->save(); |
| 200 | canvas->translate(x, y); |
| 201 | SkPaint paint; |
| 202 | canvas->drawPath(*path, paint); |
| 203 | canvas->translate(path->getBounds().width() + 10.f, 0); |
| 204 | paint.setAntiAlias(true); |
| 205 | canvas->drawPath(*path, paint); |
| 206 | canvas->restore(); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 207 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 208 | y += SkScalarCeilToScalar(path->getBounds().height() + 20.f); |
| 209 | } |
| 210 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 211 | for (SkTLList<SkRect>::Iter iter(fRects, SkTLList<SkRect>::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 212 | iter.get(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 213 | iter.next()) { |
| 214 | |
| 215 | SkScalar x = 0; |
| 216 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 217 | for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 218 | GrTestTarget tt; |
| 219 | context->getTestTarget(&tt); |
| 220 | if (NULL == tt.target()) { |
| 221 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 222 | return; |
| 223 | } |
| 224 | SkRect rect = *iter.get(); |
| 225 | rect.offset(x, y); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 226 | GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
| 227 | SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, rect)); |
| 228 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 229 | continue; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 230 | } |
| 231 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 232 | GrPipelineBuilder pipelineBuilder; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 233 | pipelineBuilder.addCoverageProcessor(fp); |
| 234 | pipelineBuilder.setRenderTarget(rt); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 235 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 236 | ConvexPolyTestBatch::Geometry geometry; |
| 237 | geometry.fColor = gp->color(); |
| 238 | geometry.fBounds = rect; |
joshualitt | 50408ad | 2014-11-03 12:31:14 -0800 | [diff] [blame] | 239 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 240 | SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geometry)); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 241 | |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 242 | tt.target()->drawBatch(&pipelineBuilder, batch); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 243 | |
| 244 | x += SkScalarCeilToScalar(rect.width() + 10.f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 245 | } |
| 246 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 247 | // 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] | 248 | canvas->save(); |
| 249 | canvas->translate(x, y); |
| 250 | SkPaint paint; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 251 | canvas->drawRect(*iter.get(), paint); |
| 252 | x += SkScalarCeilToScalar(iter.get()->width() + 10.f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 253 | paint.setAntiAlias(true); |
| 254 | canvas->drawRect(*iter.get(), paint); |
| 255 | canvas->restore(); |
| 256 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 257 | y += SkScalarCeilToScalar(iter.get()->height() + 20.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | private: |
| 262 | SkTLList<SkPath> fPaths; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 263 | SkTLList<SkRect> fRects; |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 264 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 265 | typedef GM INHERITED; |
| 266 | }; |
| 267 | |
| 268 | DEF_GM( return SkNEW(ConvexPolyEffect); ) |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | #endif |