blob: 8a47be178d87b52edb3424831fcf20f8cf1aa5ac [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>
20
halcanary45420a92016-06-02 12:41:14 -070021/*
22 File format:
23 BEGINNING_OF_FILE:
24 kMagic
halcanaryc966ef92016-08-23 09:15:04 -070025 uint32_t version_number (==2)
halcanary45420a92016-06-02 12:41:14 -070026 uint32_t page_count
27 {
halcanary45420a92016-06-02 12:41:14 -070028 float sizeX
29 float sizeY
30 } * page_count
halcanary45420a92016-06-02 12:41:14 -070031 skp file
halcanary45420a92016-06-02 12:41:14 -070032*/
33
34namespace {
Hal Canary45cde312017-04-03 16:06:42 -040035// The unique file signature for this file type.
36static constexpr char kMagic[] = "Skia Multi-Picture Doc\n\n";
37
38static constexpr char kEndPage[] = "SkMultiPictureEndPage";
39
40const uint32_t kVersion = 2;
41
42static SkSize join(const SkTArray<SkSize>& sizes) {
Hal Canaryfafe1352017-04-11 12:12:02 -040043 SkSize joined = {0, 0};
Hal Canary45cde312017-04-03 16:06:42 -040044 for (SkSize s : sizes) {
Brian Osman788b9162020-02-07 10:36:46 -050045 joined = SkSize{std::max(joined.width(), s.width()), std::max(joined.height(), s.height())};
Hal Canary45cde312017-04-03 16:06:42 -040046 }
47 return joined;
48}
49
halcanary45420a92016-06-02 12:41:14 -070050struct MultiPictureDocument final : public SkDocument {
Mike Reed47fdf6c2017-12-20 14:12:07 -050051 const SkSerialProcs fProcs;
halcanary45420a92016-06-02 12:41:14 -070052 SkPictureRecorder fPictureRecorder;
halcanary59d64022016-06-14 11:06:37 -070053 SkSize fCurrentPageSize;
halcanaryc966ef92016-08-23 09:15:04 -070054 SkTArray<sk_sp<SkPicture>> fPages;
55 SkTArray<SkSize> fSizes;
Hal Canaryc5980d02018-01-08 15:02:36 -050056 MultiPictureDocument(SkWStream* s, const SkSerialProcs* procs)
57 : SkDocument(s)
Mike Reed47fdf6c2017-12-20 14:12:07 -050058 , fProcs(procs ? *procs : SkSerialProcs())
59 {}
Brian Salomond3b65972017-03-22 12:05:03 -040060 ~MultiPictureDocument() override { this->close(); }
halcanary45420a92016-06-02 12:41:14 -070061
Hal Canaryb1de5f92017-07-01 22:17:15 -040062 SkCanvas* onBeginPage(SkScalar w, SkScalar h) override {
halcanary59d64022016-06-14 11:06:37 -070063 fCurrentPageSize.set(w, h);
Hal Canaryb1de5f92017-07-01 22:17:15 -040064 return fPictureRecorder.beginRecording(w, h);
halcanary45420a92016-06-02 12:41:14 -070065 }
66 void onEndPage() override {
halcanaryc966ef92016-08-23 09:15:04 -070067 fSizes.push_back(fCurrentPageSize);
68 fPages.push_back(fPictureRecorder.finishRecordingAsPicture());
halcanary45420a92016-06-02 12:41:14 -070069 }
reedd14df7c2016-09-22 14:12:46 -070070 void onClose(SkWStream* wStream) override {
halcanary45420a92016-06-02 12:41:14 -070071 SkASSERT(wStream);
72 SkASSERT(wStream->bytesWritten() == 0);
Hal Canary45cde312017-04-03 16:06:42 -040073 wStream->writeText(kMagic);
74 wStream->write32(kVersion);
reedd14df7c2016-09-22 14:12:46 -070075 wStream->write32(SkToU32(fPages.count()));
halcanaryc966ef92016-08-23 09:15:04 -070076 for (SkSize s : fSizes) {
reedd14df7c2016-09-22 14:12:46 -070077 wStream->write(&s, sizeof(s));
halcanary45420a92016-06-02 12:41:14 -070078 }
Hal Canary45cde312017-04-03 16:06:42 -040079 SkSize bigsize = join(fSizes);
halcanaryc966ef92016-08-23 09:15:04 -070080 SkCanvas* c = fPictureRecorder.beginRecording(SkRect::MakeSize(bigsize));
81 for (const sk_sp<SkPicture>& page : fPages) {
82 c->drawPicture(page);
Nathaniel Nifong0426c382019-06-21 11:09:19 -040083 // Annotations must include some data.
84 c->drawAnnotation(SkRect::MakeEmpty(), kEndPage, SkData::MakeWithCString("X"));
halcanary45420a92016-06-02 12:41:14 -070085 }
halcanaryc966ef92016-08-23 09:15:04 -070086 sk_sp<SkPicture> p = fPictureRecorder.finishRecordingAsPicture();
Mike Reed47fdf6c2017-12-20 14:12:07 -050087 p->serialize(wStream, &fProcs);
halcanaryc966ef92016-08-23 09:15:04 -070088 fPages.reset();
89 fSizes.reset();
reedd14df7c2016-09-22 14:12:46 -070090 return;
halcanary45420a92016-06-02 12:41:14 -070091 }
halcanaryc966ef92016-08-23 09:15:04 -070092 void onAbort() override {
93 fPages.reset();
94 fSizes.reset();
95 }
halcanary45420a92016-06-02 12:41:14 -070096};
97}
98
Mike Reed47fdf6c2017-12-20 14:12:07 -050099sk_sp<SkDocument> SkMakeMultiPictureDocument(SkWStream* wStream, const SkSerialProcs* procs) {
Hal Canaryc5980d02018-01-08 15:02:36 -0500100 return sk_make_sp<MultiPictureDocument>(wStream, procs);
halcanary45420a92016-06-02 12:41:14 -0700101}
Hal Canary45cde312017-04-03 16:06:42 -0400102
103////////////////////////////////////////////////////////////////////////////////
104
105int SkMultiPictureDocumentReadPageCount(SkStreamSeekable* stream) {
106 if (!stream) {
107 return 0;
108 }
109 stream->seek(0);
110 const size_t size = sizeof(kMagic) - 1;
111 char buffer[size];
112 if (size != stream->read(buffer, size) || 0 != memcmp(kMagic, buffer, size)) {
113 stream = nullptr;
114 return 0;
115 }
Ben Wagner255ab8d2016-10-07 15:50:53 -0400116 uint32_t versionNumber;
117 if (!stream->readU32(&versionNumber) || versionNumber != kVersion) {
Hal Canary45cde312017-04-03 16:06:42 -0400118 return 0;
119 }
Ben Wagner255ab8d2016-10-07 15:50:53 -0400120 uint32_t pageCount;
121 if (!stream->readU32(&pageCount) || pageCount > INT_MAX) {
Hal Canary45cde312017-04-03 16:06:42 -0400122 return 0;
123 }
124 // leave stream position right here.
Ben Wagner255ab8d2016-10-07 15:50:53 -0400125 return SkTo<int>(pageCount);
Hal Canary45cde312017-04-03 16:06:42 -0400126}
127
128bool SkMultiPictureDocumentReadPageSizes(SkStreamSeekable* stream,
129 SkDocumentPage* dstArray,
130 int dstArrayCount) {
131 if (!dstArray || dstArrayCount < 1) {
132 return false;
133 }
134 int pageCount = SkMultiPictureDocumentReadPageCount(stream);
135 if (pageCount < 1 || pageCount != dstArrayCount) {
136 return false;
137 }
138 for (int i = 0; i < pageCount; ++i) {
139 SkSize& s = dstArray[i].fSize;
140 if (sizeof(s) != stream->read(&s, sizeof(s))) {
141 return false;
142 }
143 }
144 // leave stream position right here.
145 return true;
146}
147
148namespace {
149struct PagerCanvas : public SkNWayCanvas {
150 SkPictureRecorder fRecorder;
151 SkDocumentPage* fDst;
152 int fCount;
153 int fIndex = 0;
154 PagerCanvas(SkISize wh, SkDocumentPage* dst, int count)
155 : SkNWayCanvas(wh.width(), wh.height()), fDst(dst), fCount(count) {
156 this->nextCanvas();
157 }
158 void nextCanvas() {
159 if (fIndex < fCount) {
160 SkRect bounds = SkRect::MakeSize(fDst[fIndex].fSize);
161 this->addCanvas(fRecorder.beginRecording(bounds));
162 }
163 }
164 void onDrawAnnotation(const SkRect& r, const char* key, SkData* d) override {
165 if (0 == strcmp(key, kEndPage)) {
166 this->removeAll();
167 if (fIndex < fCount) {
168 fDst[fIndex].fPicture = fRecorder.finishRecordingAsPicture();
169 ++fIndex;
170 }
171 this->nextCanvas();
172 } else {
173 this->SkNWayCanvas::onDrawAnnotation(r, key, d);
174 }
175 }
176};
177} // namespace
178
179bool SkMultiPictureDocumentRead(SkStreamSeekable* stream,
180 SkDocumentPage* dstArray,
Mike Reed47fdf6c2017-12-20 14:12:07 -0500181 int dstArrayCount,
182 const SkDeserialProcs* procs) {
Hal Canary45cde312017-04-03 16:06:42 -0400183 if (!SkMultiPictureDocumentReadPageSizes(stream, dstArray, dstArrayCount)) {
184 return false;
185 }
Hal Canaryfafe1352017-04-11 12:12:02 -0400186 SkSize joined = {0.0f, 0.0f};
Hal Canary45cde312017-04-03 16:06:42 -0400187 for (int i = 0; i < dstArrayCount; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500188 joined = SkSize{std::max(joined.width(), dstArray[i].fSize.width()),
189 std::max(joined.height(), dstArray[i].fSize.height())};
Hal Canary45cde312017-04-03 16:06:42 -0400190 }
191
Mike Reed47fdf6c2017-12-20 14:12:07 -0500192 auto picture = SkPicture::MakeFromStream(stream, procs);
Hal Canary45cde312017-04-03 16:06:42 -0400193
194 PagerCanvas canvas(joined.toCeil(), dstArray, dstArrayCount);
195 // Must call playback(), not drawPicture() to reach
196 // PagerCanvas::onDrawAnnotation().
197 picture->playback(&canvas);
198 if (canvas.fIndex != dstArrayCount) {
Nathaniel Nifong0426c382019-06-21 11:09:19 -0400199 SkDEBUGF("Malformed SkMultiPictureDocument: canvas.fIndex=%d dstArrayCount=%d\n",
200 canvas.fIndex, dstArrayCount);
Hal Canary45cde312017-04-03 16:06:42 -0400201 }
202 return true;
203}