Debian Codegraph @

src/utils/SkMultiPictureDocument.cpp

raw ◀ ▶
1 Debian patch touch this file
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
8 #include "include/docs/SkMultiPictureDocument.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkDocument.h"
13 #include "include/core/SkPicture.h"
14 #include "include/core/SkPictureRecorder.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSerialProcs.h"
18 #include "include/core/SkStream.h"
19 #include "include/private/base/SkDebug.h"
20 #include "include/private/base/SkTArray.h"
21 #include "include/private/base/SkTo.h"
22 #include "include/utils/SkNWayCanvas.h"
23 #include "src/utils/SkMultiPictureDocumentPriv.h"
24
25 #include <algorithm>
26 #include <climits>
27 #include <cstdint>
28 #include <cstring>
29 #include <functional>
30 #include <utility>
31
32 using namespace skia_private;
33
34 /*
35 File format:
36 BEGINNING_OF_FILE:
37 kMagic
38 uint32_t version_number (==2)
39 uint32_t page_count
40 {
41 float sizeX
42 float sizeY
43 } * page_count
44 skp file
45 */
46
47 namespace {
48 // The unique file signature for this file type.
49 static constexpr char kMagic[] = "Skia Multi-Picture Doc\n\n";
50
51 static constexpr char kEndPage[] = "SkMultiPictureEndPage";
52
•53 static constexpr uint32_t kVersion = 2;
54
55 static constexpr uint32_t kMaxDimension = 1 << 16;
56
57 static SkSize join(const TArray<SkSize>& sizes) {
58 SkSize joined = {0, 0};
59 for (SkSize s : sizes) {
60 joined = SkSize{std::max(joined.width(), s.width()), std::max(joined.height(), s.height())};
61 }
62 return joined;
63 }
64
65 struct MultiPictureDocument final : public SkDocument {
66 const SkSerialProcs fProcs;
67 SkPictureRecorder fPictureRecorder;
68 SkSize fCurrentPageSize;
69 TArray<sk_sp<SkPicture>> fPages;
70 TArray<SkSize> fSizes;
71 std::function<void(const SkPicture*)> fOnEndPage;
72 MultiPictureDocument(SkWStream* s,
73 const SkSerialProcs* procs,
74 std::function<void(const SkPicture*)> onEndPage)
75 : SkDocument(s)
76 , fProcs(procs ? *procs : SkSerialProcs())
77 , fOnEndPage(std::move(onEndPage)) {}
78
79 ~MultiPictureDocument() override { this->close(); }
80
81 SkCanvas* onBeginPage(SkScalar w, SkScalar h) override {
82 fCurrentPageSize.set(w, h);
83 return fPictureRecorder.beginRecording(w, h);
84 }
85 void onEndPage() override {
86 fSizes.push_back(fCurrentPageSize);
87 sk_sp<SkPicture> lastPage = fPictureRecorder.finishRecordingAsPicture();
88 fPages.push_back(lastPage);
89 if (fOnEndPage) {
90 fOnEndPage(lastPage.get());
91 }
92 }
93 void onClose(SkWStream* wStream) override {
94 SkASSERT(wStream);
95 SkASSERT(wStream->bytesWritten() == 0);
96 wStream->writeText(kMagic);
97 wStream->write32(kVersion);
98 wStream->write32(SkToU32(fPages.size()));
99 for (SkSize s : fSizes) {
100 wStream->write(&s, sizeof(s));
101 }
102 SkSize bigsize = join(fSizes);
103 SkCanvas* c = fPictureRecorder.beginRecording(SkRect::MakeSize(bigsize));
104 for (const sk_sp<SkPicture>& page : fPages) {
105 c->drawPicture(page);
106 // Annotations must include some data.
107 c->drawAnnotation(SkRect::MakeEmpty(), kEndPage, SkData::MakeWithCString("X"));
108 }
109 sk_sp<SkPicture> p = fPictureRecorder.finishRecordingAsPicture();
110 p->serialize(wStream, &fProcs);
111 fPages.clear();
112 fSizes.clear();
113 }
114 void onAbort() override {
115 fPages.clear();
116 fSizes.clear();
117 }
118 };
119
120 struct PagerCanvas : public SkNWayCanvas {
121 SkPictureRecorder fRecorder;
122 SkDocumentPage* fDst;
123 int fCount;
124 int fIndex = 0;
125 PagerCanvas(SkISize wh, SkDocumentPage* dst, int count)
126 : SkNWayCanvas(wh.width(), wh.height()), fDst(dst), fCount(count) {
127 this->nextCanvas();
128 }
129 void nextCanvas() {
130 if (fIndex < fCount) {
131 SkRect bounds = SkRect::MakeSize(fDst[fIndex].fSize);
132 this->addCanvas(fRecorder.beginRecording(bounds));
133 }
134 }
135 void onDrawAnnotation(const SkRect& r, const char* key, SkData* d) override {
136 if (0 == strcmp(key, kEndPage)) {
137 this->removeAll();
138 if (fIndex < fCount) {
139 fDst[fIndex].fPicture = fRecorder.finishRecordingAsPicture();
140 ++fIndex;
141 }
142 this->nextCanvas();
143 } else {
144 this->SkNWayCanvas::onDrawAnnotation(r, key, d);
145 }
146 }
147 };
148
149 } // namespace
150
151 namespace SkMultiPictureDocument {
152 sk_sp<SkDocument> Make(SkWStream* dst,
153 const SkSerialProcs* procs,
154 std::function<void(const SkPicture*)> onEndPage) {
155 return sk_make_sp<MultiPictureDocument>(dst, procs, std::move(onEndPage));
156 }
157
158 int ReadPageCount(SkStreamSeekable* src) {
159 if (!src) {
160 return 0;
161 }
162 src->seek(0);
163 const size_t size = sizeof(kMagic) - 1;
164 char buffer[size];
165 if (size != src->read(buffer, size) || 0 != memcmp(kMagic, buffer, size)) {
166 src = nullptr;
167 return 0;
168 }
169 uint32_t versionNumber;
170 if (!src->readU32(&versionNumber) || versionNumber != kVersion) {
171 return 0;
172 }
173 uint32_t pageCount;
•174 if (!src->readU32(&pageCount) || pageCount > (INT_MAX / sizeof(SkSize))) {
175 return 0;
176 }
177 // leave stream position right here.
178 return SkTo<int>(pageCount);
179 }
180
•181 bool ReadPageSizes(SkStreamSeekable* src,
182 SkDocumentPage* dstArray,
183 int dstArrayCount) {
184 if (!dstArray || dstArrayCount < 1) {
185 return false;
186 }
•187 int pageCount = ReadPageCount(src);
188 if (pageCount < 1 || pageCount != dstArrayCount) {
189 return false;
190 }
•191 if (src->hasLength() && src->getLength() < (static_cast<size_t>(pageCount) * sizeof(SkSize))) {
192 return false; // not enough memory to read all the sizes
193 }
194 for (int i = 0; i < pageCount; ++i) {
195 SkSize& s = dstArray[i].fSize;
•196 if (sizeof(s) != src->read(&s, sizeof(s))) {
197 return false;
198 }
•199 if (s.isEmpty() || s.width() >= kMaxDimension || s.height() >= kMaxDimension) {
200 return false; // invalid sizes
201 }
202 }
203 // leave stream position right here.
204 return true;
205 }
206
207 bool Read(SkStreamSeekable* src,
208 SkDocumentPage* dstArray,
209 int dstArrayCount,
210 const SkDeserialProcs* procs) {
211 if (!ReadPageSizes(src, dstArray, dstArrayCount)) {
212 return false;
213 }
214 SkSize joined = {0.0f, 0.0f};
215 for (int i = 0; i < dstArrayCount; ++i) {
216 joined = SkSize{std::max(joined.width(), dstArray[i].fSize.width()),
217 std::max(joined.height(), dstArray[i].fSize.height())};
218 }
219
220 auto picture = SkPicture::MakeFromStream(src, procs);
221 if (!picture) {
222 return false;
223 }
224
225 PagerCanvas canvas(joined.toCeil(), dstArray, dstArrayCount);
226 // Must call playback(), not drawPicture() to reach
227 // PagerCanvas::onDrawAnnotation().
228 picture->playback(&canvas);
229 if (canvas.fIndex != dstArrayCount) {
230 SkDEBUGF("Malformed SkMultiPictureDocument: canvas.fIndex=%d dstArrayCount=%d\n",
231 canvas.fIndex, dstArrayCount);
232 }
233 return true;
234 }
235 } // namespace SkMultiPictureDocument