OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 // This test only works with the GPU backend. | 9 // This test only works with the GPU backend. |
10 | 10 |
11 #include "gm.h" | 11 #include "gm.h" |
12 | 12 |
13 #if SK_SUPPORT_GPU | 13 #if SK_SUPPORT_GPU |
14 | 14 |
| 15 #include "GrBatchTarget.h" |
| 16 #include "GrBufferAllocPool.h" |
15 #include "GrContext.h" | 17 #include "GrContext.h" |
16 #include "GrDefaultGeoProcFactory.h" | 18 #include "GrDefaultGeoProcFactory.h" |
17 #include "GrPathUtils.h" | 19 #include "GrPathUtils.h" |
18 #include "GrTest.h" | 20 #include "GrTest.h" |
| 21 #include "GrTestBatch.h" |
19 #include "SkColorPriv.h" | 22 #include "SkColorPriv.h" |
20 #include "SkDevice.h" | 23 #include "SkDevice.h" |
21 #include "SkGeometry.h" | 24 #include "SkGeometry.h" |
22 #include "SkTLList.h" | 25 #include "SkTLList.h" |
23 | 26 |
24 #include "effects/GrConvexPolyEffect.h" | 27 #include "effects/GrConvexPolyEffect.h" |
25 | 28 |
26 namespace skiagm { | 29 namespace skiagm { |
| 30 |
| 31 class ConvexPolyTestBatch : public GrTestBatch { |
| 32 public: |
| 33 struct Geometry : public GrTestBatch::Geometry { |
| 34 SkRect fBounds; |
| 35 }; |
| 36 |
| 37 const char* name() const SK_OVERRIDE { return "ConvexPolyTestBatch"; } |
| 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) |
| 45 : INHERITED(gp) |
| 46 , fGeometry(geo) { |
| 47 } |
| 48 |
| 49 Geometry* geoData(int index) SK_OVERRIDE { |
| 50 SkASSERT(0 == index); |
| 51 return &fGeometry; |
| 52 } |
| 53 |
| 54 void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeli
ne) SK_OVERRIDE { |
| 55 size_t vertexStride = this->geometryProcessor()->getVertexStride(); |
| 56 |
| 57 const GrVertexBuffer* vertexBuffer; |
| 58 int firstVertex; |
| 59 |
| 60 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 61 kVertsPerCubic, |
| 62 &vertexBuffer, |
| 63 &firstVertex); |
| 64 |
| 65 SkASSERT(vertexStride == sizeof(SkPoint)); |
| 66 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices); |
| 67 |
| 68 // Make sure any artifacts around the exterior of path are visible by us
ing overly |
| 69 // conservative bounding geometry. |
| 70 fGeometry.fBounds.outset(5.f, 5.f); |
| 71 fGeometry.fBounds.toQuad(verts); |
| 72 |
| 73 GrDrawTarget::DrawInfo drawInfo; |
| 74 drawInfo.setPrimitiveType(kTriangleFan_GrPrimitiveType); |
| 75 drawInfo.setVertexBuffer(vertexBuffer); |
| 76 drawInfo.setStartVertex(firstVertex); |
| 77 drawInfo.setVertexCount(kVertsPerCubic); |
| 78 drawInfo.setStartIndex(0); |
| 79 drawInfo.setIndexCount(kIndicesPerCubic); |
| 80 drawInfo.setIndexBuffer(batchTarget->quadIndexBuffer()); |
| 81 batchTarget->draw(drawInfo); |
| 82 } |
| 83 |
| 84 Geometry fGeometry; |
| 85 |
| 86 static const int kVertsPerCubic = 4; |
| 87 static const int kIndicesPerCubic = 6; |
| 88 |
| 89 typedef GrTestBatch INHERITED; |
| 90 }; |
| 91 |
27 /** | 92 /** |
28 * This GM directly exercises a GrProcessor that draws convex polygons. | 93 * This GM directly exercises a GrProcessor that draws convex polygons. |
29 */ | 94 */ |
30 class ConvexPolyEffect : public GM { | 95 class ConvexPolyEffect : public GM { |
31 public: | 96 public: |
32 ConvexPolyEffect() { | 97 ConvexPolyEffect() { |
33 this->setBGColor(0xFFFFFFFF); | 98 this->setBGColor(0xFFFFFFFF); |
34 } | 99 } |
35 | 100 |
36 protected: | 101 protected: |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget
(); | 161 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget
(); |
97 if (NULL == rt) { | 162 if (NULL == rt) { |
98 this->drawGpuOnlyMessage(canvas); | 163 this->drawGpuOnlyMessage(canvas); |
99 return; | 164 return; |
100 } | 165 } |
101 GrContext* context = rt->getContext(); | 166 GrContext* context = rt->getContext(); |
102 if (NULL == context) { | 167 if (NULL == context) { |
103 return; | 168 return; |
104 } | 169 } |
105 | 170 |
| 171 SkAutoTUnref<const GrGeometryProcessor> gp( |
| 172 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPositi
on_GPType, |
| 173 0xff000000)); |
| 174 |
106 SkScalar y = 0; | 175 SkScalar y = 0; |
107 for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_I
terStart); | 176 for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_I
terStart); |
108 iter.get(); | 177 iter.get(); |
109 iter.next()) { | 178 iter.next()) { |
110 const SkPath* path = iter.get(); | 179 const SkPath* path = iter.get(); |
111 SkScalar x = 0; | 180 SkScalar x = 0; |
112 | 181 |
113 for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { | 182 for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) { |
114 GrTestTarget tt; | 183 GrTestTarget tt; |
115 context->getTestTarget(&tt); | 184 context->getTestTarget(&tt); |
116 if (NULL == tt.target()) { | 185 if (NULL == tt.target()) { |
117 SkDEBUGFAIL("Couldn't get Gr test target."); | 186 SkDEBUGFAIL("Couldn't get Gr test target."); |
118 return; | 187 return; |
119 } | 188 } |
120 SkMatrix m; | 189 SkMatrix m; |
121 SkPath p; | 190 SkPath p; |
122 m.setTranslate(x, y); | 191 m.setTranslate(x, y); |
123 path->transform(m, &p); | 192 path->transform(m, &p); |
124 | 193 |
125 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; | 194 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
126 SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(
edgeType, p)); | 195 SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(
edgeType, p)); |
127 if (!fp) { | 196 if (!fp) { |
128 continue; | 197 continue; |
129 } | 198 } |
130 | 199 |
131 GrPipelineBuilder pipelineBuilder; | 200 GrPipelineBuilder pipelineBuilder; |
132 SkAutoTUnref<const GrGeometryProcessor> gp( | |
133 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory:
:kPosition_GPType, | |
134 0xff000000)); | |
135 pipelineBuilder.addCoverageProcessor(fp); | 201 pipelineBuilder.addCoverageProcessor(fp); |
136 pipelineBuilder.setRenderTarget(rt); | 202 pipelineBuilder.setRenderTarget(rt); |
137 | 203 |
138 GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVer
texStride(), 0); | 204 ConvexPolyTestBatch::Geometry geometry; |
139 SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); | 205 geometry.fColor = gp->color(); |
140 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); | 206 geometry.fBounds = p.getBounds(); |
141 | 207 |
142 SkRect bounds = p.getBounds(); | 208 SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geom
etry)); |
143 // Make sure any artifacts around the exterior of path are visib
le by using overly | |
144 // conservative bounding geometry. | |
145 bounds.outset(5.f, 5.f); | |
146 bounds.toQuad(verts); | |
147 | 209 |
148 tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer(
)); | 210 tt.target()->drawBatch(&pipelineBuilder, batch, NULL); |
149 tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPr
imitiveType, | |
150 0, 0, 4, 6); | |
151 | 211 |
152 x += SkScalarCeilToScalar(path->getBounds().width() + 10.f); | 212 x += SkScalarCeilToScalar(path->getBounds().width() + 10.f); |
153 } | 213 } |
154 | 214 |
155 // Draw AA and non AA paths using normal API for reference. | 215 // Draw AA and non AA paths using normal API for reference. |
156 canvas->save(); | 216 canvas->save(); |
157 canvas->translate(x, y); | 217 canvas->translate(x, y); |
158 SkPaint paint; | 218 SkPaint paint; |
159 canvas->drawPath(*path, paint); | 219 canvas->drawPath(*path, paint); |
160 canvas->translate(path->getBounds().width() + 10.f, 0); | 220 canvas->translate(path->getBounds().width() + 10.f, 0); |
(...skipping 19 matching lines...) Expand all Loading... |
180 } | 240 } |
181 SkRect rect = *iter.get(); | 241 SkRect rect = *iter.get(); |
182 rect.offset(x, y); | 242 rect.offset(x, y); |
183 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; | 243 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et; |
184 SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(
edgeType, rect)); | 244 SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(
edgeType, rect)); |
185 if (!fp) { | 245 if (!fp) { |
186 continue; | 246 continue; |
187 } | 247 } |
188 | 248 |
189 GrPipelineBuilder pipelineBuilder; | 249 GrPipelineBuilder pipelineBuilder; |
190 SkAutoTUnref<const GrGeometryProcessor> gp( | |
191 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory:
:kPosition_GPType, | |
192 0xff000000)); | |
193 pipelineBuilder.addCoverageProcessor(fp); | 250 pipelineBuilder.addCoverageProcessor(fp); |
194 pipelineBuilder.setRenderTarget(rt); | 251 pipelineBuilder.setRenderTarget(rt); |
195 | 252 |
196 GrDrawTarget::AutoReleaseGeometry geo(tt.target(), 4, gp->getVer
texStride(), 0); | 253 ConvexPolyTestBatch::Geometry geometry; |
197 SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); | 254 geometry.fColor = gp->color(); |
198 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); | 255 geometry.fBounds = rect; |
199 | 256 |
200 SkRect bounds = rect; | 257 SkAutoTUnref<GrBatch> batch(ConvexPolyTestBatch::Create(gp, geom
etry)); |
201 bounds.outset(5.f, 5.f); | |
202 bounds.toQuad(verts); | |
203 | 258 |
204 tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer(
)); | 259 tt.target()->drawBatch(&pipelineBuilder, batch, NULL); |
205 tt.target()->drawIndexed(&pipelineBuilder, gp, kTriangleFan_GrPr
imitiveType, | |
206 0, 0, 4, 6); | |
207 | 260 |
208 x += SkScalarCeilToScalar(rect.width() + 10.f); | 261 x += SkScalarCeilToScalar(rect.width() + 10.f); |
209 } | 262 } |
210 | 263 |
211 // Draw rect without and with AA using normal API for reference | 264 // Draw rect without and with AA using normal API for reference |
212 canvas->save(); | 265 canvas->save(); |
213 canvas->translate(x, y); | 266 canvas->translate(x, y); |
214 SkPaint paint; | 267 SkPaint paint; |
215 canvas->drawRect(*iter.get(), paint); | 268 canvas->drawRect(*iter.get(), paint); |
216 x += SkScalarCeilToScalar(iter.get()->width() + 10.f); | 269 x += SkScalarCeilToScalar(iter.get()->width() + 10.f); |
217 paint.setAntiAlias(true); | 270 paint.setAntiAlias(true); |
218 canvas->drawRect(*iter.get(), paint); | 271 canvas->drawRect(*iter.get(), paint); |
219 canvas->restore(); | 272 canvas->restore(); |
220 | 273 |
221 y += SkScalarCeilToScalar(iter.get()->height() + 20.f); | 274 y += SkScalarCeilToScalar(iter.get()->height() + 20.f); |
222 } | 275 } |
223 } | 276 } |
224 | 277 |
225 private: | 278 private: |
226 SkTLList<SkPath> fPaths; | 279 SkTLList<SkPath> fPaths; |
227 SkTLList<SkRect> fRects; | 280 SkTLList<SkRect> fRects; |
228 | 281 |
229 typedef GM INHERITED; | 282 typedef GM INHERITED; |
230 }; | 283 }; |
231 | 284 |
232 DEF_GM( return SkNEW(ConvexPolyEffect); ) | 285 DEF_GM( return SkNEW(ConvexPolyEffect); ) |
233 } | 286 } |
234 | 287 |
235 #endif | 288 #endif |
OLD | NEW |