[PDF] Improve the SkClipStack skipping prefix code.

Because of intersecting done in SkClipStack, we may have to do more work in the last entry of the prefix.

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

git-svn-id: http://skia.googlecode.com/svn/trunk@1418 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkClipStack.h b/include/core/SkClipStack.h
index ae0b974..6e8da76 100644
--- a/include/core/SkClipStack.h
+++ b/include/core/SkClipStack.h
@@ -43,6 +43,7 @@
 
         struct Clip {
             friend bool operator==(const Clip& a, const Clip& b);
+            friend bool operator!=(const Clip& a, const Clip& b);
             const SkRect*   fRect;  // if non-null, this is a rect clip
             const SkPath*   fPath;  // if non-null, this is a path clip
             SkRegion::Op    fOp;
diff --git a/src/core/SkClipStack.cpp b/src/core/SkClipStack.cpp
index 5ffa5b1..4ad4d41 100644
--- a/src/core/SkClipStack.cpp
+++ b/src/core/SkClipStack.cpp
@@ -188,6 +188,11 @@
            ((a.fPath == NULL && b.fPath == NULL) || *a.fPath == *b.fPath);
 }
 
+bool operator!=(const SkClipStack::B2FIter::Clip& a,
+               const SkClipStack::B2FIter::Clip& b) {
+    return !(a == b);
+}
+
 SkClipStack::B2FIter::B2FIter(const SkClipStack& stack) {
     this->reset(stack);
 }
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 1fe1734..4c0021a 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -233,11 +233,29 @@
     const SkClipStack::B2FIter::Clip* prefixEntry;
     const SkClipStack::B2FIter::Clip* iterEntry;
 
+    int count = 0;
     for (prefixEntry = prefixIter.next(); prefixEntry;
-            prefixEntry = prefixIter.next()) {
+            prefixEntry = prefixIter.next(), count++) {
         iterEntry = iter->next();
         SkASSERT(iterEntry);
-        SkASSERT(*prefixEntry == *iterEntry);
+        // Because of SkClipStack does internal intersection, the last clip
+        // entry may differ.
+        if(*prefixEntry != *iterEntry) {
+            SkASSERT(prefixEntry->fOp == SkRegion::kIntersect_Op);
+            SkASSERT(iterEntry->fOp == SkRegion::kIntersect_Op);
+            SkASSERT((iterEntry->fRect == NULL) ==
+                    (prefixEntry->fRect == NULL));
+            SkASSERT((iterEntry->fPath == NULL) ==
+                    (prefixEntry->fPath == NULL));
+            // We need to back up the iterator by one but don't have that
+            // function, so reset and go forward by one less.
+            iter->reset(stack);
+            for (int i = 0; i < count; i++) {
+                iter->next();
+            }
+            prefixEntry = prefixIter.next();
+            break;
+        }
     }
 
     SkASSERT(prefixEntry == NULL);