blob: 3924f3eb42cf9c930777eeab84ae39b88b7000de [file] [log] [blame]
halcanary45420a92016-06-02 12:41:14 -07001/*
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"
halcanaryc966ef92016-08-23 09:15:04 -070012#include "SkPictureRecorder.h"
13#include "SkNWayCanvas.h"
halcanary45420a92016-06-02 12:41:14 -070014
15bool 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();
halcanaryc966ef92016-08-23 09:15:04 -070029 if (versionNumber != SkMultiPictureDocumentProtocol::kVersion) {
halcanary45420a92016-06-02 12:41:14 -070030 return false;
31 }
32 uint32_t pageCount = stream->readU32();
33 fSizes.reset(pageCount);
halcanary45420a92016-06-02 12:41:14 -070034 for (uint32_t i = 0; i < pageCount; ++i) {
halcanaryc966ef92016-08-23 09:15:04 -070035 SkSize size;
36 good &= sizeof(size) == stream->read(&size, sizeof(size));
37 fSizes[i] = size;
halcanary45420a92016-06-02 12:41:14 -070038 }
halcanaryc966ef92016-08-23 09:15:04 -070039 fOffset = stream->getPosition();
halcanary45420a92016-06-02 12:41:14 -070040 return good;
41}
42
halcanaryc966ef92016-08-23 09:15:04 -070043namespace {
44struct 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
halcanary45420a92016-06-02 12:41:14 -070075sk_sp<SkPicture> SkMultiPictureDocumentReader::readPage(SkStreamSeekable* stream,
76 int pageNumber) const {
77 SkASSERT(pageNumber >= 0);
halcanaryc966ef92016-08-23 09:15:04 -070078 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;
halcanary45420a92016-06-02 12:41:14 -070093}