Chromium Code Reviews| Index: tests/SkResourceCacheTest.cpp |
| diff --git a/tests/SkResourceCacheTest.cpp b/tests/SkResourceCacheTest.cpp |
| index 4432b87d3b3b7af15fe5c5334b7808013ac811c6..55e20aaf080fee3f6849c0089cd5e70d3b0fdf65 100644 |
| --- a/tests/SkResourceCacheTest.cpp |
| +++ b/tests/SkResourceCacheTest.cpp |
| @@ -10,6 +10,8 @@ |
| #include "SkCanvas.h" |
| #include "SkDiscardableMemoryPool.h" |
| #include "SkGraphics.h" |
| +#include "SkPicture.h" |
| +#include "SkPictureRecorder.h" |
| #include "SkResourceCache.h" |
| #include "SkSurface.h" |
| @@ -219,3 +221,59 @@ DEF_TEST(BitmapCache_discarded_bitmap, reporter) { |
| test_bitmap_notify(reporter, cache); |
| test_mipmap_notify(reporter, cache); |
| } |
| + |
| +static void test_discarded_image(skiatest::Reporter* reporter, const SkMatrix& transform, |
|
reed1
2015/09/18 14:32:53
How about a block-comment here or on the main test
f(malita)
2015/09/18 15:00:40
Done.
|
| + SkImage* (*buildImage)()) { |
| + SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); |
| + SkCanvas* canvas = surface->getCanvas(); |
| + |
| + // SkBitmapCache is global, so other threads could be evicting our bitmaps. Loop a few times |
| + // to mitigate this risk. |
|
tomhudson
2015/09/18 13:30:54
Not sure how this is safe from flake?
f(malita)
2015/09/18 13:44:24
Note that we don't assert image pixels are cached,
mtklein
2015/09/18 13:56:15
DM has a way to group tasks that cannot run concur
|
| + const unsigned kRepeatCount = 42; |
| + for (unsigned i = 0; i < kRepeatCount; ++i) { |
| + SkAutoCanvasRestore acr(canvas, true); |
| + |
| + SkAutoTUnref<SkImage> image(buildImage()); |
| + |
| + // always use high quality to ensure caching when scaled |
| + SkPaint paint; |
| + paint.setFilterQuality(kHigh_SkFilterQuality); |
| + |
| + // draw the image (with a transform, to tickle different code paths) to ensure |
| + // any associated resources get cached |
| + canvas->concat(transform); |
| + canvas->drawImage(image, 0, 0, &paint); |
| + |
| + auto imageId = image->uniqueID(); |
| + |
| + // delete the image |
| + image.reset(nullptr); |
| + |
| + // all resources should have been purged |
| + SkBitmap result; |
| + REPORTER_ASSERT(reporter, !SkBitmapCache::Find(imageId, &result)); |
| + } |
| +} |
| + |
| +DEF_TEST(BitmapCache_discarded_image, reporter) { |
| + const SkMatrix xforms[] = { |
| + SkMatrix::MakeScale(1, 1), |
| + SkMatrix::MakeScale(1.7f, 0.5f), |
| + }; |
| + |
| + for (size_t i = 0; i < SK_ARRAY_COUNT(xforms); ++i) { |
| + test_discarded_image(reporter, xforms[i], []() { |
| + SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10)); |
| + surface->getCanvas()->clear(SK_ColorCYAN); |
| + return surface->newImageSnapshot(); |
| + }); |
| + |
| + test_discarded_image(reporter, xforms[i], []() { |
| + SkPictureRecorder recorder; |
| + SkCanvas* canvas = recorder.beginRecording(10, 10); |
| + canvas->clear(SK_ColorCYAN); |
| + SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| + return SkImage::NewFromPicture(picture, SkISize::Make(10, 10), nullptr, nullptr); |
| + }); |
| + } |
| +} |