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 <vector> |
| 9 |
8 #include "SkMultiPictureDocument.h" | 10 #include "SkMultiPictureDocument.h" |
9 #include "SkMultiPictureDocumentPriv.h" | 11 #include "SkMultiPictureDocumentPriv.h" |
10 #include "SkPicture.h" | 12 #include "SkPicture.h" |
11 #include "SkPictureRecorder.h" | 13 #include "SkPictureRecorder.h" |
12 #include "SkStream.h" | 14 #include "SkStream.h" |
13 | 15 |
14 /* | 16 /* |
15 File format: | 17 File format: |
16 BEGINNING_OF_FILE: | 18 BEGINNING_OF_FILE: |
17 kMagic | 19 kMagic |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 struct NullWStream : public SkWStream { | 51 struct NullWStream : public SkWStream { |
50 NullWStream() : fN(0) {} | 52 NullWStream() : fN(0) {} |
51 bool write(const void*, size_t n) override { | 53 bool write(const void*, size_t n) override { |
52 fN += n; | 54 fN += n; |
53 return true; | 55 return true; |
54 } | 56 } |
55 size_t bytesWritten() const override { return fN; } | 57 size_t bytesWritten() const override { return fN; } |
56 size_t fN; | 58 size_t fN; |
57 }; | 59 }; |
58 | 60 |
| 61 struct Page { |
| 62 Page(SkSize s, sk_sp<SkPicture> c) : fSize(s), fContent(std::move(c)) {} |
| 63 Page(Page&& that) : fSize(that.fSize), fContent(std::move(that.fContent)) {} |
| 64 Page(const Page&) = default; |
| 65 Page& operator=(const Page&) = default; |
| 66 Page& operator=(Page&& that) { |
| 67 fSize = that.fSize; |
| 68 fContent = std::move(that.fContent); |
| 69 return *this; |
| 70 } |
| 71 SkSize fSize; |
| 72 sk_sp<SkPicture> fContent; |
| 73 }; |
| 74 |
59 struct MultiPictureDocument final : public SkDocument { | 75 struct MultiPictureDocument final : public SkDocument { |
60 SkPictureRecorder fPictureRecorder; | 76 SkPictureRecorder fPictureRecorder; |
61 SkTArray<sk_sp<SkPicture>> fPages; | 77 SkSize fCurrentPageSize; |
| 78 std::vector<Page> fPages; |
62 MultiPictureDocument(SkWStream* s, void (*d)(SkWStream*, bool)) | 79 MultiPictureDocument(SkWStream* s, void (*d)(SkWStream*, bool)) |
63 : SkDocument(s, d) {} | 80 : SkDocument(s, d) {} |
64 ~MultiPictureDocument() { this->close(); } | 81 ~MultiPictureDocument() { this->close(); } |
65 | 82 |
66 SkCanvas* onBeginPage(SkScalar w, SkScalar h, const SkRect& c) override { | 83 SkCanvas* onBeginPage(SkScalar w, SkScalar h, const SkRect& c) override { |
| 84 fCurrentPageSize.set(w, h); |
67 return trim(fPictureRecorder.beginRecording(w, h), w, h, c); | 85 return trim(fPictureRecorder.beginRecording(w, h), w, h, c); |
68 } | 86 } |
69 void onEndPage() override { | 87 void onEndPage() override { |
70 fPages.emplace_back(fPictureRecorder.finishRecordingAsPicture()); | 88 fPages.emplace_back(fCurrentPageSize, |
| 89 fPictureRecorder.finishRecordingAsPicture()); |
71 } | 90 } |
72 bool onClose(SkWStream* wStream) override { | 91 bool onClose(SkWStream* wStream) override { |
73 SkASSERT(wStream); | 92 SkASSERT(wStream); |
74 SkASSERT(wStream->bytesWritten() == 0); | 93 SkASSERT(wStream->bytesWritten() == 0); |
75 bool good = true; | 94 bool good = true; |
76 good &= wStream->writeText(SkMultiPictureDocumentProtocol::kMagic); | 95 good &= wStream->writeText(SkMultiPictureDocumentProtocol::kMagic); |
77 good &= wStream->write32(SkToU32(1)); // version | 96 good &= wStream->write32(SkToU32(1)); // version |
78 good &= wStream->write32(SkToU32(fPages.count())); | 97 good &= wStream->write32(SkToU32(fPages.size())); |
79 uint64_t offset = wStream->bytesWritten(); | 98 uint64_t offset = wStream->bytesWritten(); |
80 offset += fPages.count() * sizeof(SkMultiPictureDocumentProtocol::Entry)
; | 99 offset += fPages.size() * sizeof(SkMultiPictureDocumentProtocol::Entry); |
81 for (const auto& page : fPages) { | 100 for (const auto& page : fPages) { |
82 SkRect cullRect = page->cullRect(); | |
83 // We recorded a picture at the origin. | |
84 SkASSERT(cullRect.x() == 0 && cullRect.y() == 0); | |
85 SkMultiPictureDocumentProtocol::Entry entry{ | 101 SkMultiPictureDocumentProtocol::Entry entry{ |
86 offset, (float)cullRect.right(), (float)cullRect.bottom()}; | 102 offset, page.fSize.width(), page.fSize.height()}; |
87 good &= wStream->write(&entry, sizeof(entry)); | 103 good &= wStream->write(&entry, sizeof(entry)); |
88 NullWStream buffer; | 104 NullWStream buffer; |
89 page->serialize(&buffer); | 105 page.fContent->serialize(&buffer); |
90 offset += buffer.bytesWritten(); | 106 offset += buffer.bytesWritten(); |
91 } | 107 } |
92 for (const auto& page : fPages) { | 108 for (const auto& page : fPages) { |
93 page->serialize(wStream); | 109 page.fContent->serialize(wStream); |
94 } | 110 } |
95 SkASSERT(wStream->bytesWritten() == offset); | 111 SkASSERT(wStream->bytesWritten() == offset); |
96 good &= wStream->writeText("\nEndOfMultiPicture\n"); | 112 good &= wStream->writeText("\nEndOfMultiPicture\n"); |
97 fPages.reset(); | 113 fPages.clear(); |
98 return good; | 114 return good; |
99 } | 115 } |
100 void onAbort() override { fPages.reset(); } | 116 void onAbort() override { fPages.clear(); } |
101 }; | 117 }; |
102 } | 118 } |
103 | 119 |
104 sk_sp<SkDocument> SkMakeMultiPictureDocument(SkWStream* wStream) { | 120 sk_sp<SkDocument> SkMakeMultiPictureDocument(SkWStream* wStream) { |
105 return sk_make_sp<MultiPictureDocument>(wStream, nullptr); | 121 return sk_make_sp<MultiPictureDocument>(wStream, nullptr); |
106 } | 122 } |
OLD | NEW |