Index: gm/gmmain.cpp |
diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp |
index e29a1d8d8faf0a7d00d2eba5b589d5cbfc835bc0..397f8740b150cbf0b56c01df4d03c0586c100813 100644 |
--- a/gm/gmmain.cpp |
+++ b/gm/gmmain.cpp |
@@ -23,6 +23,7 @@ |
#include "SkData.h" |
#include "SkDeferredCanvas.h" |
#include "SkDevice.h" |
+#include "SkDocument.h" |
#include "SkDrawFilter.h" |
#include "SkForceLinking.h" |
#include "SkGPipe.h" |
@@ -33,6 +34,7 @@ |
#include "SkPDFRasterizer.h" |
#include "SkPicture.h" |
#include "SkRefCnt.h" |
+#include "SkScalar.h" |
#include "SkStream.h" |
#include "SkTArray.h" |
#include "SkTDict.h" |
@@ -75,6 +77,7 @@ typedef int GLContextType; |
#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message") |
extern bool gSkSuppressFontCachePurgeSpew; |
+DECLARE_bool(useDocumentInsteadOfDevice); |
#ifdef SK_SUPPORT_PDF |
#include "SkPDFDevice.h" |
@@ -627,34 +630,51 @@ public: |
} |
} |
- static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) { |
+ static bool generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) { |
#ifdef SK_SUPPORT_PDF |
SkMatrix initialTransform = gm->getInitialTransform(); |
- SkISize pageSize = gm->getISize(); |
- SkPDFDevice* dev = NULL; |
- if (initialTransform.isIdentity()) { |
- dev = new SkPDFDevice(pageSize, pageSize, initialTransform); |
- } else { |
- SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()), |
- SkIntToScalar(pageSize.height())); |
- initialTransform.mapRect(&content); |
- content.intersect(0, 0, SkIntToScalar(pageSize.width()), |
- SkIntToScalar(pageSize.height())); |
- SkISize contentSize = |
- SkISize::Make(SkScalarRoundToInt(content.width()), |
- SkScalarRoundToInt(content.height())); |
- dev = new SkPDFDevice(pageSize, contentSize, initialTransform); |
- } |
- dev->setDCTEncoder(encode_to_dct_data); |
- SkAutoUnref aur(dev); |
+ if (FLAGS_useDocumentInsteadOfDevice) { |
+ SkISize pageISize = gm->getISize(); |
+ SkAutoTUnref<SkDocument> pdfDoc(SkDocument::CreatePDF(&pdf, NULL, encode_to_dct_data)); |
- SkCanvas c(dev); |
- invokeGM(gm, &c, true, false); |
+ if (!pdfDoc.get()) { |
+ return false; |
+ } |
- SkPDFDocument doc; |
- doc.appendPage(dev); |
- doc.emitPDF(&pdf); |
-#endif |
+ SkCanvas* canvas = NULL; |
+ canvas = pdfDoc->beginPage(SkIntToScalar(pageISize.width()), |
+ SkIntToScalar(pageISize.height())); |
+ canvas->concat(initialTransform); |
+ |
+ invokeGM(gm, canvas, true, false); |
+ |
+ return pdfDoc->close(); |
+ } else { |
+ SkISize pageSize = gm->getISize(); |
+ SkPDFDevice* dev = NULL; |
+ if (initialTransform.isIdentity()) { |
+ dev = new SkPDFDevice(pageSize, pageSize, initialTransform); |
+ } else { |
+ SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()), |
+ SkIntToScalar(pageSize.height())); |
+ initialTransform.mapRect(&content); |
+ content.intersect(0, 0, SkIntToScalar(pageSize.width()), |
+ SkIntToScalar(pageSize.height())); |
+ SkISize contentSize = |
+ SkISize::Make(SkScalarRoundToInt(content.width()), |
+ SkScalarRoundToInt(content.height())); |
+ dev = new SkPDFDevice(pageSize, contentSize, initialTransform); |
+ } |
+ dev->setDCTEncoder(encode_to_dct_data); |
+ SkAutoUnref aur(dev); |
+ SkCanvas c(dev); |
+ invokeGM(gm, &c, true, false); |
+ SkPDFDocument doc; |
+ doc.appendPage(dev); |
+ doc.emitPDF(&pdf); |
+ } |
+#endif // SK_SUPPORT_PDF |
+ return true; // Do not report failure if pdf is not supported. |
} |
static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) { |
@@ -1046,43 +1066,46 @@ public: |
errors.add(write_bitmap(path, bitmapAndDigest.fBitmap)); |
} |
} else if (gRec.fBackend == kPDF_Backend) { |
- generate_pdf(gm, document); |
- |
- SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream()); |
- if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) { |
- path = make_filename(writePath, gm->shortName(), gRec.fName, "", "pdf"); |
- errors.add(write_document(path, documentStream)); |
- } |
- |
- if (!(gm->getFlags() & GM::kSkipPDFRasterization_Flag)) { |
- for (int i = 0; i < pdfRasterizers.count(); i++) { |
- SkBitmap pdfBitmap; |
- SkASSERT(documentStream->rewind()); |
- bool success = (*pdfRasterizers[i]->fRasterizerFunction)( |
- documentStream.get(), &pdfBitmap); |
- if (!success) { |
- gm_fprintf(stderr, "FAILED to render PDF for %s using renderer %s\n", |
- gm->shortName(), |
- pdfRasterizers[i]->fName); |
- continue; |
- } |
- |
- SkString configName(gRec.fName); |
- configName.append("-"); |
- configName.append(pdfRasterizers[i]->fName); |
- |
- BitmapAndDigest bitmapAndDigest(pdfBitmap); |
- errors.add(compare_test_results_to_stored_expectations( |
- gm, gRec, configName.c_str(), &bitmapAndDigest)); |
+ if (!generate_pdf(gm, document)) { |
+ errors.add(kGeneratePdfFailed_ErrorType); |
+ } else { |
+ SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream()); |
+ if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) { |
+ path = make_filename(writePath, gm->shortName(), gRec.fName, "", "pdf"); |
+ errors.add(write_document(path, documentStream)); |
+ } |
- if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) { |
- path = make_bitmap_filename(writePath, gm->shortName(), configName.c_str(), |
- "", bitmapAndDigest.fDigest); |
- errors.add(write_bitmap(path, bitmapAndDigest.fBitmap)); |
+ if (!(gm->getFlags() & GM::kSkipPDFRasterization_Flag)) { |
+ for (int i = 0; i < pdfRasterizers.count(); i++) { |
+ SkBitmap pdfBitmap; |
+ SkASSERT(documentStream->rewind()); |
+ bool success = (*pdfRasterizers[i]->fRasterizerFunction)( |
+ documentStream.get(), &pdfBitmap); |
+ if (!success) { |
+ gm_fprintf(stderr, "FAILED to render PDF for %s using renderer %s\n", |
+ gm->shortName(), |
+ pdfRasterizers[i]->fName); |
+ continue; |
+ } |
+ |
+ SkString configName(gRec.fName); |
+ configName.append("-"); |
+ configName.append(pdfRasterizers[i]->fName); |
+ |
+ BitmapAndDigest bitmapAndDigest(pdfBitmap); |
+ errors.add(compare_test_results_to_stored_expectations( |
+ gm, gRec, configName.c_str(), &bitmapAndDigest)); |
+ |
+ if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) { |
+ path = make_bitmap_filename(writePath, gm->shortName(), |
+ configName.c_str(), |
+ "", bitmapAndDigest.fDigest); |
+ errors.add(write_bitmap(path, bitmapAndDigest.fBitmap)); |
+ } |
} |
+ } else { |
+ errors.add(kIntentionallySkipped_ErrorType); |
} |
- } else { |
- errors.add(kIntentionallySkipped_ErrorType); |
} |
} else if (gRec.fBackend == kXPS_Backend) { |
generate_xps(gm, document); |
@@ -1444,6 +1467,7 @@ DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, " |
// Probably define spacial names like centerx, centery, top, bottom, left, right |
// then we can write something reabable like --rotate centerx centery 90 |
DEFINE_bool(forcePerspectiveMatrix, false, "Force a perspective matrix."); |
+DEFINE_bool(useDocumentInsteadOfDevice, false, "Use SkDocument::CreateFoo instead of SkFooDevice."); |
static SkData* encode_to_dct_data(size_t* pixelRefOffset, const SkBitmap& bitmap) { |
// Filter output of warnings that JPEG is not available for the image. |