From 6d20d99daae6691c0659248638053d350c6babf3 Mon Sep 17 00:00:00 2001 From: Mike Klein Date: Thu, 12 Sep 2019 12:51:27 -0500 Subject: [PATCH] test we already have SkPicture::drawsNothing() I was about to add a new call here, but for users like Flutter that are using an R-tree, we may already have a precise drawsNothing() call. There are a couple simple specializations of SkPicture, but they'll already return the right answer: - an SkEmptyPicture will return empty bounds - a single-draw SkMiniPicture will return the bounds of that draw That leaves the general SkBigPicture case. With an R-tree we'll calculate the bounds of every draw in the picture, unioning them up into the cullRect() of the picture itself. So cullRect().isEmpty() should mostly just work as drawsNothing() already? Bug: skia:9411 Change-Id: I5e5dfc21cb7c5c77d173ebee2e91e7bef880367b Reviewed-on: https://skia-review.googlesource.com/c/skia/+/240973 Reviewed-by: Mike Reed Commit-Queue: Mike Klein --- tests/PictureTest.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp index a3bbcbf4c4..d4fdd2d3c2 100644 --- a/tests/PictureTest.cpp +++ b/tests/PictureTest.cpp @@ -879,3 +879,33 @@ DEF_TEST(Picture_empty_serial, reporter) { REPORTER_ASSERT(reporter, pic2); } + +DEF_TEST(Picture_drawsNothing, r) { + // Tests that pic->cullRect().isEmpty() is a good way to test a picture + // recorded with an R-tree draws nothing. + struct { + bool draws_nothing; + void (*fn)(SkCanvas*); + } cases[] = { + { true, [](SkCanvas* c) { } }, + { true, [](SkCanvas* c) { c->save(); c->restore(); } }, + { true, [](SkCanvas* c) { c->save(); c->clipRect({0,0,5,5}); c->restore(); } }, + { true, [](SkCanvas* c) { c->clipRect({0,0,5,5}); } }, + + { false, [](SkCanvas* c) { c->drawRect({0,0,5,5}, SkPaint{}); } }, + { false, [](SkCanvas* c) { c->save(); c->drawRect({0,0,5,5}, SkPaint{}); c->restore(); } }, + { false, [](SkCanvas* c) { + c->drawRect({0,0, 5, 5}, SkPaint{}); + c->drawRect({5,5,10,10}, SkPaint{}); + }}, + }; + + for (const auto& c : cases) { + SkPictureRecorder rec; + SkRTreeFactory factory; + c.fn(rec.beginRecording(10,10, &factory)); + sk_sp pic = rec.finishRecordingAsPicture(); + + REPORTER_ASSERT(r, pic->cullRect().isEmpty() == c.draws_nothing); + } +}