From 59d6402ba7dae4ed1e26c155b7c446d6f7ec3527 Mon Sep 17 00:00:00 2001 From: halcanary Date: Tue, 14 Jun 2016 11:06:37 -0700 Subject: SkMultiPictureDocument: don't rely on SkPicture::cullRect SkPicture::cullRect() is not guaranteed to be the same as the bounds passed to SkPictureRecorder::beginRecording(). Review-Url: https://codereview.chromium.org/2067473003 --- src/utils/SkMultiPictureDocument.cpp | 40 +++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/utils/SkMultiPictureDocument.cpp b/src/utils/SkMultiPictureDocument.cpp index 1cbf0aebc1..e4105fa31d 100644 --- a/src/utils/SkMultiPictureDocument.cpp +++ b/src/utils/SkMultiPictureDocument.cpp @@ -5,6 +5,8 @@ * found in the LICENSE file. */ +#include + #include "SkMultiPictureDocument.h" #include "SkMultiPictureDocumentPriv.h" #include "SkPicture.h" @@ -56,18 +58,35 @@ struct NullWStream : public SkWStream { size_t fN; }; +struct Page { + Page(SkSize s, sk_sp c) : fSize(s), fContent(std::move(c)) {} + Page(Page&& that) : fSize(that.fSize), fContent(std::move(that.fContent)) {} + Page(const Page&) = default; + Page& operator=(const Page&) = default; + Page& operator=(Page&& that) { + fSize = that.fSize; + fContent = std::move(that.fContent); + return *this; + } + SkSize fSize; + sk_sp fContent; +}; + struct MultiPictureDocument final : public SkDocument { SkPictureRecorder fPictureRecorder; - SkTArray> fPages; + SkSize fCurrentPageSize; + std::vector fPages; MultiPictureDocument(SkWStream* s, void (*d)(SkWStream*, bool)) : SkDocument(s, d) {} ~MultiPictureDocument() { this->close(); } SkCanvas* onBeginPage(SkScalar w, SkScalar h, const SkRect& c) override { + fCurrentPageSize.set(w, h); return trim(fPictureRecorder.beginRecording(w, h), w, h, c); } void onEndPage() override { - fPages.emplace_back(fPictureRecorder.finishRecordingAsPicture()); + fPages.emplace_back(fCurrentPageSize, + fPictureRecorder.finishRecordingAsPicture()); } bool onClose(SkWStream* wStream) override { SkASSERT(wStream); @@ -75,29 +94,26 @@ struct MultiPictureDocument final : public SkDocument { bool good = true; good &= wStream->writeText(SkMultiPictureDocumentProtocol::kMagic); good &= wStream->write32(SkToU32(1)); // version - good &= wStream->write32(SkToU32(fPages.count())); + good &= wStream->write32(SkToU32(fPages.size())); uint64_t offset = wStream->bytesWritten(); - offset += fPages.count() * sizeof(SkMultiPictureDocumentProtocol::Entry); + offset += fPages.size() * sizeof(SkMultiPictureDocumentProtocol::Entry); for (const auto& page : fPages) { - SkRect cullRect = page->cullRect(); - // We recorded a picture at the origin. - SkASSERT(cullRect.x() == 0 && cullRect.y() == 0); SkMultiPictureDocumentProtocol::Entry entry{ - offset, (float)cullRect.right(), (float)cullRect.bottom()}; + offset, page.fSize.width(), page.fSize.height()}; good &= wStream->write(&entry, sizeof(entry)); NullWStream buffer; - page->serialize(&buffer); + page.fContent->serialize(&buffer); offset += buffer.bytesWritten(); } for (const auto& page : fPages) { - page->serialize(wStream); + page.fContent->serialize(wStream); } SkASSERT(wStream->bytesWritten() == offset); good &= wStream->writeText("\nEndOfMultiPicture\n"); - fPages.reset(); + fPages.clear(); return good; } - void onAbort() override { fPages.reset(); } + void onAbort() override { fPages.clear(); } }; } -- cgit v1.2.3