blob: c9a15fd766293cd541fb10717238e8606d036fd5 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/utils/SkMultiPictureDocument.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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"
halcanary45420a92016-06-02 12:41:14 -070018
Hal Canary45cde312017-04-03 16:06:42 -040019#include <limits.h>
Nathaniel Nifong4a565682021-01-05 10:34:48 -050020#include <functional>
Hal Canary45cde312017-04-03 16:06:42 -040021
halcanary45420a92016-06-02 12:41:14 -070022/*
23 File format:
24 BEGINNING_OF_FILE:
25 kMagic
halcanaryc966ef92016-08-23 09:15:04 -070026 uint32_t version_number (==2)
halcanary45420a92016-06-02 12:41:14 -070027 uint32_t page_count
28 {
halcanary45420a92016-06-02 12:41:14 -070029 float sizeX
30 float sizeY
31 } * page_count
halcanary45420a92016-06-02 12:41:14 -070032 skp file
halcanary45420a92016-06-02 12:41:14 -070033*/
34
35namespace {
Hal Canary45cde312017-04-03 16:06:42 -040036// The unique file signature for this file type.
37static constexpr char kMagic[] = "Skia Multi-Picture Doc\n\n";
38
39static constexpr char kEndPage[] = "SkMultiPictureEndPage";
40
41const uint32_t kVersion = 2;
42
43static SkSize join(const SkTArray<SkSize>& sizes) {
Hal Canaryfafe1352017-04-11 12:12:02 -040044 SkSize joined = {0, 0};
Hal Canary45cde312017-04-03 16:06:42 -040045 for (SkSize s : sizes) {
Brian Osman788b9162020-02-07 10:36:46 -050046 joined = SkSize{std::max(joined.width(), s.width()), std::max(joined.height(), s.height())};
Hal Canary45cde312017-04-03 16:06:42 -040047 }
48 return joined;
49}
50
halcanary45420a92016-06-02 12:41:14 -070051struct MultiPictureDocument final : public SkDocument {
Mike Reed47fdf6c2017-12-20 14:12:07 -050052 const SkSerialProcs fProcs;
halcanary45420a92016-06-02 12:41:14 -070053 SkPictureRecorder fPictureRecorder;
halcanary59d64022016-06-14 11:06:37 -070054 SkSize fCurrentPageSize;
halcanaryc966ef92016-08-23 09:15:04 -070055 SkTArray<sk_sp<SkPicture>> fPages;
56 SkTArray<SkSize> fSizes;
Nathaniel Nifong4a565682021-01-05 10:34:48 -050057 std::function<void(const SkPicture*)> fOnEndPage;
58 MultiPictureDocument(SkWStream* s, const SkSerialProcs* procs,
59 std::function<void(const SkPicture*)> onEndPage)
Hal Canaryc5980d02018-01-08 15:02:36 -050060 : SkDocument(s)
Mike Reed47fdf6c2017-12-20 14:12:07 -050061 , fProcs(procs ? *procs : SkSerialProcs())
Nathaniel Nifong4a565682021-01-05 10:34:48 -050062 , fOnEndPage(onEndPage)
Mike Reed47fdf6c2017-12-20 14:12:07 -050063 {}
Brian Salomond3b65972017-03-22 12:05:03 -040064 ~MultiPictureDocument() override { this->close(); }
halcanary45420a92016-06-02 12:41:14 -070065
Hal Canaryb1de5f92017-07-01 22:17:15 -040066 SkCanvas* onBeginPage(SkScalar w, SkScalar h) override {
halcanary59d64022016-06-14 11:06:37 -070067 fCurrentPageSize.set(w, h);
Hal Canaryb1de5f92017-07-01 22:17:15 -040068 return fPictureRecorder.beginRecording(w, h);
halcanary45420a92016-06-02 12:41:14 -070069 }
70 void onEndPage() override {
halcanaryc966ef92016-08-23 09:15:04 -070071 fSizes.push_back(fCurrentPageSize);
Nathaniel Nifong4a565682021-01-05 10:34:48 -050072 sk_sp<SkPicture> lastPage = fPictureRecorder.finishRecordingAsPicture();
73 fPages.push_back(lastPage);
74 if (fOnEndPage) {
75 fOnEndPage(lastPage.get());
76 }
halcanary45420a92016-06-02 12:41:14 -070077 }
reedd14df7c2016-09-22 14:12:46 -070078 void onClose(SkWStream* wStream) override {
halcanary45420a92016-06-02 12:41:14 -070079 SkASSERT(wStream);
80 SkASSERT(wStream->bytesWritten() == 0);
Hal Canary45cde312017-04-03 16:06:42 -040081 wStream->writeText(kMagic);
82 wStream->write32(kVersion);
reedd14df7c2016-09-22 14:12:46 -070083 wStream->write32(SkToU32(fPages.count()));
halcanaryc966ef92016-08-23 09:15:04 -070084 for (SkSize s : fSizes) {
reedd14df7c2016-09-22 14:12:46 -070085 wStream->write(&s, sizeof(s));
halcanary45420a92016-06-02 12:41:14 -070086 }
Hal Canary45cde312017-04-03 16:06:42 -040087 SkSize bigsize = join(fSizes);
halcanaryc966ef92016-08-23 09:15:04 -070088 SkCanvas* c = fPictureRecorder.beginRecording(SkRect::MakeSize(bigsize));
89 for (const sk_sp<SkPicture>& page : fPages) {
90 c->drawPicture(page);
Nathaniel Nifong0426c382019-06-21 11:09:19 -040091 // Annotations must include some data.
92 c->drawAnnotation(SkRect::MakeEmpty(), kEndPage, SkData::MakeWithCString("X"));
halcanary45420a92016-06-02 12:41:14 -070093 }
halcanaryc966ef92016-08-23 09:15:04 -070094 sk_sp<SkPicture> p = fPictureRecorder.finishRecordingAsPicture();
Mike Reed47fdf6c2017-12-20 14:12:07 -050095 p->serialize(wStream, &fProcs);
halcanaryc966ef92016-08-23 09:15:04 -070096 fPages.reset();
97 fSizes.reset();
reedd14df7c2016-09-22 14:12:46 -070098 return;
halcanary45420a92016-06-02 12:41:14 -070099 }
halcanaryc966ef92016-08-23 09:15:04 -0700100 void onAbort() override {
101 fPages.reset();
102 fSizes.reset();
103 }
halcanary45420a92016-06-02 12:41:14 -0700104};
John Stilesa6841be2020-08-06 14:11:56 -0400105} // namespace
halcanary45420a92016-06-02 12:41:14 -0700106
Nathaniel Nifong4a565682021-01-05 10:34:48 -0500107sk_sp<SkDocument> SkMakeMultiPictureDocument(SkWStream* wStream, const SkSerialProcs* procs,
108 std::function<void(const SkPicture*)> onEndPage) {
109 return sk_make_sp<MultiPictureDocument>(wStream, procs, onEndPage);
halcanary45420a92016-06-02 12:41:14 -0700110}
Hal Canary45cde312017-04-03 16:06:42 -0400111
112////////////////////////////////////////////////////////////////////////////////
113
114int 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 Wagner255ab8d2016-10-07 15:50:53 -0400125 uint32_t versionNumber;
126 if (!stream->readU32(&versionNumber) || versionNumber != kVersion) {
Hal Canary45cde312017-04-03 16:06:42 -0400127 return 0;
128 }
Ben Wagner255ab8d2016-10-07 15:50:53 -0400129 uint32_t pageCount;
130 if (!stream->readU32(&pageCount) || pageCount > INT_MAX) {
Hal Canary45cde312017-04-03 16:06:42 -0400131 return 0;
132 }
133 // leave stream position right here.
Ben Wagner255ab8d2016-10-07 15:50:53 -0400134 return SkTo<int>(pageCount);
Hal Canary45cde312017-04-03 16:06:42 -0400135}
136
137bool 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
157namespace {
158struct 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
188bool SkMultiPictureDocumentRead(SkStreamSeekable* stream,
189 SkDocumentPage* dstArray,
Mike Reed47fdf6c2017-12-20 14:12:07 -0500190 int dstArrayCount,
191 const SkDeserialProcs* procs) {
Hal Canary45cde312017-04-03 16:06:42 -0400192 if (!SkMultiPictureDocumentReadPageSizes(stream, dstArray, dstArrayCount)) {
193 return false;
194 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400195 SkSize joined = {0.0f, 0.0f};
Hal Canary45cde312017-04-03 16:06:42 -0400196 for (int i = 0; i < dstArrayCount; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500197 joined = SkSize{std::max(joined.width(), dstArray[i].fSize.width()),
198 std::max(joined.height(), dstArray[i].fSize.height())};
Hal Canary45cde312017-04-03 16:06:42 -0400199 }
200
Mike Reed47fdf6c2017-12-20 14:12:07 -0500201 auto picture = SkPicture::MakeFromStream(stream, procs);
Brian Salomon06c9e292021-04-29 14:10:23 -0400202 if (!picture) {
203 return false;
204 }
Hal Canary45cde312017-04-03 16:06:42 -0400205
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 Nifong0426c382019-06-21 11:09:19 -0400211 SkDEBUGF("Malformed SkMultiPictureDocument: canvas.fIndex=%d dstArrayCount=%d\n",
212 canvas.fIndex, dstArrayCount);
Hal Canary45cde312017-04-03 16:06:42 -0400213 }
214 return true;
215}