First portion of refactoring to bundle SW path rendering into GrPathRenderer

http://codereview.appspot.com/6125046/



git-svn-id: http://skia.googlecode.com/svn/trunk@3769 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 3fb944b..8e45e15 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -1290,8 +1290,8 @@
 }
 
 // gets device coord bounds of path (not considering the fill) and clip. The
-// path bounds will be a subset of the clip bounds. returns false if path bounds
-// would be empty.
+// path bounds will be a subset of the clip bounds. returns false if 
+// path bounds would be empty.
 bool get_path_and_clip_bounds(const GrDrawTarget* target,
                               const GrPath& path,
                               const GrVec* translate,
@@ -1341,7 +1341,8 @@
                                   GrPathFill fill,
                                   GrContext* context,
                                   const GrPoint* translate,
-                                  GrAutoScratchTexture* tex) {
+                                  GrAutoScratchTexture* tex,
+                                  bool antiAlias) {
     SkPaint paint;
     SkPath tmpPath;
     const SkPath* pathToDraw = &clientPath;
@@ -1357,7 +1358,7 @@
             pathToDraw = &tmpPath;
         }
     }
-    paint.setAntiAlias(true);
+    paint.setAntiAlias(antiAlias);
     paint.setColor(SK_ColorWHITE);
 
     GrMatrix matrix = context->getMatrix();
@@ -1436,6 +1437,58 @@
 
 }
 
+
+
+// return true on success; false on failure
+bool onDrawPath(const SkPath& path,
+                GrPathFill fill,
+                const GrVec* translate,
+                GrDrawTarget* target,
+                GrDrawState::StageMask stageMask,
+                bool antiAlias,
+                GrContext* context) {
+
+    GrAutoScratchTexture ast;
+    GrIRect pathBounds, clipBounds;
+    if (!get_path_and_clip_bounds(target, path, translate,
+                                  &pathBounds, &clipBounds)) {
+        return true;    // path is empty so there is nothing to do
+    }
+    if (sw_draw_path_to_mask_texture(path, pathBounds,
+                                     fill, context,
+                                     translate, &ast, antiAlias)) {
+        GrTexture* texture = ast.texture();
+        GrAssert(NULL != texture);
+        GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
+        enum {
+            kPathMaskStage = GrPaint::kTotalStages,
+        };
+        target->drawState()->setTexture(kPathMaskStage, texture);
+        target->drawState()->sampler(kPathMaskStage)->reset();
+        GrScalar w = GrIntToScalar(pathBounds.width());
+        GrScalar h = GrIntToScalar(pathBounds.height());
+        GrRect maskRect = GrRect::MakeWH(w / texture->width(),
+                                         h / texture->height());
+        const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
+        srcRects[kPathMaskStage] = &maskRect;
+        stageMask |= 1 << kPathMaskStage;
+        GrRect dstRect = GrRect::MakeLTRB(
+                              SK_Scalar1* pathBounds.fLeft,
+                              SK_Scalar1* pathBounds.fTop,
+                              SK_Scalar1* pathBounds.fRight,
+                              SK_Scalar1* pathBounds.fBottom);
+        target->drawRect(dstRect, NULL, stageMask, srcRects, NULL);
+        target->drawState()->setTexture(kPathMaskStage, NULL);
+        if (GrIsFillInverted(fill)) {
+            draw_around_inv_path(target, stageMask,
+                                 clipBounds, pathBounds);
+        }
+        return true;
+    }
+
+    return false;
+}
+
 void GrContext::drawPath(const GrPaint& paint, const GrPath& path,
                          GrPathFill fill, const GrPoint* translate) {
 
@@ -1473,41 +1526,8 @@
     if (prAA) {
         pr = this->getPathRenderer(path, fill, target, true);
         if (NULL == pr) {
-            GrAutoScratchTexture ast;
-            GrIRect pathBounds, clipBounds;
-            if (!get_path_and_clip_bounds(target, path, translate,
-                                          &pathBounds, &clipBounds)) {
-                return;
-            }
-            if (NULL == pr && sw_draw_path_to_mask_texture(path, pathBounds,
-                                                           fill, this,
-                                                           translate, &ast)) {
-                GrTexture* texture = ast.texture();
-                GrAssert(NULL != texture);
-                GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
-                enum {
-                    kPathMaskStage = GrPaint::kTotalStages,
-                };
-                target->drawState()->setTexture(kPathMaskStage, texture);
-                target->drawState()->sampler(kPathMaskStage)->reset();
-                GrScalar w = GrIntToScalar(pathBounds.width());
-                GrScalar h = GrIntToScalar(pathBounds.height());
-                GrRect maskRect = GrRect::MakeWH(w / texture->width(),
-                                                 h / texture->height());
-                const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
-                srcRects[kPathMaskStage] = &maskRect;
-                stageMask |= 1 << kPathMaskStage;
-                GrRect dstRect = GrRect::MakeLTRB(
-                    SK_Scalar1* pathBounds.fLeft,
-                    SK_Scalar1* pathBounds.fTop,
-                    SK_Scalar1* pathBounds.fRight,
-                    SK_Scalar1* pathBounds.fBottom);
-                target->drawRect(dstRect, NULL, stageMask, srcRects, NULL);
-                target->drawState()->setTexture(kPathMaskStage, NULL);
-                if (GrIsFillInverted(fill)) {
-                    draw_around_inv_path(target, stageMask,
-                                         clipBounds, pathBounds);
-                }
+            if (onDrawPath(path, fill, translate, 
+                           target, stageMask, prAA, this)) {
                 return;
             }
         }
diff --git a/src/gpu/GrSoftwarePathRenderer.cpp b/src/gpu/GrSoftwarePathRenderer.cpp
new file mode 100644
index 0000000..c591ac5
--- /dev/null
+++ b/src/gpu/GrSoftwarePathRenderer.cpp
@@ -0,0 +1,35 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "GrSoftwarePathRenderer.h"
+
+
+bool GrSoftwarePathRenderer::canDrawPath(const SkPath& path,
+                                         GrPathFill fill,
+                                         const GrDrawTarget* target,
+                                         bool antiAlias) const {
+    if (!antiAlias) {
+        // TODO: the SW renderer can also handle non-AA paths
+        return false;
+    }
+
+    // TODO: set to true when filled out
+    return false;
+}
+
+bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path,
+                                        GrPathFill fill,
+                                        const GrVec* translate,
+                                        GrDrawTarget* target,
+                                        GrDrawState::StageMask stageMask,
+                                        bool antiAlias) {
+
+    // TODO: move onDrawPath routine (& its helpers) here from GrContext.cpp
+    return false;
+}
+
diff --git a/src/gpu/GrSoftwarePathRenderer.h b/src/gpu/GrSoftwarePathRenderer.h
new file mode 100644
index 0000000..859a53d
--- /dev/null
+++ b/src/gpu/GrSoftwarePathRenderer.h
@@ -0,0 +1,38 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrSoftwarePathRenderer_DEFINED
+#define GrSoftwarePathRenderer_DEFINED
+
+#include "GrPathRenderer.h"
+
+class GrSoftwarePathRenderer : public GrPathRenderer {
+public:
+    GrSoftwarePathRenderer(GrContext* context) 
+        : fContext(context) {
+    }
+
+    virtual bool canDrawPath(const SkPath& path,
+                            GrPathFill fill,
+                            const GrDrawTarget* target,
+                            bool antiAlias) const SK_OVERRIDE;
+protected:
+    virtual bool onDrawPath(const SkPath& path,
+                            GrPathFill fill,
+                            const GrVec* translate,
+                            GrDrawTarget* target,
+                            GrDrawState::StageMask stageMask,
+                            bool antiAlias) SK_OVERRIDE;
+ 
+private:
+    GrContext*     fContext;
+
+    typedef GrPathRenderer INHERITED;
+};
+
+#endif