fuzzer fixes

Fix path bugs exposed by the path fuzzer.

Changes to existing gm and samplecode files defer their calls to construct
SkPath objects until the first draw instead of at test initialization.

Add an experimental call to SkPath to validate the internal SkPathRef.

Fix SkPath::addPoly to set the last moveto after adding a close verb.

Fix stroke to handle failures when computing the unit normal.

Add a unit test for the unit normal failure.

R=reed@google.com

Review URL: https://codereview.chromium.org/953383002
diff --git a/gm/bitmapshader.cpp b/gm/bitmapshader.cpp
index 8085f5c..ac8bf66 100644
--- a/gm/bitmapshader.cpp
+++ b/gm/bitmapshader.cpp
@@ -41,15 +41,14 @@
 }
 
 class BitmapShaderGM : public GM {
-public:
 
-    BitmapShaderGM() {
+protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
         this->setBGColor(SK_ColorGRAY);
         draw_bm(&fBitmap);
         draw_mask(&fMask);
     }
 
-protected:
     virtual SkString onShortName() {
         return SkString("bitmapshaders");
     }
diff --git a/gm/circularclips.cpp b/gm/circularclips.cpp
index ec766fb..614ebb6 100644
--- a/gm/circularclips.cpp
+++ b/gm/circularclips.cpp
@@ -13,8 +13,8 @@
     SkScalar fX1, fX2, fY, fR;
     SkPath   fCircle1, fCircle2;
 
-public:
-    CircularClipsGM() {
+protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
         fX1 = 80;
         fX2 = 120;
         fY = 50;
@@ -24,7 +24,6 @@
         fCircle2.addCircle(fX2, fY, fR, SkPath::kCW_Direction);
     }
 
-protected:
 
     bool runAsBench() const SK_OVERRIDE { return true; }
 
diff --git a/gm/complexclip2.cpp b/gm/complexclip2.cpp
index a55d073..361b1c0 100644
--- a/gm/complexclip2.cpp
+++ b/gm/complexclip2.cpp
@@ -24,6 +24,21 @@
     ComplexClip2GM(Clip clip, bool antiAlias)
     : fClip(clip)
     , fAntiAlias(antiAlias) {
+        SkScalar xA = 0.65f;
+        SkScalar xF = 50.65f;
+
+        SkScalar yA = 0.65f;
+        SkScalar yF = 50.65f;
+
+        fWidth = xF - xA;
+        fHeight = yF - yA;
+
+        fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
+        fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
+    }
+
+protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
         this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
 
         // offset the rects a bit so we get antialiasing even in the rect case
@@ -41,9 +56,6 @@
         SkScalar yE = 40.65f;
         SkScalar yF = 50.65f;
 
-        fWidth = xF - xA;
-        fHeight = yF - yA;
-
         fRects[0].set(xB, yB, xE, yE);
         fRRects[0].setRectXY(fRects[0], 7, 7);
         fPaths[0].addRoundRect(fRects[0], 5, 5);
@@ -69,9 +81,6 @@
         fPaths[4].addRoundRect(fRects[4], 5, 5);
         fRectColors[4] = SK_ColorCYAN;
 
-        fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
-        fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
-
         SkRegion::Op ops[] = {
             SkRegion::kDifference_Op,
             SkRegion::kIntersect_Op,
@@ -91,8 +100,6 @@
         }
     }
 
-protected:
-
     static const int kRows = 5;
     static const int kCols = 5;
     static const int kPadX = 20;
diff --git a/gm/picture.cpp b/gm/picture.cpp
index b07a596..5432fc9 100644
--- a/gm/picture.cpp
+++ b/gm/picture.cpp
@@ -39,9 +39,15 @@
 //
 class PictureGM : public skiagm::GM {
 public:
-    PictureGM() : fPicture(make_picture()) {}
+    PictureGM()
+        : fPicture(NULL)
+    {}
 
 protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
+         fPicture.reset(make_picture());
+    }
+
     SkString onShortName() SK_OVERRIDE {
         return SkString("pictures");
     }
diff --git a/gm/pictureshader.cpp b/gm/pictureshader.cpp
index 630ecd8..3f184ec 100644
--- a/gm/pictureshader.cpp
+++ b/gm/pictureshader.cpp
@@ -27,21 +27,23 @@
     PictureShaderGM(SkScalar tileSize, SkScalar sceneSize)
         : fTileSize(tileSize)
         , fSceneSize(sceneSize) {
+    }
 
-        // Build the picture.
+ protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
+       // Build the picture.
         SkPictureRecorder recorder;
-        SkCanvas* pictureCanvas = recorder.beginRecording(tileSize, tileSize, NULL, 0);
+        SkCanvas* pictureCanvas = recorder.beginRecording(fTileSize, fTileSize, NULL, 0);
         this->drawTile(pictureCanvas);
         fPicture.reset(recorder.endRecording());
 
         // Build a reference bitmap.
-        fBitmap.allocN32Pixels(SkScalarCeilToInt(tileSize), SkScalarCeilToInt(tileSize));
+        fBitmap.allocN32Pixels(SkScalarCeilToInt(fTileSize), SkScalarCeilToInt(fTileSize));
         fBitmap.eraseColor(SK_ColorTRANSPARENT);
         SkCanvas bitmapCanvas(fBitmap);
         this->drawTile(&bitmapCanvas);
     }
 
-protected:
 
     SkString onShortName() SK_OVERRIDE {
         return SkString("pictureshader");
diff --git a/gm/shadows.cpp b/gm/shadows.cpp
index 3ecbdd8..66ad32e 100644
--- a/gm/shadows.cpp
+++ b/gm/shadows.cpp
@@ -28,14 +28,14 @@
     SkPath fCirclePath;
     SkRect fRect;
 
-    ShadowsGM() {
+protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
         this->setBGColor(0xFFDDDDDD);
         fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10) );
         fRect.set(SkIntToScalar(10), SkIntToScalar(10),
                   SkIntToScalar(30), SkIntToScalar(30));
     }
 
-protected:
     virtual SkString onShortName() {
         return SkString("shadows");
     }
diff --git a/gm/simpleaaclip.cpp b/gm/simpleaaclip.cpp
index 950a1c0..8a18f48 100644
--- a/gm/simpleaaclip.cpp
+++ b/gm/simpleaaclip.cpp
@@ -49,7 +49,10 @@
 
     SimpleClipGM(SkGeomTypes geomType)
     : fGeomType(geomType) {
+    }
 
+protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
         // offset the rects a bit so we get anti-aliasing in the rect case
         fBase.set(100.65f,
                   100.65f,
@@ -64,7 +67,6 @@
         INHERITED::setBGColor(0xFFDDDDDD);
     }
 
-protected:
     void buildRgn(SkAAClip* clip, SkRegion::Op op) {
         clip->setPath(fBasePath, NULL, true);
 
diff --git a/gm/strokes.cpp b/gm/strokes.cpp
index af01b91..cadd0d6 100644
--- a/gm/strokes.cpp
+++ b/gm/strokes.cpp
@@ -80,8 +80,8 @@
 
 class Strokes2GM : public skiagm::GM {
     SkPath fPath;
-public:
-    Strokes2GM() {
+protected:
+    void onOnceBeforeDraw() SK_OVERRIDE {
         SkRandom rand;
         fPath.moveTo(0, 0);
         for (int i = 0; i < 13; i++) {
@@ -91,7 +91,6 @@
         }
     }
 
-protected:
 
     SkString onShortName() SK_OVERRIDE {
         return SkString("strokes_poly");