Add an SKP to PDF rendered. test_pdfs.py will be hooked up in buildbot testing later.
Review URL: https://codereview.appspot.com/6610056

git-svn-id: http://skia.googlecode.com/svn/trunk@5880 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/tools.gyp b/gyp/tools.gyp
index 69c6cae..eb2d31c 100644
--- a/gyp/tools.gyp
+++ b/gyp/tools.gyp
@@ -140,6 +140,39 @@
     ],
     },
     {
+      'target_name': 'render_pdfs',
+      'type': 'executable',
+      'sources': [
+        '../tools/render_pdfs_main.cpp',
+      ],
+      'include_dirs': [
+        '../src/pipe/utils/',
+      ],
+      'dependencies': [
+        'core.gyp:core',
+        'effects.gyp:effects',
+        'images.gyp:images',
+        'ports.gyp:ports',
+        'tools.gyp:pdf_renderer',
+        'tools.gyp:picture_utils',
+      ],
+    },
+    {
+      'target_name': 'pdf_renderer',
+      'type': 'static_library',
+      'sources': [
+        '../tools/PdfRenderer.cpp',
+        '../tools/PdfRenderer.h',
+      ],
+      'include_dirs': [
+        '../src/utils/',
+      ],
+      'dependencies': [
+        'core.gyp:core',
+        'pdf.gyp:pdf',
+      ],
+    },
+    {
       'target_name': 'picture_utils',
       'type': 'static_library',
       'sources': [
diff --git a/tools/PdfRenderer.cpp b/tools/PdfRenderer.cpp
new file mode 100644
index 0000000..8819266
--- /dev/null
+++ b/tools/PdfRenderer.cpp
@@ -0,0 +1,73 @@
+/*
+ * 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 "PdfRenderer.h"
+#include "SkCanvas.h"
+#include "SkDevice.h"
+#include "SkPDFDevice.h"
+#include "SkPDFDocument.h"
+
+namespace sk_tools {
+
+void PdfRenderer::init(SkPicture* pict) {
+    SkASSERT(NULL == fPicture);
+    SkASSERT(NULL == fCanvas.get());
+    if (fPicture != NULL || NULL != fCanvas.get()) {
+        return;
+    }
+
+    SkASSERT(pict != NULL);
+    if (NULL == pict) {
+        return;
+    }
+
+    fPicture = pict;
+    fCanvas.reset(this->setupCanvas());
+}
+
+SkCanvas* PdfRenderer::setupCanvas() {
+    return this->setupCanvas(fPicture->width(), fPicture->height());
+}
+
+SkCanvas* PdfRenderer::setupCanvas(int width, int height) {
+    SkISize pageSize = SkISize::Make(width, height);
+    fPDFDevice = SkNEW_ARGS(SkPDFDevice, (pageSize, pageSize, SkMatrix::I()));
+    return SkNEW_ARGS(SkCanvas, (fPDFDevice));
+}
+
+void PdfRenderer::end() {
+    fPicture = NULL;
+    fCanvas.reset(NULL);
+    if (fPDFDevice) {
+        SkDELETE(fPDFDevice);
+        fPDFDevice = NULL;
+    }
+}
+
+bool PdfRenderer::write(const SkString& path) const {
+    SkPDFDocument doc;
+    doc.appendPage(fPDFDevice);
+    SkFILEWStream stream(path.c_str());
+    if (stream.isValid()) {
+        doc.emitPDF(&stream);
+        return true;
+    }
+    return false;
+}
+
+void SimplePdfRenderer::render() {
+    SkASSERT(fCanvas.get() != NULL);
+    SkASSERT(fPicture != NULL);
+    if (NULL == fCanvas.get() || NULL == fPicture) {
+        return;
+    }
+
+    fCanvas->drawPicture(*fPicture);
+    fCanvas->flush();
+}
+
+}
diff --git a/tools/PdfRenderer.h b/tools/PdfRenderer.h
new file mode 100644
index 0000000..bce6197
--- /dev/null
+++ b/tools/PdfRenderer.h
@@ -0,0 +1,67 @@
+/*
+ * 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 PdfRenderer_DEFINED
+#define PdfRenderer_DEFINED
+
+//
+// PdfRender takes a SkPicture and writes it to a PDF file.
+// An SkPicture can be built manually, or read from an SKP file.
+//
+
+#include "SkMath.h"
+#include "SkPicture.h"
+#include "SkTypes.h"
+#include "SkTDArray.h"
+#include "SkRefCnt.h"
+#include "SkString.h"
+
+class SkBitmap;
+class SkCanvas;
+class SkGLContext;
+class SkPDFDevice;
+
+namespace sk_tools {
+
+class PdfRenderer : public SkRefCnt {
+public:
+    virtual void init(SkPicture* pict);
+    virtual void setup() {}
+    virtual void render() = 0;
+    virtual void end();
+
+    PdfRenderer()
+        : fPicture(NULL)
+        , fPDFDevice(NULL)
+        {}
+
+    bool write(const SkString& path) const;
+
+protected:
+    SkCanvas* setupCanvas();
+    SkCanvas* setupCanvas(int width, int height);
+
+    SkAutoTUnref<SkCanvas> fCanvas;
+    SkPicture* fPicture;
+    SkPDFDevice* fPDFDevice;
+
+
+private:
+    typedef SkRefCnt INHERITED;
+};
+
+class SimplePdfRenderer : public PdfRenderer {
+public:
+    virtual void render() SK_OVERRIDE;
+
+private:
+    typedef PdfRenderer INHERITED;
+};
+
+}
+
+#endif  // PdfRenderer_DEFINED
diff --git a/tools/test_pdfs.py b/tools/test_pdfs.py
new file mode 100644
index 0000000..ac3eab9
--- /dev/null
+++ b/tools/test_pdfs.py
@@ -0,0 +1,60 @@
+'''
+Compares the rendererings of serialized SkPictures to expected images.
+
+Launch with --help to see more information.
+
+
+Copyright 2012 Google Inc.
+
+Use of this source code is governed by a BSD-style license that can be
+found in the LICENSE file.
+'''
+# common Python modules
+import os
+import optparse
+import sys
+import shutil
+import tempfile
+import test_rendering
+
+USAGE_STRING = 'Usage: %s input... expectedDir'
+HELP_STRING = '''
+
+Takes input SkPicture files and renders them as PDF files, and then compares
+those resulting PDF files against PDF files found in expectedDir.
+
+Each instance of "input" can be either a file (name must end in .skp), or a
+directory (in which case this script will process all .skp files within the
+directory).
+'''
+
+
+def Main(args):
+    """Allow other scripts to call this script with fake command-line args.
+
+    @param The commandline argument list
+    """
+    parser = optparse.OptionParser(USAGE_STRING % '%prog' + HELP_STRING)
+    parser.add_option('--render_dir', dest='render_dir',
+                      help = ('specify the location to output the rendered '
+                      'files. Default is a temp directory.'))
+    parser.add_option('--diff_dir', dest='diff_dir',
+                      help = ('specify the location to output the diff files. '
+                      'Default is a temp directory.'))
+
+    options, arguments = parser.parse_args(args)
+
+    if (len(arguments) < 3):
+        print("Expected at least one input and one ouput folder.")
+        parser.print_help()
+        sys.exit(-1)
+
+    inputs = arguments[1:-1]
+    expected_dir = arguments[-1]
+
+    test_rendering.TestRenderSkps(inputs, expected_dir, options.render_dir,
+                                  options.diff_dir, 'render_pdfs', '')
+
+if __name__ == '__main__':
+    Main(sys.argv)
+