pdfviewer: improve memory usage, improve parse time (by 30-50%) and don't allocate extra buffers (more to do, but low priority now), and put the page specific memory in an allocator.

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

git-svn-id: http://skia.googlecode.com/svn/trunk@10282 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp b/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp
index 57e9abb..eb342af 100644
--- a/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp
+++ b/experimental/PdfViewer/pdfparser/native/SkPdfObject.cpp
@@ -8,19 +8,27 @@
 
 SkPdfObject SkPdfObject::kNull = SkPdfObject::makeNull();
 
-bool SkPdfObject::applyFlateDecodeFilter(SkPdfAllocator* allocator) {
+bool SkPdfObject::applyFlateDecodeFilter() {
     if (!SkFlate::HaveFlate()) {
         // TODO(edisonn): warn, make callers handle it
         return false;
     }
 
-    SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 1, false);
+    const unsigned char* old = fStr.fBuffer;
+    bool deleteOld = isStreamOwned();
+
+    SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 2, false);
     SkDynamicMemoryWStream uncompressedData;
 
     if (SkFlate::Inflate(&skstream, &uncompressedData)) {
-        fStr.fBytes = (uncompressedData.bytesWritten() << 1) + kUnfilteredStreamBit;
-        fStr.fBuffer = (unsigned char*)allocator->alloc(uncompressedData.bytesWritten());
-        uncompressedData.copyTo(fStr.fBuffer);
+        fStr.fBytes = (uncompressedData.bytesWritten() << 2) + kOwnedStreamBit + kUnfilteredStreamBit;
+        fStr.fBuffer = (const unsigned char*)new unsigned char[uncompressedData.bytesWritten()];
+        uncompressedData.copyTo((void*)fStr.fBuffer);
+
+        if (deleteOld) {
+            delete[] old;
+        }
+
         return true;
     } else {
         // TODO(edisonn): warn, make callers handle it
@@ -28,24 +36,24 @@
     }
 }
 
-bool SkPdfObject::applyDCTDecodeFilter(SkPdfAllocator* allocator) {
+bool SkPdfObject::applyDCTDecodeFilter() {
     // this would fail, and it won't allow any more filters.
     // technically, it would be possible, but not a real world scenario
     // TODO(edisonn): or get the image here and store it for fast retrieval?
     return false;
 }
 
-bool SkPdfObject::applyFilter(const char* name, SkPdfAllocator* allocator) {
+bool SkPdfObject::applyFilter(const char* name) {
     if (strcmp(name, "FlateDecode") == 0) {
-        return applyFlateDecodeFilter(allocator);
+        return applyFlateDecodeFilter();
     } else if (strcmp(name, "DCTDecode") == 0) {
-        return applyDCTDecodeFilter(allocator);
+        return applyDCTDecodeFilter();
     }
     // TODO(edisonn): allert, not supported, but should be implemented asap
     return false;
 }
 
-bool SkPdfObject::filterStream(SkPdfAllocator* allocator) {
+bool SkPdfObject::filterStream() {
     if (!hasStream()) {
         return false;
     }
@@ -58,19 +66,16 @@
 
     if (!stream->has_Filter()) {
         fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit;
-        return true;
-    }
-
-    if (stream->isFilterAName(NULL)) {
+    } else if (stream->isFilterAName(NULL)) {
         std::string filterName = stream->getFilterAsName(NULL);
-        applyFilter(filterName.c_str(), allocator);
+        applyFilter(filterName.c_str());
     } else if (stream->isFilterAArray(NULL)) {
         const SkPdfArray* filters = stream->getFilterAsArray(NULL);
         int cnt = filters->size();
         for (int i = cnt - 1; i >= 0; i--) {
             const SkPdfObject* filterName = filters->objAtAIndex(i);
             if (filterName != NULL && filterName->isName()) {
-                if (!applyFilter(filterName->nameValue(), allocator)) {
+                if (!applyFilter(filterName->nameValue())) {
                     break;
                 }
             } else {
@@ -79,7 +84,5 @@
         }
     }
 
-    fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit;
-
     return true;
 }