Add test that confirms Pictures don't leak pixel refs.
Have been investigating memory leak failures that show SkRecords'
ImmutableBitmap copy leaking. I think this test proves its not
an obvious problem with ImmutableBitmap or SkRecord.
http://build.chromium.org/p/chromium.memory/builders/Linux%20Chromium%20OS%20ASan%20LSan%20Tests%20%282%29/builds/2509/steps/interactive_ui_tests/logs/stdio
BUG=skia:
R=bungeman@google.com, mtklein@google.com
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/516593002
diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp
index a189c1a..627e96e 100644
--- a/tests/PictureTest.cpp
+++ b/tests/PictureTest.cpp
@@ -19,6 +19,7 @@
#include "SkPicture.h"
#include "SkPictureRecorder.h"
#include "SkPictureUtils.h"
+#include "SkPixelRef.h"
#include "SkRRect.h"
#include "SkRandom.h"
#include "SkShader.h"
@@ -1916,3 +1917,31 @@
picture->draw(&small);
REPORTER_ASSERT(r, bbh.searchCalls == 1);
}
+
+DEF_TEST(Picture_BitmapLeak, r) {
+ SkBitmap mut, immut;
+ mut.allocN32Pixels(300, 200);
+ immut.allocN32Pixels(300, 200);
+ immut.setImmutable();
+ SkASSERT(!mut.isImmutable());
+ SkASSERT(immut.isImmutable());
+
+ // No one can hold a ref on our pixels yet.
+ REPORTER_ASSERT(r, mut.pixelRef()->unique());
+ REPORTER_ASSERT(r, immut.pixelRef()->unique());
+
+ SkPictureRecorder rec;
+ SkCanvas* canvas = rec.beginRecording(1920, 1200);
+ canvas->drawBitmap(mut, 0, 0);
+ canvas->drawBitmap(immut, 800, 600);
+ SkAutoTDelete<const SkPicture> pic(rec.endRecording());
+
+ // The picture shares the immutable pixels but copies the mutable ones.
+ REPORTER_ASSERT(r, mut.pixelRef()->unique());
+ REPORTER_ASSERT(r, !immut.pixelRef()->unique());
+
+ // When the picture goes away, it's just our bitmaps holding the refs.
+ pic.reset(NULL);
+ REPORTER_ASSERT(r, mut.pixelRef()->unique());
+ REPORTER_ASSERT(r, immut.pixelRef()->unique());
+}