Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2004)

Unified Diff: gm/gmmain.cpp

Issue 21669004: Support multiple PDF rendering backends in the GM (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix rasterizer parser in invalid case, more style changes Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/gmmain.cpp
diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp
index 69f1f16ade0eee9c47759b25cfeae34a1fbeb07f..788dc665a61166a01b65ef62097a3b5b7f426f09 100644
--- a/gm/gmmain.cpp
+++ b/gm/gmmain.cpp
@@ -30,6 +30,7 @@
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkOSFile.h"
+#include "SkPDFRasterizer.h"
#include "SkPicture.h"
#include "SkRefCnt.h"
#include "SkStream.h"
@@ -83,9 +84,6 @@ extern bool gSkSuppressFontCachePurgeSpew;
#ifdef SK_BUILD_FOR_MAC
#include "SkCGUtils.h"
- #define CAN_IMAGE_PDF 1
-#else
- #define CAN_IMAGE_PDF 0
#endif
using namespace skiagm;
@@ -160,6 +158,12 @@ struct ConfigData {
bool fRunByDefault;
};
+struct PDFRasterizerData {
+ bool (*fRasterizerFunction)(SkStream*, SkBitmap*);
+ const char* fName;
+ bool fRunByDefault;
+};
+
class BWTextDrawFilter : public SkDrawFilter {
public:
virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
@@ -282,14 +286,19 @@ public:
}
}
- static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
+ static ErrorCombination write_bitmap(const SkString& path, const SkBitmap& bitmap) {
// TODO(epoger): Now that we have removed force_all_opaque()
// from this method, we should be able to get rid of the
// transformation to 8888 format also.
SkBitmap copy;
bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
- return SkImageEncoder::EncodeFile(path.c_str(), copy,
- SkImageEncoder::kPNG_Type, 100);
+ if (!SkImageEncoder::EncodeFile(path.c_str(), copy,
+ SkImageEncoder::kPNG_Type,
+ 100)) {
+ gm_fprintf(stderr, "FAILED to write bitmap: %s\n", path.c_str());
+ return ErrorCombination(kWritingReferenceImage_ErrorType);
+ }
+ return kEmpty_ErrorCombination;
}
/**
@@ -410,9 +419,13 @@ public:
gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
}
- static bool write_document(const SkString& path, SkStreamAsset* asset) {
+ static ErrorCombination write_document(const SkString& path, SkStreamAsset* asset) {
SkFILEWStream stream(path.c_str());
- return stream.writeStream(asset, asset->getLength());
+ if (!stream.writeStream(asset, asset->getLength())) {
+ gm_fprintf(stderr, "FAILED to write document: %s\n", path.c_str());
+ return ErrorCombination(kWritingReferenceImage_ErrorType);
+ }
+ return kEmpty_ErrorCombination;
}
/**
@@ -659,51 +672,6 @@ public:
#endif
}
- ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
- const char renderModeDescriptor [],
- const char *shortName,
- const BitmapAndDigest* bitmapAndDigest,
- SkStreamAsset* document) {
- SkString path;
- bool success = false;
- if (gRec.fBackend == kRaster_Backend ||
- gRec.fBackend == kGPU_Backend ||
- (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
-
- path = make_bitmap_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
- bitmapAndDigest->fDigest);
- success = write_bitmap(path, bitmapAndDigest->fBitmap);
- }
- if (kPDF_Backend == gRec.fBackend) {
- path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
- "pdf");
- success = write_document(path, document);
- }
- if (kXPS_Backend == gRec.fBackend) {
- path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
- "xps");
- success = write_document(path, document);
- }
- if (success) {
- return kEmpty_ErrorCombination;
- } else {
- gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
- ErrorCombination errors(kWritingReferenceImage_ErrorType);
- // TODO(epoger): Don't call RecordTestResults() here...
- // Instead, we should make sure to call RecordTestResults
- // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
- // will be incremented twice for this test: once in
- // compare_test_results_to_stored_expectations() before
- // that method calls this one, and again here.)
- //
- // When we make that change, we should probably add a
- // WritingReferenceImage test to the gm self-tests.)
- RecordTestResults(errors, make_shortname_plus_config(shortName, gRec.fName),
- renderModeDescriptor);
- return errors;
- }
- }
-
/**
* Log more detail about the mistmatch between expectedBitmap and
* actualBitmap.
@@ -908,13 +876,10 @@ public:
* @param document pdf or xps representation, if appropriate
*/
ErrorCombination compare_test_results_to_stored_expectations(
- GM* gm, const ConfigData& gRec, const char writePath[],
- const BitmapAndDigest* actualBitmapAndDigest, SkStreamAsset* document) {
+ GM* gm, const ConfigData& gRec,
+ const BitmapAndDigest* actualBitmapAndDigest) {
SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(), gRec.fName);
- SkString nameWithExtension(shortNamePlusConfig);
- nameWithExtension.append(".");
- nameWithExtension.append(kPNG_FileExtension);
ErrorCombination errors;
@@ -922,18 +887,20 @@ public:
// Note that we intentionally skipped validating the results for
// this test, because we don't know how to generate an SkBitmap
// version of the output.
- RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
- shortNamePlusConfig, "");
+ errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
} else if (!(gRec.fFlags & kWrite_ConfigFlag)) {
// We don't record the results for this test or compare them
// against any expectations, because the output image isn't
// meaningful.
// See https://code.google.com/p/skia/issues/detail?id=1410 ('some
// GM result images not available for download from Google Storage')
- RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
- shortNamePlusConfig, "");
+ errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
} else {
ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
+ SkString nameWithExtension(shortNamePlusConfig);
+ nameWithExtension.append(".");
+ nameWithExtension.append(kPNG_FileExtension);
+
if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
/*
* Get the expected results for this test, as one or more allowed
@@ -958,20 +925,9 @@ public:
actualBitmapAndDigest->fDigest,
ErrorCombination(kMissingExpectations_ErrorType),
false);
- RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType),
- shortNamePlusConfig, "");
+ errors.add(ErrorCombination(kMissingExpectations_ErrorType));
}
}
-
- // TODO: Consider moving this into compare_to_expectations(),
- // similar to fMismatchPath... for now, we don't do that, because
- // we don't want to write out the actual bitmaps for all
- // renderModes of all tests! That would be a lot of files.
- if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
- errors.add(write_reference_image(gRec, writePath, "", gm->shortName(),
- actualBitmapAndDigest, document));
- }
-
return errors;
}
@@ -1032,46 +988,81 @@ public:
// Test: draw into a bitmap or pdf.
// Depending on flags, possibly compare to an expected image.
- ErrorCombination test_drawing(GM* gm,
- const ConfigData& gRec,
+ ErrorCombination test_drawing(GM* gm, const ConfigData& gRec,
+ const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
const char writePath [],
GrSurface* gpuTarget,
SkBitmap* bitmap) {
+ ErrorCombination errors;
SkDynamicMemoryWStream document;
+ SkString path;
if (gRec.fBackend == kRaster_Backend ||
gRec.fBackend == kGPU_Backend) {
// Early exit if we can't generate the image.
- ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
+ errors.add(generate_image(gm, gRec, gpuTarget, bitmap, false));
if (!errors.isEmpty()) {
// TODO: Add a test to exercise what the stdout and
// JSON look like if we get an "early error" while
// trying to generate the image.
return errors;
}
+ BitmapAndDigest bitmapAndDigest(*bitmap);
+ errors.add(compare_test_results_to_stored_expectations(
+ gm, gRec, &bitmapAndDigest));
+
+ if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
+ path = make_bitmap_filename(writePath, gm->shortName(), gRec.fName,
+ "", bitmapAndDigest.fDigest);
+ errors.add(write_bitmap(path, bitmapAndDigest.fBitmap));
+ }
} else if (gRec.fBackend == kPDF_Backend) {
generate_pdf(gm, document);
-#if CAN_IMAGE_PDF
- SkAutoDataUnref data(document.copyToData());
- SkMemoryStream stream(data->data(), data->size());
- SkPDFDocumentToBitmap(&stream, bitmap);
-#else
- bitmap = NULL; // we don't generate a bitmap rendering of the PDF file
-#endif
+
+ 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));
+ }
+
+ 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;
+ }
+
+ BitmapAndDigest bitmapAndDigest(pdfBitmap);
+ errors.add(compare_test_results_to_stored_expectations(
+ gm, gRec, &bitmapAndDigest));
+
+ SkString configName(gRec.fName);
+ configName.append("_");
+ configName.append(pdfRasterizers[i]->fName);
+
+ 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 if (gRec.fBackend == kXPS_Backend) {
generate_xps(gm, document);
- bitmap = NULL; // we don't generate a bitmap rendering of the XPS file
- }
+ SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
- SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
- if (NULL == bitmap) {
- return compare_test_results_to_stored_expectations(
- gm, gRec, writePath, NULL, documentStream);
+ if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
+ path = make_filename(writePath, gm->shortName(), gRec.fName, "", "xps");
+ errors.add(write_document(path, documentStream));
+ }
} else {
- BitmapAndDigest bitmapAndDigest(*bitmap);
- return compare_test_results_to_stored_expectations(
- gm, gRec, writePath, &bitmapAndDigest, documentStream);
+ SkASSERT(false);
}
+ return errors;
}
ErrorCombination test_deferred_drawing(GM* gm,
@@ -1223,11 +1214,6 @@ static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_G
static const GLContextType kDontCare_GLContextType = 0;
#endif
-// If the platform does not support writing PNGs of PDFs then there will be no
-// reference images to read. However, we can always write the .pdf files
-static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
- kWrite_ConfigFlag;
-
static const ConfigData gRec[] = {
{ SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
#if 0 // stop testing this (for now at least) since we want to remove support for it (soon please!!!)
@@ -1259,10 +1245,19 @@ static const ConfigData gRec[] = {
{ SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
#endif // SK_SUPPORT_XPS
#ifdef SK_SUPPORT_PDF
- { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
+ { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "pdf", true },
#endif // SK_SUPPORT_PDF
};
+static const PDFRasterizerData kPDFRasterizers[] = {
+#ifdef SK_BUILD_FOR_MAC
+ { &SkPDFDocumentToBitmap, "mac", true },
+#endif
+#ifdef SK_BUILD_POPPLER
+ { &SkPopplerRasterizePDF, "poppler", true },
vandebo (ex-Chrome) 2013/08/13 17:48:00 nit: remove the extra whitespace from these declar
ducky 2013/08/14 00:37:11 Done.
+#endif
+};
+
static const char kDefaultsConfigStr[] = "defaults";
static const char kExcludeConfigChar = '~';
@@ -1314,6 +1309,29 @@ static SkString configUsage() {
return result;
}
+static SkString pdfRasterizerUsage() {
+ SkString result;
+ result.appendf("Space delimited list of which PDF rasterizers to run. Possible options: [");
+ for (size_t i = 0; i < SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
+ if (i > 0) {
+ result.append(", ");
vandebo (ex-Chrome) 2013/08/13 17:48:00 Oops, if the input should be space delimited, then
ducky 2013/08/14 00:37:11 Done.
+ }
+ result.append(kPDFRasterizers[i].fName);
+ }
+ result.append("]\n");
+ result.append("The default value is: \"");
+ for (size_t i = 0; i < SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
+ if (kPDFRasterizers[i].fRunByDefault) {
+ if (i > 0) {
+ result.append(" ");
+ }
+ result.append(kPDFRasterizers[i].fName);
+ }
+ }
+ result.append("\"");
+ return result;
+}
+
// Macro magic to convert a numeric preprocessor token into a string.
// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
// This should probably be moved into one of our common headers...
@@ -1322,6 +1340,7 @@ static SkString configUsage() {
// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
DEFINE_string(config, "", configUsage().c_str());
+DEFINE_string(pdfRasterizers, "", pdfRasterizerUsage().c_str());
DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
@@ -1410,6 +1429,15 @@ static int findConfig(const char config[]) {
return -1;
}
+static const PDFRasterizerData* findPDFRasterizer(const char rasterizer[]) {
+ for (size_t i = 0; i < SK_ARRAY_COUNT(kPDFRasterizers); i++) {
+ if (!strcmp(rasterizer, kPDFRasterizers[i].fName)) {
+ return &kPDFRasterizers[i];
+ }
+ }
+ return NULL;
+}
+
namespace skiagm {
#if SK_SUPPORT_GPU
SkAutoTUnref<GrContext> gGrContext;
@@ -1466,9 +1494,13 @@ template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
*
* Returns all errors encountered while doing so.
*/
-ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
+ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
+ const SkTDArray<size_t> &configs,
+ const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
GrContextFactory *grFactory);
-ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
+ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
+ const SkTDArray<size_t> &configs,
+ const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
GrContextFactory *grFactory) {
const char renderModeDescriptor[] = "";
ErrorCombination errorsForAllConfigs;
@@ -1559,9 +1591,12 @@ ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<si
} else {
writePath = NULL;
}
+
if (errorsForThisConfig.isEmpty()) {
- errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
+ errorsForThisConfig.add(gmmain.test_drawing(gm, config, pdfRasterizers,
+ writePath, gpuTarget,
&comparisonBitmap));
+ gmmain.RecordTestResults(errorsForThisConfig, shortNamePlusConfig, "");
}
if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
@@ -1870,6 +1905,39 @@ static bool parse_flags_configs(SkTDArray<size_t>* outConfigs,
return true;
}
+static bool parse_flags_pdf_rasterizers(SkTDArray<const PDFRasterizerData*>* outRasterizers) {
+ for (int i = 0; i < FLAGS_pdfRasterizers.count(); i++) {
+ const char* rasterizer = FLAGS_pdfRasterizers[i];
+ const PDFRasterizerData* rasterizerPtr = findPDFRasterizer(rasterizer);
+
+ if (rasterizerPtr == NULL) {
+ gm_fprintf(stderr, "unrecognized rasterizer %s\n", rasterizer);
+ return false;
+ }
+ appendUnique<const PDFRasterizerData*>(outRasterizers,
+ rasterizerPtr);
+ }
+
+ if (outRasterizers->count() == 0) {
+ // if no config is specified by user, add the defaults
+ for (size_t i = 0; i < SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
+ if (kPDFRasterizers[i].fRunByDefault) {
+ *outRasterizers->append() = &kPDFRasterizers[i];
+ }
+ }
+ }
+
+ // now show the user the set of configs that will be run.
+ SkString configStr("These PDF rasterizers will be run: ");
+ // show the user the config that will run.
+ for (int i = 0; i < outRasterizers->count(); ++i) {
+ configStr.appendf("%s ", (*outRasterizers)[i]->fName);
+ }
+ gm_fprintf(stdout, "%s\n", configStr.c_str());
+
+ return true;
+}
+
static bool parse_flags_ignore_error_types(ErrorCombination* outErrorTypes) {
if (FLAGS_ignoreErrorTypes.count() > 0) {
*outErrorTypes = ErrorCombination();
@@ -2012,8 +2080,10 @@ int tool_main(int argc, char** argv) {
SkCommandLineFlags::Parse(argc, argv);
SkTDArray<size_t> configs;
+
int moduloRemainder = -1;
int moduloDivisor = -1;
+ SkTDArray<const PDFRasterizerData*> pdfRasterizers;
SkTDArray<SkScalar> tileGridReplayScales;
#if SK_SUPPORT_GPU
GrContextFactory* grFactory = new GrContextFactory;
@@ -2032,7 +2102,8 @@ int tool_main(int argc, char** argv) {
!parse_flags_resource_path() ||
!parse_flags_match_strs(&matchStrs) ||
!parse_flags_jpeg_quality() ||
- !parse_flags_configs(&configs, grFactory)) {
+ !parse_flags_configs(&configs, grFactory) ||
+ !parse_flags_pdf_rasterizers(&pdfRasterizers)) {
return -1;
}
@@ -2100,7 +2171,7 @@ int tool_main(int argc, char** argv) {
gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
size.width(), size.height());
- run_multiple_configs(gmmain, gm, configs, grFactory);
+ run_multiple_configs(gmmain, gm, configs, pdfRasterizers, grFactory);
SkBitmap comparisonBitmap;
const ConfigData compareConfig =
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698