diff options
author | halcanary <halcanary@google.com> | 2016-06-02 12:41:14 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-06-02 12:41:14 -0700 |
commit | 45420a945cfce1b83da954ab5fcb63516f646c0f (patch) | |
tree | a05e2ac95d4425ae9d945c2f2490a78439a03d4f /dm | |
parent | fbce71f1ae2ff05adc584f2f0433435c93987e13 (diff) |
SkMultiPictureDocument & SkMultiPictureDocumentReader
also, a new DM::Src.
motivation: To be used to test the printing pipeline in Chromium.
BUG=skia:5370
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2023593002
Review-Url: https://codereview.chromium.org/2023593002
Diffstat (limited to 'dm')
-rw-r--r-- | dm/DM.cpp | 15 | ||||
-rw-r--r-- | dm/DMSrcSink.cpp | 72 | ||||
-rw-r--r-- | dm/DMSrcSink.h | 24 |
3 files changed, 78 insertions, 33 deletions
@@ -72,6 +72,8 @@ DEFINE_int32(shards, 1, "We're splitting source data into this many shards."); DEFINE_int32(shard, 0, "Which shard do I run?"); DEFINE_bool(simpleCodec, false, "Only decode images to native scale"); +DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file."); + using namespace DM; using sk_gpu_test::GrContextFactory; using sk_gpu_test::GLTestContext; @@ -702,6 +704,19 @@ static bool gather_srcs() { } } + for (int i = 0; i < FLAGS_mskps.count(); i++) { + const char* path = FLAGS_mskps[i]; + if (sk_isdir(path)) { + SkOSFile::Iter it(path, "mskp"); + for (SkString file; it.next(&file);) { + push_src("mskp", "", + new MSKPSrc(SkOSPath::Join(path, file.c_str()))); + } + } else { + push_src("mskp", "", new MSKPSrc(path)); + } + } + SkTArray<SkString> images; if (!CollectImages(FLAGS_images, &images)) { return false; diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp index 356416035b..a1be0979fd 100644 --- a/dm/DMSrcSink.cpp +++ b/dm/DMSrcSink.cpp @@ -1007,6 +1007,41 @@ Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ +MSKPSrc::MSKPSrc(Path path) : fPath(path) { + std::unique_ptr<SkStreamAsset> stream(SkStream::NewFromFile(fPath.c_str())); + (void)fReader.init(stream.get()); +} + +int MSKPSrc::pageCount() const { return fReader.pageCount(); } + +SkISize MSKPSrc::size() const { return this->size(0); } +SkISize MSKPSrc::size(int i) const { return fReader.pageSize(i).toCeil(); } + +Error MSKPSrc::draw(SkCanvas* c) const { return this->draw(0, c); } +Error MSKPSrc::draw(int i, SkCanvas* canvas) const { + std::unique_ptr<SkStreamAsset> stream(SkStream::NewFromFile(fPath.c_str())); + if (!stream) { + return SkStringPrintf("Unable to open file: %s", fPath.c_str()); + } + if (fReader.pageCount() == 0) { + return SkStringPrintf("Unable to parse MultiPictureDocument file: %s", fPath.c_str()); + } + if (i >= fReader.pageCount()) { + return SkStringPrintf("MultiPictureDocument page number out of range: %d", i); + } + sk_sp<SkPicture> page = fReader.readPage(stream.get(), i); + if (!page) { + return SkStringPrintf("SkMultiPictureDocumentReader failed on page %d: %s", + i, fPath.c_str()); + } + canvas->drawPicture(page); + return ""; +} + +Name MSKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } + +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const { SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas()); return src.draw(canvas); @@ -1099,44 +1134,15 @@ static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) { return "Source has empty dimensions"; } SkASSERT(doc); - int width = src.size().width(), - height = src.size().height(); - - if (FLAGS_multiPage) { - // Print the given DM:Src to a document, breaking on 8.5x11 pages. - const int kLetterWidth = 612, // 8.5 * 72 - kLetterHeight = 792; // 11 * 72 - const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth), - SkIntToScalar(kLetterHeight)); - - int xPages = ((width - 1) / kLetterWidth) + 1; - int yPages = ((height - 1) / kLetterHeight) + 1; - - for (int y = 0; y < yPages; ++y) { - for (int x = 0; x < xPages; ++x) { - int w = SkTMin(kLetterWidth, width - (x * kLetterWidth)); - int h = SkTMin(kLetterHeight, height - (y * kLetterHeight)); - SkCanvas* canvas = - doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)); - if (!canvas) { - return "SkDocument::beginPage(w,h) returned nullptr"; - } - canvas->clipRect(letter); - canvas->translate(-letter.width() * x, -letter.height() * y); - Error err = src.draw(canvas); - if (!err.isEmpty()) { - return err; - } - doc->endPage(); - } - } - } else { + int pageCount = src.pageCount(); + for (int i = 0; i < pageCount; ++i) { + int width = src.size(i).width(), height = src.size(i).height(); SkCanvas* canvas = doc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); if (!canvas) { return "SkDocument::beginPage(w,h) returned nullptr"; } - Error err = src.draw(canvas); + Error err = src.draw(i, canvas); if (!err.isEmpty()) { return err; } diff --git a/dm/DMSrcSink.h b/dm/DMSrcSink.h index 42f0e6da36..395f59e1a9 100644 --- a/dm/DMSrcSink.h +++ b/dm/DMSrcSink.h @@ -15,6 +15,7 @@ #include "SkBitmapRegionDecoder.h" #include "SkCanvas.h" #include "SkData.h" +#include "SkMultiPictureDocumentReader.h" #include "SkPicture.h" #include "gm.h" @@ -66,6 +67,11 @@ struct Src { virtual void modifyGrContextOptions(GrContextOptions* options) const {} virtual bool veto(SinkFlags) const { return false; } + virtual int pageCount() const { return 1; } + virtual Error SK_WARN_UNUSED_RESULT draw(int, SkCanvas* canvas) const { + return this->draw(canvas); + } + virtual SkISize size(int) const { return this->size(); } // Force Tasks using this Src to run on the main thread? virtual bool serial() const { return false; } }; @@ -243,6 +249,24 @@ private: /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ +class MSKPSrc : public Src { +public: + explicit MSKPSrc(Path path); + + int pageCount() const override; + Error draw(SkCanvas* c) const override; + Error draw(int, SkCanvas*) const override; + SkISize size() const override; + SkISize size(int) const override; + Name name() const override; + +private: + Path fPath; + SkMultiPictureDocumentReader fReader; +}; + +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ + class NullSink : public Sink { public: NullSink() {} |