Chromium Code Reviews| Index: src/core/SkMultiPictureDocument.cpp |
| diff --git a/src/core/SkMultiPictureDocument.cpp b/src/core/SkMultiPictureDocument.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c9bbab030c3ba8818ded5e528f1922109dbb040 |
| --- /dev/null |
| +++ b/src/core/SkMultiPictureDocument.cpp |
| @@ -0,0 +1,100 @@ |
| +/* |
| + * Copyright 2016 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkMultiPictureDocument.h" |
| +#include "SkMultiPictureDocumentPriv.h" |
| +#include "SkPicture.h" |
| +#include "SkPictureRecorder.h" |
| +#include "SkStream.h" |
| + |
| +/* |
| + File format: |
| + BEGINNING_OF_FILE: |
| + kMagic |
| + uint32_t version_number |
| + uint32_t page_count |
| + { |
| + uint64_t offset |
| + float sizeX |
| + float sizeY |
| + } * page_count |
| + FIRST_OFFSET: |
| + skp file |
| + SECOND_OFFSET: |
| + skp file |
| + ... |
| + LAST_OFFSET: |
| + skp file |
| + "\nEndOfMultiPicture\n" |
| +*/ |
| + |
| +namespace { |
| +static SkCanvas* trim(SkCanvas* canvas, |
| + const SkRect& size, |
| + const SkRect* trimBox) { |
| + if (trimBox && *trimBox != size) { |
|
tomhudson
2016/06/01 16:55:00
I can't intuit your intent here. This is surprisin
hal.canary
2016/06/01 18:09:16
Only trim if necessary. SkPictures already have a
|
| + canvas->clipRect(*trimBox); |
| + canvas->translate(trimBox->x(), trimBox->y()); |
| + } |
| + return canvas; |
| +} |
| + |
| +struct NullWStream : public SkWStream { |
| + NullWStream() : fN(0) {} |
| + bool write(const void*, size_t n) override { |
| + fN += n; |
| + return true; |
| + } |
| + size_t bytesWritten() const override { return fN; } |
| + size_t fN; |
| +}; |
| + |
| +struct MultiPictureDocument final : public SkDocument { |
| + SkPictureRecorder fPictureRecorder; |
| + SkTArray<sk_sp<SkPicture>> fPages; |
| + MultiPictureDocument(SkWStream* s, void (*d)(SkWStream*, bool)) |
| + : SkDocument(s, d) {} |
| + ~MultiPictureDocument() { this->close(); } |
| + |
| + SkCanvas* onBeginPage(SkScalar w, SkScalar h, const SkRect& c) override { |
| + return trim(fPictureRecorder.beginRecording(w, h), SkRect::MakeWH(w, h), |
| + &c); |
| + } |
| + void onEndPage() override { |
| + fPages.emplace_back(fPictureRecorder.finishRecordingAsPicture()); |
| + } |
| + bool onClose(SkWStream* wStream) override { |
| + SkASSERT(wStream); |
| + bool good = true; |
| + good &= wStream->writeText(SkMultiPictureDocumentProtocol::kMagic); |
|
tomhudson
2016/06/01 16:55:00
&&=?
hal.canary
2016/06/01 18:09:16
Logically, yes. C++ does not have a `&&=` operato
|
| + good &= wStream->write32(SkToU32(1)); // version |
| + good &= wStream->write32(SkToU32(fPages.count())); |
| + uint64_t offset = wStream->bytesWritten(); |
| + offset += fPages.count() * sizeof(SkMultiPictureDocumentProtocol::Entry); |
| + for (const auto& page : fPages) { |
| + SkRect cullRect = page->cullRect(); |
| + SkMultiPictureDocumentProtocol::Entry entry{ |
| + offset, (float)cullRect.right(), (float)cullRect.bottom()}; |
|
tomhudson
2016/06/01 16:54:59
It's an inviolable fact that cullRects are always
hal.canary
2016/06/01 18:09:16
If the picture was created by the code in the othe
|
| + good &= wStream->write(&entry, sizeof(entry)); |
| + NullWStream buffer; |
| + page->serialize(&buffer); |
| + offset += buffer.bytesWritten(); |
| + } |
| + for (const auto& page : fPages) { |
| + page->serialize(wStream); |
| + } |
| + good &= wStream->writeText("\nEndOfMultiPicture\n"); |
|
tomhudson
2016/06/01 16:54:59
Any reason not to move this to another constexpr o
hal.canary
2016/06/01 18:09:16
I actually use it. It's there more for human debu
hal.canary
2016/06/02 13:24:04
I DON'T actually use it. It's there more for huma
|
| + fPages.reset(); |
| + return good; |
| + } |
| + void onAbort() override { fPages.reset(); } |
| +}; |
| +} |
| + |
| +sk_sp<SkDocument> SkMakeMultiPictureDocument(SkWStream* wStream) { |
| + return sk_make_sp<MultiPictureDocument>(wStream, nullptr); |
| +} |