Add support to use Skia printing from Mac.

Reference the CG metafile from the Skia
version, so CG can be used to pass the PDF
data to the OS X pipeline.

If Skia is enabled as the Mac rendering engine,
generate Skia PDF files instead of CG ones.

This change adds a code path that will be enabled
in the future, but does not modify any existing
code, so there is no functional change.

BUG=79463
TEST=none

Review URL: http://codereview.chromium.org/7120006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90023 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/printing/metafile_impl.h b/printing/metafile_impl.h
index d25481d..a40e9b15 100644
--- a/printing/metafile_impl.h
+++ b/printing/metafile_impl.h
@@ -7,11 +7,13 @@
 
 #if defined(OS_WIN)
 #include "printing/emf_win.h"
-#include "printing/pdf_metafile_skia.h"
 #elif defined(OS_MACOSX)
 #include "printing/pdf_metafile_cg_mac.h"
 #elif defined(OS_POSIX)
 #include "printing/pdf_metafile_cairo_linux.h"
+#endif
+
+#if !defined(OS_MACOSX) || defined(USE_SKIA)
 #include "printing/pdf_metafile_skia.h"
 #endif
 
@@ -21,8 +23,13 @@
 typedef Emf NativeMetafile;
 typedef PdfMetafileSkia PreviewMetafile;
 #elif defined(OS_MACOSX)
+#if defined(USE_SKIA)
+typedef PdfMetafileSkia NativeMetafile;
+typedef PdfMetafileSkia PreviewMetafile;
+#else
 typedef PdfMetafileCg NativeMetafile;
 typedef PdfMetafileCg PreviewMetafile;
+#endif
 #elif defined(OS_POSIX)
 typedef PdfMetafileCairo NativeMetafile;
 typedef PdfMetafileSkia PreviewMetafile;
diff --git a/printing/pdf_metafile_skia.cc b/printing/pdf_metafile_skia.cc
index bcb5af93..d335545 100644
--- a/printing/pdf_metafile_skia.cc
+++ b/printing/pdf_metafile_skia.cc
@@ -22,12 +22,19 @@
 #include "ui/gfx/rect.h"
 #include "ui/gfx/size.h"
 
+#if defined(OS_MACOSX)
+#include "printing/pdf_metafile_cg_mac.h"
+#endif
+
 namespace printing {
 
 struct PdfMetafileSkiaData {
   SkRefPtr<SkPDFDevice> current_page_;
   SkPDFDocument pdf_doc_;
   SkDynamicMemoryWStream pdf_stream_;
+#if defined(OS_MACOSX)
+  PdfMetafileCg pdf_cg_;
+#endif
 };
 
 PdfMetafileSkia::~PdfMetafileSkia() {}
@@ -166,7 +173,30 @@
   NOTREACHED();
   return NULL;
 }
-#endif  // if defined(OS_WIN)
+#elif defined(OS_MACOSX)
+/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
+   rasterized output.  Even if that flow uses PdfMetafileCg::RenderPage,
+   the drawing of the PDF into the canvas may result in a rasterized output.
+   PDFMetafileSkia::RenderPage should be not implemented as shown and instead
+   should do something like the following CL in PluginInstance::PrintPDFOutput:
+http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
+*/
+bool PdfMetafileSkia::RenderPage(unsigned int page_number,
+                                 CGContextRef context,
+                                 const CGRect rect,
+                                 bool shrink_to_fit,
+                                 bool stretch_to_fit,
+                                 bool center_horizontally,
+                                 bool center_vertically) const {
+  DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
+  if (data_->pdf_cg_.GetDataSize() == 0)
+    data_->pdf_cg_.InitFromData(data_->pdf_stream_.getStream(),
+                                data_->pdf_stream_.getOffset());
+  return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit,
+                                   stretch_to_fit, center_horizontally,
+                                   center_vertically);
+}
+#endif
 
 #if defined(OS_CHROMEOS)
 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
diff --git a/printing/pdf_metafile_skia.h b/printing/pdf_metafile_skia.h
index 3d1c7bd..2b8c7c1c 100644
--- a/printing/pdf_metafile_skia.h
+++ b/printing/pdf_metafile_skia.h
@@ -54,7 +54,15 @@
   virtual bool Playback(gfx::NativeDrawingContext hdc, const RECT* rect) const;
   virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const;
   virtual HENHMETAFILE emf() const;
-#endif  // if defined(OS_WIN)
+#elif defined(OS_MACOSX)
+  virtual bool RenderPage(unsigned int page_number,
+                          CGContextRef context,
+                          const CGRect rect,
+                          bool shrink_to_fit,
+                          bool stretch_to_fit,
+                          bool center_horizontally,
+                          bool center_vertically) const;
+#endif
 
 #if defined(OS_CHROMEOS)
   virtual bool SaveToFD(const base::FileDescriptor& fd) const;