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 static const GrVertexAttrib kAttribs[] = { | |
bsalomon
2014/01/29 20:18:30
ditto.
| |
26 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, | |
27 }; | |
28 | |
29 namespace skiagm { | |
30 /** | |
31 * This GM directly exercises a GrEffect that draws convex polygons. | |
32 */ | |
33 class ConvexPolyEffect : public GM { | |
34 public: | |
35 ConvexPolyEffect() { | |
36 this->setBGColor(0xFFFFFFFF); | |
37 } | |
38 | |
39 protected: | |
40 virtual SkString onShortName() SK_OVERRIDE { | |
41 return SkString("convex_poly_effect"); | |
42 } | |
43 | |
44 virtual SkISize onISize() SK_OVERRIDE { | |
45 return make_isize(475, 530); | |
46 } | |
47 | |
48 virtual uint32_t onGetFlags() const SK_OVERRIDE { | |
49 // This is a GPU-specific GM. | |
50 return kGPUOnly_Flag; | |
51 } | |
52 | |
53 virtual void onOnceBeforeDraw() SK_OVERRIDE { | |
54 SkPath tri; | |
55 tri.moveTo(5.f, 5.f); | |
56 tri.lineTo(100.f, 20.f); | |
57 tri.lineTo(15.f, 100.f); | |
58 | |
59 fPaths.addToTail(tri); | |
60 fPaths.addToTail(SkPath())->reverseAddPath(tri); | |
61 | |
62 tri.close(); | |
63 fPaths.addToTail(tri); | |
64 | |
65 SkPath ngon; | |
66 static const SkScalar kRadius = 50.f; | |
robertphillips
2014/01/29 20:57:55
You can ignore this hyper-nit but, add a space bet
bsalomon
2014/01/29 22:19:42
Done.
| |
67 const SkPoint center = { kRadius, kRadius}; | |
68 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) { | |
69 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges ; | |
70 SkPoint point; | |
71 point.fY = SkScalarSinCos(angle, &point.fX); | |
72 point.scale(kRadius); | |
73 point = center + point; | |
74 if (0 == i) { | |
75 ngon.moveTo(point); | |
76 } else { | |
77 ngon.lineTo(point); | |
78 } | |
79 } | |
80 | |
81 fPaths.addToTail(ngon); | |
82 SkMatrix scaleM; | |
83 scaleM.setScale(1.1f, 0.4f); | |
84 ngon.transform(scaleM); | |
85 fPaths.addToTail(ngon); | |
86 } | |
87 | |
88 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
89 SkBaseDevice* device = canvas->getTopDevice(); | |
90 GrRenderTarget* rt = device->accessRenderTarget(); | |
91 if (NULL == rt) { | |
92 return; | |
93 } | |
94 GrContext* context = rt->getContext(); | |
95 if (NULL == context) { | |
96 return; | |
97 } | |
98 | |
robertphillips
2014/01/29 20:57:55
Do we need this struct?
bsalomon
2014/01/29 22:19:42
Done.
| |
99 struct Vertex { | |
100 SkPoint fPosition; | |
101 }; | |
102 | |
103 const SkPath* path; | |
104 SkScalar y = 0; | |
105 for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_I terStart); | |
106 NULL != (path = iter.get()); | |
107 iter.next()) { | |
108 SkScalar x = 0; | |
109 | |
110 for (int et = 0; et < GrConvexPolyEffect::kEdgeTypeCnt; ++et) { | |
111 GrTestTarget tt; | |
112 context->getTestTarget(&tt); | |
113 if (NULL == tt.target()) { | |
114 SkDEBUGFAIL("Couldn't get Gr test target."); | |
115 return; | |
116 } | |
117 GrDrawState* drawState = tt.target()->drawState(); | |
118 drawState->setVertexAttribs<kAttribs>(SK_ARRAY_COUNT(kAttribs)); | |
119 | |
120 SkMatrix m; | |
121 SkPath p; | |
122 m.setTranslate(x, y); | |
123 path->transform(m, &p); | |
124 | |
125 GrConvexPolyEffect::EdgeType edgeType = (GrConvexPolyEffect::Edg eType) et; | |
126 SkAutoTUnref<GrEffectRef> effect(GrConvexPolyEffect::Create(edge Type, p)); | |
127 if (!effect) { | |
128 SkDEBUGFAIL("Couldn't create convex poly effect."); | |
129 return; | |
130 } | |
131 drawState->addCoverageEffect(effect, 1); | |
132 drawState->setIdentityViewMatrix(); | |
133 drawState->setRenderTarget(rt); | |
134 drawState->setColor(0xff000000); | |
135 | |
136 SkPoint verts[4]; | |
137 SkRect bounds = p.getBounds(); | |
robertphillips
2014/01/29 20:57:55
Can we more conservatively outset? By 1?
bsalomon
2014/01/29 22:19:42
I chose a larger outset so that there'd be some ar
| |
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 |