blob: 3102478c2fe5af2fb92bd568da1fed0bb413c284 [file] [log] [blame]
mtklein30bf3e22014-06-03 13:57:14 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "DMPDFTask.h"
mtklein30bf3e22014-06-03 13:57:14 -07009#include "DMPDFRasterizeTask.h"
10#include "DMUtil.h"
11#include "DMWriteTask.h"
12#include "SkCommandLineFlags.h"
13#include "SkDocument.h"
14
mtkleine696a592014-06-03 15:59:23 -070015// The PDF backend is not threadsafe. If you run dm with --pdf repeatedly, you
16// will quickly find yourself crashed. (while catchsegv out/Release/dm;; end).
17//
18// TODO(mtklein): re-enable by default, maybe moving to its own single thread.
19DEFINE_bool(pdf, false, "PDF backend master switch.");
mtklein30bf3e22014-06-03 13:57:14 -070020
21namespace DM {
22
mtkleine4d3e602014-06-06 09:28:43 -070023PDFTask::PDFTask(const char* config,
mtklein30bf3e22014-06-03 13:57:14 -070024 Reporter* reporter,
25 TaskRunner* taskRunner,
mtklein30bf3e22014-06-03 13:57:14 -070026 skiagm::GMRegistry::Factory factory,
27 RasterizePdfProc rasterizePdfProc)
28 : CpuTask(reporter, taskRunner)
29 , fGM(factory(NULL))
mtkleine4d3e602014-06-06 09:28:43 -070030 , fName(UnderJoin(fGM->getName(), config))
31 , fRasterize(rasterizePdfProc) {}
32
33PDFTask::PDFTask(Reporter* reporter,
34 TaskRunner* taskRunner,
mtkleind3e474e2014-06-27 12:34:44 -070035 const SkPicture* picture,
mtkleine4d3e602014-06-06 09:28:43 -070036 SkString filename,
37 RasterizePdfProc rasterizePdfProc)
38 : CpuTask(reporter, taskRunner)
39 , fPicture(SkRef(picture))
40 , fName(UnderJoin(FileToTaskName(filename).c_str(), "pdf"))
mtklein30bf3e22014-06-03 13:57:14 -070041 , fRasterize(rasterizePdfProc) {}
42
43namespace {
44
45class SinglePagePDF {
46public:
47 SinglePagePDF(SkScalar width, SkScalar height)
48 : fDocument(SkDocument::CreatePDF(&fWriteStream))
49 , fCanvas(fDocument->beginPage(width, height)) {}
50
51 SkCanvas* canvas() { return fCanvas; }
52
halcanarya4c60942014-08-26 10:38:07 -070053 SkStreamAsset* end() {
mtklein30bf3e22014-06-03 13:57:14 -070054 fCanvas->flush();
55 fDocument->endPage();
56 fDocument->close();
halcanarya4c60942014-08-26 10:38:07 -070057 return fWriteStream.detachAsStream();
mtklein30bf3e22014-06-03 13:57:14 -070058 }
59
60private:
61 SkDynamicMemoryWStream fWriteStream;
62 SkAutoTUnref<SkDocument> fDocument;
63 SkCanvas* fCanvas;
64};
65
66} // namespace
67
68void PDFTask::draw() {
halcanarya4c60942014-08-26 10:38:07 -070069 SkAutoTDelete<SkStreamAsset> pdfData;
mtkleine4d3e602014-06-06 09:28:43 -070070 bool rasterize = true;
71 if (fGM.get()) {
72 rasterize = 0 == (fGM->getFlags() & skiagm::GM::kSkipPDFRasterization_Flag);
73 SinglePagePDF pdf(fGM->width(), fGM->height());
mtklein0b36e6b2014-09-11 12:30:12 -070074 CanvasPreflight(pdf.canvas());
mtkleine4d3e602014-06-06 09:28:43 -070075 //TODO(mtklein): GM doesn't do this. Why not?
76 //pdf.canvas()->concat(fGM->getInitialTransform());
77 fGM->draw(pdf.canvas());
78 pdfData.reset(pdf.end());
79 } else {
robertphillipsa8d7f0b2014-08-29 08:03:56 -070080 SinglePagePDF pdf(fPicture->cullRect().width(), fPicture->cullRect().height());
mtklein0b36e6b2014-09-11 12:30:12 -070081 CanvasPreflight(pdf.canvas());
robertphillipsc5ba71d2014-09-04 08:42:50 -070082 fPicture->playback(pdf.canvas());
mtkleine4d3e602014-06-06 09:28:43 -070083 pdfData.reset(pdf.end());
84 }
mtklein30bf3e22014-06-03 13:57:14 -070085
mtklein30bf3e22014-06-03 13:57:14 -070086 SkASSERT(pdfData.get());
mtkleine4d3e602014-06-06 09:28:43 -070087 if (rasterize) {
halcanarya4c60942014-08-26 10:38:07 -070088 this->spawnChild(SkNEW_ARGS(PDFRasterizeTask,
89 (*this, pdfData->duplicate(), fRasterize)));
mtklein30bf3e22014-06-03 13:57:14 -070090 }
mtkleinea65bfa2014-09-09 07:59:46 -070091 const char* sourceType = fGM.get() ? "GM" : "SKP";
halcanarya4c60942014-08-26 10:38:07 -070092 this->spawnChild(SkNEW_ARGS(WriteTask,
mtkleinea65bfa2014-09-09 07:59:46 -070093 (*this, sourceType, pdfData->duplicate(), ".pdf")));
mtklein30bf3e22014-06-03 13:57:14 -070094}
95
96bool PDFTask::shouldSkip() const {
97 if (!FLAGS_pdf) {
98 return true;
99 }
mtkleine4d3e602014-06-06 09:28:43 -0700100 if (fGM.get() && 0 != (fGM->getFlags() & skiagm::GM::kSkipPDF_Flag)) {
mtklein30bf3e22014-06-03 13:57:14 -0700101 return true;
102 }
103 return false;
104}
105
106} // namespace DM