diff options
author | Mike Klein <mtklein@chromium.org> | 2018-01-26 09:49:48 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-01-28 00:18:37 +0000 |
commit | fbe6620284a5df423f00ef5b935a24f0682cba29 (patch) | |
tree | 5446b5d72fdf9aa6d67696d4e4d7f389ae99628b | |
parent | d3c1b84a6e26d9f6acd09c221048c4f5ddbd9bbb (diff) |
add SkPicture::MakePlaceholder()
Bug: skia:7536
Change-Id: I6ca7c680ef4fd69419254dc7f1af27343dbb8e89
Reviewed-on: https://skia-review.googlesource.com/99664
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Klein <mtklein@chromium.org>
-rw-r--r-- | include/core/SkPicture.h | 14 | ||||
-rw-r--r-- | src/core/SkPicture.cpp | 16 | ||||
-rw-r--r-- | tests/PictureTest.cpp | 19 |
3 files changed, 46 insertions, 3 deletions
diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h index 4887db928f..b2beb56b86 100644 --- a/include/core/SkPicture.h +++ b/include/core/SkPicture.h @@ -46,7 +46,7 @@ public: /** * Recreate a picture that was serialized into a buffer. If the creation requires bitmap * decoding, the decoder must be set on the SkReadBuffer parameter by calling - * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer(). + * SkReadBuffer::setBitmapDecoder() before calling SkPicture::MakeFromBuffer(). * @param SkReadBuffer Serialized picture data. * @return A new SkPicture representing the serialized data, or NULL if the buffer is * invalid. @@ -91,6 +91,14 @@ public: void serialize(SkWStream*, const SkSerialProcs* = nullptr) const; /** + * Return a placeholder SkPicture. + * This placeholder does not draw anything itself. It has a distinct uniqueID() + * (just like all SkPictures) and will always be visible to SkSerialProcs. + * @param cull the placeholder's dimensions + */ + static sk_sp<SkPicture> MakePlaceholder(SkRect cull); + + /** * Serialize to a buffer. */ void flatten(SkWriteBuffer&) const; @@ -122,8 +130,8 @@ private: /** Return true if the SkStream/Buffer represents a serialized picture, and fills out SkPictInfo. After this function returns, the data source is not rewound so it will have to be manually reset before passing to - CreateFromStream or CreateFromBuffer. Note, CreateFromStream and - CreateFromBuffer perform this check internally so these entry points are + MakeFromStream or MakeFromBuffer. Note, MakeFromStream and + MakeFromBuffer perform this check internally so these entry points are intended for stand alone tools. If false is returned, SkPictInfo is unmodified. */ diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp index a8cb0d7c40..85b22ac20f 100644 --- a/src/core/SkPicture.cpp +++ b/src/core/SkPicture.cpp @@ -316,3 +316,19 @@ void SkPicture::flatten(SkWriteBuffer& buffer) const { } } +sk_sp<SkPicture> SkPicture::MakePlaceholder(SkRect cull) { + struct Placeholder : public SkPicture { + explicit Placeholder(SkRect cull) : fCull(cull) {} + + void playback(SkCanvas*, AbortCallback*) const override { } + + // approximateOpCount() needs to be greater than kMaxPictureOpsToUnrollInsteadOfRef + // in SkCanvas.cpp to avoid that unrolling. SK_MaxS32 can't not be big enough! + int approximateOpCount() const override { return SK_MaxS32; } + size_t approximateBytesUsed() const override { return sizeof(*this); } + SkRect cullRect() const override { return fCull; } + + SkRect fCull; + }; + return sk_make_sp<Placeholder>(cull); +} diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp index 714338f9d1..7acce49943 100644 --- a/tests/PictureTest.cpp +++ b/tests/PictureTest.cpp @@ -834,3 +834,22 @@ DEF_TEST(Picture_RecordsFlush, r) { auto back = SkPicture::MakeFromData(skp->data(), skp->size()); REPORTER_ASSERT(r, back->approximateOpCount() == pic->approximateOpCount()); } + +DEF_TEST(Placeholder, r) { + SkRect cull = { 0,0, 10,20 }; + + // Each placeholder is unique. + sk_sp<SkPicture> p1 = SkPicture::MakePlaceholder(cull), + p2 = SkPicture::MakePlaceholder(cull); + REPORTER_ASSERT(r, p1->cullRect() == p2->cullRect()); + REPORTER_ASSERT(r, p1->cullRect() == cull); + REPORTER_ASSERT(r, p1->uniqueID() != p2->uniqueID()); + + // Placeholders are never unrolled by SkCanvas (while other small pictures may be). + SkPictureRecorder recorder; + SkCanvas* canvas = recorder.beginRecording(cull); + canvas->drawPicture(p1); + canvas->drawPicture(p2); + sk_sp<SkPicture> pic = recorder.finishRecordingAsPicture(); + REPORTER_ASSERT(r, pic->approximateOpCount() == 2); +} |