From af013bf8afc4c77ab8ff230f536e2ade973427fa Mon Sep 17 00:00:00 2001 From: egdaniel Date: Wed, 27 Aug 2014 12:17:24 -0700 Subject: [PATCH] Make setVertexAttribs in GrDrawState take a stride parameter. BUG=skia: R=bsalomon@google.com Author: egdaniel@google.com Review URL: https://codereview.chromium.org/511593004 --- gm/beziereffects.cpp | 6 +- gm/convexpolyeffect.cpp | 4 +- src/gpu/GrAAConvexPathRenderer.cpp | 3 +- src/gpu/GrAAHairLinePathRenderer.cpp | 8 +-- src/gpu/GrAARectRenderer.cpp | 116 ++++++++++++++++----------------- src/gpu/GrBitmapTextContext.cpp | 12 +++- src/gpu/GrContext.cpp | 23 ++++--- src/gpu/GrDistanceFieldTextContext.cpp | 12 +++- src/gpu/GrDrawState.cpp | 24 +++---- src/gpu/GrDrawState.h | 18 +++-- src/gpu/GrDrawTarget.cpp | 20 +++--- src/gpu/GrInOrderDrawBuffer.cpp | 28 ++++---- src/gpu/GrOvalRenderer.cpp | 20 +++--- src/gpu/GrRODrawState.cpp | 2 +- src/gpu/GrRODrawState.h | 6 +- src/gpu/effects/GrDashingEffect.cpp | 3 +- src/gpu/gl/GrGpuGL_program.cpp | 2 +- 17 files changed, 161 insertions(+), 146 deletions(-) diff --git a/gm/beziereffects.cpp b/gm/beziereffects.cpp index 2045e38..e0a1d87 100644 --- a/gm/beziereffects.cpp +++ b/gm/beziereffects.cpp @@ -168,7 +168,7 @@ protected: context->getTestTarget(&tt); SkASSERT(NULL != tt.target()); GrDrawState* drawState = tt.target()->drawState(); - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, sizeof(Vertex)); drawState->addCoverageEffect(effect, 1); drawState->setRenderTarget(rt); @@ -323,7 +323,7 @@ protected: context->getTestTarget(&tt); SkASSERT(NULL != tt.target()); GrDrawState* drawState = tt.target()->drawState(); - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, sizeof(Vertex)); drawState->addCoverageEffect(effect, 1); drawState->setRenderTarget(rt); @@ -507,7 +507,7 @@ protected: context->getTestTarget(&tt); SkASSERT(NULL != tt.target()); GrDrawState* drawState = tt.target()->drawState(); - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, sizeof(Vertex)); drawState->addCoverageEffect(effect, 1); drawState->setRenderTarget(rt); diff --git a/gm/convexpolyeffect.cpp b/gm/convexpolyeffect.cpp index 00e89db..275193f 100644 --- a/gm/convexpolyeffect.cpp +++ b/gm/convexpolyeffect.cpp @@ -127,7 +127,7 @@ protected: return; } GrDrawState* drawState = tt.target()->drawState(); - drawState->setVertexAttribs(SK_ARRAY_COUNT(kAttribs)); + drawState->setVertexAttribs(SK_ARRAY_COUNT(kAttribs), sizeof(SkPoint)); SkMatrix m; SkPath p; @@ -193,7 +193,7 @@ protected: } GrDrawState* drawState = tt.target()->drawState(); - drawState->setVertexAttribs(SK_ARRAY_COUNT(kAttribs)); + drawState->setVertexAttribs(SK_ARRAY_COUNT(kAttribs), sizeof(SkPoint)); drawState->addCoverageEffect(effect, 1); drawState->setIdentityViewMatrix(); drawState->setRenderTarget(rt); diff --git a/src/gpu/GrAAConvexPathRenderer.cpp b/src/gpu/GrAAConvexPathRenderer.cpp index b29c7c8..c260382 100644 --- a/src/gpu/GrAAConvexPathRenderer.cpp +++ b/src/gpu/GrAAConvexPathRenderer.cpp @@ -674,7 +674,7 @@ bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath, // Our computed verts should all be within one pixel of the segment control points. devBounds.outset(SK_Scalar1, SK_Scalar1); - drawState->setVertexAttribs(SK_ARRAY_COUNT(gPathAttribs)); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gPathAttribs), sizeof(QuadVertex)); static const int kEdgeAttrIndex = 1; GrEffect* quadEffect = QuadEdgeEffect::Create(); @@ -684,7 +684,6 @@ bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath, if (!arg.succeeded()) { return false; } - SkASSERT(sizeof(QuadVertex) == drawState->getVertexSize()); verts = reinterpret_cast(arg.vertices()); idxs = reinterpret_cast(arg.indices()); diff --git a/src/gpu/GrAAHairLinePathRenderer.cpp b/src/gpu/GrAAHairLinePathRenderer.cpp index d5ff40d..2114720 100644 --- a/src/gpu/GrAAHairLinePathRenderer.cpp +++ b/src/gpu/GrAAHairLinePathRenderer.cpp @@ -731,8 +731,8 @@ bool GrAAHairLinePathRenderer::createLineGeom(const SkPath& path, int vertCnt = kVertsPerLineSeg * lineCnt; - drawState->setVertexAttribs(SK_ARRAY_COUNT(gHairlineLineAttribs)); - SkASSERT(sizeof(LineVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gHairlineLineAttribs), + sizeof(LineVertex)); if (!arg->set(target, vertCnt, 0)) { return false; @@ -778,8 +778,8 @@ bool GrAAHairLinePathRenderer::createBezierGeom( int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt; - target->drawState()->setVertexAttribs(SK_ARRAY_COUNT(gHairlineBezierAttribs)); - SkASSERT(sizeof(BezierVertex) == target->getDrawState().getVertexSize()); + int vAttribCnt = SK_ARRAY_COUNT(gHairlineBezierAttribs); + target->drawState()->setVertexAttribs(vAttribCnt, sizeof(BezierVertex)); if (!arg->set(target, vertCnt, 0)) { return false; diff --git a/src/gpu/GrAARectRenderer.cpp b/src/gpu/GrAARectRenderer.cpp index b4790bb..a7ddfde 100644 --- a/src/gpu/GrAARectRenderer.cpp +++ b/src/gpu/GrAARectRenderer.cpp @@ -276,10 +276,10 @@ enum CoverageAttribType { static CoverageAttribType set_rect_attribs(GrDrawState* drawState) { if (drawState->canTweakAlphaForCoverage()) { - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, sizeof(SkPoint) + sizeof(SkColor)); return kUseColor_CoverageAttribType; } else { - drawState->setVertexAttribs(3); + drawState->setVertexAttribs(3, sizeof(SkPoint) + 2 * sizeof(SkColor)); return kUseCoverage_CoverageAttribType; } } @@ -482,10 +482,10 @@ void GrAARectRenderer::geometryFillAARect(GrGpu* gpu, } intptr_t verts = reinterpret_cast(geo.vertices()); - size_t vsize = drawState->getVertexSize(); + size_t vstride = drawState->getVertexStride(); SkPoint* fan0Pos = reinterpret_cast(verts); - SkPoint* fan1Pos = reinterpret_cast(verts + 4 * vsize); + SkPoint* fan1Pos = reinterpret_cast(verts + 4 * vstride); SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); @@ -499,8 +499,8 @@ void GrAARectRenderer::geometryFillAARect(GrGpu* gpu, combinedMatrix.mapRect(&devRect, rect); #endif - set_inset_fan(fan0Pos, vsize, devRect, -SK_ScalarHalf, -SK_ScalarHalf); - set_inset_fan(fan1Pos, vsize, devRect, inset, inset); + set_inset_fan(fan0Pos, vstride, devRect, -SK_ScalarHalf, -SK_ScalarHalf); + set_inset_fan(fan1Pos, vstride, devRect, inset, inset); } else { // compute transformed (1, 0) and (0, 1) vectors SkVector vec[2] = { @@ -515,38 +515,38 @@ void GrAARectRenderer::geometryFillAARect(GrGpu* gpu, // create the rotated rect fan0Pos->setRectFan(rect.fLeft, rect.fTop, - rect.fRight, rect.fBottom, vsize); - combinedMatrix.mapPointsWithStride(fan0Pos, vsize, 4); + rect.fRight, rect.fBottom, vstride); + combinedMatrix.mapPointsWithStride(fan0Pos, vstride, 4); // Now create the inset points and then outset the original // rotated points // TL - *((SkPoint*)((intptr_t)fan1Pos + 0 * vsize)) = - *((SkPoint*)((intptr_t)fan0Pos + 0 * vsize)) + vec[0] + vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 0 * vsize)) -= vec[0] + vec[1]; + *((SkPoint*)((intptr_t)fan1Pos + 0 * vstride)) = + *((SkPoint*)((intptr_t)fan0Pos + 0 * vstride)) + vec[0] + vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 0 * vstride)) -= vec[0] + vec[1]; // BL - *((SkPoint*)((intptr_t)fan1Pos + 1 * vsize)) = - *((SkPoint*)((intptr_t)fan0Pos + 1 * vsize)) + vec[0] - vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 1 * vsize)) -= vec[0] - vec[1]; + *((SkPoint*)((intptr_t)fan1Pos + 1 * vstride)) = + *((SkPoint*)((intptr_t)fan0Pos + 1 * vstride)) + vec[0] - vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 1 * vstride)) -= vec[0] - vec[1]; // BR - *((SkPoint*)((intptr_t)fan1Pos + 2 * vsize)) = - *((SkPoint*)((intptr_t)fan0Pos + 2 * vsize)) - vec[0] - vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 2 * vsize)) += vec[0] + vec[1]; + *((SkPoint*)((intptr_t)fan1Pos + 2 * vstride)) = + *((SkPoint*)((intptr_t)fan0Pos + 2 * vstride)) - vec[0] - vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 2 * vstride)) += vec[0] + vec[1]; // TR - *((SkPoint*)((intptr_t)fan1Pos + 3 * vsize)) = - *((SkPoint*)((intptr_t)fan0Pos + 3 * vsize)) - vec[0] + vec[1]; - *((SkPoint*)((intptr_t)fan0Pos + 3 * vsize)) += vec[0] - vec[1]; + *((SkPoint*)((intptr_t)fan1Pos + 3 * vstride)) = + *((SkPoint*)((intptr_t)fan0Pos + 3 * vstride)) - vec[0] + vec[1]; + *((SkPoint*)((intptr_t)fan0Pos + 3 * vstride)) += vec[0] - vec[1]; } // Make verts point to vertex color and then set all the color and coverage vertex attrs values. verts += sizeof(SkPoint); for (int i = 0; i < 4; ++i) { if (kUseCoverage_CoverageAttribType == covAttribType) { - *reinterpret_cast(verts + i * vsize) = color; - *reinterpret_cast(verts + i * vsize + sizeof(GrColor)) = 0; + *reinterpret_cast(verts + i * vstride) = color; + *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = 0; } else { - *reinterpret_cast(verts + i * vsize) = 0; + *reinterpret_cast(verts + i * vstride) = 0; } } @@ -564,13 +564,13 @@ void GrAARectRenderer::geometryFillAARect(GrGpu* gpu, } else { innerCoverage = (0xff == scale) ? color : SkAlphaMulQ(color, scale); } - verts += 4 * vsize; + verts += 4 * vstride; for (int i = 0; i < 4; ++i) { if (kUseCoverage_CoverageAttribType == covAttribType) { - *reinterpret_cast(verts + i * vsize) = color; - *reinterpret_cast(verts + i * vsize + sizeof(GrColor)) = innerCoverage; + *reinterpret_cast(verts + i * vstride) = color; + *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = innerCoverage; } else { - *reinterpret_cast(verts + i * vsize) = innerCoverage; + *reinterpret_cast(verts + i * vstride) = innerCoverage; } } @@ -634,8 +634,8 @@ void GrAARectRenderer::shaderFillAARect(GrGpu* gpu, SkScalar newWidth = SkScalarHalf(rect.width() * vec[0].length()) + SK_ScalarHalf; SkScalar newHeight = SkScalarHalf(rect.height() * vec[1].length()) + SK_ScalarHalf; - drawState->setVertexAttribs(SK_ARRAY_COUNT(gAARectVertexAttribs)); - SkASSERT(sizeof(RectVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gAARectVertexAttribs), + sizeof(RectVertex)); GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); if (!geo.succeeded()) { @@ -684,8 +684,8 @@ void GrAARectRenderer::shaderFillAlignedAARect(GrGpu* gpu, GrDrawState* drawState = target->drawState(); SkASSERT(combinedMatrix.rectStaysRect()); - drawState->setVertexAttribs(SK_ARRAY_COUNT(gAAAARectVertexAttribs)); - SkASSERT(sizeof(AARectVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gAAAARectVertexAttribs), + sizeof(AARectVertex)); GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); if (!geo.succeeded()) { @@ -834,15 +834,15 @@ void GrAARectRenderer::geometryStrokeAARect(GrGpu* gpu, } intptr_t verts = reinterpret_cast(geo.vertices()); - size_t vsize = drawState->getVertexSize(); + size_t vstride = drawState->getVertexStride(); // We create vertices for four nested rectangles. There are two ramps from 0 to full // coverage, one on the exterior of the stroke and the other on the interior. // The following pointers refer to the four rects, from outermost to innermost. SkPoint* fan0Pos = reinterpret_cast(verts); - SkPoint* fan1Pos = reinterpret_cast(verts + outerVertexNum * vsize); - SkPoint* fan2Pos = reinterpret_cast(verts + 2 * outerVertexNum * vsize); - SkPoint* fan3Pos = reinterpret_cast(verts + (2 * outerVertexNum + innerVertexNum) * vsize); + SkPoint* fan1Pos = reinterpret_cast(verts + outerVertexNum * vstride); + SkPoint* fan2Pos = reinterpret_cast(verts + 2 * outerVertexNum * vstride); + SkPoint* fan3Pos = reinterpret_cast(verts + (2 * outerVertexNum + innerVertexNum) * vstride); #ifndef SK_IGNORE_THIN_STROKED_RECT_FIX // TODO: this only really works if the X & Y margins are the same all around @@ -862,25 +862,25 @@ void GrAARectRenderer::geometryStrokeAARect(GrGpu* gpu, if (miterStroke) { // outermost - set_inset_fan(fan0Pos, vsize, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); + set_inset_fan(fan0Pos, vstride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); // inner two - set_inset_fan(fan1Pos, vsize, devOutside, inset, inset); - set_inset_fan(fan2Pos, vsize, devInside, -inset, -inset); + set_inset_fan(fan1Pos, vstride, devOutside, inset, inset); + set_inset_fan(fan2Pos, vstride, devInside, -inset, -inset); // innermost - set_inset_fan(fan3Pos, vsize, devInside, SK_ScalarHalf, SK_ScalarHalf); + set_inset_fan(fan3Pos, vstride, devInside, SK_ScalarHalf, SK_ScalarHalf); } else { - SkPoint* fan0AssistPos = reinterpret_cast(verts + 4 * vsize); - SkPoint* fan1AssistPos = reinterpret_cast(verts + (outerVertexNum + 4) * vsize); + SkPoint* fan0AssistPos = reinterpret_cast(verts + 4 * vstride); + SkPoint* fan1AssistPos = reinterpret_cast(verts + (outerVertexNum + 4) * vstride); // outermost - set_inset_fan(fan0Pos, vsize, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); - set_inset_fan(fan0AssistPos, vsize, devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf); + set_inset_fan(fan0Pos, vstride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); + set_inset_fan(fan0AssistPos, vstride, devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf); // outer one of the inner two - set_inset_fan(fan1Pos, vsize, devOutside, inset, inset); - set_inset_fan(fan1AssistPos, vsize, devOutsideAssist, inset, inset); + set_inset_fan(fan1Pos, vstride, devOutside, inset, inset); + set_inset_fan(fan1AssistPos, vstride, devOutsideAssist, inset, inset); // inner one of the inner two - set_inset_fan(fan2Pos, vsize, devInside, -inset, -inset); + set_inset_fan(fan2Pos, vstride, devInside, -inset, -inset); // innermost - set_inset_fan(fan3Pos, vsize, devInside, SK_ScalarHalf, SK_ScalarHalf); + set_inset_fan(fan3Pos, vstride, devInside, SK_ScalarHalf, SK_ScalarHalf); } // Make verts point to vertex color and then set all the color and coverage vertex attrs values. @@ -888,10 +888,10 @@ void GrAARectRenderer::geometryStrokeAARect(GrGpu* gpu, verts += sizeof(SkPoint); for (int i = 0; i < outerVertexNum; ++i) { if (kUseCoverage_CoverageAttribType == covAttribType) { - *reinterpret_cast(verts + i * vsize) = color; - *reinterpret_cast(verts + i * vsize + sizeof(GrColor)) = 0; + *reinterpret_cast(verts + i * vstride) = color; + *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = 0; } else { - *reinterpret_cast(verts + i * vsize) = 0; + *reinterpret_cast(verts + i * vstride) = 0; } } @@ -904,7 +904,7 @@ void GrAARectRenderer::geometryStrokeAARect(GrGpu* gpu, scale = 0xff; } - verts += outerVertexNum * vsize; + verts += outerVertexNum * vstride; GrColor innerCoverage; if (kUseCoverage_CoverageAttribType == covAttribType) { innerCoverage = GrColorPackRGBA(scale, scale, scale, scale); @@ -914,21 +914,21 @@ void GrAARectRenderer::geometryStrokeAARect(GrGpu* gpu, for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) { if (kUseCoverage_CoverageAttribType == covAttribType) { - *reinterpret_cast(verts + i * vsize) = color; - *reinterpret_cast(verts + i * vsize + sizeof(GrColor)) = innerCoverage; + *reinterpret_cast(verts + i * vstride) = color; + *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = innerCoverage; } else { - *reinterpret_cast(verts + i * vsize) = innerCoverage; + *reinterpret_cast(verts + i * vstride) = innerCoverage; } } // The innermost rect has 0 coverage - verts += (outerVertexNum + innerVertexNum) * vsize; + verts += (outerVertexNum + innerVertexNum) * vstride; for (int i = 0; i < innerVertexNum; ++i) { if (kUseCoverage_CoverageAttribType == covAttribType) { - *reinterpret_cast(verts + i * vsize) = color; - *reinterpret_cast(verts + i * vsize + sizeof(GrColor)) = 0; + *reinterpret_cast(verts + i * vstride) = color; + *reinterpret_cast(verts + i * vstride + sizeof(GrColor)) = 0; } else { - *reinterpret_cast(verts + i * vsize) = 0; + *reinterpret_cast(verts + i * vstride) = 0; } } diff --git a/src/gpu/GrBitmapTextContext.cpp b/src/gpu/GrBitmapTextContext.cpp index c992b95..17439b5 100755 --- a/src/gpu/GrBitmapTextContext.cpp +++ b/src/gpu/GrBitmapTextContext.cpp @@ -40,6 +40,8 @@ extern const GrVertexAttrib gTextVertexAttribs[] = { {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kEffect_GrVertexAttribBinding} }; +static const size_t kTextVASize = 2 * sizeof(SkPoint); + // position + color + texture coord extern const GrVertexAttrib gTextVertexWithColorAttribs[] = { {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, @@ -47,6 +49,8 @@ extern const GrVertexAttrib gTextVertexWithColorAttribs[] = { {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kEffect_GrVertexAttribBinding} }; +static const size_t kTextVAColorSize = 2 * sizeof(SkPoint) + sizeof(GrColor); + }; GrBitmapTextContext::GrBitmapTextContext(GrContext* context, @@ -176,10 +180,12 @@ void GrBitmapTextContext::allocateVertices(const char text[], size_t byteLength) bool useColorVerts = kA8_GrMaskFormat == fStrike->getMaskFormat(); if (useColorVerts) { fDrawTarget->drawState()->setVertexAttribs( - SK_ARRAY_COUNT(gTextVertexWithColorAttribs)); + SK_ARRAY_COUNT(gTextVertexWithColorAttribs), + kTextVAColorSize); } else { fDrawTarget->drawState()->setVertexAttribs( - SK_ARRAY_COUNT(gTextVertexAttribs)); + SK_ARRAY_COUNT(gTextVertexAttribs), + kTextVASize); } fVertexCount = 4*fSkPaint.textToGlyphs(text, byteLength, NULL); bool success = fDrawTarget->reserveVertexAndIndexSpace(fVertexCount, @@ -559,7 +565,7 @@ HAS_ATLAS: size_t vertSize = useColorVerts ? (2 * sizeof(SkPoint) + sizeof(GrColor)) : (2 * sizeof(SkPoint)); - SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexSize()); + SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride()); SkPoint* positions = reinterpret_cast( reinterpret_cast(fVertices) + vertSize * fCurrVertex); diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp index 3b44b7e..322ea87 100755 --- a/src/gpu/GrContext.cpp +++ b/src/gpu/GrContext.cpp @@ -365,7 +365,8 @@ GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc, GrTextureParams::kNone_FilterMode); drawState->addColorTextureEffect(clampedTexture, SkMatrix::I(), params); - drawState->setVertexAttribs(SK_ARRAY_COUNT(gVertexAttribs)); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gVertexAttribs), + 2 * sizeof(SkPoint)); GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0); @@ -935,11 +936,17 @@ extern const GrVertexAttrib gPosUVColorAttribs[] = { {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding} }; +static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint); +static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor); + extern const GrVertexAttrib gPosColorAttribs[] = { {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding}, }; +static const size_t kPosAttribsSize = sizeof(SkPoint); +static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor); + static void set_vertex_attributes(GrDrawState* drawState, const SkPoint* texCoords, const GrColor* colors, @@ -951,15 +958,15 @@ static void set_vertex_attributes(GrDrawState* drawState, if (NULL != texCoords && NULL != colors) { *texOffset = sizeof(SkPoint); *colorOffset = 2*sizeof(SkPoint); - drawState->setVertexAttribs(3); + drawState->setVertexAttribs(3, kPosUVColorAttribsSize); } else if (NULL != texCoords) { *texOffset = sizeof(SkPoint); - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, kPosUVAttribsSize); } else if (NULL != colors) { *colorOffset = sizeof(SkPoint); - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, kPosColorAttribsSize); } else { - drawState->setVertexAttribs(1); + drawState->setVertexAttribs(1, kPosAttribsSize); } } @@ -988,8 +995,8 @@ void GrContext::drawVertices(const GrPaint& paint, int colorOffset = -1, texOffset = -1; set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset); - size_t vertexSize = drawState->getVertexSize(); - if (sizeof(SkPoint) != vertexSize) { + size_t VertexStride = drawState->getVertexStride(); + if (sizeof(SkPoint) != VertexStride) { if (!geo.set(target, vertexCount, 0)) { GrPrintf("Failed to get space for vertices!\n"); return; @@ -1005,7 +1012,7 @@ void GrContext::drawVertices(const GrPaint& paint, if (colorOffset >= 0) { *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i]; } - curVertex = (void*)((intptr_t)curVertex + vertexSize); + curVertex = (void*)((intptr_t)curVertex + VertexStride); } } else { target->setVertexSourceToArray(positions, vertexCount); diff --git a/src/gpu/GrDistanceFieldTextContext.cpp b/src/gpu/GrDistanceFieldTextContext.cpp index 9cf53ce..6c51d01 100755 --- a/src/gpu/GrDistanceFieldTextContext.cpp +++ b/src/gpu/GrDistanceFieldTextContext.cpp @@ -44,6 +44,8 @@ extern const GrVertexAttrib gTextVertexAttribs[] = { {kVec2f_GrVertexAttribType, sizeof(SkPoint) , kEffect_GrVertexAttribBinding} }; +static const size_t kTextVASize = 2 * sizeof(SkPoint); + // position + color + texture coord extern const GrVertexAttrib gTextVertexWithColorAttribs[] = { {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, @@ -51,6 +53,8 @@ extern const GrVertexAttrib gTextVertexWithColorAttribs[] = { {kVec2f_GrVertexAttribType, sizeof(SkPoint) + sizeof(GrColor), kEffect_GrVertexAttribBinding} }; +static const size_t kTextVAColorSize = 2 * sizeof(SkPoint) + sizeof(GrColor); + }; GrDistanceFieldTextContext::GrDistanceFieldTextContext(GrContext* context, @@ -355,7 +359,7 @@ HAS_ATLAS: size_t vertSize = fUseLCDText ? (2 * sizeof(SkPoint)) : (2 * sizeof(SkPoint) + sizeof(GrColor)); - SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexSize()); + SkASSERT(vertSize == fDrawTarget->getDrawState().getVertexStride()); SkPoint* positions = reinterpret_cast( reinterpret_cast(fVertices) + vertSize * fCurrVertex); @@ -463,10 +467,12 @@ void GrDistanceFieldTextContext::allocateVertices(const char text[], size_t byte SkASSERT(NULL == fVertices); if (!fUseLCDText) { fDrawTarget->drawState()->setVertexAttribs( - SK_ARRAY_COUNT(gTextVertexWithColorAttribs)); + SK_ARRAY_COUNT(gTextVertexWithColorAttribs), + kTextVAColorSize); } else { fDrawTarget->drawState()->setVertexAttribs( - SK_ARRAY_COUNT(gTextVertexAttribs)); + SK_ARRAY_COUNT(gTextVertexAttribs), + kTextVASize); } fVertexCount = 4*fSkPaint.textToGlyphs(text, byteLength, NULL); bool success = fDrawTarget->reserveVertexAndIndexSpace(fVertexCount, diff --git a/src/gpu/GrDrawState.cpp b/src/gpu/GrDrawState.cpp index a6b3d68..0c2b1c3 100644 --- a/src/gpu/GrDrawState.cpp +++ b/src/gpu/GrDrawState.cpp @@ -75,7 +75,7 @@ GrDrawState& GrDrawState::operator=(const GrDrawState& that) { fFlagBits = that.fFlagBits; fVACount = that.fVACount; fVAPtr = that.fVAPtr; - fVertexSize = that.fVertexSize; + fVAStride = that.fVAStride; fStencilSettings = that.fStencilSettings; fCoverage = that.fCoverage; fDrawFace = that.fDrawFace; @@ -178,35 +178,34 @@ void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRende //////////////////////////////////////////////////////////////////////////////// -static size_t vertex_size(const GrVertexAttrib* attribs, int count) { +static void validate_vertex_attribs(const GrVertexAttrib* attribs, int count, size_t stride) { // this works as long as we're 4 byte-aligned #ifdef SK_DEBUG uint32_t overlapCheck = 0; -#endif SkASSERT(count <= GrRODrawState::kMaxVertexAttribCnt); - size_t size = 0; for (int index = 0; index < count; ++index) { size_t attribSize = GrVertexAttribTypeSize(attribs[index].fType); - size += attribSize; -#ifdef SK_DEBUG + size_t attribOffset = attribs[index].fOffset; + SkASSERT(attribOffset + attribSize <= stride); size_t dwordCount = attribSize >> 2; uint32_t mask = (1 << dwordCount)-1; - size_t offsetShift = attribs[index].fOffset >> 2; + size_t offsetShift = attribOffset >> 2; SkASSERT(!(overlapCheck & (mask << offsetShift))); overlapCheck |= (mask << offsetShift); -#endif } - return size; +#endif } //////////////////////////////////////////////////////////////////////////////// -void GrDrawState::setVertexAttribs(const GrVertexAttrib* attribs, int count) { +void GrDrawState::internalSetVertexAttribs(const GrVertexAttrib* attribs, int count, + size_t stride) { SkASSERT(count <= kMaxVertexAttribCnt); fVAPtr = attribs; fVACount = count; - fVertexSize = vertex_size(fVAPtr, fVACount); + fVAStride = stride; + validate_vertex_attribs(fVAPtr, fVACount, fVAStride); // Set all the indices to -1 memset(fFixedFunctionVertexAttribIndices, @@ -244,7 +243,7 @@ void GrDrawState::setDefaultVertexAttribs() { fVAPtr = &kPositionAttrib; fVACount = 1; - fVertexSize = GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); + fVAStride = GrVertexAttribTypeSize(kVec2f_GrVertexAttribType); // set all the fixed function indices to -1 except position. memset(fFixedFunctionVertexAttribIndices, @@ -279,6 +278,7 @@ GrDrawState::AutoVertexAttribRestore::AutoVertexAttribRestore( fDrawState = drawState; fVAPtr = drawState->fVAPtr; fVACount = drawState->fVACount; + fVAStride = drawState->fVAStride; fDrawState->setDefaultVertexAttribs(); } diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h index dcd6ff9..ec0ccad 100644 --- a/src/gpu/GrDrawState.h +++ b/src/gpu/GrDrawState.h @@ -79,9 +79,12 @@ public: /** * Sets vertex attributes for next draw. The object driving the templatization * should be a global GrVertexAttrib array that is never changed. + * + * @param count the number of attributes being set, limited to kMaxVertexAttribCnt. + * @param stride the number of bytes between successive vertex data. */ - template void setVertexAttribs(int count) { - this->setVertexAttribs(A, count); + template void setVertexAttribs(int count, size_t stride) { + this->internalSetVertexAttribs(A, count, stride); } /** @@ -97,12 +100,13 @@ public: public: AutoVertexAttribRestore(GrDrawState* drawState); - ~AutoVertexAttribRestore() { fDrawState->setVertexAttribs(fVAPtr, fVACount); } + ~AutoVertexAttribRestore() { fDrawState->internalSetVertexAttribs(fVAPtr, fVACount, fVAStride); } private: GrDrawState* fDrawState; const GrVertexAttrib* fVAPtr; int fVACount; + size_t fVAStride; }; /// @} @@ -559,13 +563,7 @@ private: // This is used to assert that this condition holds. SkDEBUGCODE(int fBlockEffectRemovalCnt;) - /** - * Sets vertex attributes for next draw. - * - * @param attribs the array of vertex attributes to set. - * @param count the number of attributes being set, limited to kMaxVertexAttribCnt. - */ - void setVertexAttribs(const GrVertexAttrib attribs[], int count); + void internalSetVertexAttribs(const GrVertexAttrib attribs[], int count, size_t stride); typedef GrRODrawState INHERITED; }; diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp index 2d27859..9605781 100644 --- a/src/gpu/GrDrawTarget.cpp +++ b/src/gpu/GrDrawTarget.cpp @@ -195,10 +195,10 @@ bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount, int indexCount, void** vertices, void** indices) { - size_t vertexSize = this->drawState()->getVertexSize(); + size_t vertexStride = this->drawState()->getVertexStride(); this->willReserveVertexAndIndexSpace(vertexCount, indexCount); if (vertexCount) { - if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) { + if (!this->reserveVertexSpace(vertexStride, vertexCount, vertices)) { if (indexCount) { this->resetIndexSource(); } @@ -278,7 +278,7 @@ void GrDrawTarget::setVertexSourceToArray(const void* vertexArray, this->releasePreviousVertexSource(); GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); geoSrc.fVertexSrc = kArray_GeometrySrcType; - geoSrc.fVertexSize = this->drawState()->getVertexSize(); + geoSrc.fVertexSize = this->drawState()->getVertexStride(); geoSrc.fVertexCount = vertexCount; this->onSetVertexSourceToArray(vertexArray, vertexCount); } @@ -298,7 +298,7 @@ void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) { geoSrc.fVertexSrc = kBuffer_GeometrySrcType; geoSrc.fVertexBuffer = buffer; buffer->ref(); - geoSrc.fVertexSize = this->drawState()->getVertexSize(); + geoSrc.fVertexSize = this->drawState()->getVertexStride(); } void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { @@ -673,9 +673,9 @@ extern const GrVertexAttrib gBWRectPosUVAttribs[] = { void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) { if (hasUVs) { - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, 2 * sizeof(SkPoint)); } else { - drawState->setVertexAttribs(1); + drawState->setVertexAttribs(1, sizeof(SkPoint)); } } @@ -693,16 +693,16 @@ void GrDrawTarget::onDrawRect(const SkRect& rect, return; } - size_t vsize = this->drawState()->getVertexSize(); - geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize); + size_t vstride = this->drawState()->getVertexStride(); + geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vstride); if (NULL != localRect) { SkPoint* coords = GrTCast(GrTCast(geo.vertices()) + sizeof(SkPoint)); coords->setRectFan(localRect->fLeft, localRect->fTop, localRect->fRight, localRect->fBottom, - vsize); + vstride); if (NULL != localMatrix) { - localMatrix->mapPointsWithStride(coords, vsize, 4); + localMatrix->mapPointsWithStride(coords, vstride, 4); } } SkRect bounds; diff --git a/src/gpu/GrInOrderDrawBuffer.cpp b/src/gpu/GrInOrderDrawBuffer.cpp index 9df54a8..4fd6b4e 100644 --- a/src/gpu/GrInOrderDrawBuffer.cpp +++ b/src/gpu/GrInOrderDrawBuffer.cpp @@ -97,9 +97,9 @@ extern const GrVertexAttrib kRectAttribs[] = { */ static void set_vertex_attributes(GrDrawState* drawState, bool hasLocalCoords, GrColor color) { if (hasLocalCoords) { - drawState->setVertexAttribs(3); + drawState->setVertexAttribs(3, 2 * sizeof(SkPoint) + sizeof(SkColor)); } else { - drawState->setVertexAttribs(2); + drawState->setVertexAttribs(2, sizeof(SkPoint) + sizeof(SkColor)); } if (0xFF == GrColorUnpackA(color)) { drawState->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true); @@ -143,24 +143,24 @@ void GrInOrderDrawBuffer::onDrawRect(const SkRect& rect, return; } - size_t vsize = drawState->getVertexSize(); + size_t vstride = drawState->getVertexStride(); - geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize); - matrix.mapPointsWithStride(geo.positions(), vsize, 4); + geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vstride); + matrix.mapPointsWithStride(geo.positions(), vstride, 4); SkRect devBounds; // since we already computed the dev verts, set the bounds hint. This will help us avoid // unnecessary clipping in our onDraw(). - get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds); + get_vertex_bounds(geo.vertices(), vstride, 4, &devBounds); if (NULL != localRect) { static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); SkPoint* coords = GrTCast(GrTCast(geo.vertices()) + kLocalOffset); coords->setRectFan(localRect->fLeft, localRect->fTop, localRect->fRight, localRect->fBottom, - vsize); + vstride); if (NULL != localMatrix) { - localMatrix->mapPointsWithStride(coords, vsize, 4); + localMatrix->mapPointsWithStride(coords, vstride, 4); } } @@ -168,7 +168,7 @@ void GrInOrderDrawBuffer::onDrawRect(const SkRect& rect, GrColor* vertColor = GrTCast(GrTCast(geo.vertices()) + kColorOffset); for (int i = 0; i < 4; ++i) { *vertColor = color; - vertColor = (GrColor*) ((intptr_t) vertColor + vsize); + vertColor = (GrColor*) ((intptr_t) vertColor + vstride); } this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer()); @@ -266,7 +266,7 @@ int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) { // update the amount of reserved vertex data actually referenced in draws size_t vertexBytes = instancesToConcat * info.verticesPerInstance() * - drawState.getVertexSize(); + drawState.getVertexStride(); poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes); draw->adjustInstanceCount(instancesToConcat); @@ -339,7 +339,7 @@ void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) { case kReserved_GeometrySrcType: // fallthrough case kArray_GeometrySrcType: { size_t vertexBytes = (info.vertexCount() + info.startVertex()) * - drawState.getVertexSize(); + drawState.getVertexStride(); poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes); draw->fVertexBuffer = poolState.fPoolVertexBuffer; draw->adjustStartVertex(poolState.fPoolStartVertex); @@ -726,11 +726,11 @@ bool GrInOrderDrawBuffer::geometryHints(int* vertexCount, *indexCount = currIndices; } if (NULL != vertexCount) { - size_t vertexSize = this->getDrawState().getVertexSize(); - int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize); + size_t vertexStride = this->getDrawState().getVertexStride(); + int32_t currVertices = fVertexPool.currentBufferVertices(vertexStride); if (*vertexCount > currVertices && (!fVertexPool.preallocatedBuffersRemaining() && - *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) { + *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexStride))) { flush = true; } diff --git a/src/gpu/GrOvalRenderer.cpp b/src/gpu/GrOvalRenderer.cpp index dcce889..f368dc8 100644 --- a/src/gpu/GrOvalRenderer.cpp +++ b/src/gpu/GrOvalRenderer.cpp @@ -519,8 +519,8 @@ void GrOvalRenderer::drawCircle(GrDrawTarget* target, return; } - drawState->setVertexAttribs(SK_ARRAY_COUNT(gCircleVertexAttribs)); - SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gCircleVertexAttribs), + sizeof(CircleVertex)); GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); if (!geo.succeeded()) { @@ -680,8 +680,8 @@ bool GrOvalRenderer::drawEllipse(GrDrawTarget* target, return false; } - drawState->setVertexAttribs(SK_ARRAY_COUNT(gEllipseVertexAttribs)); - SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gEllipseVertexAttribs), + sizeof(EllipseVertex)); GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); if (!geo.succeeded()) { @@ -799,8 +799,8 @@ bool GrOvalRenderer::drawDIEllipse(GrDrawTarget* target, SkScalar innerRatioX = SkScalarDiv(xRadius, innerXRadius); SkScalar innerRatioY = SkScalarDiv(yRadius, innerYRadius); - drawState->setVertexAttribs(SK_ARRAY_COUNT(gDIEllipseVertexAttribs)); - SkASSERT(sizeof(DIEllipseVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gDIEllipseVertexAttribs), + sizeof(DIEllipseVertex)); GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); if (!geo.succeeded()) { @@ -1032,8 +1032,8 @@ bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool us // if the corners are circles, use the circle renderer if ((!hasStroke || scaledStroke.fX == scaledStroke.fY) && xRadius == yRadius) { - drawState->setVertexAttribs(SK_ARRAY_COUNT(gCircleVertexAttribs)); - SkASSERT(sizeof(CircleVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gCircleVertexAttribs), + sizeof(CircleVertex)); GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); if (!geo.succeeded()) { @@ -1121,8 +1121,8 @@ bool GrOvalRenderer::drawRRect(GrDrawTarget* target, GrContext* context, bool us // otherwise we use the ellipse renderer } else { - drawState->setVertexAttribs(SK_ARRAY_COUNT(gEllipseVertexAttribs)); - SkASSERT(sizeof(EllipseVertex) == drawState->getVertexSize()); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gEllipseVertexAttribs), + sizeof(EllipseVertex)); SkScalar innerXRadius = 0.0f; SkScalar innerYRadius = 0.0f; diff --git a/src/gpu/GrRODrawState.cpp b/src/gpu/GrRODrawState.cpp index 1460f57..9118d0d 100644 --- a/src/gpu/GrRODrawState.cpp +++ b/src/gpu/GrRODrawState.cpp @@ -25,6 +25,7 @@ bool GrRODrawState::isEqual(const GrRODrawState& that) const { this->fBlendConstant != that.fBlendConstant || this->fFlagBits != that.fFlagBits || this->fVACount != that.fVACount || + this->fVAStride != that.fVAStride || memcmp(this->fVAPtr, that.fVAPtr, this->fVACount * sizeof(GrVertexAttrib)) || this->fStencilSettings != that.fStencilSettings || this->fDrawFace != that.fDrawFace) { @@ -50,7 +51,6 @@ bool GrRODrawState::isEqual(const GrRODrawState& that) const { } } - SkASSERT(this->fVertexSize == that.fVertexSize); SkASSERT(0 == memcmp(this->fFixedFunctionVertexAttribIndices, that.fFixedFunctionVertexAttribIndices, sizeof(this->fFixedFunctionVertexAttribIndices))); diff --git a/src/gpu/GrRODrawState.h b/src/gpu/GrRODrawState.h index 2315608..0abd4a6 100644 --- a/src/gpu/GrRODrawState.h +++ b/src/gpu/GrRODrawState.h @@ -37,7 +37,7 @@ public: const GrVertexAttrib* getVertexAttribs() const { return fVAPtr; } int getVertexAttribCount() const { return fVACount; } - size_t getVertexSize() const { return fVertexSize; } + size_t getVertexStride() const { return fVAStride; } /** * Getters for index into getVertexAttribs() for particular bindings. -1 is returned if the @@ -340,8 +340,6 @@ public: kB_CombinedState, }; - GrRODrawState& operator= (const GrRODrawState& that); - protected: bool isEqual(const GrRODrawState& that) const; @@ -353,7 +351,7 @@ protected: uint32_t fFlagBits; const GrVertexAttrib* fVAPtr; int fVACount; - size_t fVertexSize; + size_t fVAStride; GrStencilSettings fStencilSettings; uint8_t fCoverage; DrawFace fDrawFace; diff --git a/src/gpu/effects/GrDashingEffect.cpp b/src/gpu/effects/GrDashingEffect.cpp index 4b2bafe..adee4ae 100644 --- a/src/gpu/effects/GrDashingEffect.cpp +++ b/src/gpu/effects/GrDashingEffect.cpp @@ -350,7 +350,8 @@ bool GrDashingEffect::DrawDashLine(const SkPoint pts[2], const GrPaint& paint, } // Set up the vertex data for the line and start/end dashes - drawState->setVertexAttribs(SK_ARRAY_COUNT(gDashLineVertexAttribs)); + drawState->setVertexAttribs(SK_ARRAY_COUNT(gDashLineVertexAttribs), + sizeof(DashLineVertex)); int totalRectCnt = 0; diff --git a/src/gpu/gl/GrGpuGL_program.cpp b/src/gpu/gl/GrGpuGL_program.cpp index b19676a..beef93e 100644 --- a/src/gpu/gl/GrGpuGL_program.cpp +++ b/src/gpu/gl/GrGpuGL_program.cpp @@ -284,7 +284,7 @@ bool GrGpuGL::flushGraphicsState(DrawType type, const GrDeviceCoordTexture* dstC void GrGpuGL::setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes) { - GrGLsizei stride = static_cast(this->getDrawState().getVertexSize()); + GrGLsizei stride = static_cast(this->getDrawState().getVertexStride()); size_t vertexOffsetInBytes = stride * info.startVertex(); -- 2.7.4