Moved paint color to vertex colors for batched rects

https://codereview.appspot.com/6620045/



git-svn-id: http://skia.googlecode.com/svn/trunk@5830 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
robertphillips@google.com 2012-10-05 15:37:00 +00:00
parent 3f5d682191
commit 8b129aa337
3 changed files with 58 additions and 6 deletions

View File

@ -1039,8 +1039,8 @@ void GrDrawTarget::drawRect(const GrRect& rect,
}
SetRectVertices(rect, matrix, srcRects,
srcMatrices, layout, geo.vertices());
srcMatrices, SK_ColorBLACK, layout, geo.vertices());
drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
}
@ -1060,10 +1060,20 @@ GrVertexLayout GrDrawTarget::GetRectVertexLayout(const GrRect* srcRects[]) {
return layout;
}
// This method fills int the four vertices for drawing 'rect'.
// matrix - is applied to each vertex
// srcRects - provide the uvs for each vertex
// srcMatrices - are applied to the corresponding 'srcRect'
// color - vertex color (replicated in each vertex)
// layout - specifies which uvs and/or color are present
// vertices - storage for the resulting vertices
// Note: the color parameter will only be used when kColor_VertexLayoutBit
// is present in 'layout'
void GrDrawTarget::SetRectVertices(const GrRect& rect,
const GrMatrix* matrix,
const GrRect* srcRects[],
const GrMatrix* srcMatrices[],
GrColor color,
GrVertexLayout layout,
void* vertices) {
#if GR_DEBUG
@ -1077,9 +1087,9 @@ void GrDrawTarget::SetRectVertices(const GrRect& rect,
}
#endif
int stageOffsets[GrDrawState::kNumStages];
int stageOffsets[GrDrawState::kNumStages], colorOffset;
int vsize = VertexSizeAndOffsetsByStage(layout, stageOffsets,
NULL, NULL, NULL);
&colorOffset, NULL, NULL);
GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop,
rect.fRight, rect.fBottom,
@ -1100,6 +1110,16 @@ void GrDrawTarget::SetRectVertices(const GrRect& rect,
}
}
}
if (layout & kColor_VertexLayoutBit) {
GrColor* vertCol = GrTCast<GrColor*>(GrTCast<intptr_t>(vertices) + colorOffset);
for (int i = 0; i < 4; ++i) {
*vertCol = color;
vertCol = (GrColor*) ((intptr_t) vertCol + vsize);
}
}
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -627,6 +627,26 @@ public:
////////////////////////////////////////////////////////////////////////////
/**
* Constructor sets the color to be 'color' which is undone by the destructor.
*/
class AutoColorRestore : public ::GrNoncopyable {
public:
AutoColorRestore(GrDrawTarget* target, GrColor color) {
fDrawTarget = target;
fOldColor = target->drawState()->getColor();
target->drawState()->setColor(color);
}
~AutoColorRestore() {
fDrawTarget->drawState()->setColor(fOldColor);
}
private:
GrDrawTarget* fDrawTarget;
GrColor fOldColor;
};
////////////////////////////////////////////////////////////////////////////
class AutoReleaseGeometry : ::GrNoncopyable {
public:
AutoReleaseGeometry(GrDrawTarget* target,
@ -1023,6 +1043,7 @@ protected:
const GrMatrix* matrix,
const GrRect* srcRects[],
const GrMatrix* srcMatrices[],
GrColor color,
GrVertexLayout layout,
void* vertices);

View File

@ -91,6 +91,11 @@ void GrInOrderDrawBuffer::drawRect(const GrRect& rect,
bool appendToPreviousDraw = false;
GrVertexLayout layout = GetRectVertexLayout(srcRects);
// When we batch rects we store the color at each vertex in order
// to allow batching when only the draw color is changing (the usual case)
layout |= kColor_VertexLayoutBit;
AutoReleaseGeometry geo(this, layout, 4, 0);
if (!geo.succeeded()) {
GrPrintf("Failed to get space for vertices!\n");
@ -99,7 +104,7 @@ void GrInOrderDrawBuffer::drawRect(const GrRect& rect,
GrMatrix combinedMatrix = drawState->getViewMatrix();
// We go to device space so that matrix changes allow us to concat
// rect draws. When the caller has provided explicit source rects
// then we don't want to modify the sampler matrices. Otherwise we do
// then we don't want to modify the sampler matrices. Otherwise
// we have to account for the view matrix change in the sampler
// matrices.
uint32_t explicitCoordMask = 0;
@ -118,7 +123,13 @@ void GrInOrderDrawBuffer::drawRect(const GrRect& rect,
combinedMatrix.preConcat(*matrix);
}
SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices());
SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices,
this->getDrawState().getColor(), layout, geo.vertices());
// Now that the paint's color is stored in the vertices set it to
// white so that the following code can batch all the rects regardless
// of paint color
AutoColorRestore acr(this, SK_ColorWHITE);
// we don't want to miss an opportunity to batch rects together
// simply because the clip has changed if the clip doesn't affect