From 8b129aa3379ece6c43d9ce2ad0cdeafb089b7eb5 Mon Sep 17 00:00:00 2001 From: "robertphillips@google.com" Date: Fri, 5 Oct 2012 15:37:00 +0000 Subject: 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 --- src/gpu/GrDrawTarget.cpp | 28 ++++++++++++++++++++++++---- src/gpu/GrDrawTarget.h | 21 +++++++++++++++++++++ src/gpu/GrInOrderDrawBuffer.cpp | 15 +++++++++++++-- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp index 0b61f6e531..ab345bc6cb 100644 --- a/src/gpu/GrDrawTarget.cpp +++ b/src/gpu/GrDrawTarget.cpp @@ -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(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(GrTCast(vertices) + colorOffset); + + for (int i = 0; i < 4; ++i) { + *vertCol = color; + vertCol = (GrColor*) ((intptr_t) vertCol + vsize); + } + } } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h index b3eee852f2..b975f69797 100644 --- a/src/gpu/GrDrawTarget.h +++ b/src/gpu/GrDrawTarget.h @@ -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); diff --git a/src/gpu/GrInOrderDrawBuffer.cpp b/src/gpu/GrInOrderDrawBuffer.cpp index c69675fd7d..c3c1e8982a 100644 --- a/src/gpu/GrInOrderDrawBuffer.cpp +++ b/src/gpu/GrInOrderDrawBuffer.cpp @@ -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 -- cgit v1.2.3