Add hairlines batch unit test

BUG=skia:

Review URL: https://codereview.chromium.org/1119563002
This commit is contained in:
joshualitt 2015-05-02 07:07:17 -07:00 committed by Commit bot
parent 4af6580b44
commit 40ded32418
3 changed files with 102 additions and 15 deletions

View File

@ -16,6 +16,7 @@
#include "SkRandom.h"
class SkMatrix;
class SkPath;
class SkRRect;
struct SkRect;
@ -28,6 +29,7 @@ const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
const SkRect& TestRect(SkRandom*);
const SkRRect& TestRRectSimple(SkRandom*);
const SkPath& TestPath(SkRandom*);
}

View File

@ -9,6 +9,7 @@
#include "GrBatch.h"
#include "GrBatchTarget.h"
#include "GrBatchTest.h"
#include "GrBufferAllocPool.h"
#include "GrContext.h"
#include "GrDefaultGeoProcFactory.h"
@ -1028,6 +1029,29 @@ void AAHairlineBatch::generateGeometry(GrBatchTarget* batchTarget, const GrPipel
}
}
static GrBatch* create_hairline_batch(GrColor color,
const SkMatrix& viewMatrix,
const SkPath& path,
const GrStrokeInfo& stroke,
const SkIRect& devClipBounds,
const GrIndexBuffer* linesIndexBuffer,
const GrIndexBuffer* quadsIndexBuffer) {
SkScalar hairlineCoverage;
uint8_t newCoverage = 0xff;
if (GrPathRenderer::IsStrokeHairlineOrEquivalent(stroke, viewMatrix, &hairlineCoverage)) {
newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
}
AAHairlineBatch::Geometry geometry;
geometry.fColor = color;
geometry.fCoverage = newCoverage;
geometry.fViewMatrix = viewMatrix;
geometry.fPath = path;
geometry.fDevClipBounds = devClipBounds;
return AAHairlineBatch::Create(geometry, linesIndexBuffer, quadsIndexBuffer);
}
bool GrAAHairLinePathRenderer::onDrawPath(GrDrawTarget* target,
GrPipelineBuilder* pipelineBuilder,
GrColor color,
@ -1040,26 +1064,45 @@ bool GrAAHairLinePathRenderer::onDrawPath(GrDrawTarget* target,
return false;
}
SkScalar hairlineCoverage;
uint8_t newCoverage = 0xff;
if (IsStrokeHairlineOrEquivalent(stroke, viewMatrix, &hairlineCoverage)) {
newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
}
SkIRect devClipBounds;
pipelineBuilder->clip().getConservativeBounds(pipelineBuilder->getRenderTarget(),
&devClipBounds);
AAHairlineBatch::Geometry geometry;
geometry.fColor = color;
geometry.fCoverage = newCoverage;
geometry.fViewMatrix = viewMatrix;
geometry.fPath = path;
geometry.fDevClipBounds = devClipBounds;
SkAutoTUnref<GrBatch> batch(AAHairlineBatch::Create(geometry, fLinesIndexBuffer,
fQuadsIndexBuffer));
SkAutoTUnref<GrBatch> batch(create_hairline_batch(color, viewMatrix, path, stroke,
devClipBounds, fLinesIndexBuffer,
fQuadsIndexBuffer));
target->drawBatch(pipelineBuilder, batch);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef GR_TEST_UTILS
BATCH_TEST_DEFINE(AAHairlineBatch) {
// TODO put these in the cache
static GrIndexBuffer* gQuadIndexBuffer;
static GrIndexBuffer* gLineIndexBuffer;
if (!gQuadIndexBuffer) {
gQuadIndexBuffer = context->getGpu()->createInstancedIndexBuffer(kQuadIdxBufPattern,
kIdxsPerQuad,
kQuadsNumInIdxBuffer,
kQuadNumVertices);
gLineIndexBuffer = context->getGpu()->createInstancedIndexBuffer(kLineSegIdxBufPattern,
kIdxsPerLineSeg,
kLineSegsNumInIdxBuffer,
kLineSegNumVertices);
}
GrColor color = GrRandomColor(random);
SkMatrix viewMatrix = GrTest::TestMatrix(random);
GrStrokeInfo stroke(SkStrokeRec::kHairline_InitStyle);
SkPath path = GrTest::TestPath(random);
SkIRect devClipBounds;
devClipBounds.setEmpty();
return create_hairline_batch(color, viewMatrix, path, stroke, devClipBounds, gLineIndexBuffer,
gQuadIndexBuffer);
}
#endif

View File

@ -7,6 +7,7 @@
#include "GrTestUtils.h"
#include "SkMatrix.h"
#include "SkPath.h"
#include "SkRRect.h"
#ifdef GR_TEST_UTILS
@ -120,6 +121,47 @@ const SkRRect& TestRRectSimple(SkRandom* random) {
return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
}
const SkPath& TestPath(SkRandom* random) {
static SkPath gPath[7];
static bool gOnce;
if (!gOnce) {
gOnce = true;
// line
gPath[0].moveTo(0.f, 0.f);
gPath[0].lineTo(10.f, 10.f);
// quad
gPath[1].moveTo(0.f, 0.f);
gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
// conic
gPath[2].moveTo(0.f, 0.f);
gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
// cubic
gPath[3].moveTo(0.f, 0.f);
gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
// all three
gPath[4].moveTo(0.f, 0.f);
gPath[4].lineTo(10.f, 10.f);
gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
// convex
gPath[5].moveTo(0.0f, 0.0f);
gPath[5].lineTo(10.0f, 0.0f);
gPath[5].lineTo(10.0f, 10.0f);
gPath[5].lineTo(0.0f, 10.0f);
gPath[5].close();
// concave
gPath[6].moveTo(0.0f, 0.0f);
gPath[6].lineTo(5.0f, 5.0f);
gPath[6].lineTo(10.0f, 0.0f);
gPath[6].lineTo(10.0f, 10.0f);
gPath[6].lineTo(0.0f, 10.0f);
gPath[6].close();
}
return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
}
};
#endif