Add special handling of rectori case for gpu

https://codereview.chromium.org/15080010/



git-svn-id: http://skia.googlecode.com/svn/trunk@9175 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
robertphillips@google.com 2013-05-17 12:50:27 +00:00
Родитель 8be02fc2a7
Коммит 83d1a68141
7 изменённых файлов: 197 добавлений и 46 удалений

Просмотреть файл

@ -246,16 +246,6 @@ public:
*/
bool isRect(SkRect* rect) const;
/** Returns true if the path specifies a pair of nested rectangles. If so, and if
rect is not null, set rect[0] to the outer rectangle and rect[1] to the inner
rectangle. If the path does not specify a pair of nested rectangles, return
false and ignore rect.
@param rect If not null, returns the path as a pair of nested rectangles
@return true if the path describes a pair of nested rectangles
*/
bool isNestedRects(SkRect rect[2]) const;
/** Return the number of points in the path
*/
int countPoints() const;
@ -586,6 +576,19 @@ public:
*/
bool isRect(bool* isClosed, Direction* direction) const;
/** Returns true if the path specifies a pair of nested rectangles. If so, and if
rect is not null, set rect[0] to the outer rectangle and rect[1] to the inner
rectangle. If so, and dirs is not null, set dirs[0] to the direction of
the outer rectangle and dirs[1] to the direction of the inner rectangle. If
the path does not specify a pair of nested rectangles, return
false and ignore rect and dirs.
@param rect If not null, returns the path as a pair of nested rectangles
@param dirs If not null, returns the direction of the rects
@return true if the path describes a pair of nested rectangles
*/
bool isNestedRects(SkRect rect[2], Direction dirs[2] = NULL) const;
/**
* Add a closed rectangle contour to the path
* @param rect The rectangle to add as a closed contour to the path

Просмотреть файл

@ -64,9 +64,16 @@ public:
const GrRect& rect,
const SkMatrix& combinedMatrix,
const GrRect& devRect,
const GrVec& devStrokeSize,
SkScalar width,
bool useVertexCoverage);
// First rect is outer; second rect is inner
void fillAANestedRects(GrGpu* gpu,
GrDrawTarget* target,
const SkRect rects[2],
const SkMatrix& combinedMatrix,
bool useVertexCoverage);
private:
GrIndexBuffer* fAAFillRectIndexBuffer;
GrIndexBuffer* fAAStrokeRectIndexBuffer;
@ -95,6 +102,12 @@ private:
const GrRect& rect,
const SkMatrix& combinedMatrix);
void geometryStrokeAARect(GrGpu* gpu,
GrDrawTarget* target,
const SkRect& devOutside,
const SkRect& devInside,
bool useVertexCoverage);
typedef GrRefCnt INHERITED;
};

Просмотреть файл

@ -600,17 +600,18 @@ bool SkPath::isRect(bool* isClosed, Direction* direction) const {
return isRectContour(false, &currVerb, &pts, isClosed, direction);
}
bool SkPath::isNestedRects(SkRect rects[2]) const {
bool SkPath::isNestedRects(SkRect rects[2], Direction dirs[2]) const {
SkDEBUGCODE(this->validate();)
int currVerb = 0;
const SkPoint* pts = fPathRef->points();
const SkPoint* first = pts;
if (!isRectContour(true, &currVerb, &pts, NULL, NULL)) {
Direction testDirs[2];
if (!isRectContour(true, &currVerb, &pts, NULL, &testDirs[0])) {
return false;
}
const SkPoint* last = pts;
SkRect testRects[2];
if (isRectContour(false, &currVerb, &pts, NULL, NULL)) {
if (isRectContour(false, &currVerb, &pts, NULL, &testDirs[1])) {
testRects[0].set(first, SkToS32(last - first));
testRects[1].set(last, SkToS32(pts - last));
if (testRects[0].contains(testRects[1])) {
@ -618,6 +619,10 @@ bool SkPath::isNestedRects(SkRect rects[2]) const {
rects[0] = testRects[0];
rects[1] = testRects[1];
}
if (dirs) {
dirs[0] = testDirs[0];
dirs[1] = testDirs[1];
}
return true;
}
if (testRects[1].contains(testRects[0])) {
@ -625,6 +630,10 @@ bool SkPath::isNestedRects(SkRect rects[2]) const {
rects[0] = testRects[1];
rects[1] = testRects[0];
}
if (dirs) {
dirs[0] = testDirs[1];
dirs[1] = testDirs[0];
}
return true;
}
}

Просмотреть файл

@ -237,6 +237,7 @@ bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
const SkScalar* intervals = fIntervals;
SkScalar dashCount = 0;
int segCount = 0;
SkPath cullPathStorage;
const SkPath* srcPtr = &src;
@ -291,6 +292,7 @@ bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
addedSegment = false;
if (is_even(index) && dlen > 0 && !skipFirstSegment) {
addedSegment = true;
++segCount;
if (specialLine) {
lineRec.addSegment(SkDoubleToScalar(distance),
@ -322,9 +324,14 @@ bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src,
if (meas.isClosed() && is_even(fInitialDashIndex) &&
fInitialDashLength > 0) {
meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment);
++segCount;
}
} while (meas.nextContour());
if (segCount > 1) {
dst->setConvexity(SkPath::kConcave_Convexity);
}
return true;
}

Просмотреть файл

@ -635,9 +635,16 @@ void GrAARectRenderer::strokeAARect(GrGpu* gpu,
const GrRect& rect,
const SkMatrix& combinedMatrix,
const GrRect& devRect,
const GrVec& devStrokeSize,
SkScalar width,
bool useVertexCoverage) {
GrDrawState* drawState = target->drawState();
GrVec devStrokeSize;
if (width > 0) {
devStrokeSize.set(width, width);
combinedMatrix.mapVectors(&devStrokeSize, 1);
devStrokeSize.setAbs(devStrokeSize);
} else {
devStrokeSize.set(SK_Scalar1, SK_Scalar1);
}
const SkScalar dx = devStrokeSize.fX;
const SkScalar dy = devStrokeSize.fY;
@ -659,13 +666,28 @@ void GrAARectRenderer::strokeAARect(GrGpu* gpu,
spare = GrMin(w, h);
}
GrRect devOutside(devRect);
devOutside.outset(rx, ry);
if (spare <= 0) {
GrRect r(devRect);
r.outset(rx, ry);
this->fillAARect(gpu, target, r, SkMatrix::I(), r, useVertexCoverage);
this->fillAARect(gpu, target, devOutside, SkMatrix::I(),
devOutside, useVertexCoverage);
return;
}
SkRect devInside(devRect);
devInside.inset(rx, ry);
this->geometryStrokeAARect(gpu, target, devOutside, devInside, useVertexCoverage);
}
void GrAARectRenderer::geometryStrokeAARect(GrGpu* gpu,
GrDrawTarget* target,
const SkRect& devOutside,
const SkRect& devInside,
bool useVertexCoverage) {
GrDrawState* drawState = target->drawState();
set_aa_rect_vertex_attributes(drawState, useVertexCoverage);
GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
@ -691,14 +713,12 @@ void GrAARectRenderer::strokeAARect(GrGpu* gpu,
GrPoint* fan2Pos = reinterpret_cast<GrPoint*>(verts + 8 * vsize);
GrPoint* fan3Pos = reinterpret_cast<GrPoint*>(verts + 12 * vsize);
set_inset_fan(fan0Pos, vsize, devRect,
-rx - SK_ScalarHalf, -ry - SK_ScalarHalf);
set_inset_fan(fan1Pos, vsize, devRect,
-rx + SK_ScalarHalf, -ry + SK_ScalarHalf);
set_inset_fan(fan2Pos, vsize, devRect,
rx - SK_ScalarHalf, ry - SK_ScalarHalf);
set_inset_fan(fan3Pos, vsize, devRect,
rx + SK_ScalarHalf, ry + SK_ScalarHalf);
// outermost
set_inset_fan(fan0Pos, vsize, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
set_inset_fan(fan1Pos, vsize, devOutside, SK_ScalarHalf, SK_ScalarHalf);
set_inset_fan(fan2Pos, vsize, devInside, -SK_ScalarHalf, -SK_ScalarHalf);
// innermost
set_inset_fan(fan3Pos, vsize, devInside, SK_ScalarHalf, SK_ScalarHalf);
// The outermost rect has 0 coverage
verts += sizeof(GrPoint);
@ -718,7 +738,7 @@ void GrAARectRenderer::strokeAARect(GrGpu* gpu,
*reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor;
}
// The innermost rect has full coverage
// The innermost rect has 0 coverage
verts += 8 * vsize;
for (int i = 0; i < 4; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
@ -728,3 +748,24 @@ void GrAARectRenderer::strokeAARect(GrGpu* gpu,
target->drawIndexed(kTriangles_GrPrimitiveType,
0, 0, 16, aaStrokeRectIndexCount());
}
void GrAARectRenderer::fillAANestedRects(GrGpu* gpu,
GrDrawTarget* target,
const SkRect rects[2],
const SkMatrix& combinedMatrix,
bool useVertexCoverage) {
SkASSERT(combinedMatrix.rectStaysRect());
SkASSERT(!rects[1].isEmpty());
SkRect devOutside, devInside;
combinedMatrix.mapRect(&devOutside, rects[0]);
// can't call mapRect for devInside since it calls sort
combinedMatrix.mapPoints((SkPoint*)&devInside, (const SkPoint*)&rects[1], 2);
if (devInside.isEmpty()) {
this->fillAARect(gpu, target, devOutside, SkMatrix::I(), devOutside, useVertexCoverage);
return;
}
this->geometryStrokeAARect(gpu, target, devOutside, devInside, useVertexCoverage);
}

Просмотреть файл

@ -792,17 +792,9 @@ void GrContext::drawRect(const GrPaint& paint,
return;
}
if (width >= 0) {
GrVec strokeSize;
if (width > 0) {
strokeSize.set(width, width);
combinedMatrix.mapVectors(&strokeSize, 1);
strokeSize.setAbs(strokeSize);
} else {
strokeSize.set(SK_Scalar1, SK_Scalar1);
}
fAARectRenderer->strokeAARect(this->getGpu(), target,
rect, combinedMatrix, devRect,
strokeSize, useVertexCoverage);
width, useVertexCoverage);
} else {
// filled AA rect
fAARectRenderer->fillAARect(this->getGpu(), target,
@ -1004,6 +996,52 @@ void GrContext::drawOval(const GrPaint& paint,
}
}
namespace {
// Can 'path' be drawn as a pair of filled nested rectangles?
static bool is_nested_rects(GrDrawTarget* target,
const SkPath& path,
const SkStrokeRec& stroke,
SkRect rects[2],
bool* useVertexCoverage) {
SkASSERT(stroke.isFillStyle());
if (path.isInverseFillType()) {
return false;
}
const GrDrawState& drawState = target->getDrawState();
// TODO: this restriction could be lifted if we were willing to apply
// the matrix to all the points individually rather than just to the rect
if (!drawState.getViewMatrix().preservesAxisAlignment()) {
return false;
}
*useVertexCoverage = false;
if (!target->getDrawState().canTweakAlphaForCoverage()) {
if (disable_coverage_aa_for_blend(target)) {
return false;
} else {
*useVertexCoverage = true;
}
}
SkPath::Direction dirs[2];
if (!path.isNestedRects(rects, dirs)) {
return false;
}
if (SkPath::kWinding_FillType == path.getFillType()) {
// The two rects need to be wound opposite to each other
return dirs[0] != dirs[1];
} else {
return true;
}
}
};
void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
if (path.isEmpty()) {
@ -1021,9 +1059,28 @@ void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrok
GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
GrDrawState::AutoStageDisable atr(fDrawState);
bool useAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
if (useAA && stroke.getWidth() < 0 && !path.isConvex()) {
// Concave AA paths are expensive - try to avoid them for special cases
bool useVertexCoverage;
SkRect rects[2];
if (is_nested_rects(target, path, stroke, rects, &useVertexCoverage)) {
GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
if (!adcd.succeeded()) {
return;
}
fAARectRenderer->fillAANestedRects(this->getGpu(), target,
rects,
adcd.getOriginalMatrix(),
useVertexCoverage);
return;
}
}
SkRect ovalRect;
bool isOval = path.isOval(&ovalRect);
bool useAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
if (!isOval || path.isInverseFillType()
|| !fOvalRenderer->drawOval(target, this, useAA, ovalRect, stroke)) {

Просмотреть файл

@ -1322,22 +1322,22 @@ static void test_isRect(skiatest::Reporter* reporter) {
static void test_isNestedRects(skiatest::Reporter* reporter) {
// passing tests (all moveTo / lineTo...
SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f},
SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, // CCW
{1, 0}, {.5f, 0}};
SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1},
SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, // CW
{0, 1}, {0, .5f}};
SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
// failing tests
SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
@ -1366,6 +1366,18 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
f1, f2, f3, f4, f5, f6, f7, f8,
c1, c2
};
SkPath::Direction dirs[] = {
SkPath::kCW_Direction, SkPath::kCW_Direction, SkPath::kCW_Direction,
SkPath::kCW_Direction, SkPath::kCCW_Direction, SkPath::kCCW_Direction,
SkPath::kCCW_Direction, SkPath::kCCW_Direction, SkPath::kCCW_Direction,
SkPath::kCCW_Direction, SkPath::kCW_Direction, SkPath::kCW_Direction,
SkPath::kCCW_Direction, SkPath::kCW_Direction, SkPath::kUnknown_Direction,
SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction,
SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction,
SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction,
};
SkASSERT(SK_ARRAY_COUNT(tests) == SK_ARRAY_COUNT(dirs));
const SkPoint* lastPass = re;
const SkPoint* lastClose = f8;
const size_t testCount = sizeof(tests) / sizeof(tests[0]);
@ -1391,13 +1403,22 @@ static void test_isNestedRects(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, fail ^ path.isNestedRects(0));
if (!fail) {
SkRect expected[2], computed[2];
SkPath::Direction expectedDirs[2], computedDirs[2];
SkRect testBounds;
testBounds.set(tests[testIndex], testLen[testIndex] / sizeof(SkPoint));
expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
expected[1] = testBounds;
REPORTER_ASSERT(reporter, path.isNestedRects(computed));
if (rectFirst) {
expectedDirs[0] = SkPath::kCW_Direction;
} else {
expectedDirs[0] = SkPath::kCCW_Direction;
}
expectedDirs[1] = dirs[testIndex];
REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
REPORTER_ASSERT(reporter, expected[0] == computed[0]);
REPORTER_ASSERT(reporter, expected[1] == computed[1]);
REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
}
if (tests[testIndex] == lastPass) {
fail = true;