3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 // This test only works with the GPU backend.
15 #include "GrBatchTarget.h"
16 #include "GrBufferAllocPool.h"
17 #include "GrContext.h"
18 #include "GrPathUtils.h"
20 #include "GrTestBatch.h"
21 #include "SkColorPriv.h"
23 #include "SkGeometry.h"
25 #include "effects/GrBezierEffect.h"
27 static inline SkScalar eval_line(const SkPoint& p, const SkScalar lineEq[3], SkScalar sign) {
28 return sign * (lineEq[0] * p.fX + lineEq[1] * p.fY + lineEq[2]);
33 class BezierCubicOrConicTestBatch : public GrTestBatch {
35 struct Geometry : public GrTestBatch::Geometry {
39 const char* name() const SK_OVERRIDE { return "BezierCubicOrConicTestBatch"; }
41 static GrBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo,
42 const SkScalar klmEqs[9], SkScalar sign) {
43 return SkNEW_ARGS(BezierCubicOrConicTestBatch, (gp, geo, klmEqs, sign));
47 BezierCubicOrConicTestBatch(const GrGeometryProcessor* gp, const Geometry& geo,
48 const SkScalar klmEqs[9], SkScalar sign)
50 for (int i = 0; i < 9; i++) {
51 fKlmEqs[i] = klmEqs[i];
60 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
63 Geometry* geoData(int index) SK_OVERRIDE {
68 void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE {
69 size_t vertexStride = this->geometryProcessor()->getVertexStride();
71 const GrVertexBuffer* vertexBuffer;
74 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
79 if (!vertices || !batchTarget->quadIndexBuffer()) {
80 SkDebugf("Could not allocate buffers\n");
84 SkASSERT(vertexStride == sizeof(Vertex));
85 Vertex* verts = reinterpret_cast<Vertex*>(vertices);
87 verts[0].fPosition.setRectFan(fGeometry.fBounds.fLeft, fGeometry.fBounds.fTop,
88 fGeometry.fBounds.fRight, fGeometry.fBounds.fBottom,
90 for (int v = 0; v < 4; ++v) {
91 verts[v].fKLM[0] = eval_line(verts[v].fPosition, fKlmEqs + 0, fSign);
92 verts[v].fKLM[1] = eval_line(verts[v].fPosition, fKlmEqs + 3, fSign);
93 verts[v].fKLM[2] = eval_line(verts[v].fPosition, fKlmEqs + 6, 1.f);
96 GrDrawTarget::DrawInfo drawInfo;
97 drawInfo.setPrimitiveType(kTriangleFan_GrPrimitiveType);
98 drawInfo.setVertexBuffer(vertexBuffer);
99 drawInfo.setStartVertex(firstVertex);
100 drawInfo.setVertexCount(kVertsPerCubic);
101 drawInfo.setStartIndex(0);
102 drawInfo.setIndexCount(kIndicesPerCubic);
103 drawInfo.setIndexBuffer(batchTarget->quadIndexBuffer());
104 batchTarget->draw(drawInfo);
111 static const int kVertsPerCubic = 4;
112 static const int kIndicesPerCubic = 6;
114 typedef GrTestBatch INHERITED;
118 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
120 class BezierCubicEffects : public GM {
122 BezierCubicEffects() {
123 this->setBGColor(0xFFFFFFFF);
127 SkString onShortName() SK_OVERRIDE {
128 return SkString("bezier_cubic_effects");
131 SkISize onISize() SK_OVERRIDE {
132 return SkISize::Make(800, 800);
135 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
136 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
138 this->drawGpuOnlyMessage(canvas);
141 GrContext* context = rt->getContext();
142 if (NULL == context) {
148 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
151 static const int kNumCubics = 15;
154 // Mult by 3 for each edge effect type
155 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3)));
156 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols);
157 SkScalar w = SkIntToScalar(rt->width()) / numCols;
158 SkScalar h = SkIntToScalar(rt->height()) / numRows;
162 for (int i = 0; i < kNumCubics; ++i) {
163 SkPoint baseControlPts[] = {
164 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
165 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
166 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
167 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
169 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
170 SkAutoTUnref<GrGeometryProcessor> gp;
171 { // scope to contain GrTestTarget
173 context->getTestTarget(&tt);
174 if (NULL == tt.target()) {
177 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
178 gp.reset(GrCubicEffect::Create(0xff000000, SkMatrix::I(), et,
179 *tt.target()->caps()));
185 SkScalar x = SkScalarMul(col, w);
186 SkScalar y = SkScalarMul(row, h);
187 SkPoint controlPts[] = {
188 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
189 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
190 {x + baseControlPts[2].fX, y + baseControlPts[2].fY},
191 {x + baseControlPts[3].fX, y + baseControlPts[3].fY}
195 SkScalar klmSigns[3];
196 int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
202 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
203 for (int i = 0; i < 4; ++i) {
204 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
208 polyPaint.setColor(0xffA0A0A0);
209 polyPaint.setStrokeWidth(0);
210 polyPaint.setStyle(SkPaint::kStroke_Style);
211 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
213 SkPaint choppedPtPaint;
214 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
216 for (int c = 0; c < cnt; ++c) {
217 SkPoint* pts = chopped + 3 * c;
219 for (int i = 0; i < 4; ++i) {
220 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
227 boundsPaint.setColor(0xff808080);
228 boundsPaint.setStrokeWidth(0);
229 boundsPaint.setStyle(SkPaint::kStroke_Style);
230 canvas->drawRect(bounds, boundsPaint);
233 context->getTestTarget(&tt);
234 SkASSERT(tt.target());
236 GrPipelineBuilder pipelineBuilder;
237 pipelineBuilder.setRenderTarget(rt);
239 BezierCubicOrConicTestBatch::Geometry geometry;
240 geometry.fColor = gp->color();
241 geometry.fBounds = bounds;
243 SkAutoTUnref<GrBatch> batch(
244 BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs, klmSigns[c]));
246 tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
249 if (numCols == col) {
258 typedef GM INHERITED;
261 //////////////////////////////////////////////////////////////////////////////
264 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
266 class BezierConicEffects : public GM {
268 BezierConicEffects() {
269 this->setBGColor(0xFFFFFFFF);
273 SkString onShortName() SK_OVERRIDE {
274 return SkString("bezier_conic_effects");
277 SkISize onISize() SK_OVERRIDE {
278 return SkISize::Make(800, 800);
282 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
283 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
285 this->drawGpuOnlyMessage(canvas);
288 GrContext* context = rt->getContext();
289 if (NULL == context) {
295 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
298 static const int kNumConics = 10;
301 // Mult by 3 for each edge effect type
302 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3)));
303 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols);
304 SkScalar w = SkIntToScalar(rt->width()) / numCols;
305 SkScalar h = SkIntToScalar(rt->height()) / numRows;
309 for (int i = 0; i < kNumConics; ++i) {
310 SkPoint baseControlPts[] = {
311 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
312 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
313 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
315 SkScalar weight = rand.nextRangeF(0.f, 2.f);
316 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
317 SkAutoTUnref<GrGeometryProcessor> gp;
318 { // scope to contain GrTestTarget
320 context->getTestTarget(&tt);
321 if (NULL == tt.target()) {
324 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
325 gp.reset(GrConicEffect::Create(0xff000000, SkMatrix::I(), et,
326 *tt.target()->caps(), SkMatrix::I()));
332 SkScalar x = SkScalarMul(col, w);
333 SkScalar y = SkScalarMul(row, h);
334 SkPoint controlPts[] = {
335 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
336 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
337 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
341 int cnt = chop_conic(controlPts, dst, weight);
342 GrPathUtils::getConicKLM(controlPts, weight, klmEqs);
345 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
346 for (int i = 0; i < 3; ++i) {
347 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
351 polyPaint.setColor(0xffA0A0A0);
352 polyPaint.setStrokeWidth(0);
353 polyPaint.setStyle(SkPaint::kStroke_Style);
354 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
356 SkPaint choppedPtPaint;
357 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
359 for (int c = 0; c < cnt; ++c) {
360 SkPoint* pts = dst[c].fPts;
361 for (int i = 0; i < 3; ++i) {
362 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
366 //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}};
367 //bounds.set(bPts, 2);
371 boundsPaint.setColor(0xff808080);
372 boundsPaint.setStrokeWidth(0);
373 boundsPaint.setStyle(SkPaint::kStroke_Style);
374 canvas->drawRect(bounds, boundsPaint);
377 context->getTestTarget(&tt);
378 SkASSERT(tt.target());
380 GrPipelineBuilder pipelineBuilder;
381 pipelineBuilder.setRenderTarget(rt);
383 BezierCubicOrConicTestBatch::Geometry geometry;
384 geometry.fColor = gp->color();
385 geometry.fBounds = bounds;
387 SkAutoTUnref<GrBatch> batch(
388 BezierCubicOrConicTestBatch::Create(gp, geometry, klmEqs, 1.f));
390 tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
393 if (numCols == col) {
402 // Uses the max curvature function for quads to estimate
403 // where to chop the conic. If the max curvature is not
404 // found along the curve segment it will return 1 and
405 // dst[0] is the original conic. If it returns 2 the dst[0]
406 // and dst[1] are the two new conics.
407 int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
408 SkScalar t = SkFindQuadMaxCurvature(src);
411 dst[0].set(src, weight);
417 conic.set(src, weight);
418 conic.chopAt(t, dst);
424 // Calls split_conic on the entire conic and then once more on each subsection.
425 // Most cases will result in either 1 conic (chop point is not within t range)
426 // or 3 points (split once and then one subsection is split again).
427 int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
429 int conicCnt = split_conic(src, dstTemp, weight);
431 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
432 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
439 typedef GM INHERITED;
442 //////////////////////////////////////////////////////////////////////////////
444 class BezierQuadTestBatch : public GrTestBatch {
446 struct Geometry : public GrTestBatch::Geometry {
450 const char* name() const SK_OVERRIDE { return "BezierQuadTestBatch"; }
452 static GrBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo,
453 const GrPathUtils::QuadUVMatrix& devToUV) {
454 return SkNEW_ARGS(BezierQuadTestBatch, (gp, geo, devToUV));
458 BezierQuadTestBatch(const GrGeometryProcessor* gp, const Geometry& geo,
459 const GrPathUtils::QuadUVMatrix& devToUV)
462 , fDevToUV(devToUV) {
467 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
470 Geometry* geoData(int index) SK_OVERRIDE {
471 SkASSERT(0 == index);
475 void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE {
476 size_t vertexStride = this->geometryProcessor()->getVertexStride();
478 const GrVertexBuffer* vertexBuffer;
481 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
486 if (!vertices || !batchTarget->quadIndexBuffer()) {
487 SkDebugf("Could not allocate buffers\n");
491 SkASSERT(vertexStride == sizeof(Vertex));
492 Vertex* verts = reinterpret_cast<Vertex*>(vertices);
494 verts[0].fPosition.setRectFan(fGeometry.fBounds.fLeft, fGeometry.fBounds.fTop,
495 fGeometry.fBounds.fRight, fGeometry.fBounds.fBottom,
498 fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
501 GrDrawTarget::DrawInfo drawInfo;
502 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
503 drawInfo.setVertexBuffer(vertexBuffer);
504 drawInfo.setStartVertex(firstVertex);
505 drawInfo.setVertexCount(kVertsPerCubic);
506 drawInfo.setStartIndex(0);
507 drawInfo.setIndexCount(kIndicesPerCubic);
508 drawInfo.setIndexBuffer(batchTarget->quadIndexBuffer());
509 batchTarget->draw(drawInfo);
513 GrPathUtils::QuadUVMatrix fDevToUV;
515 static const int kVertsPerCubic = 4;
516 static const int kIndicesPerCubic = 6;
518 typedef GrTestBatch INHERITED;
522 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
524 class BezierQuadEffects : public GM {
526 BezierQuadEffects() {
527 this->setBGColor(0xFFFFFFFF);
531 SkString onShortName() SK_OVERRIDE {
532 return SkString("bezier_quad_effects");
535 SkISize onISize() SK_OVERRIDE {
536 return SkISize::Make(800, 800);
540 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
541 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
543 this->drawGpuOnlyMessage(canvas);
546 GrContext* context = rt->getContext();
547 if (NULL == context) {
553 float fUV[4]; // The last two values are ignored. The effect expects a vec4f.
556 static const int kNumQuads = 5;
559 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3)));
560 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols);
561 SkScalar w = SkIntToScalar(rt->width()) / numCols;
562 SkScalar h = SkIntToScalar(rt->height()) / numRows;
566 for (int i = 0; i < kNumQuads; ++i) {
567 SkPoint baseControlPts[] = {
568 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
569 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
570 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
572 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
573 SkAutoTUnref<GrGeometryProcessor> gp;
574 { // scope to contain GrTestTarget
576 context->getTestTarget(&tt);
577 if (NULL == tt.target()) {
580 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
581 gp.reset(GrQuadEffect::Create(0xff000000, SkMatrix::I(), et,
582 *tt.target()->caps(), SkMatrix::I()));
588 SkScalar x = SkScalarMul(col, w);
589 SkScalar y = SkScalarMul(row, h);
590 SkPoint controlPts[] = {
591 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
592 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
593 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
596 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
599 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
600 for (int i = 0; i < 3; ++i) {
601 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
605 polyPaint.setColor(0xffA0A0A0);
606 polyPaint.setStrokeWidth(0);
607 polyPaint.setStyle(SkPaint::kStroke_Style);
608 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
610 SkPaint choppedPtPaint;
611 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
613 for (int c = 0; c < cnt; ++c) {
614 SkPoint* pts = chopped + 2 * c;
616 for (int i = 0; i < 3; ++i) {
617 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
624 boundsPaint.setColor(0xff808080);
625 boundsPaint.setStrokeWidth(0);
626 boundsPaint.setStyle(SkPaint::kStroke_Style);
627 canvas->drawRect(bounds, boundsPaint);
630 context->getTestTarget(&tt);
631 SkASSERT(tt.target());
633 GrPipelineBuilder pipelineBuilder;
634 pipelineBuilder.setRenderTarget(rt);
636 GrPathUtils::QuadUVMatrix DevToUV(pts);
638 BezierQuadTestBatch::Geometry geometry;
639 geometry.fColor = gp->color();
640 geometry.fBounds = bounds;
642 SkAutoTUnref<GrBatch> batch(BezierQuadTestBatch::Create(gp, geometry, DevToUV));
644 tt.target()->drawBatch(&pipelineBuilder, batch, NULL);
647 if (numCols == col) {
656 typedef GM INHERITED;
659 DEF_GM( return SkNEW(BezierCubicEffects); )
660 DEF_GM( return SkNEW(BezierConicEffects); )
661 DEF_GM( return SkNEW(BezierQuadEffects); )