Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrAAHairLinePathRenderer.cpp
index 0014bbe..2e9c82c 100644 (file)
 #include "GrContext.h"
 #include "GrDrawState.h"
 #include "GrDrawTargetCaps.h"
-#include "GrEffect.h"
+#include "GrProcessor.h"
 #include "GrGpu.h"
 #include "GrIndexBuffer.h"
 #include "GrPathUtils.h"
-#include "GrTBackendEffectFactory.h"
+#include "GrTBackendProcessorFactory.h"
 #include "SkGeometry.h"
 #include "SkStroke.h"
 #include "SkTemplates.h"
 
 #include "effects/GrBezierEffect.h"
 
-namespace {
 // quadratics are rendered as 5-sided polys in order to bound the
 // AA stroke around the center-curve. See comments in push_quad_index_buffer and
 // bloat_quad. Quadratics and conics share an index buffer
-static const int kVertsPerQuad = 5;
-static const int kIdxsPerQuad = 9;
 
 // lines are rendered as:
 //      *______________*
@@ -36,127 +33,66 @@ static const int kIdxsPerQuad = 9;
 //      | /  ______/ \ |
 //      */_-__________\*
 // For: 6 vertices and 18 indices (for 6 triangles)
-static const int kVertsPerLineSeg = 6;
-static const int kIdxsPerLineSeg = 18;
-
-static const int kNumQuadsInIdxBuffer = 256;
-static const size_t kQuadIdxSBufize = kIdxsPerQuad *
-                                      sizeof(uint16_t) *
-                                      kNumQuadsInIdxBuffer;
-
-static const int kNumLineSegsInIdxBuffer = 256;
-static const size_t kLineSegIdxSBufize = kIdxsPerLineSeg *
-                                         sizeof(uint16_t) *
-                                         kNumLineSegsInIdxBuffer;
-
-static bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
-    uint16_t* data = (uint16_t*) qIdxBuffer->map();
-    bool tempData = NULL == data;
-    if (tempData) {
-        data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad);
-    }
-    for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
-
-        // Each quadratic is rendered as a five sided polygon. This poly bounds
-        // the quadratic's bounding triangle but has been expanded so that the
-        // 1-pixel wide area around the curve is inside the poly.
-        // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
-        // that is rendered would look like this:
-        //              b0
-        //              b
-        //
-        //     a0              c0
-        //      a            c
-        //       a1       c1
-        // Each is drawn as three triangles specified by these 9 indices:
-        int baseIdx = i * kIdxsPerQuad;
-        uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
-        data[0 + baseIdx] = baseVert + 0; // a0
-        data[1 + baseIdx] = baseVert + 1; // a1
-        data[2 + baseIdx] = baseVert + 2; // b0
-        data[3 + baseIdx] = baseVert + 2; // b0
-        data[4 + baseIdx] = baseVert + 4; // c1
-        data[5 + baseIdx] = baseVert + 3; // c0
-        data[6 + baseIdx] = baseVert + 1; // a1
-        data[7 + baseIdx] = baseVert + 4; // c1
-        data[8 + baseIdx] = baseVert + 2; // b0
-    }
-    if (tempData) {
-        bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
-        delete[] data;
-        return ret;
-    } else {
-        qIdxBuffer->unmap();
-        return true;
-    }
-}
 
-static bool push_line_index_data(GrIndexBuffer* lIdxBuffer) {
-    uint16_t* data = (uint16_t*) lIdxBuffer->map();
-    bool tempData = NULL == data;
-    if (tempData) {
-        data = SkNEW_ARRAY(uint16_t, kNumLineSegsInIdxBuffer * kIdxsPerLineSeg);
-    }
-    for (int i = 0; i < kNumLineSegsInIdxBuffer; ++i) {
-        // Each line segment is rendered as two quads and two triangles.
-        // p0 and p1 have alpha = 1 while all other points have alpha = 0.
-        // The four external points are offset 1 pixel perpendicular to the
-        // line and half a pixel parallel to the line.
-        //
-        // p4                  p5
-        //      p0         p1
-        // p2                  p3
-        //
-        // Each is drawn as six triangles specified by these 18 indices:
-        int baseIdx = i * kIdxsPerLineSeg;
-        uint16_t baseVert = (uint16_t)(i * kVertsPerLineSeg);
-        data[0 + baseIdx] = baseVert + 0;
-        data[1 + baseIdx] = baseVert + 1;
-        data[2 + baseIdx] = baseVert + 3;
-
-        data[3 + baseIdx] = baseVert + 0;
-        data[4 + baseIdx] = baseVert + 3;
-        data[5 + baseIdx] = baseVert + 2;
-
-        data[6 + baseIdx] = baseVert + 0;
-        data[7 + baseIdx] = baseVert + 4;
-        data[8 + baseIdx] = baseVert + 5;
-
-        data[9 + baseIdx] = baseVert + 0;
-        data[10+ baseIdx] = baseVert + 5;
-        data[11+ baseIdx] = baseVert + 1;
-
-        data[12 + baseIdx] = baseVert + 0;
-        data[13 + baseIdx] = baseVert + 2;
-        data[14 + baseIdx] = baseVert + 4;
-
-        data[15 + baseIdx] = baseVert + 1;
-        data[16 + baseIdx] = baseVert + 5;
-        data[17 + baseIdx] = baseVert + 3;
-    }
-    if (tempData) {
-        bool ret = lIdxBuffer->updateData(data, kLineSegIdxSBufize);
-        delete[] data;
-        return ret;
-    } else {
-        lIdxBuffer->unmap();
-        return true;
-    }
-}
-}
+// Each quadratic is rendered as a five sided polygon. This poly bounds
+// the quadratic's bounding triangle but has been expanded so that the
+// 1-pixel wide area around the curve is inside the poly.
+// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
+// that is rendered would look like this:
+//              b0
+//              b
+//
+//     a0              c0
+//      a            c
+//       a1       c1
+// Each is drawn as three triangles specified by these 9 indices:
+static const uint16_t kQuadIdxBufPattern[] = {
+    0, 1, 2,
+    2, 4, 3,
+    1, 4, 2
+};
+
+static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
+static const int kQuadNumVertices = 5;
+static const int kQuadsNumInIdxBuffer = 256;
+
+
+// Each line segment is rendered as two quads and two triangles.
+// p0 and p1 have alpha = 1 while all other points have alpha = 0.
+// The four external points are offset 1 pixel perpendicular to the
+// line and half a pixel parallel to the line.
+//
+// p4                  p5
+//      p0         p1
+// p2                  p3
+//
+// Each is drawn as six triangles specified by these 18 indices:
+
+static const uint16_t kLineSegIdxBufPattern[] = {
+    0, 1, 3,
+    0, 3, 2,
+    0, 4, 5,
+    0, 5, 1,
+    0, 2, 4,
+    1, 5, 3
+};
+
+static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
+static const int kLineSegNumVertices = 6;
+static const int kLineSegsNumInIdxBuffer = 256;
 
 GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
     GrGpu* gpu = context->getGpu();
-    GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
+    GrIndexBuffer* qIdxBuf = gpu->createInstancedIndexBuffer(kQuadIdxBufPattern,
+                                                             kIdxsPerQuad,
+                                                             kQuadsNumInIdxBuffer,
+                                                             kQuadNumVertices);
     SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
-    if (NULL == qIdxBuf || !push_quad_index_data(qIdxBuf)) {
-        return NULL;
-    }
-    GrIndexBuffer* lIdxBuf = gpu->createIndexBuffer(kLineSegIdxSBufize, false);
+    GrIndexBuffer* lIdxBuf = gpu->createInstancedIndexBuffer(kLineSegIdxBufPattern,
+                                                             kIdxsPerLineSeg,
+                                                             kLineSegsNumInIdxBuffer,
+                                                             kLineSegNumVertices);
     SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf);
-    if (NULL == lIdxBuf || !push_line_index_data(lIdxBuf)) {
-        return NULL;
-    }
     return SkNEW_ARGS(GrAAHairLinePathRenderer,
                       (context, lIdxBuf, qIdxBuf));
 }
@@ -487,7 +423,7 @@ int generate_lines_and_quads(const SkPath& path,
 
 struct LineVertex {
     SkPoint fPos;
-    GrColor fCoverage;
+    float fCoverage;
 };
 
 struct BezierVertex {
@@ -525,14 +461,14 @@ void intersect_lines(const SkPoint& ptA, const SkVector& normA,
     result->fY = SkScalarMul(result->fY, wInv);
 }
 
-void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kVertsPerQuad]) {
+void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
     // this should be in the src space, not dev coords, when we have perspective
     GrPathUtils::QuadUVMatrix DevToUV(qpts);
-    DevToUV.apply<kVertsPerQuad, sizeof(BezierVertex), sizeof(SkPoint)>(verts);
+    DevToUV.apply<kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint)>(verts);
 }
 
 void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
-                const SkMatrix* toSrc, BezierVertex verts[kVertsPerQuad],
+                const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices],
                 SkRect* devBounds) {
     SkASSERT(!toDevice == !toSrc);
     // original quad is specified by tri a,b,c
@@ -598,10 +534,10 @@ void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
     c1.fPos -= cbN;
 
     intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
-    devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad);
+    devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
 
     if (toSrc) {
-        toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad);
+        toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
     }
 }
 
@@ -612,13 +548,13 @@ void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
 // f(x, y, w) = f(P) = K^2 - LM
 // K = dot(k, P), L = dot(l, P), M = dot(m, P)
 // k, l, m are calculated in function GrPathUtils::getConicKLM
-void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kVertsPerQuad],
+void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
                       const SkScalar weight) {
     SkScalar klm[9];
 
     GrPathUtils::getConicKLM(p, weight, klm);
 
-    for (int i = 0; i < kVertsPerQuad; ++i) {
+    for (int i = 0; i < kQuadNumVertices; ++i) {
         const SkPoint pnt = verts[i].fPos;
         verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2];
         verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5];
@@ -634,7 +570,7 @@ void add_conics(const SkPoint p[3],
                 SkRect* devBounds) {
     bloat_quad(p, toDevice, toSrc, *vert, devBounds);
     set_conic_coeffs(p, *vert, weight);
-    *vert += kVertsPerQuad;
+    *vert += kQuadNumVertices;
 }
 
 void add_quads(const SkPoint p[3],
@@ -652,13 +588,13 @@ void add_quads(const SkPoint p[3],
     } else {
         bloat_quad(p, toDevice, toSrc, *vert, devBounds);
         set_uv_quad(p, *vert);
-        *vert += kVertsPerQuad;
+        *vert += kQuadNumVertices;
     }
 }
 
 void add_line(const SkPoint p[2],
               const SkMatrix* toSrc,
-              GrColor coverage,
+              uint8_t coverage,
               LineVertex** vert) {
     const SkPoint& a = p[0];
     const SkPoint& b = p[1];
@@ -671,10 +607,12 @@ void add_line(const SkPoint p[2],
         ortho.fX = 2.0f * vec.fY;
         ortho.fY = -2.0f * vec.fX;
 
+        float floatCoverage = GrNormalizeByteToFloat(coverage);
+
         (*vert)[0].fPos = a;
-        (*vert)[0].fCoverage = coverage;
+        (*vert)[0].fCoverage = floatCoverage;
         (*vert)[1].fPos = b;
-        (*vert)[1].fCoverage = coverage;
+        (*vert)[1].fCoverage = floatCoverage;
         (*vert)[2].fPos = a - vec + ortho;
         (*vert)[2].fCoverage = 0;
         (*vert)[3].fPos = b + vec + ortho;
@@ -684,19 +622,19 @@ void add_line(const SkPoint p[2],
         (*vert)[5].fPos = b + vec - ortho;
         (*vert)[5].fCoverage = 0;
 
-        if (NULL != toSrc) {
+        if (toSrc) {
             toSrc->mapPointsWithStride(&(*vert)->fPos,
                                        sizeof(LineVertex),
-                                       kVertsPerLineSeg);
+                                       kLineSegNumVertices);
         }
     } else {
         // just make it degenerate and likely offscreen
-        for (int i = 0; i < kVertsPerLineSeg; ++i) {
+        for (int i = 0; i < kLineSegNumVertices; ++i) {
             (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
         }
     }
 
-    *vert += kVertsPerLineSeg;
+    *vert += kLineSegNumVertices;
 }
 
 }
@@ -708,13 +646,13 @@ namespace {
 // position + edge
 extern const GrVertexAttrib gHairlineBezierAttribs[] = {
     {kVec2f_GrVertexAttribType, 0,                  kPosition_GrVertexAttribBinding},
-    {kVec4f_GrVertexAttribType, sizeof(SkPoint),    kEffect_GrVertexAttribBinding}
+    {kVec4f_GrVertexAttribType, sizeof(SkPoint),    kGeometryProcessor_GrVertexAttribBinding}
 };
 
 // position + coverage
 extern const GrVertexAttrib gHairlineLineAttribs[] = {
     {kVec2f_GrVertexAttribType,  0,               kPosition_GrVertexAttribBinding},
-    {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kCoverage_GrVertexAttribBinding},
+    {kFloat_GrVertexAttribType, sizeof(SkPoint), kCoverage_GrVertexAttribBinding},
 };
 
 };
@@ -729,10 +667,10 @@ bool GrAAHairLinePathRenderer::createLineGeom(const SkPath& path,
 
     const SkMatrix& viewM = drawState->getViewMatrix();
 
-    int vertCnt = kVertsPerLineSeg * lineCnt;
+    int vertCnt = kLineSegNumVertices * lineCnt;
 
-    drawState->setVertexAttribs<gHairlineLineAttribs>(SK_ARRAY_COUNT(gHairlineLineAttribs));
-    SkASSERT(sizeof(LineVertex) == drawState->getVertexSize());
+    drawState->setVertexAttribs<gHairlineLineAttribs>(SK_ARRAY_COUNT(gHairlineLineAttribs),
+                                                      sizeof(LineVertex));
 
     if (!arg->set(target, vertCnt, 0)) {
         return false;
@@ -750,7 +688,7 @@ bool GrAAHairLinePathRenderer::createLineGeom(const SkPath& path,
     }
     devBounds->set(lines.begin(), lines.count());
     for (int i = 0; i < lineCnt; ++i) {
-        add_line(&lines[2*i], toSrc, drawState->getCoverageColor(), &verts);
+        add_line(&lines[2*i], toSrc, drawState->getCoverage(), &verts);
     }
     // All the verts computed by add_line are within sqrt(1^2 + 0.5^2) of the end points.
     static const SkScalar kSqrtOfOneAndAQuarter = 1.118f;
@@ -776,10 +714,10 @@ bool GrAAHairLinePathRenderer::createBezierGeom(
 
     const SkMatrix& viewM = drawState->getViewMatrix();
 
-    int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt;
+    int vertCnt = kQuadNumVertices * quadCnt + kQuadNumVertices * conicCnt;
 
-    target->drawState()->setVertexAttribs<gHairlineBezierAttribs>(SK_ARRAY_COUNT(gHairlineBezierAttribs));
-    SkASSERT(sizeof(BezierVertex) == target->getDrawState().getVertexSize());
+    int vAttribCnt = SK_ARRAY_COUNT(gHairlineBezierAttribs);
+    target->drawState()->setVertexAttribs<gHairlineBezierAttribs>(vAttribCnt, sizeof(BezierVertex));
 
     if (!arg->set(target, vertCnt, 0)) {
         return false;
@@ -808,7 +746,7 @@ bool GrAAHairLinePathRenderer::createBezierGeom(
         seedPts[0] = conics[0];
         seedPts[1] = conics[2];
     }
-    if (NULL != toDevice) {
+    if (toDevice) {
         toDevice->mapPoints(seedPts, 2);
     }
     devBounds->set(seedPts[0], seedPts[1]);
@@ -942,19 +880,19 @@ bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
 
         // Check devBounds
         SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(),
-                                          kVertsPerLineSeg * lineCnt));
+                                          kLineSegNumVertices * lineCnt));
 
         {
             GrDrawState::AutoRestoreEffects are(drawState);
             target->setIndexSourceToBuffer(fLinesIndexBuffer);
             int lines = 0;
             while (lines < lineCnt) {
-                int n = SkTMin(lineCnt - lines, kNumLineSegsInIdxBuffer);
+                int n = SkTMin(lineCnt - lines, kLineSegsNumInIdxBuffer);
                 target->drawIndexed(kTriangles_GrPrimitiveType,
-                                    kVertsPerLineSeg*lines,     // startV
-                                    0,                          // startI
-                                    kVertsPerLineSeg*n,         // vCount
-                                    kIdxsPerLineSeg*n,          // iCount
+                                    kLineSegNumVertices*lines,     // startV
+                                    0,                             // startI
+                                    kLineSegNumVertices*n,         // vCount
+                                    kIdxsPerLineSeg*n,             // iCount
                                     &devBounds);
                 lines += n;
             }
@@ -990,27 +928,25 @@ bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
         }
         GrDrawState* drawState = target->drawState();
 
-        static const int kEdgeAttrIndex = 1;
-
         // Check devBounds
         SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(),
-                                            kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt));
+                                            kQuadNumVertices * quadCnt + kQuadNumVertices * conicCnt));
 
         if (quadCnt > 0) {
-            GrEffectRef* hairQuadEffect = GrQuadEffect::Create(kHairlineAA_GrEffectEdgeType,
-                                                               *target->caps());
-            SkASSERT(NULL != hairQuadEffect);
+            GrGeometryProcessor* hairQuadProcessor =
+                    GrQuadEffect::Create(kHairlineAA_GrProcessorEdgeType, *target->caps());
+            SkASSERT(hairQuadProcessor);
             GrDrawState::AutoRestoreEffects are(drawState);
             target->setIndexSourceToBuffer(fQuadsIndexBuffer);
-            drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref();
+            drawState->setGeometryProcessor(hairQuadProcessor)->unref();
             int quads = 0;
             while (quads < quadCnt) {
-                int n = SkTMin(quadCnt - quads, kNumQuadsInIdxBuffer);
+                int n = SkTMin(quadCnt - quads, kQuadsNumInIdxBuffer);
                 target->drawIndexed(kTriangles_GrPrimitiveType,
-                                    kVertsPerQuad*quads,               // startV
-                                    0,                                 // startI
-                                    kVertsPerQuad*n,                   // vCount
-                                    kIdxsPerQuad*n,                    // iCount
+                                    kQuadNumVertices*quads,               // startV
+                                    0,                                    // startI
+                                    kQuadNumVertices*n,                   // vCount
+                                    kIdxsPerQuad*n,                       // iCount
                                     &devBounds);
                 quads += n;
             }
@@ -1018,18 +954,18 @@ bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
 
         if (conicCnt > 0) {
             GrDrawState::AutoRestoreEffects are(drawState);
-            GrEffectRef* hairConicEffect = GrConicEffect::Create(kHairlineAA_GrEffectEdgeType,
-                                                                 *target->caps());
-            SkASSERT(NULL != hairConicEffect);
-            drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref();
+            GrGeometryProcessor* hairConicProcessor = GrConicEffect::Create(
+                    kHairlineAA_GrProcessorEdgeType, *target->caps());
+            SkASSERT(hairConicProcessor);
+            drawState->setGeometryProcessor(hairConicProcessor)->unref();
             int conics = 0;
             while (conics < conicCnt) {
-                int n = SkTMin(conicCnt - conics, kNumQuadsInIdxBuffer);
+                int n = SkTMin(conicCnt - conics, kQuadsNumInIdxBuffer);
                 target->drawIndexed(kTriangles_GrPrimitiveType,
-                                    kVertsPerQuad*(quadCnt + conics),  // startV
-                                    0,                                 // startI
-                                    kVertsPerQuad*n,                   // vCount
-                                    kIdxsPerQuad*n,                    // iCount
+                                    kQuadNumVertices*(quadCnt + conics),  // startV
+                                    0,                                    // startI
+                                    kQuadNumVertices*n,                   // vCount
+                                    kIdxsPerQuad*n,                       // iCount
                                     &devBounds);
                 conics += n;
             }