OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMultiPictureDocumentPriv.h" | 8 #include "SkMultiPictureDocumentPriv.h" |
9 #include "SkMultiPictureDocumentReader.h" | 9 #include "SkMultiPictureDocumentReader.h" |
10 #include "SkPicture.h" | 10 #include "SkPicture.h" |
11 #include "SkStream.h" | 11 #include "SkStream.h" |
12 #include "SkPictureRecorder.h" | |
13 #include "SkNWayCanvas.h" | |
12 | 14 |
13 bool SkMultiPictureDocumentReader::init(SkStreamSeekable* stream) { | 15 bool SkMultiPictureDocumentReader::init(SkStreamSeekable* stream) { |
14 if (!stream) { | 16 if (!stream) { |
15 return false; | 17 return false; |
16 } | 18 } |
17 stream->seek(0); | 19 stream->seek(0); |
18 const size_t size = sizeof(SkMultiPictureDocumentProtocol::kMagic) - 1; | 20 const size_t size = sizeof(SkMultiPictureDocumentProtocol::kMagic) - 1; |
19 char buffer[size]; | 21 char buffer[size]; |
20 if (size != stream->read(buffer, size) || | 22 if (size != stream->read(buffer, size) || |
21 0 != memcmp(SkMultiPictureDocumentProtocol::kMagic, buffer, size)) { | 23 0 != memcmp(SkMultiPictureDocumentProtocol::kMagic, buffer, size)) { |
22 stream = nullptr; | 24 stream = nullptr; |
23 return false; | 25 return false; |
24 } | 26 } |
25 bool good = true; | 27 bool good = true; |
26 uint32_t versionNumber = stream->readU32(); | 28 uint32_t versionNumber = stream->readU32(); |
27 if (versionNumber != 1) { | 29 if (versionNumber != SkMultiPictureDocumentProtocol::kVersion) { |
28 return false; | 30 return false; |
29 } | 31 } |
30 uint32_t pageCount = stream->readU32(); | 32 uint32_t pageCount = stream->readU32(); |
31 fSizes.reset(pageCount); | 33 fSizes.reset(pageCount); |
32 fOffsets.reset(pageCount); | |
33 for (uint32_t i = 0; i < pageCount; ++i) { | 34 for (uint32_t i = 0; i < pageCount; ++i) { |
34 SkMultiPictureDocumentProtocol::Entry entry; | 35 SkSize size; |
35 good &= sizeof(entry) == stream->read(&entry, sizeof(entry)); | 36 good &= sizeof(size) == stream->read(&size, sizeof(size)); |
tomhudson
2016/08/23 12:12:19
This is probably correct precedence, but can we th
hal.canary
2016/08/23 15:53:39
I understood that the secret implicit Skia style g
| |
36 fSizes[i] = SkSize::Make(entry.sizeX, entry.sizeY); | 37 fSizes[i] = size; |
37 good &= SkTFitsIn<size_t>(entry.offset); | |
38 fOffsets[i] = static_cast<size_t>(entry.offset); | |
39 } | 38 } |
39 fOffset = stream->getPosition(); | |
40 return good; | 40 return good; |
41 } | 41 } |
42 | 42 |
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 | |
43 sk_sp<SkPicture> SkMultiPictureDocumentReader::readPage(SkStreamSeekable* stream , | 75 sk_sp<SkPicture> SkMultiPictureDocumentReader::readPage(SkStreamSeekable* stream , |
44 int pageNumber) const { | 76 int pageNumber) const { |
45 SkASSERT(pageNumber >= 0); | 77 SkASSERT(pageNumber >= 0); |
46 SkASSERT(pageNumber < fOffsets.count()); | 78 SkASSERT(pageNumber < fSizes.count()); |
47 SkAssertResult(stream->seek(fOffsets[pageNumber])); | 79 if (0 == fPages.count()) { |
48 return SkPicture::MakeFromStream(stream); | 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; | |
49 } | 93 } |
OLD | NEW |