mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 1 | /* |
| 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" |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 9 | #include "DMPDFRasterizeTask.h" |
| 10 | #include "DMUtil.h" |
| 11 | #include "DMWriteTask.h" |
| 12 | #include "SkCommandLineFlags.h" |
| 13 | #include "SkDocument.h" |
| 14 | |
mtklein | e696a59 | 2014-06-03 15:59:23 -0700 | [diff] [blame] | 15 | // 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. |
| 19 | DEFINE_bool(pdf, false, "PDF backend master switch."); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 20 | |
| 21 | namespace DM { |
| 22 | |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 23 | PDFTask::PDFTask(const char* config, |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 24 | Reporter* reporter, |
| 25 | TaskRunner* taskRunner, |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 26 | skiagm::GMRegistry::Factory factory, |
| 27 | RasterizePdfProc rasterizePdfProc) |
| 28 | : CpuTask(reporter, taskRunner) |
| 29 | , fGM(factory(NULL)) |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 30 | , fName(UnderJoin(fGM->getName(), config)) |
| 31 | , fRasterize(rasterizePdfProc) {} |
| 32 | |
| 33 | PDFTask::PDFTask(Reporter* reporter, |
| 34 | TaskRunner* taskRunner, |
mtklein | d3e474e | 2014-06-27 12:34:44 -0700 | [diff] [blame] | 35 | const SkPicture* picture, |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 36 | SkString filename, |
| 37 | RasterizePdfProc rasterizePdfProc) |
| 38 | : CpuTask(reporter, taskRunner) |
| 39 | , fPicture(SkRef(picture)) |
| 40 | , fName(UnderJoin(FileToTaskName(filename).c_str(), "pdf")) |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 41 | , fRasterize(rasterizePdfProc) {} |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | class SinglePagePDF { |
| 46 | public: |
| 47 | SinglePagePDF(SkScalar width, SkScalar height) |
| 48 | : fDocument(SkDocument::CreatePDF(&fWriteStream)) |
| 49 | , fCanvas(fDocument->beginPage(width, height)) {} |
| 50 | |
| 51 | SkCanvas* canvas() { return fCanvas; } |
| 52 | |
halcanary | a4c6094 | 2014-08-26 10:38:07 -0700 | [diff] [blame] | 53 | SkStreamAsset* end() { |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 54 | fCanvas->flush(); |
| 55 | fDocument->endPage(); |
| 56 | fDocument->close(); |
halcanary | a4c6094 | 2014-08-26 10:38:07 -0700 | [diff] [blame] | 57 | return fWriteStream.detachAsStream(); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
| 61 | SkDynamicMemoryWStream fWriteStream; |
| 62 | SkAutoTUnref<SkDocument> fDocument; |
| 63 | SkCanvas* fCanvas; |
| 64 | }; |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | void PDFTask::draw() { |
halcanary | a4c6094 | 2014-08-26 10:38:07 -0700 | [diff] [blame] | 69 | SkAutoTDelete<SkStreamAsset> pdfData; |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 70 | bool rasterize = true; |
| 71 | if (fGM.get()) { |
| 72 | rasterize = 0 == (fGM->getFlags() & skiagm::GM::kSkipPDFRasterization_Flag); |
| 73 | SinglePagePDF pdf(fGM->width(), fGM->height()); |
mtklein | 0b36e6b | 2014-09-11 12:30:12 -0700 | [diff] [blame] | 74 | CanvasPreflight(pdf.canvas()); |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 75 | //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 { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 80 | SinglePagePDF pdf(fPicture->cullRect().width(), fPicture->cullRect().height()); |
mtklein | 0b36e6b | 2014-09-11 12:30:12 -0700 | [diff] [blame] | 81 | CanvasPreflight(pdf.canvas()); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 82 | fPicture->playback(pdf.canvas()); |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 83 | pdfData.reset(pdf.end()); |
| 84 | } |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 85 | |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 86 | SkASSERT(pdfData.get()); |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 87 | if (rasterize) { |
halcanary | a4c6094 | 2014-08-26 10:38:07 -0700 | [diff] [blame] | 88 | this->spawnChild(SkNEW_ARGS(PDFRasterizeTask, |
| 89 | (*this, pdfData->duplicate(), fRasterize))); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 90 | } |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 91 | const char* sourceType = fGM.get() ? "GM" : "SKP"; |
halcanary | a4c6094 | 2014-08-26 10:38:07 -0700 | [diff] [blame] | 92 | this->spawnChild(SkNEW_ARGS(WriteTask, |
mtklein | ea65bfa | 2014-09-09 07:59:46 -0700 | [diff] [blame] | 93 | (*this, sourceType, pdfData->duplicate(), ".pdf"))); |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool PDFTask::shouldSkip() const { |
| 97 | if (!FLAGS_pdf) { |
| 98 | return true; |
| 99 | } |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 100 | if (fGM.get() && 0 != (fGM->getFlags() & skiagm::GM::kSkipPDF_Flag)) { |
mtklein | 30bf3e2 | 2014-06-03 13:57:14 -0700 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | } // namespace DM |