OLD | NEW |
(Empty) | |
| 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" |
| 16 #include "GrPathUtils.h" |
| 17 #include "GrTest.h" |
| 18 #include "SkColorPriv.h" |
| 19 #include "SkDevice.h" |
| 20 #include "SkGeometry.h" |
| 21 #include "SkTLList.h" |
| 22 |
| 23 #include "effects/GrConvexPolyEffect.h" |
| 24 |
| 25 namespace { |
| 26 extern const GrVertexAttrib kAttribs[] = { |
| 27 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 28 }; |
| 29 } |
| 30 |
| 31 namespace skiagm { |
| 32 /** |
| 33 * This GM directly exercises a GrEffect that draws convex polygons. |
| 34 */ |
| 35 class ConvexPolyEffect : public GM { |
| 36 public: |
| 37 ConvexPolyEffect() { |
| 38 this->setBGColor(0xFFFFFFFF); |
| 39 } |
| 40 |
| 41 protected: |
| 42 virtual SkString onShortName() SK_OVERRIDE { |
| 43 return SkString("convex_poly_effect"); |
| 44 } |
| 45 |
| 46 virtual SkISize onISize() SK_OVERRIDE { |
| 47 return make_isize(475, 530); |
| 48 } |
| 49 |
| 50 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 51 // This is a GPU-specific GM. |
| 52 return kGPUOnly_Flag; |
| 53 } |
| 54 |
| 55 virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 56 SkPath tri; |
| 57 tri.moveTo(5.f, 5.f); |
| 58 tri.lineTo(100.f, 20.f); |
| 59 tri.lineTo(15.f, 100.f); |
| 60 |
| 61 fPaths.addToTail(tri); |
| 62 fPaths.addToTail(SkPath())->reverseAddPath(tri); |
| 63 |
| 64 tri.close(); |
| 65 fPaths.addToTail(tri); |
| 66 |
| 67 SkPath ngon; |
| 68 static const SkScalar kRadius = 50.f; |
| 69 const SkPoint center = { kRadius, kRadius }; |
| 70 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) { |
| 71 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges
; |
| 72 SkPoint point; |
| 73 point.fY = SkScalarSinCos(angle, &point.fX); |
| 74 point.scale(kRadius); |
| 75 point = center + point; |
| 76 if (0 == i) { |
| 77 ngon.moveTo(point); |
| 78 } else { |
| 79 ngon.lineTo(point); |
| 80 } |
| 81 } |
| 82 |
| 83 fPaths.addToTail(ngon); |
| 84 SkMatrix scaleM; |
| 85 scaleM.setScale(1.1f, 0.4f); |
| 86 ngon.transform(scaleM); |
| 87 fPaths.addToTail(ngon); |
| 88 } |
| 89 |
| 90 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 91 SkBaseDevice* device = canvas->getTopDevice(); |
| 92 GrRenderTarget* rt = device->accessRenderTarget(); |
| 93 if (NULL == rt) { |
| 94 return; |
| 95 } |
| 96 GrContext* context = rt->getContext(); |
| 97 if (NULL == context) { |
| 98 return; |
| 99 } |
| 100 |
| 101 const SkPath* path; |
| 102 SkScalar y = 0; |
| 103 for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_I
terStart); |
| 104 NULL != (path = iter.get()); |
| 105 iter.next()) { |
| 106 SkScalar x = 0; |
| 107 |
| 108 for (int et = 0; et < GrConvexPolyEffect::kEdgeTypeCnt; ++et) { |
| 109 GrTestTarget tt; |
| 110 context->getTestTarget(&tt); |
| 111 if (NULL == tt.target()) { |
| 112 SkDEBUGFAIL("Couldn't get Gr test target."); |
| 113 return; |
| 114 } |
| 115 GrDrawState* drawState = tt.target()->drawState(); |
| 116 drawState->setVertexAttribs<kAttribs>(SK_ARRAY_COUNT(kAttribs)); |
| 117 |
| 118 SkMatrix m; |
| 119 SkPath p; |
| 120 m.setTranslate(x, y); |
| 121 path->transform(m, &p); |
| 122 |
| 123 GrConvexPolyEffect::EdgeType edgeType = (GrConvexPolyEffect::Edg
eType) et; |
| 124 SkAutoTUnref<GrEffectRef> effect(GrConvexPolyEffect::Create(edge
Type, p)); |
| 125 if (!effect) { |
| 126 SkDEBUGFAIL("Couldn't create convex poly effect."); |
| 127 return; |
| 128 } |
| 129 drawState->addCoverageEffect(effect, 1); |
| 130 drawState->setIdentityViewMatrix(); |
| 131 drawState->setRenderTarget(rt); |
| 132 drawState->setColor(0xff000000); |
| 133 |
| 134 SkPoint verts[4]; |
| 135 SkRect bounds = p.getBounds(); |
| 136 // Make sure any artifacts around the exterior of path are visib
le by using overly |
| 137 // conservative bounding geometry. |
| 138 bounds.outset(5.f, 5.f); |
| 139 bounds.toQuad(verts); |
| 140 |
| 141 tt.target()->setVertexSourceToArray(verts, 4); |
| 142 tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer(
)); |
| 143 tt.target()->drawIndexed(kTriangleFan_GrPrimitiveType, 0, 0, 4,
6); |
| 144 |
| 145 x += path->getBounds().width() + 10.f; |
| 146 } |
| 147 |
| 148 // Draw AA and non AA paths using normal API for reference. |
| 149 canvas->save(); |
| 150 canvas->translate(x, y); |
| 151 SkPaint paint; |
| 152 canvas->drawPath(*path, paint); |
| 153 canvas->translate(path->getBounds().width() + 10.f, 0); |
| 154 paint.setAntiAlias(true); |
| 155 canvas->drawPath(*path, paint); |
| 156 canvas->restore(); |
| 157 |
| 158 y += path->getBounds().height() + 20.f; |
| 159 } |
| 160 } |
| 161 |
| 162 private: |
| 163 SkTLList<SkPath> fPaths; |
| 164 |
| 165 typedef GM INHERITED; |
| 166 }; |
| 167 |
| 168 DEF_GM( return SkNEW(ConvexPolyEffect); ) |
| 169 |
| 170 } |
| 171 |
| 172 #endif |
OLD | NEW |