This CL implements a tesselated path renderer, using GLU's libtess. All of the
authorsenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 28 Mar 2011 20:47:09 +0000 (20:47 +0000)
committersenorblanco@chromium.org <senorblanco@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 28 Mar 2011 20:47:09 +0000 (20:47 +0000)
fill modes except hairline are supported.  Note that the path renderer is not
enabled by default; to enable it, replace "GrCreatePathRenderer_none.cpp" with
"GrCreatePathRenderer_tesselated.cpp" in skia.gyp, and run gyp_skia, and build.

This change also contains a number of build fixes for Win32 (for building
SampleApp on VS2008) and Mac (for my ancient Mac Pro which supports
GL_EXT_framebuffer_object but not GL_ARB_framebuffer_object).  Also,
priorityq-heap.c was removed from the SampleApp build, since it's #included by
priorityq.c (weird, I know).

NB:  When this change is rolled into chrome, some modifications to chromium's
skia.gyp will be necessary.

Review URL:  http://codereview.appspot.com/4289072/

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

gpu/include/GrConfig.h
gpu/include/GrPathRenderer.h
gpu/include/GrTesselatedPathRenderer.h [new file with mode: 0644]
gpu/src/GrCreatePathRenderer_tesselated.cpp [new file with mode: 0644]
gpu/src/GrGLInterface.cpp
gpu/src/GrGpu.cpp
gpu/src/GrPathRenderer.cpp
gpu/src/GrPathUtils.cpp [new file with mode: 0644]
gpu/src/GrPathUtils.h [new file with mode: 0644]
gpu/src/GrTesselatedPathRenderer.cpp [new file with mode: 0644]
gyp/skia.gyp

index 5a08077..5d9d20f 100644 (file)
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
+#if GR_WIN32_BUILD
+// VC8 doesn't support stdint.h, so we define those types here.
+typedef signed char int8_t;
+typedef unsigned char uint8_t;
+typedef short int16_t;
+typedef unsigned short uint16_t;
+typedef int int32_t;
+typedef unsigned uint32_t;
+#else
 /*
  *  Include stdint.h with defines that trigger declaration of C99 limit/const
  *  macros here before anyone else has a chance to include stdint.h without 
 #define __STDC_LIMIT_MACROS
 #define __STDC_CONSTANT_MACROS
 #include <stdint.h>
+#endif
 
 /*
  *  The "user config" file can be empty, and everything should work. It is
index 2d846c8..aadcb0d 100644 (file)
@@ -161,159 +161,3 @@ private:
 };
 
 #endif
-/*
-    Copyright 2011 Google Inc.
-
-    Licensed under the Apache License, Version 2.0 (the "License");
-    you may not use this file except in compliance with the License.
-    You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-    Unless required by applicable law or agreed to in writing, software
-    distributed under the License is distributed on an "AS IS" BASIS,
-    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-    See the License for the specific language governing permissions and
-    limitations under the License.
- */
-
-#ifndef GrPathRenderer_DEFINED
-#define GrPathRenderer_DEFINED
-
-#include "GrDrawTarget.h"
-
-class GrPathIter;
-struct GrPoint;
-
-/**
- *  Base class for drawing paths into a GrDrawTarget.
- */
-class GrPathRenderer : public GrRefCnt {
-public:
-    /**
-     * Returns true if this path renderer is able to render the path.
-     * Returning false allows the caller to fallback to another path renderer.
-     *
-     * @param target    The target to draw into
-     * @param path      The path to draw
-     * @param fill      The fill rule to use
-     *
-     * @return  true if the path can be drawn by this object, false otherwise.
-     */
-    virtual bool canDrawPath(const GrDrawTarget* target,
-                             GrPathIter* path,
-                             GrPathFill fill) const = 0;
-
-    /**
-     * Draws a path into the draw target. The target will already have its draw
-     * state configured for the draw.
-     * @param target                the target to draw into.
-     * @param stages                indicates which stages the are already
-     *                              in use. All enabled stages expect positions
-     *                              as texture coordinates. The path renderer
-     *                              use the remaining stages for its path
-     *                              filling algorithm.
-     * @param path                  the path to draw.
-     * @param fill                  the fill rule to apply.
-     * @param translate             optional additional translation to apply to
-     *                              the path. NULL means (0,0).
-     */
-    virtual void drawPath(GrDrawTarget* target,
-                          GrDrawTarget::StageBitfield stages,
-                          GrPathIter* path,
-                          GrPathFill fill,
-                          const GrPoint* translate) = 0;
-
-    /**
-     * For complex clips Gr uses the stencil buffer. The path renderer must be
-     * able to render paths into the stencil buffer. However, the path renderer
-     * itself may require the stencil buffer to resolve the path fill rule. This
-     * function queries whether the path render needs its own stencil
-     * pass. If this returns false then drawPath() should not modify the
-     * the target's stencil settings but use those already set on target.
-     *
-     * @param target target that the path will be rendered to
-     * @param path   the path that will be drawn
-     * @param fill   the fill rule that will be used, will never be an inverse
-     *               rule.
-     *
-     * @return false if this path renderer can generate interior-only fragments
-     *         without changing the stencil settings on the target. If it
-     *         returns true the drawPathToStencil will be used when rendering
-     *         clips.
-     */
-    virtual bool requiresStencilPass(const GrDrawTarget* target,
-                                     GrPathIter* path,
-                                     GrPathFill fill) const { return false; }
-
-    /**
-     * Draws a path to the stencil buffer. Assume the writable stencil bits
-     * are already initialized to zero. Fill will always be either
-     * kWinding_PathFill or kEvenOdd_PathFill.
-     *
-     * Only called if requiresStencilPass returns true for the same combo of
-     * target, path, and fill. Never called with an inverse fill.
-     *
-     * The default implementation assumes the path filling algorithm doesn't
-     * require a separate stencil pass and so crashes.
-     *
-     *
-     * @param target                the target to draw into.
-     * @param path                  the path to draw.
-     * @param fill                  the fill rule to apply.
-     * @param translate             optional additional translation to apply to
-     *                              the path. NULL means (0,0).
-     */
-    virtual void drawPathToStencil(GrDrawTarget* target,
-                                   GrPathIter* path,
-                                   GrPathFill fill,
-                                   const GrPoint* translate) {
-        GrCrash("Unexpected call to drawPathToStencil.");
-    }
-
-private:
-
-    typedef GrRefCnt INHERITED;
-};
-
-/**
- *  Subclass that renders the path using the stencil buffer to resolve fill
- *  rules (e.g. winding, even-odd)
- */
-class GrDefaultPathRenderer : public GrPathRenderer {
-public:
-    GrDefaultPathRenderer(bool separateStencilSupport,
-                          bool stencilWrapOpsSupport);
-
-    virtual bool canDrawPath(const GrDrawTarget* target,
-                             GrPathIter* path,
-                             GrPathFill fill) const { return true; }
-
-    virtual void drawPath(GrDrawTarget* target,
-                          GrDrawTarget::StageBitfield stages,
-                          GrPathIter* path,
-                          GrPathFill fill,
-                          const GrPoint* translate);
-    virtual bool requiresStencilPass(const GrDrawTarget* target,
-                                     GrPathIter* path,
-                                     GrPathFill fill) const;
-    virtual void drawPathToStencil(GrDrawTarget* target,
-                                   GrPathIter* path,
-                                   GrPathFill fill,
-                                   const GrPoint* translate);
-private:
-
-    void drawPathHelper(GrDrawTarget* target,
-                        GrDrawTarget::StageBitfield stages,
-                        GrPathIter* path,
-                        GrPathFill fill,
-                        const GrPoint* translate,
-                        bool stencilOnly);
-
-    bool    fSeparateStencil;
-    bool    fStencilWrapOps;
-
-    typedef GrPathRenderer INHERITED;
-};
-
-#endif
diff --git a/gpu/include/GrTesselatedPathRenderer.h b/gpu/include/GrTesselatedPathRenderer.h
new file mode 100644 (file)
index 0000000..3efc471
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+    Copyright 2011 Google Inc.
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+ */
+
+#ifndef GrTesselatedPathRenderer_DEFINED
+#define GrTesselatedPathRenderer_DEFINED
+
+#include "GrPathRenderer.h"
+
+class GrTesselatedPathRenderer : public GrPathRenderer {
+public:
+    GrTesselatedPathRenderer();
+
+    virtual void drawPath(GrDrawTarget* target,
+                          GrDrawTarget::StageBitfield stages,
+                          GrPathIter* path,
+                          GrPathFill fill,
+                          const GrPoint* translate);
+    virtual bool canDrawPath(const GrDrawTarget* target,
+                             GrPathIter* path,
+                             GrPathFill fill) const;
+
+    virtual bool requiresStencilPass(const GrDrawTarget* target,
+                                     GrPathIter* path,
+                                     GrPathFill fill) const { return false; }
+    virtual void drawPathToStencil(GrDrawTarget* target,
+                                   GrPathIter* path,
+                                   GrPathFill fill,
+                                   const GrPoint* translate);
+};
+
+#endif
diff --git a/gpu/src/GrCreatePathRenderer_tesselated.cpp b/gpu/src/GrCreatePathRenderer_tesselated.cpp
new file mode 100644 (file)
index 0000000..b00cf35
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+    Copyright 2011 Google Inc.
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+ */
+
+#include "GrTesselatedPathRenderer.h"
+
+
+GrPathRenderer* GrPathRenderer::CreatePathRenderer() { return new GrTesselatedPathRenderer(); }
index 9c3d5bb..8be0f2b 100644 (file)
@@ -147,7 +147,7 @@ void InitializeGLInterfaceExtensions(GrGLInterface* glBindings) {
         fboFound = true;
     }
 
-    #if GL_EXT_framebuffer_object && !GR_MAC_BUILD
+    #if GL_EXT_framebuffer_object
     if (!fboFound &&
             has_gl_extension_from_string("GL_EXT_framebuffer_object",
                                          extensionString)) {
index 4b52fd8..1db9252 100644 (file)
@@ -610,7 +610,7 @@ void GrGpu::prepareVertexPool() {
 }
 
 void GrGpu::prepareIndexPool() {
-    if (NULL == fVertexPool) {
+    if (NULL == fIndexPool) {
         fIndexPool = new GrIndexBufferAllocPool(this, true, 0, 1);
     } else if (!fIndexPoolInUse) {
         // the client doesn't have valid data in the pool
index fc3c124..f226a99 100644 (file)
@@ -3,6 +3,7 @@
 #include "GrPoint.h"
 #include "GrDrawTarget.h"
 #include "GrPathIter.h"
+#include "GrPathUtils.h"
 #include "GrMemory.h"
 #include "GrTexture.h"
 
@@ -147,127 +148,6 @@ static const GrStencilSettings gDirectToStencil = {
 // Helpers for drawPath
 
 #define STENCIL_OFF     0   // Always disable stencil (even when needed)
-static const GrScalar gTolerance = GR_Scalar1;
-
-static const uint32_t MAX_POINTS_PER_CURVE = 1 << 10;
-
-static uint32_t quadratic_point_count(const GrPoint points[], GrScalar tol) {
-    GrScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]);
-    if (d < tol) {
-        return 1;
-    } else {
-        // Each time we subdivide, d should be cut in 4. So we need to
-        // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x)
-        // points.
-        // 2^(log4(x)) = sqrt(x);
-        d = ceilf(sqrtf(d/tol));
-        return GrMin(GrNextPow2((uint32_t)d), MAX_POINTS_PER_CURVE);
-    }
-}
-
-static uint32_t generate_quadratic_points(const GrPoint& p0,
-                                          const GrPoint& p1,
-                                          const GrPoint& p2,
-                                          GrScalar tolSqd,
-                                          GrPoint** points,
-                                          uint32_t pointsLeft) {
-    if (pointsLeft < 2 ||
-        (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) {
-        (*points)[0] = p2;
-        *points += 1;
-        return 1;
-    }
-
-    GrPoint q[] = {
-        GrPoint(GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY)),
-        GrPoint(GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY)),
-    };
-    GrPoint r(GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY));
-
-    pointsLeft >>= 1;
-    uint32_t a = generate_quadratic_points(p0, q[0], r, tolSqd, points, pointsLeft);
-    uint32_t b = generate_quadratic_points(r, q[1], p2, tolSqd, points, pointsLeft);
-    return a + b;
-}
-
-static uint32_t cubic_point_count(const GrPoint points[], GrScalar tol) {
-    GrScalar d = GrMax(points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]),
-                       points[2].distanceToLineSegmentBetweenSqd(points[0], points[3]));
-    d = sqrtf(d);
-    if (d < tol) {
-        return 1;
-    } else {
-        d = ceilf(sqrtf(d/tol));
-        return GrMin(GrNextPow2((uint32_t)d), MAX_POINTS_PER_CURVE);
-    }
-}
-
-static uint32_t generate_cubic_points(const GrPoint& p0,
-                                      const GrPoint& p1,
-                                      const GrPoint& p2,
-                                      const GrPoint& p3,
-                                      GrScalar tolSqd,
-                                      GrPoint** points,
-                                      uint32_t pointsLeft) {
-    if (pointsLeft < 2 ||
-        (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd &&
-         p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) {
-            (*points)[0] = p3;
-            *points += 1;
-            return 1;
-        }
-    GrPoint q[] = {
-        GrPoint(GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY)),
-        GrPoint(GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY)),
-        GrPoint(GrScalarAve(p2.fX, p3.fX), GrScalarAve(p2.fY, p3.fY))
-    };
-    GrPoint r[] = {
-        GrPoint(GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY)),
-        GrPoint(GrScalarAve(q[1].fX, q[2].fX), GrScalarAve(q[1].fY, q[2].fY))
-    };
-    GrPoint s(GrScalarAve(r[0].fX, r[1].fX), GrScalarAve(r[0].fY, r[1].fY));
-    pointsLeft >>= 1;
-    uint32_t a = generate_cubic_points(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
-    uint32_t b = generate_cubic_points(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
-    return a + b;
-}
-
-static int worst_case_point_count(GrPathIter* path,
-                                  int* subpaths,
-                                  GrScalar tol) {
-    int pointCount = 0;
-    *subpaths = 1;
-
-    bool first = true;
-
-    GrPathCmd cmd;
-
-    GrPoint pts[4];
-    while ((cmd = path->next(pts)) != kEnd_PathCmd) {
-
-        switch (cmd) {
-            case kLine_PathCmd:
-                pointCount += 1;
-                break;
-            case kQuadratic_PathCmd:
-                pointCount += quadratic_point_count(pts, tol);
-                break;
-            case kCubic_PathCmd:
-                pointCount += cubic_point_count(pts, tol);
-                break;
-            case kMove_PathCmd:
-                pointCount += 1;
-                if (!first) {
-                    ++(*subpaths);
-                }
-                break;
-            default:
-                break;
-        }
-        first = false;
-    }
-    return pointCount;
-}
 
 static inline bool single_pass_path(const GrDrawTarget& target,
                                     const GrPathIter& path,
@@ -314,7 +194,7 @@ void GrDefaultPathRenderer::drawPathHelper(GrDrawTarget* target,
     // stretch when mapping to screen coordinates.
     GrScalar stretch = viewM.getMaxStretch();
     bool useStretch = stretch > 0;
-    GrScalar tol = gTolerance;
+    GrScalar tol = GrPathUtils::gTolerance;
 
     if (!useStretch) {
         // TODO: deal with perspective in some better way.
@@ -328,9 +208,7 @@ void GrDefaultPathRenderer::drawPathHelper(GrDrawTarget* target,
     path->rewind();
 
     int subpathCnt;
-    int maxPts = worst_case_point_count(path,
-                                        &subpathCnt,
-                                        tol);
+    int maxPts = GrPathUtils::worstCasePointCount(path, &subpathCnt, tol);
 
     GrVertexLayout layout = 0;
     for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
@@ -468,15 +346,15 @@ void GrDefaultPathRenderer::drawPathHelper(GrDrawTarget* target,
                 vert++;
                 break;
             case kQuadratic_PathCmd: {
-                generate_quadratic_points(pts[0], pts[1], pts[2],
-                                          tolSqd, &vert,
-                                          quadratic_point_count(pts, tol));
+                GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
+                                                     tolSqd, &vert,
+                                                     GrPathUtils::quadraticPointCount(pts, tol));
                 break;
             }
             case kCubic_PathCmd: {
-                generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
-                                      tolSqd, &vert,
-                                      cubic_point_count(pts, tol));
+                GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
+                                                 tolSqd, &vert,
+                                                 GrPathUtils::cubicPointCount(pts, tol));
                 break;
             }
             case kClose_PathCmd:
@@ -563,7 +441,7 @@ void GrDefaultPathRenderer::drawPathToStencil(GrDrawTarget* target,
                                               GrPathIter* path,
                                               GrPathFill fill,
                                               const GrPoint* translate) {
-     GrAssert(kInverseEvenOdd_PathFill != fill);
-     GrAssert(kInverseWinding_PathFill != fill);
-     this->drawPathHelper(target, 0, path, fill, translate, true);
- }
+    GrAssert(kInverseEvenOdd_PathFill != fill);
+    GrAssert(kInverseWinding_PathFill != fill);
+    this->drawPathHelper(target, 0, path, fill, translate, true);
+}
diff --git a/gpu/src/GrPathUtils.cpp b/gpu/src/GrPathUtils.cpp
new file mode 100644 (file)
index 0000000..274dc49
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+    Copyright 2011 Google Inc.
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+ */
+
+#include "GrPathUtils.h"
+
+#include "GrPathIter.h"
+#include "GrPoint.h"
+
+const GrScalar GrPathUtils::gTolerance = GR_Scalar1;
+
+static const uint32_t MAX_POINTS_PER_CURVE = 1 << 10;
+
+uint32_t GrPathUtils::quadraticPointCount(const GrPoint points[],
+                                             GrScalar tol) {
+    GrScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]);
+    if (d < tol) {
+        return 1;
+    } else {
+        // Each time we subdivide, d should be cut in 4. So we need to
+        // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x)
+        // points.
+        // 2^(log4(x)) = sqrt(x);
+        d = ceilf(sqrtf(d/tol));
+        return GrMin(GrNextPow2((uint32_t)d), MAX_POINTS_PER_CURVE);
+    }
+}
+
+uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0,
+                                                 const GrPoint& p1,
+                                                 const GrPoint& p2,
+                                                 GrScalar tolSqd,
+                                                 GrPoint** points,
+                                                 uint32_t pointsLeft) {
+    if (pointsLeft < 2 ||
+        (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) {
+        (*points)[0] = p2;
+        *points += 1;
+        return 1;
+    }
+
+    GrPoint q[] = {
+        GrPoint(GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY)),
+        GrPoint(GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY)),
+    };
+    GrPoint r(GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY));
+
+    pointsLeft >>= 1;
+    uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft);
+    uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft);
+    return a + b;
+}
+
+uint32_t GrPathUtils::cubicPointCount(const GrPoint points[],
+                                           GrScalar tol) {
+    GrScalar d = GrMax(points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]),
+                       points[2].distanceToLineSegmentBetweenSqd(points[0], points[3]));
+    d = sqrtf(d);
+    if (d < tol) {
+        return 1;
+    } else {
+        d = ceilf(sqrtf(d/tol));
+        return GrMin(GrNextPow2((uint32_t)d), MAX_POINTS_PER_CURVE);
+    }
+}
+
+uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0,
+                                             const GrPoint& p1,
+                                             const GrPoint& p2,
+                                             const GrPoint& p3,
+                                             GrScalar tolSqd,
+                                             GrPoint** points,
+                                             uint32_t pointsLeft) {
+    if (pointsLeft < 2 ||
+        (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd &&
+         p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) {
+            (*points)[0] = p3;
+            *points += 1;
+            return 1;
+        }
+    GrPoint q[] = {
+        GrPoint(GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY)),
+        GrPoint(GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY)),
+        GrPoint(GrScalarAve(p2.fX, p3.fX), GrScalarAve(p2.fY, p3.fY))
+    };
+    GrPoint r[] = {
+        GrPoint(GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY)),
+        GrPoint(GrScalarAve(q[1].fX, q[2].fX), GrScalarAve(q[1].fY, q[2].fY))
+    };
+    GrPoint s(GrScalarAve(r[0].fX, r[1].fX), GrScalarAve(r[0].fY, r[1].fY));
+    pointsLeft >>= 1;
+    uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
+    uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
+    return a + b;
+}
+
+int GrPathUtils::worstCasePointCount(GrPathIter* path,
+                                        int* subpaths,
+                                        GrScalar tol) {
+    int pointCount = 0;
+    *subpaths = 1;
+
+    bool first = true;
+
+    GrPathCmd cmd;
+
+    GrPoint pts[4];
+    while ((cmd = path->next(pts)) != kEnd_PathCmd) {
+
+        switch (cmd) {
+            case kLine_PathCmd:
+                pointCount += 1;
+                break;
+            case kQuadratic_PathCmd:
+                pointCount += quadraticPointCount(pts, tol);
+                break;
+            case kCubic_PathCmd:
+                pointCount += cubicPointCount(pts, tol);
+                break;
+            case kMove_PathCmd:
+                pointCount += 1;
+                if (!first) {
+                    ++(*subpaths);
+                }
+                break;
+            default:
+                break;
+        }
+        first = false;
+    }
+    return pointCount;
+}
diff --git a/gpu/src/GrPathUtils.h b/gpu/src/GrPathUtils.h
new file mode 100644 (file)
index 0000000..97841dd
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+    Copyright 2011 Google Inc.
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+ */
+
+#ifndef GrPathUtils_DEFINED
+#define GrPathUtils_DEFINED
+
+#include "GrNoncopyable.h"
+#include "GrScalar.h"
+
+class GrPathIter;
+struct GrPoint;
+
+/**
+ *  Utilities for evaluating paths.
+ */
+class GrPathUtils : public GrNoncopyable {
+public:
+    static int worstCasePointCount(GrPathIter* path,
+                                   int* subpaths,
+                                   GrScalar tol);
+    static uint32_t quadraticPointCount(const GrPoint points[], GrScalar tol);
+    static uint32_t generateQuadraticPoints(const GrPoint& p0,
+                                            const GrPoint& p1,
+                                            const GrPoint& p2,
+                                            GrScalar tolSqd,
+                                            GrPoint** points,
+                                            uint32_t pointsLeft);
+    static uint32_t cubicPointCount(const GrPoint points[], GrScalar tol);
+    static uint32_t generateCubicPoints(const GrPoint& p0,
+                                        const GrPoint& p1,
+                                        const GrPoint& p2,
+                                        const GrPoint& p3,
+                                        GrScalar tolSqd,
+                                        GrPoint** points,
+                                        uint32_t pointsLeft);
+
+    static const GrScalar gTolerance;
+};
+#endif
diff --git a/gpu/src/GrTesselatedPathRenderer.cpp b/gpu/src/GrTesselatedPathRenderer.cpp
new file mode 100644 (file)
index 0000000..95e8abe
--- /dev/null
@@ -0,0 +1,276 @@
+/*
+    Copyright 2011 Google Inc.
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+ */
+
+#include "GrTesselatedPathRenderer.h"
+
+#include "GrMemory.h"
+#include "GrPathUtils.h"
+
+#include <internal_glu.h>
+
+struct PolygonData {
+    PolygonData(GrTDArray<GrPoint>* vertices, GrTDArray<short>* indices)
+      : fVertices(vertices)
+      , fIndices(indices)
+    {
+    }
+    GrTDArray<GrPoint>* fVertices;
+    GrTDArray<short>* fIndices;
+};
+
+static void beginData(GLenum type, void* data)
+{
+    GR_DEBUGASSERT(type == GL_TRIANGLES);
+}
+
+static void edgeFlagData(GLboolean flag, void* data)
+{
+}
+
+static void vertexData(void* vertexData, void* data)
+{
+    short* end = static_cast<PolygonData*>(data)->fIndices->append();
+    *end = reinterpret_cast<long>(vertexData);
+}
+
+static void endData(void* data)
+{
+}
+
+static void combineData(GLdouble coords[3], void* vertexData[4],
+                                 GLfloat weight[4], void **outData, void* data)
+{
+    PolygonData* polygonData = static_cast<PolygonData*>(data);
+    int index = polygonData->fVertices->count();
+    *polygonData->fVertices->append() = GrPoint(static_cast<float>(coords[0]),
+                                                 static_cast<float>(coords[1]));
+    *outData = reinterpret_cast<void*>(index);
+}
+
+typedef void (*TESSCB)();
+
+static unsigned fill_type_to_glu_winding_rule(GrPathFill fill) {
+    switch (fill) {
+        case kWinding_PathFill:
+            return GLU_TESS_WINDING_NONZERO;
+        case kEvenOdd_PathFill:
+            return GLU_TESS_WINDING_ODD;
+        case kInverseWinding_PathFill:
+            return GLU_TESS_WINDING_POSITIVE;
+        case kInverseEvenOdd_PathFill:
+            return GLU_TESS_WINDING_ODD;
+        case kHairLine_PathFill:
+            return GLU_TESS_WINDING_NONZERO;  // FIXME:  handle this
+        default:
+            GrAssert(!"Unknown path fill!");
+            return 0;
+    }
+}
+
+GrTesselatedPathRenderer::GrTesselatedPathRenderer() {
+}
+
+void GrTesselatedPathRenderer::drawPath(GrDrawTarget* target,
+                                        GrDrawTarget::StageBitfield stages,
+                                        GrPathIter* path,
+                                        GrPathFill fill,
+                                        const GrPoint* translate) {
+    GrDrawTarget::AutoStateRestore asr(target);
+    bool colorWritesWereDisabled = target->isColorWriteDisabled();
+    // face culling doesn't make sense here
+    GrAssert(GrDrawTarget::kBoth_DrawFace == target->getDrawFace());
+
+    GrMatrix viewM = target->getViewMatrix();
+    // In order to tesselate the path we get a bound on how much the matrix can
+    // stretch when mapping to screen coordinates.
+    GrScalar stretch = viewM.getMaxStretch();
+    bool useStretch = stretch > 0;
+    GrScalar tol = GrPathUtils::gTolerance;
+
+    if (!useStretch) {
+        // TODO: deal with perspective in some better way.
+        tol /= 10;
+    } else {
+        GrScalar sinv = GR_Scalar1 / stretch;
+        tol = GrMul(tol, sinv);
+    }
+    GrScalar tolSqd = GrMul(tol, tol);
+
+    path->rewind();
+
+    int subpathCnt;
+    int maxPts = GrPathUtils::worstCasePointCount(path, &subpathCnt, tol);
+
+    GrVertexLayout layout = 0;
+    for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
+        if ((1 << s) & stages) {
+            layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
+        }
+    }
+
+    bool inverted = IsFillInverted(fill);
+    if (inverted) {
+        maxPts += 4;
+        subpathCnt++;
+    }
+    GrPoint* base = new GrPoint[maxPts];
+    GrPoint* vert = base;
+    GrPoint* subpathBase = base;
+
+    GrAutoSTMalloc<8, uint16_t> subpathVertCount(subpathCnt);
+
+    path->rewind();
+
+    GrPoint pts[4];
+
+    bool first = true;
+    int subpath = 0;
+
+    for (;;) {
+        GrPathCmd cmd = path->next(pts);
+        switch (cmd) {
+            case kMove_PathCmd:
+                if (!first) {
+                    subpathVertCount[subpath] = vert-subpathBase;
+                    subpathBase = vert;
+                    ++subpath;
+                }
+                *vert = pts[0];
+                vert++;
+                break;
+            case kLine_PathCmd:
+                *vert = pts[1];
+                vert++;
+                break;
+            case kQuadratic_PathCmd: {
+                GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
+                                                     tolSqd, &vert,
+                                                     GrPathUtils::quadraticPointCount(pts, tol));
+                break;
+            }
+            case kCubic_PathCmd: {
+                GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
+                                                 tolSqd, &vert,
+                                                 GrPathUtils::cubicPointCount(pts, tol));
+                break;
+            }
+            case kClose_PathCmd:
+                break;
+            case kEnd_PathCmd:
+                subpathVertCount[subpath] = vert-subpathBase;
+                ++subpath; // this could be only in debug
+                goto FINISHED;
+        }
+        first = false;
+    }
+FINISHED:
+    if (translate) {
+        for (int i = 0; i < vert - base; i++) {
+            base[i].offset(translate->fX, translate->fY);
+        }
+    }
+
+    if (inverted) {
+        GrRect bounds;
+        GrAssert(NULL != target->getRenderTarget());
+        bounds.setLTRB(0, 0,
+                       GrIntToScalar(target->getRenderTarget()->width()),
+                       GrIntToScalar(target->getRenderTarget()->height()));
+        GrMatrix vmi;
+        if (target->getViewInverse(&vmi)) {
+            vmi.mapRect(&bounds);
+        }
+        *vert++ = GrPoint(bounds.fLeft, bounds.fTop);
+        *vert++ = GrPoint(bounds.fLeft, bounds.fBottom);
+        *vert++ = GrPoint(bounds.fRight, bounds.fBottom);
+        *vert++ = GrPoint(bounds.fRight, bounds.fTop);
+        subpathVertCount[subpath++] = 4;
+    }
+
+    GrAssert(subpath == subpathCnt);
+    GrAssert((vert - base) <= maxPts);
+
+    size_t count = vert - base;
+
+    // FIXME:  This copy could be removed if we had (templated?) versions of
+    // generate_*_point above that wrote directly into doubles.
+    double* inVertices = new double[count * 3];
+    for (size_t i = 0; i < count; ++i) {
+        inVertices[i * 3]     = base[i].fX;
+        inVertices[i * 3 + 1] = base[i].fY;
+        inVertices[i * 3 + 2] = 1.0;
+    }
+
+    GLUtesselator* tess = internal_gluNewTess();
+    unsigned windingRule = fill_type_to_glu_winding_rule(fill);
+    internal_gluTessProperty(tess, GLU_TESS_WINDING_RULE, windingRule);
+    internal_gluTessCallback(tess, GLU_TESS_BEGIN_DATA, (TESSCB) &beginData);
+    internal_gluTessCallback(tess, GLU_TESS_VERTEX_DATA, (TESSCB) &vertexData);
+    internal_gluTessCallback(tess, GLU_TESS_END_DATA, (TESSCB) &endData);
+    internal_gluTessCallback(tess, GLU_TESS_EDGE_FLAG_DATA, (TESSCB) &edgeFlagData);
+    internal_gluTessCallback(tess, GLU_TESS_COMBINE_DATA, (TESSCB) &combineData);
+    GrTDArray<short> indices;
+    GrTDArray<GrPoint> vertices;
+    PolygonData data(&vertices, &indices);
+
+    internal_gluTessBeginPolygon(tess, &data);
+    size_t i = 0;
+    for (int sp = 0; sp < subpathCnt; ++sp) {
+        internal_gluTessBeginContour(tess);
+        int start = i;
+        int end = start + subpathVertCount[sp];
+        for (; i < end; ++i) {
+            double* inVertex = &inVertices[i * 3];
+            *vertices.append() = GrPoint(inVertex[0], inVertex[1]);
+            internal_gluTessVertex(tess, inVertex, reinterpret_cast<void*>(i));
+        }
+        internal_gluTessEndContour(tess);
+    }
+
+    internal_gluTessEndPolygon(tess);
+    internal_gluDeleteTess(tess);
+
+    // FIXME:  If we could figure out the maxIndices before running the
+    // tesselator, we could allocate the geometry upfront, rather than making
+    // yet another copy.
+    GrDrawTarget::AutoReleaseGeometry geom(target, layout, vertices.count(), indices.count());
+
+    memcpy(geom.vertices(), vertices.begin(), vertices.count() * sizeof(GrPoint));
+    memcpy(geom.indices(), indices.begin(), indices.count() * sizeof(short));
+
+    if (indices.count() > 0) {
+        target->drawIndexed(kTriangles_PrimitiveType,
+                            0,
+                            0,
+                            vertices.count(),
+                            indices.count());
+    }
+    delete[] inVertices;
+    delete[] base;
+}
+
+bool GrTesselatedPathRenderer::canDrawPath(const GrDrawTarget* target,
+                                           GrPathIter* path,
+                                           GrPathFill fill) const {
+    return kHairLine_PathFill != fill;
+}
+
+void GrTesselatedPathRenderer::drawPathToStencil(GrDrawTarget* target,
+                                                 GrPathIter* path,
+                                                 GrPathFill fill,
+                                                 const GrPoint* translate) {
+    GrAlwaysAssert(!"multipass stencil should not be needed");
+}
index d4bb4d0..012c6a8 100644 (file)
@@ -24,6 +24,7 @@
       [ 'OS == "win"', {
         'defines': [
           'SK_BUILD_FOR_WIN32',
+          'SK_IGNORE_STDINT_DOT_H',
         ],
       },],
     ],
       'include_dirs': [
         '../gpu/include',
       ],
+      #'dependencies': [
+      #  'libtess',
+      #],
       'sources': [
         '../gpu/include/GrAllocator.h',
         '../gpu/include/GrAllocPool.h',
         '../gpu/include/GrPathIter.h',
         '../gpu/include/GrPathRenderer.h',
         '../gpu/include/GrPathSink.h',
+        '../gpu/include/GrPathUtils.h',
         '../gpu/include/GrPlotMgr.h',
         '../gpu/include/GrPoint.h',
         '../gpu/include/GrRandom.h',
         '../gpu/include/GrTArray.h',
         '../gpu/include/GrTBSearch.h',
         '../gpu/include/GrTDArray.h',
+        #'../gpu/include/GrTesselatedPathRenderer.h',
         '../gpu/include/GrTextContext.h',
         '../gpu/include/GrTextStrike.h',
         '../gpu/include/GrTexture.h',
         '../gpu/src/GrMemory.cpp',
         '../gpu/src/GrPath.cpp',
         '../gpu/src/GrPathRenderer.cpp',
+        '../gpu/src/GrPathUtils.cpp',
         '../gpu/src/GrPrintf_printf.cpp',
         '../gpu/src/GrRectanizer.cpp',
         '../gpu/src/GrRedBlackTree.h',
         '../gpu/src/GrStencil.cpp',
+        #'../gpu/src/GrTesselatedPathRenderer.cpp',
         '../gpu/src/GrTextContext.cpp',
         '../gpu/src/GrTextStrike.cpp',
         '../gpu/src/GrTextStrike_impl.h',
         '../third_party/glu/libtess/mesh.h',
         '../third_party/glu/libtess/normal.c',
         '../third_party/glu/libtess/normal.h',
-        '../third_party/glu/libtess/priorityq-heap.c',
         '../third_party/glu/libtess/priorityq-heap.h',
         '../third_party/glu/libtess/priorityq-sort.h',
         '../third_party/glu/libtess/priorityq.c',