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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/utils/SkMultiPictureDocument.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPicture.h" |
| 11 | #include "include/core/SkPictureRecorder.h" |
| 12 | #include "include/core/SkSerialProcs.h" |
| 13 | #include "include/core/SkStream.h" |
| 14 | #include "include/private/SkTArray.h" |
| 15 | #include "include/private/SkTo.h" |
| 16 | #include "include/utils/SkNWayCanvas.h" |
| 17 | #include "src/utils/SkMultiPictureDocumentPriv.h" |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 18 | |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 19 | #include <limits.h> |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 20 | #include <functional> |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 21 | |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 22 | /* |
| 23 | File format: |
| 24 | BEGINNING_OF_FILE: |
| 25 | kMagic |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 26 | uint32_t version_number (==2) |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 27 | uint32_t page_count |
| 28 | { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 29 | float sizeX |
| 30 | float sizeY |
| 31 | } * page_count |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 32 | skp file |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | namespace { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 36 | // The unique file signature for this file type. |
| 37 | static constexpr char kMagic[] = "Skia Multi-Picture Doc\n\n"; |
| 38 | |
| 39 | static constexpr char kEndPage[] = "SkMultiPictureEndPage"; |
| 40 | |
| 41 | const uint32_t kVersion = 2; |
| 42 | |
| 43 | static SkSize join(const SkTArray<SkSize>& sizes) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 44 | SkSize joined = {0, 0}; |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 45 | for (SkSize s : sizes) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 46 | joined = SkSize{std::max(joined.width(), s.width()), std::max(joined.height(), s.height())}; |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 47 | } |
| 48 | return joined; |
| 49 | } |
| 50 | |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 51 | struct MultiPictureDocument final : public SkDocument { |
Mike Reed | 47fdf6c | 2017-12-20 14:12:07 -0500 | [diff] [blame] | 52 | const SkSerialProcs fProcs; |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 53 | SkPictureRecorder fPictureRecorder; |
halcanary | 59d6402 | 2016-06-14 11:06:37 -0700 | [diff] [blame] | 54 | SkSize fCurrentPageSize; |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 55 | SkTArray<sk_sp<SkPicture>> fPages; |
| 56 | SkTArray<SkSize> fSizes; |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 57 | std::function<void(const SkPicture*)> fOnEndPage; |
| 58 | MultiPictureDocument(SkWStream* s, const SkSerialProcs* procs, |
| 59 | std::function<void(const SkPicture*)> onEndPage) |
Hal Canary | c5980d0 | 2018-01-08 15:02:36 -0500 | [diff] [blame] | 60 | : SkDocument(s) |
Mike Reed | 47fdf6c | 2017-12-20 14:12:07 -0500 | [diff] [blame] | 61 | , fProcs(procs ? *procs : SkSerialProcs()) |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 62 | , fOnEndPage(onEndPage) |
Mike Reed | 47fdf6c | 2017-12-20 14:12:07 -0500 | [diff] [blame] | 63 | {} |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 64 | ~MultiPictureDocument() override { this->close(); } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 65 | |
Hal Canary | b1de5f9 | 2017-07-01 22:17:15 -0400 | [diff] [blame] | 66 | SkCanvas* onBeginPage(SkScalar w, SkScalar h) override { |
halcanary | 59d6402 | 2016-06-14 11:06:37 -0700 | [diff] [blame] | 67 | fCurrentPageSize.set(w, h); |
Hal Canary | b1de5f9 | 2017-07-01 22:17:15 -0400 | [diff] [blame] | 68 | return fPictureRecorder.beginRecording(w, h); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 69 | } |
| 70 | void onEndPage() override { |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 71 | fSizes.push_back(fCurrentPageSize); |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 72 | sk_sp<SkPicture> lastPage = fPictureRecorder.finishRecordingAsPicture(); |
| 73 | fPages.push_back(lastPage); |
| 74 | if (fOnEndPage) { |
| 75 | fOnEndPage(lastPage.get()); |
| 76 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 77 | } |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 78 | void onClose(SkWStream* wStream) override { |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 79 | SkASSERT(wStream); |
| 80 | SkASSERT(wStream->bytesWritten() == 0); |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 81 | wStream->writeText(kMagic); |
| 82 | wStream->write32(kVersion); |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 83 | wStream->write32(SkToU32(fPages.count())); |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 84 | for (SkSize s : fSizes) { |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 85 | wStream->write(&s, sizeof(s)); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 86 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 87 | SkSize bigsize = join(fSizes); |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 88 | SkCanvas* c = fPictureRecorder.beginRecording(SkRect::MakeSize(bigsize)); |
| 89 | for (const sk_sp<SkPicture>& page : fPages) { |
| 90 | c->drawPicture(page); |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 91 | // Annotations must include some data. |
| 92 | c->drawAnnotation(SkRect::MakeEmpty(), kEndPage, SkData::MakeWithCString("X")); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 93 | } |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 94 | sk_sp<SkPicture> p = fPictureRecorder.finishRecordingAsPicture(); |
Mike Reed | 47fdf6c | 2017-12-20 14:12:07 -0500 | [diff] [blame] | 95 | p->serialize(wStream, &fProcs); |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 96 | fPages.reset(); |
| 97 | fSizes.reset(); |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 98 | return; |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 99 | } |
halcanary | c966ef9 | 2016-08-23 09:15:04 -0700 | [diff] [blame] | 100 | void onAbort() override { |
| 101 | fPages.reset(); |
| 102 | fSizes.reset(); |
| 103 | } |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 104 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 105 | } // namespace |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 106 | |
Nathaniel Nifong | 4a56568 | 2021-01-05 10:34:48 -0500 | [diff] [blame] | 107 | sk_sp<SkDocument> SkMakeMultiPictureDocument(SkWStream* wStream, const SkSerialProcs* procs, |
| 108 | std::function<void(const SkPicture*)> onEndPage) { |
| 109 | return sk_make_sp<MultiPictureDocument>(wStream, procs, onEndPage); |
halcanary | 45420a9 | 2016-06-02 12:41:14 -0700 | [diff] [blame] | 110 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 111 | |
| 112 | //////////////////////////////////////////////////////////////////////////////// |
| 113 | |
| 114 | int SkMultiPictureDocumentReadPageCount(SkStreamSeekable* stream) { |
| 115 | if (!stream) { |
| 116 | return 0; |
| 117 | } |
| 118 | stream->seek(0); |
| 119 | const size_t size = sizeof(kMagic) - 1; |
| 120 | char buffer[size]; |
| 121 | if (size != stream->read(buffer, size) || 0 != memcmp(kMagic, buffer, size)) { |
| 122 | stream = nullptr; |
| 123 | return 0; |
| 124 | } |
Ben Wagner | 255ab8d | 2016-10-07 15:50:53 -0400 | [diff] [blame] | 125 | uint32_t versionNumber; |
| 126 | if (!stream->readU32(&versionNumber) || versionNumber != kVersion) { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 127 | return 0; |
| 128 | } |
Ben Wagner | 255ab8d | 2016-10-07 15:50:53 -0400 | [diff] [blame] | 129 | uint32_t pageCount; |
| 130 | if (!stream->readU32(&pageCount) || pageCount > INT_MAX) { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | // leave stream position right here. |
Ben Wagner | 255ab8d | 2016-10-07 15:50:53 -0400 | [diff] [blame] | 134 | return SkTo<int>(pageCount); |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool SkMultiPictureDocumentReadPageSizes(SkStreamSeekable* stream, |
| 138 | SkDocumentPage* dstArray, |
| 139 | int dstArrayCount) { |
| 140 | if (!dstArray || dstArrayCount < 1) { |
| 141 | return false; |
| 142 | } |
| 143 | int pageCount = SkMultiPictureDocumentReadPageCount(stream); |
| 144 | if (pageCount < 1 || pageCount != dstArrayCount) { |
| 145 | return false; |
| 146 | } |
| 147 | for (int i = 0; i < pageCount; ++i) { |
| 148 | SkSize& s = dstArray[i].fSize; |
| 149 | if (sizeof(s) != stream->read(&s, sizeof(s))) { |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | // leave stream position right here. |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | namespace { |
| 158 | struct PagerCanvas : public SkNWayCanvas { |
| 159 | SkPictureRecorder fRecorder; |
| 160 | SkDocumentPage* fDst; |
| 161 | int fCount; |
| 162 | int fIndex = 0; |
| 163 | PagerCanvas(SkISize wh, SkDocumentPage* dst, int count) |
| 164 | : SkNWayCanvas(wh.width(), wh.height()), fDst(dst), fCount(count) { |
| 165 | this->nextCanvas(); |
| 166 | } |
| 167 | void nextCanvas() { |
| 168 | if (fIndex < fCount) { |
| 169 | SkRect bounds = SkRect::MakeSize(fDst[fIndex].fSize); |
| 170 | this->addCanvas(fRecorder.beginRecording(bounds)); |
| 171 | } |
| 172 | } |
| 173 | void onDrawAnnotation(const SkRect& r, const char* key, SkData* d) override { |
| 174 | if (0 == strcmp(key, kEndPage)) { |
| 175 | this->removeAll(); |
| 176 | if (fIndex < fCount) { |
| 177 | fDst[fIndex].fPicture = fRecorder.finishRecordingAsPicture(); |
| 178 | ++fIndex; |
| 179 | } |
| 180 | this->nextCanvas(); |
| 181 | } else { |
| 182 | this->SkNWayCanvas::onDrawAnnotation(r, key, d); |
| 183 | } |
| 184 | } |
| 185 | }; |
| 186 | } // namespace |
| 187 | |
| 188 | bool SkMultiPictureDocumentRead(SkStreamSeekable* stream, |
| 189 | SkDocumentPage* dstArray, |
Mike Reed | 47fdf6c | 2017-12-20 14:12:07 -0500 | [diff] [blame] | 190 | int dstArrayCount, |
| 191 | const SkDeserialProcs* procs) { |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 192 | if (!SkMultiPictureDocumentReadPageSizes(stream, dstArray, dstArrayCount)) { |
| 193 | return false; |
| 194 | } |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 195 | SkSize joined = {0.0f, 0.0f}; |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 196 | for (int i = 0; i < dstArrayCount; ++i) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 197 | joined = SkSize{std::max(joined.width(), dstArray[i].fSize.width()), |
| 198 | std::max(joined.height(), dstArray[i].fSize.height())}; |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Mike Reed | 47fdf6c | 2017-12-20 14:12:07 -0500 | [diff] [blame] | 201 | auto picture = SkPicture::MakeFromStream(stream, procs); |
Brian Salomon | 06c9e29 | 2021-04-29 14:10:23 -0400 | [diff] [blame] | 202 | if (!picture) { |
| 203 | return false; |
| 204 | } |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 205 | |
| 206 | PagerCanvas canvas(joined.toCeil(), dstArray, dstArrayCount); |
| 207 | // Must call playback(), not drawPicture() to reach |
| 208 | // PagerCanvas::onDrawAnnotation(). |
| 209 | picture->playback(&canvas); |
| 210 | if (canvas.fIndex != dstArrayCount) { |
Nathaniel Nifong | 0426c38 | 2019-06-21 11:09:19 -0400 | [diff] [blame] | 211 | SkDEBUGF("Malformed SkMultiPictureDocument: canvas.fIndex=%d dstArrayCount=%d\n", |
| 212 | canvas.fIndex, dstArrayCount); |
Hal Canary | 45cde31 | 2017-04-03 16:06:42 -0400 | [diff] [blame] | 213 | } |
| 214 | return true; |
| 215 | } |