halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "SkMultiPictureDocumentPriv.h" |
| 9 | #include "SkMultiPictureDocumentReader.h" |
| 10 | #include "SkPicture.h" |
| 11 | #include "SkStream.h" |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 12 | #include "SkPictureRecorder.h" |
| 13 | #include "SkNWayCanvas.h" |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 14 | |
| 15 | bool SkMultiPictureDocumentReader::init(SkStreamSeekable* stream) { |
| 16 | if (!stream) { |
| 17 | return false; |
| 18 | } |
| 19 | stream->seek(0); |
| 20 | const size_t size = sizeof(SkMultiPictureDocumentProtocol::kMagic) - 1; |
| 21 | char buffer[size]; |
| 22 | if (size != stream->read(buffer, size) || |
| 23 | 0 != memcmp(SkMultiPictureDocumentProtocol::kMagic, buffer, size)) { |
| 24 | stream = nullptr; |
| 25 | return false; |
| 26 | } |
| 27 | bool good = true; |
| 28 | uint32_t versionNumber = stream->readU32(); |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 29 | if (versionNumber != SkMultiPictureDocumentProtocol::kVersion) { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 30 | return false; |
| 31 | } |
| 32 | uint32_t pageCount = stream->readU32(); |
| 33 | fSizes.reset(pageCount); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 34 | for (uint32_t i = 0; i < pageCount; ++i) { |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 35 | SkSize size; |
| 36 | good &= sizeof(size) == stream->read(&size, sizeof(size)); |
| 37 | fSizes[i] = size; |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 38 | } |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 39 | fOffset = stream->getPosition(); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 40 | return good; |
| 41 | } |
| 42 | |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 43 | namespace { |
| 44 | struct PagerCanvas : public SkNWayCanvas { |
| 45 | SkPictureRecorder fRecorder; |
| 46 | const SkTArray<SkSize>* fSizes; |
| 47 | SkTArray<sk_sp<SkPicture>>* fDest; |
| 48 | PagerCanvas(SkISize wh, |
| 49 | const SkTArray<SkSize>* s, |
| 50 | SkTArray<sk_sp<SkPicture>>* d) |
| 51 | : SkNWayCanvas(wh.width(), wh.height()), fSizes(s), fDest(d) { |
| 52 | this->nextCanvas(); |
| 53 | } |
| 54 | void nextCanvas() { |
| 55 | int i = fDest->count(); |
| 56 | if (i < fSizes->count()) { |
| 57 | SkRect bounds = SkRect::MakeSize((*fSizes)[i]); |
| 58 | this->addCanvas(fRecorder.beginRecording(bounds)); |
| 59 | } |
| 60 | } |
| 61 | void onDrawAnnotation(const SkRect& r, const char* key, SkData* d) override { |
| 62 | if (0 == strcmp(key, SkMultiPictureDocumentProtocol::kEndPage)) { |
| 63 | this->removeAll(); |
| 64 | if (fRecorder.getRecordingCanvas()) { |
| 65 | fDest->emplace_back(fRecorder.finishRecordingAsPicture()); |
| 66 | } |
| 67 | this->nextCanvas(); |
| 68 | } else { |
| 69 | this->SkNWayCanvas::onDrawAnnotation(r, key, d); |
| 70 | } |
| 71 | } |
| 72 | }; |
| 73 | } // namespace |
| 74 | |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 75 | sk_sp<SkPicture> SkMultiPictureDocumentReader::readPage(SkStreamSeekable* stream, |
| 76 | int pageNumber) const { |
| 77 | SkASSERT(pageNumber >= 0); |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 78 | SkASSERT(pageNumber < fSizes.count()); |
| 79 | if (0 == fPages.count()) { |
| 80 | stream->seek(fOffset); // jump to beginning of skp |
| 81 | auto picture = SkPicture::MakeFromStream(stream); |
| 82 | SkISize size = SkMultiPictureDocumentProtocol::Join(fSizes).toCeil(); |
| 83 | PagerCanvas canvas(size, &fSizes, &this->fPages); |
| 84 | // Must call playback(), not drawPicture() to reach |
| 85 | // PagerCanvas::onDrawAnnotation(). |
| 86 | picture->playback(&canvas); |
| 87 | if (fPages.count() != fSizes.count()) { |
| 88 | SkDEBUGF(("Malformed SkMultiPictureDocument\n")); |
| 89 | } |
| 90 | } |
| 91 | // Allow for malformed document. |
| 92 | return pageNumber < fPages.count() ? fPages[pageNumber] : nullptr; |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 93 | } |