Moved paint color to vertex colors for batched rects
authorrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 5 Oct 2012 15:37:00 +0000 (15:37 +0000)
committerrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 5 Oct 2012 15:37:00 +0000 (15:37 +0000)
https://codereview.appspot.com/6620045/

git-svn-id: http://skia.googlecode.com/svn/trunk@5830 2bbb7eff-a529-9590-31e7-b0007b416f81

src/gpu/GrDrawTarget.cpp
src/gpu/GrDrawTarget.h
src/gpu/GrInOrderDrawBuffer.cpp

index 0b61f6e53175d2dff7212cf0b575a956751d0e67..ab345bc6cb56d4d28f1329ce6958e7b254db2022 100644 (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);
+        }
+    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
index b3eee852f240d801ac9fbf26ae4770f40087e8f3..b975f69797890fa5ada153a2acdab0a560611105 100644 (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);
 
index c69675fd7d929975f101c53aae07edf3674d115b..c3c1e8982aedf8b4bf4bcdd16589cbe6cc1ac747 100644 (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