SkDocument api changes (abort, close return success, set DCT encoder...).

R=reed@google.com, reed

Author: edisonn@google.com

Review URL: https://codereview.chromium.org/26744002

git-svn-id: http://skia.googlecode.com/svn/trunk/src@11688 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/doc/SkDocument.cpp b/doc/SkDocument.cpp
index 055c749..cefe34e 100644
--- a/doc/SkDocument.cpp
+++ b/doc/SkDocument.cpp
@@ -60,12 +60,12 @@
     }
 }
 
-void SkDocument::close() {
+bool SkDocument::close() {
     for (;;) {
         switch (fState) {
-            case kBetweenPages_State:
+            case kBetweenPages_State: {
                 fState = kClosed_State;
-                this->onClose(fStream);
+                bool success = this->onClose(fStream);
 
                 if (fDoneProc) {
                     fDoneProc(fStream);
@@ -73,12 +73,17 @@
                 // we don't own the stream, but we mark it NULL since we can
                 // no longer write to it.
                 fStream = NULL;
-                return;
+                return success;
+            }
             case kInPage_State:
                 this->endPage();
                 break;
             case kClosed_State:
-                return;
+                return false;
         }
     }
 }
+
+void SkDocument::abort() {
+    fState = kClosed_State;
+}
diff --git a/doc/SkDocument_PDF.cpp b/doc/SkDocument_PDF.cpp
index 27cf2e8..b9b55f8 100644
--- a/doc/SkDocument_PDF.cpp
+++ b/doc/SkDocument_PDF.cpp
@@ -6,13 +6,15 @@
  */
 
 #include "SkDocument.h"
-#include "SkPDFDocument.h"
 #include "SkPDFDevice.h"
+#include "SkPDFDocument.h"
 
 class SkDocument_PDF : public SkDocument {
 public:
-    SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*))
-            : SkDocument(stream, doneProc) {
+    SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*),
+                   SkPicture::EncodeBitmap encoder)
+            : SkDocument(stream, doneProc)
+            , fEncoder(encoder) {
         fDoc = SkNEW(SkPDFDocument);
         fCanvas = NULL;
         fDevice = NULL;
@@ -38,6 +40,9 @@
         matrix.setTranslate(content.fLeft, content.fTop);
 
         fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix));
+        if (fEncoder) {
+            fDevice->setDCTEncoder(fEncoder);
+        }
         fCanvas = SkNEW_ARGS(SkCanvas, (fDevice));
         return fCanvas;
     }
@@ -56,36 +61,44 @@
         fDevice = NULL;
     }
 
-    virtual void onClose(SkWStream* stream) SK_OVERRIDE {
+    virtual bool onClose(SkWStream* stream) SK_OVERRIDE {
         SkASSERT(NULL == fCanvas);
         SkASSERT(NULL == fDevice);
 
-        fDoc->emitPDF(stream);
+        bool success = fDoc->emitPDF(stream);
+        SkDELETE(fDoc);
+        fDoc = NULL;
+        return success;
+    }
+
+    virtual void onAbort() SK_OVERRIDE {
         SkDELETE(fDoc);
         fDoc = NULL;
     }
 
 private:
     SkPDFDocument*  fDoc;
-    SkPDFDevice*    fDevice;
+    SkPDFDevice* fDevice;
     SkCanvas*       fCanvas;
+    SkPicture::EncodeBitmap fEncoder;
 };
 
 ///////////////////////////////////////////////////////////////////////////////
 
-SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*)) {
-    return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done)) : NULL;
+SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*),
+                                  SkPicture::EncodeBitmap enc) {
+    return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc)) : NULL;
 }
 
 static void delete_wstream(SkWStream* stream) {
     SkDELETE(stream);
 }
 
-SkDocument* SkDocument::CreatePDF(const char path[]) {
+SkDocument* SkDocument::CreatePDF(const char path[], SkPicture::EncodeBitmap enc) {
     SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
     if (!stream->isValid()) {
         SkDELETE(stream);
         return NULL;
     }
-    return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream));
+    return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc));
 }