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: |
| 37 | virtual SkString onShortName() SK_OVERRIDE { |
| 38 | return SkString("convex_poly_effect"); |
| 39 | } |
| 40 | |
| 41 | virtual 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 | |
| 45 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 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 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 50 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 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 | |
| 100 | virtual 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 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 135 | GrDrawState ds; |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 136 | const GrGeometryProcessor* gp = GrDefaultGeoProcFactory::Create(0xff000000); |
| 137 | ds.setGeometryProcessor(gp)->unref(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 138 | ds.addCoverageProcessor(fp); |
| 139 | ds.setIdentityViewMatrix(); |
| 140 | ds.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()); |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 153 | tt.target()->drawIndexed(&ds, kTriangleFan_GrPrimitiveType, 0, 0, 4, 6); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 154 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 155 | x += SkScalarCeilToScalar(path->getBounds().width() + 10.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 156 | } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 157 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 158 | // Draw AA and non AA paths using normal API for reference. |
| 159 | canvas->save(); |
| 160 | canvas->translate(x, y); |
| 161 | SkPaint paint; |
| 162 | canvas->drawPath(*path, paint); |
| 163 | canvas->translate(path->getBounds().width() + 10.f, 0); |
| 164 | paint.setAntiAlias(true); |
| 165 | canvas->drawPath(*path, paint); |
| 166 | canvas->restore(); |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 167 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 168 | y += SkScalarCeilToScalar(path->getBounds().height() + 20.f); |
| 169 | } |
| 170 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 171 | for (SkTLList<SkRect>::Iter iter(fRects, SkTLList<SkRect>::Iter::kHead_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 172 | iter.get(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 173 | iter.next()) { |
| 174 | |
| 175 | SkScalar x = 0; |
| 176 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 178 | GrTestTarget tt; |
| 179 | context->getTestTarget(&tt); |
| 180 | if (NULL == tt.target()) { |
| 181 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 182 | return; |
| 183 | } |
| 184 | SkRect rect = *iter.get(); |
| 185 | rect.offset(x, y); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 186 | GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
| 187 | SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, rect)); |
| 188 | if (!fp) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 189 | continue; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 190 | } |
| 191 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 192 | GrDrawState ds; |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 193 | const GrGeometryProcessor* gp = GrDefaultGeoProcFactory::Create(0xff000000); |
| 194 | ds.setGeometryProcessor(gp)->unref(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 195 | ds.addCoverageProcessor(fp); |
| 196 | ds.setIdentityViewMatrix(); |
| 197 | ds.setRenderTarget(rt); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 198 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 199 | GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVertexStride(), 0); |
| 200 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
joshualitt | 50408ad | 2014-11-03 12:31:14 -0800 | [diff] [blame] | 201 | SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); |
| 202 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 203 | SkRect bounds = rect; |
| 204 | bounds.outset(5.f, 5.f); |
| 205 | bounds.toQuad(verts); |
| 206 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 207 | tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer()); |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 208 | tt.target()->drawIndexed(&ds, kTriangleFan_GrPrimitiveType, 0, 0, 4, 6); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 209 | |
| 210 | x += SkScalarCeilToScalar(rect.width() + 10.f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 211 | } |
| 212 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 213 | // 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] | 214 | canvas->save(); |
| 215 | canvas->translate(x, y); |
| 216 | SkPaint paint; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 217 | canvas->drawRect(*iter.get(), paint); |
| 218 | x += SkScalarCeilToScalar(iter.get()->width() + 10.f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 219 | paint.setAntiAlias(true); |
| 220 | canvas->drawRect(*iter.get(), paint); |
| 221 | canvas->restore(); |
| 222 | |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 223 | y += SkScalarCeilToScalar(iter.get()->height() + 20.f); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | SkTLList<SkPath> fPaths; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 229 | SkTLList<SkRect> fRects; |
skia.committer@gmail.com | f0b0cda | 2014-02-09 03:02:01 +0000 | [diff] [blame] | 230 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 231 | typedef GM INHERITED; |
| 232 | }; |
| 233 | |
| 234 | DEF_GM( return SkNEW(ConvexPolyEffect); ) |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | #endif |