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 | |
| 15 | #include "GrContext.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 16 | #include "GrDefaultGeoProcFactory.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" |
| 20 | #include "SkDevice.h" |
| 21 | #include "SkGeometry.h" |
| 22 | #include "SkTLList.h" |
| 23 | |
| 24 | #include "effects/GrConvexPolyEffect.h" |
| 25 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 26 | namespace skiagm { |
| 27 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | * This GM directly exercises a GrProcessor that draws convex polygons. |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 29 | */ |
| 30 | class ConvexPolyEffect : public GM { |
| 31 | public: |
| 32 | ConvexPolyEffect() { |
| 33 | this->setBGColor(0xFFFFFFFF); |
| 34 | } |
| 35 | |
| 36 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 37 | SkString onShortName() SK_OVERRIDE { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 38 | return SkString("convex_poly_effect"); |
| 39 | } |
| 40 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 41 | SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 42 | return SkISize::Make(720, 800); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 43 | } |
| 44 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 45 | uint32_t onGetFlags() const SK_OVERRIDE { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 46 | // This is a GPU-specific GM. |
| 47 | return kGPUOnly_Flag; |
| 48 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 49 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 50 | void onOnceBeforeDraw() SK_OVERRIDE { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 51 | SkPath tri; |
| 52 | tri.moveTo(5.f, 5.f); |
| 53 | tri.lineTo(100.f, 20.f); |
| 54 | tri.lineTo(15.f, 100.f); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 56 | fPaths.addToTail(tri); |
| 57 | fPaths.addToTail(SkPath())->reverseAddPath(tri); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 58 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 59 | tri.close(); |
| 60 | fPaths.addToTail(tri); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 61 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 62 | SkPath ngon; |
| 63 | static const SkScalar kRadius = 50.f; |
| 64 | const SkPoint center = { kRadius, kRadius }; |
| 65 | for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) { |
| 66 | SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges; |
| 67 | SkPoint point; |
| 68 | point.fY = SkScalarSinCos(angle, &point.fX); |
| 69 | point.scale(kRadius); |
| 70 | point = center + point; |
| 71 | if (0 == i) { |
| 72 | ngon.moveTo(point); |
| 73 | } else { |
| 74 | ngon.lineTo(point); |
| 75 | } |
| 76 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 77 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 78 | fPaths.addToTail(ngon); |
| 79 | SkMatrix scaleM; |
| 80 | scaleM.setScale(1.1f, 0.4f); |
| 81 | ngon.transform(scaleM); |
| 82 | fPaths.addToTail(ngon); |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 84 | // integer edges |
| 85 | fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f)); |
| 86 | // half-integer edges |
| 87 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f)); |
| 88 | // vertically/horizontally thin rects that cover pixel centers |
| 89 | fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f)); |
| 90 | fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f)); |
| 91 | // vertically/horizontally thin rects that don't cover pixel centers |
| 92 | fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f)); |
| 93 | fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f)); |
| 94 | // small in x and y |
| 95 | fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f)); |
| 96 | // inverted in x and y |
| 97 | 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] | 98 | } |
| 99 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 100 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 101 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 102 | if (NULL == rt) { |
| 103 | return; |
| 104 | } |
| 105 | GrContext* context = rt->getContext(); |
| 106 | if (NULL == context) { |
| 107 | return; |
| 108 | } |
| 109 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 110 | SkScalar y = 0; |
| 111 | for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 112 | iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 113 | iter.next()) { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 114 | const SkPath* path = iter.get(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 115 | SkScalar x = 0; |
| 116 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 117 | for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 118 | GrTestTarget tt; |
| 119 | context->getTestTarget(&tt); |
| 120 | if (NULL == tt.target()) { |
| 121 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 122 | return; |
| 123 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 124 | SkMatrix m; |
| 125 | SkPath p; |
| 126 | m.setTranslate(x, y); |
| 127 | path->transform(m, &p); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 128 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 129 | GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
| 130 | SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, p)); |
| 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 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame^] | 135 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 136 | SkAutoTUnref<const GrGeometryProcessor> gp( |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 137 | GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, |
| 138 | 0xff000000)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame^] | 139 | pipelineBuilder.addCoverageProcessor(fp); |
| 140 | pipelineBuilder.setRenderTarget(rt); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 141 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 142 | GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0); |
| 143 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
joshualitt | 50408ad | 2014-11-03 12:31:14 -0800 | [diff] [blame] | 144 | SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); |
| 145 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 146 | SkRect bounds = p.getBounds(); |
| 147 | // Make sure any artifacts around the exterior of path are visible by using overly |
| 148 | // conservative bounding geometry. |
| 149 | bounds.outset(5.f, 5.f); |
| 150 | bounds.toQuad(verts); |
| 151 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 152 | tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer()); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame^] | 153 | tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPrimitiveType, |
| 154 | 0, 0, 4, 6); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 155 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 156 | x += SkScalarCeilToScalar(path->getBounds().width() + 10.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 157 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 158 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 159 | // Draw AA and non AA paths using normal API for reference. |
| 160 | canvas->save(); |
| 161 | canvas->translate(x, y); |
| 162 | SkPaint paint; |
| 163 | canvas->drawPath(*path, paint); |
| 164 | canvas->translate(path->getBounds().width() + 10.f, 0); |
| 165 | paint.setAntiAlias(true); |
| 166 | canvas->drawPath(*path, paint); |
| 167 | canvas->restore(); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 168 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 169 | y += SkScalarCeilToScalar(path->getBounds().height() + 20.f); |
| 170 | } |
| 171 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 172 | for (SkTLList<SkRect>::Iter iter(fRects, SkTLList<SkRect>::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 173 | iter.get(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 174 | iter.next()) { |
| 175 | |
| 176 | SkScalar x = 0; |
| 177 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 178 | for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 179 | GrTestTarget tt; |
| 180 | context->getTestTarget(&tt); |
| 181 | if (NULL == tt.target()) { |
| 182 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 183 | return; |
| 184 | } |
| 185 | SkRect rect = *iter.get(); |
| 186 | rect.offset(x, y); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 187 | GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
| 188 | SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, rect)); |
| 189 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 190 | continue; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 191 | } |
| 192 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame^] | 193 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 194 | SkAutoTUnref<const GrGeometryProcessor> gp( |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 195 | GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, |
| 196 | 0xff000000)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame^] | 197 | pipelineBuilder.addCoverageProcessor(fp); |
| 198 | pipelineBuilder.setRenderTarget(rt); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 199 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 200 | GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0); |
| 201 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
joshualitt | 50408ad | 2014-11-03 12:31:14 -0800 | [diff] [blame] | 202 | SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); |
| 203 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 204 | SkRect bounds = rect; |
| 205 | bounds.outset(5.f, 5.f); |
| 206 | bounds.toQuad(verts); |
| 207 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 208 | tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer()); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame^] | 209 | tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPrimitiveType, |
| 210 | 0, 0, 4, 6); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 211 | |
| 212 | x += SkScalarCeilToScalar(rect.width() + 10.f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 213 | } |
| 214 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 215 | // 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] | 216 | canvas->save(); |
| 217 | canvas->translate(x, y); |
| 218 | SkPaint paint; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 219 | canvas->drawRect(*iter.get(), paint); |
| 220 | x += SkScalarCeilToScalar(iter.get()->width() + 10.f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 221 | paint.setAntiAlias(true); |
| 222 | canvas->drawRect(*iter.get(), paint); |
| 223 | canvas->restore(); |
| 224 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 225 | y += SkScalarCeilToScalar(iter.get()->height() + 20.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | private: |
| 230 | SkTLList<SkPath> fPaths; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 231 | SkTLList<SkRect> fRects; |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 232 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 233 | typedef GM INHERITED; |
| 234 | }; |
| 235 | |
| 236 | DEF_GM( return SkNEW(ConvexPolyEffect); ) |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | #endif |