Split GrContext's AA Rect drawing methods into helper class

http://codereview.appspot.com/6300070/



git-svn-id: http://skia.googlecode.com/svn/trunk@4233 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
robertphillips@google.com 2012-06-12 00:32:28 +00:00
parent 7886ad3de1
commit f6747b0b90
7 changed files with 324 additions and 239 deletions

View File

@ -1088,6 +1088,7 @@ int main(int argc, char * const argv[]) {
PRINT_INST_COUNT(SkRefCnt);
PRINT_INST_COUNT(GrResource);
PRINT_INST_COUNT(GrAARectRenderer);
return (0 == testsFailed) ? 0 : -1;
}

View File

@ -172,6 +172,7 @@
'angle.gyp:*',
],
'sources': [
'../include/gpu/GrAARectRenderer.h',
'../include/gpu/GrClip.h',
'../include/gpu/GrClipIterator.h',
'../include/gpu/GrColor.h',
@ -208,6 +209,7 @@
'../src/gpu/GrAAHairLinePathRenderer.h',
'../src/gpu/GrAAConvexPathRenderer.cpp',
'../src/gpu/GrAAConvexPathRenderer.h',
'../src/gpu/GrAARectRenderer.cpp',
'../src/gpu/GrAddPathRenderers_default.cpp',
'../src/gpu/GrAllocator.h',
'../src/gpu/GrAllocPool.h',

View File

@ -1,8 +1,8 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

View File

@ -0,0 +1,67 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrAARectRenderer_DEFINED
#define GrAARectRenderer_DEFINED
#include "GrRect.h"
#include "GrRefCnt.h"
class GrGpu;
class GrDrawTarget;
class GrIndexBuffer;
/*
* This class wraps helper functions that draw AA rects (filled & stroked)
*/
class GrAARectRenderer : public GrRefCnt {
public:
DECLARE_INST_COUNT
GrAARectRenderer()
: fAAFillRectIndexBuffer(NULL)
, fAAStrokeRectIndexBuffer(NULL) {
}
void reset();
~GrAARectRenderer() {
this->reset();
}
// TODO: potentialy fuse the fill & stroke methods and differentiate
// btween them by passing in strokeWidth (<0 means fill).
// TODO: Remove the useVertexCoverage boolean. Just use it all the time
// since we now have a coverage vertex attribute
void fillAARect(GrGpu* gpu,
GrDrawTarget* target,
const GrRect& devRect,
bool useVertexCoverage);
void strokeAARect(GrGpu* gpu,
GrDrawTarget* target,
const GrRect& devRect,
const GrVec& devStrokeSize,
bool useVertexCoverage);
private:
GrIndexBuffer* fAAFillRectIndexBuffer;
GrIndexBuffer* fAAStrokeRectIndexBuffer;
static const uint16_t gFillAARectIdx[];
static const uint16_t gStrokeAARectIdx[];
static int aaFillRectIndexCount();
GrIndexBuffer* aaFillRectIndexBuffer(GrGpu* gpu);
static int aaStrokeRectIndexCount();
GrIndexBuffer* aaStrokeRectIndexBuffer(GrGpu* gpu);
};
#endif // GrAARectRenderer_DEFINED

View File

@ -12,6 +12,7 @@
#include "GrClip.h"
#include "GrPaint.h"
#include "GrAARectRenderer.h"
// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
// remove.
#include "GrRenderTarget.h"
@ -704,25 +705,9 @@ private:
GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
GrInOrderDrawBuffer* fDrawBuffer;
GrIndexBuffer* fAAFillRectIndexBuffer;
GrIndexBuffer* fAAStrokeRectIndexBuffer;
GrContext(GrGpu* gpu);
void fillAARect(GrDrawTarget* target,
const GrRect& devRect,
bool useVertexCoverage);
void strokeAARect(GrDrawTarget* target,
const GrRect& devRect,
const GrVec& devStrokeSize,
bool useVertexCoverage);
inline int aaFillRectIndexCount() const;
GrIndexBuffer* aaFillRectIndexBuffer();
inline int aaStrokeRectIndexCount() const;
GrIndexBuffer* aaStrokeRectIndexBuffer();
GrAARectRenderer* fAARectRenderer;
void setupDrawBuffer();

View File

@ -0,0 +1,237 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrAARectRenderer.h"
#include "GrRefCnt.h"
#include "GrGpu.h"
DEFINE_INST_COUNT(GrAARectRenderer)
namespace {
static GrVertexLayout aa_rect_layout(const GrDrawTarget* target,
bool useCoverage) {
GrVertexLayout layout = 0;
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
if (NULL != target->getDrawState().getTexture(s)) {
layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
}
}
if (useCoverage) {
layout |= GrDrawTarget::kCoverage_VertexLayoutBit;
} else {
layout |= GrDrawTarget::kColor_VertexLayoutBit;
}
return layout;
}
static void setInsetFan(GrPoint* pts, size_t stride,
const GrRect& r, GrScalar dx, GrScalar dy) {
pts->setRectFan(r.fLeft + dx, r.fTop + dy,
r.fRight - dx, r.fBottom - dy, stride);
}
};
void GrAARectRenderer::reset() {
GrSafeSetNull(fAAFillRectIndexBuffer);
GrSafeSetNull(fAAStrokeRectIndexBuffer);
}
const uint16_t GrAARectRenderer::gFillAARectIdx[] = {
0, 1, 5, 5, 4, 0,
1, 2, 6, 6, 5, 1,
2, 3, 7, 7, 6, 2,
3, 0, 4, 4, 7, 3,
4, 5, 6, 6, 7, 4,
};
int GrAARectRenderer::aaFillRectIndexCount() {
return GR_ARRAY_COUNT(gFillAARectIdx);
}
GrIndexBuffer* GrAARectRenderer::aaFillRectIndexBuffer(GrGpu* gpu) {
if (NULL == fAAFillRectIndexBuffer) {
fAAFillRectIndexBuffer = gpu->createIndexBuffer(sizeof(gFillAARectIdx),
false);
if (NULL != fAAFillRectIndexBuffer) {
#if GR_DEBUG
bool updated =
#endif
fAAFillRectIndexBuffer->updateData(gFillAARectIdx,
sizeof(gFillAARectIdx));
GR_DEBUGASSERT(updated);
}
}
return fAAFillRectIndexBuffer;
}
const uint16_t GrAARectRenderer::gStrokeAARectIdx[] = {
0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
};
int GrAARectRenderer::aaStrokeRectIndexCount() {
return GR_ARRAY_COUNT(gStrokeAARectIdx);
}
GrIndexBuffer* GrAARectRenderer::aaStrokeRectIndexBuffer(GrGpu* gpu) {
if (NULL == fAAStrokeRectIndexBuffer) {
fAAStrokeRectIndexBuffer =
gpu->createIndexBuffer(sizeof(gStrokeAARectIdx), false);
if (NULL != fAAStrokeRectIndexBuffer) {
#if GR_DEBUG
bool updated =
#endif
fAAStrokeRectIndexBuffer->updateData(gStrokeAARectIdx,
sizeof(gStrokeAARectIdx));
GR_DEBUGASSERT(updated);
}
}
return fAAStrokeRectIndexBuffer;
}
void GrAARectRenderer::fillAARect(GrGpu* gpu,
GrDrawTarget* target,
const GrRect& devRect,
bool useVertexCoverage) {
GrVertexLayout layout = aa_rect_layout(target, useVertexCoverage);
size_t vsize = GrDrawTarget::VertexSize(layout);
GrDrawTarget::AutoReleaseGeometry geo(target, layout, 8, 0);
if (!geo.succeeded()) {
GrPrintf("Failed to get space for vertices!\n");
return;
}
GrIndexBuffer* indexBuffer = this->aaFillRectIndexBuffer(gpu);
if (NULL == indexBuffer) {
GrPrintf("Failed to create index buffer!\n");
return;
}
intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices());
GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts);
GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize);
setInsetFan(fan0Pos, vsize, devRect, -GR_ScalarHalf, -GR_ScalarHalf);
setInsetFan(fan1Pos, vsize, devRect, GR_ScalarHalf, GR_ScalarHalf);
verts += sizeof(GrPoint);
for (int i = 0; i < 4; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
}
GrColor innerColor;
if (useVertexCoverage) {
innerColor = 0xffffffff;
} else {
innerColor = target->getDrawState().getColor();
}
verts += 4 * vsize;
for (int i = 0; i < 4; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor;
}
target->setIndexSourceToBuffer(indexBuffer);
target->drawIndexed(kTriangles_GrPrimitiveType, 0,
0, 8, this->aaFillRectIndexCount());
}
void GrAARectRenderer::strokeAARect(GrGpu* gpu,
GrDrawTarget* target,
const GrRect& devRect,
const GrVec& devStrokeSize,
bool useVertexCoverage) {
const GrScalar& dx = devStrokeSize.fX;
const GrScalar& dy = devStrokeSize.fY;
const GrScalar rx = GrMul(dx, GR_ScalarHalf);
const GrScalar ry = GrMul(dy, GR_ScalarHalf);
GrScalar spare;
{
GrScalar w = devRect.width() - dx;
GrScalar h = devRect.height() - dy;
spare = GrMin(w, h);
}
if (spare <= 0) {
GrRect r(devRect);
r.inset(-rx, -ry);
this->fillAARect(gpu, target, r, useVertexCoverage);
return;
}
GrVertexLayout layout = aa_rect_layout(target, useVertexCoverage);
size_t vsize = GrDrawTarget::VertexSize(layout);
GrDrawTarget::AutoReleaseGeometry geo(target, layout, 16, 0);
if (!geo.succeeded()) {
GrPrintf("Failed to get space for vertices!\n");
return;
}
GrIndexBuffer* indexBuffer = this->aaStrokeRectIndexBuffer(gpu);
if (NULL == indexBuffer) {
GrPrintf("Failed to create index buffer!\n");
return;
}
intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices());
GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts);
GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize);
GrPoint* fan2Pos = reinterpret_cast<GrPoint*>(verts + 8 * vsize);
GrPoint* fan3Pos = reinterpret_cast<GrPoint*>(verts + 12 * vsize);
setInsetFan(fan0Pos, vsize, devRect,
-rx - GR_ScalarHalf, -ry - GR_ScalarHalf);
setInsetFan(fan1Pos, vsize, devRect,
-rx + GR_ScalarHalf, -ry + GR_ScalarHalf);
setInsetFan(fan2Pos, vsize, devRect,
rx - GR_ScalarHalf, ry - GR_ScalarHalf);
setInsetFan(fan3Pos, vsize, devRect,
rx + GR_ScalarHalf, ry + GR_ScalarHalf);
verts += sizeof(GrPoint);
for (int i = 0; i < 4; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
}
GrColor innerColor;
if (useVertexCoverage) {
innerColor = 0xffffffff;
} else {
innerColor = target->getDrawState().getColor();
}
verts += 4 * vsize;
for (int i = 0; i < 8; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor;
}
verts += 8 * vsize;
for (int i = 0; i < 8; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
}
target->setIndexSourceToBuffer(indexBuffer);
target->drawIndexed(kTriangles_GrPrimitiveType,
0, 0, 16, aaStrokeRectIndexCount());
}

View File

@ -81,8 +81,8 @@ GrContext::~GrContext() {
delete fDrawBufferVBAllocPool;
delete fDrawBufferIBAllocPool;
GrSafeUnref(fAAFillRectIndexBuffer);
GrSafeUnref(fAAStrokeRectIndexBuffer);
fAARectRenderer->unref();
fGpu->unref();
GrSafeUnref(fPathRendererChain);
GrSafeUnref(fSoftwarePathRenderer);
@ -113,8 +113,7 @@ void GrContext::contextDestroyed() {
delete fDrawBufferIBAllocPool;
fDrawBufferIBAllocPool = NULL;
GrSafeSetNull(fAAFillRectIndexBuffer);
GrSafeSetNull(fAAStrokeRectIndexBuffer);
fAARectRenderer->reset();
fTextureCache->removeAll();
fFontCache->freeAll();
@ -130,6 +129,8 @@ void GrContext::freeGpuResources() {
fGpu->purgeResources();
fAARectRenderer->reset();
fTextureCache->removeAll();
fFontCache->freeAll();
// a path renderer may be holding onto resources
@ -668,215 +669,6 @@ static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
verts[9] = verts[1];
}
static void setInsetFan(GrPoint* pts, size_t stride,
const GrRect& r, GrScalar dx, GrScalar dy) {
pts->setRectFan(r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
}
static const uint16_t gFillAARectIdx[] = {
0, 1, 5, 5, 4, 0,
1, 2, 6, 6, 5, 1,
2, 3, 7, 7, 6, 2,
3, 0, 4, 4, 7, 3,
4, 5, 6, 6, 7, 4,
};
int GrContext::aaFillRectIndexCount() const {
return GR_ARRAY_COUNT(gFillAARectIdx);
}
GrIndexBuffer* GrContext::aaFillRectIndexBuffer() {
if (NULL == fAAFillRectIndexBuffer) {
fAAFillRectIndexBuffer = fGpu->createIndexBuffer(sizeof(gFillAARectIdx),
false);
if (NULL != fAAFillRectIndexBuffer) {
#if GR_DEBUG
bool updated =
#endif
fAAFillRectIndexBuffer->updateData(gFillAARectIdx,
sizeof(gFillAARectIdx));
GR_DEBUGASSERT(updated);
}
}
return fAAFillRectIndexBuffer;
}
static const uint16_t gStrokeAARectIdx[] = {
0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
};
int GrContext::aaStrokeRectIndexCount() const {
return GR_ARRAY_COUNT(gStrokeAARectIdx);
}
GrIndexBuffer* GrContext::aaStrokeRectIndexBuffer() {
if (NULL == fAAStrokeRectIndexBuffer) {
fAAStrokeRectIndexBuffer = fGpu->createIndexBuffer(sizeof(gStrokeAARectIdx),
false);
if (NULL != fAAStrokeRectIndexBuffer) {
#if GR_DEBUG
bool updated =
#endif
fAAStrokeRectIndexBuffer->updateData(gStrokeAARectIdx,
sizeof(gStrokeAARectIdx));
GR_DEBUGASSERT(updated);
}
}
return fAAStrokeRectIndexBuffer;
}
static GrVertexLayout aa_rect_layout(const GrDrawTarget* target,
bool useCoverage) {
GrVertexLayout layout = 0;
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
if (NULL != target->getDrawState().getTexture(s)) {
layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
}
}
if (useCoverage) {
layout |= GrDrawTarget::kCoverage_VertexLayoutBit;
} else {
layout |= GrDrawTarget::kColor_VertexLayoutBit;
}
return layout;
}
void GrContext::fillAARect(GrDrawTarget* target,
const GrRect& devRect,
bool useVertexCoverage) {
GrVertexLayout layout = aa_rect_layout(target, useVertexCoverage);
size_t vsize = GrDrawTarget::VertexSize(layout);
GrDrawTarget::AutoReleaseGeometry geo(target, layout, 8, 0);
if (!geo.succeeded()) {
GrPrintf("Failed to get space for vertices!\n");
return;
}
GrIndexBuffer* indexBuffer = this->aaFillRectIndexBuffer();
if (NULL == indexBuffer) {
GrPrintf("Failed to create index buffer!\n");
return;
}
intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices());
GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts);
GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize);
setInsetFan(fan0Pos, vsize, devRect, -GR_ScalarHalf, -GR_ScalarHalf);
setInsetFan(fan1Pos, vsize, devRect, GR_ScalarHalf, GR_ScalarHalf);
verts += sizeof(GrPoint);
for (int i = 0; i < 4; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
}
GrColor innerColor;
if (useVertexCoverage) {
innerColor = 0xffffffff;
} else {
innerColor = target->getDrawState().getColor();
}
verts += 4 * vsize;
for (int i = 0; i < 4; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor;
}
target->setIndexSourceToBuffer(indexBuffer);
target->drawIndexed(kTriangles_GrPrimitiveType, 0,
0, 8, this->aaFillRectIndexCount());
}
void GrContext::strokeAARect(GrDrawTarget* target,
const GrRect& devRect,
const GrVec& devStrokeSize,
bool useVertexCoverage) {
const GrScalar& dx = devStrokeSize.fX;
const GrScalar& dy = devStrokeSize.fY;
const GrScalar rx = GrMul(dx, GR_ScalarHalf);
const GrScalar ry = GrMul(dy, GR_ScalarHalf);
GrScalar spare;
{
GrScalar w = devRect.width() - dx;
GrScalar h = devRect.height() - dy;
spare = GrMin(w, h);
}
if (spare <= 0) {
GrRect r(devRect);
r.inset(-rx, -ry);
fillAARect(target, r, useVertexCoverage);
return;
}
GrVertexLayout layout = aa_rect_layout(target, useVertexCoverage);
size_t vsize = GrDrawTarget::VertexSize(layout);
GrDrawTarget::AutoReleaseGeometry geo(target, layout, 16, 0);
if (!geo.succeeded()) {
GrPrintf("Failed to get space for vertices!\n");
return;
}
GrIndexBuffer* indexBuffer = this->aaStrokeRectIndexBuffer();
if (NULL == indexBuffer) {
GrPrintf("Failed to create index buffer!\n");
return;
}
intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices());
GrPoint* fan0Pos = reinterpret_cast<GrPoint*>(verts);
GrPoint* fan1Pos = reinterpret_cast<GrPoint*>(verts + 4 * vsize);
GrPoint* fan2Pos = reinterpret_cast<GrPoint*>(verts + 8 * vsize);
GrPoint* fan3Pos = reinterpret_cast<GrPoint*>(verts + 12 * vsize);
setInsetFan(fan0Pos, vsize, devRect, -rx - GR_ScalarHalf, -ry - GR_ScalarHalf);
setInsetFan(fan1Pos, vsize, devRect, -rx + GR_ScalarHalf, -ry + GR_ScalarHalf);
setInsetFan(fan2Pos, vsize, devRect, rx - GR_ScalarHalf, ry - GR_ScalarHalf);
setInsetFan(fan3Pos, vsize, devRect, rx + GR_ScalarHalf, ry + GR_ScalarHalf);
verts += sizeof(GrPoint);
for (int i = 0; i < 4; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
}
GrColor innerColor;
if (useVertexCoverage) {
innerColor = 0xffffffff;
} else {
innerColor = target->getDrawState().getColor();
}
verts += 4 * vsize;
for (int i = 0; i < 8; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor;
}
verts += 8 * vsize;
for (int i = 0; i < 8; ++i) {
*reinterpret_cast<GrColor*>(verts + i * vsize) = 0;
}
target->setIndexSourceToBuffer(indexBuffer);
target->drawIndexed(kTriangles_GrPrimitiveType,
0, 0, 16, aaStrokeRectIndexCount());
}
/**
* Returns true if the rects edges are integer-aligned.
*/
@ -974,9 +766,11 @@ void GrContext::drawRect(const GrPaint& paint,
} else {
strokeSize.set(GR_Scalar1, GR_Scalar1);
}
strokeAARect(target, devRect, strokeSize, useVertexCoverage);
fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
strokeSize, useVertexCoverage);
} else {
fillAARect(target, devRect, useVertexCoverage);
fAARectRenderer->fillAARect(this->getGpu(), target,
devRect, useVertexCoverage);
}
return;
}
@ -1919,8 +1713,7 @@ GrContext::GrContext(GrGpu* gpu) {
fDrawBufferVBAllocPool = NULL;
fDrawBufferIBAllocPool = NULL;
fAAFillRectIndexBuffer = NULL;
fAAStrokeRectIndexBuffer = NULL;
fAARectRenderer = new GrAARectRenderer;
this->setupDrawBuffer();
}