aboutsummaryrefslogtreecommitdiff
path: root/tests/CachedDecodingPixelRefTest.cpp
diff options
context:
space:
mode:
authorhalcanary@google.com <halcanary@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-12-05 18:31:42 +0000
committerhalcanary@google.com <halcanary@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-12-05 18:31:42 +0000
commit2c7c7ee47d75e7815ea8db05e924ab55958cb402 (patch)
treee6b7221c8f6a6d3b9d548c923499f3cf23f79634 /tests/CachedDecodingPixelRefTest.cpp
parent3e0446ccbfbeadd0d02d417cb68e950d6c92f8e1 (diff)
Big Cleanup: SkBitmapFactory, SkLazyPixelRef, SkImageCache
Removed SkBitmapFactory since no clients were using it. New cache selection mechanism can simply pass a SkDiscardableMemory::Factory into the SkDiscardablePixelRef if non-default SkDiscardableMemory should be used. Removed BitmapFactoryTest. SkDiscardableMemory::Factory interface. Android will need this functionality in the future inside their BitmapFactory. Removed SkLazyPixelRef, since it's functionality is now subsumed into SkDiscardablePixelRef. Removed LazyPixelRef test. Modified SkDiscardablePixelRef to optionally allow it to use a SkDiscardableMemory::Factory. This tiny change makes it a replacement for SkLazyPixelRef. This functioanlity is also necessary for moving Android over to SkDiscardablePixelRef from SkImageRef in a later CL. Added a test for this. SkDecodingImageGenerator::Install can optionally pass a factory in to SkDiscardablePixelRef. Removed SkImageCache, SkLruImageCache, and SkPurgeableImageCache. This functionality can be handled much more cleanly by SkDiscardableMemory. New SkDiscardableMemoryPool class to replace SkLruImageCache. In a later CL, we will replace SkImageRef_GlobalPool (used by android) as well. This is a concrete implementation of SkDiscardableMemory::Factory. Added a test for this. modified gm/factory.cpp to remove dependnce on SkBitmapFactory + SkLruImageCache. Now uses SkDecodingImageGenerator + SkDiscardablePixelRef + SkDiscardableMemoryPool. SkImageDecoder::Target replaces SkBitmapFactory::Target. The DecodeMemoryToTarget function may disappear in the future. Moved SkLazyCachingPixelRef::DecodeProc replaces SkBitmapFactory::DecodeProc. This is a short term change, since another CL changes SkLazyCachingPixelRef to use SkImageGenerator instead of DecodeProc. Modified DrawBitmapRectTest to use SkDiscardablePixelRef instead of SkLazyPixelRef. tools/LazyDecodeBitmap.cpp now uses SkDecodingImageGenerator + SkDiscardablePixelRef instead of a SkBitmapFactory. bench_pictures uses the Global SkDiscardableMemoryPool instead of a global gLruImageCache. R=reed@google.com, scroggo@google.com Review URL: https://codereview.chromium.org/103033002 git-svn-id: http://skia.googlecode.com/svn/trunk@12515 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests/CachedDecodingPixelRefTest.cpp')
-rw-r--r--tests/CachedDecodingPixelRefTest.cpp195
1 files changed, 156 insertions, 39 deletions
diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp
index 0706ab1d85..f92b1de1bf 100644
--- a/tests/CachedDecodingPixelRefTest.cpp
+++ b/tests/CachedDecodingPixelRefTest.cpp
@@ -9,12 +9,13 @@
#include "SkCanvas.h"
#include "SkData.h"
#include "SkDecodingImageGenerator.h"
+#include "SkDiscardablePixelRef.h"
+#include "SkDiscardableMemoryPool.h"
#include "SkImageDecoder.h"
-#include "SkImagePriv.h"
-#include "SkLazyPixelRef.h"
#include "SkCachingPixelRef.h"
#include "SkScaledImageCache.h"
#include "SkStream.h"
+#include "SkUtils.h"
#include "Test.h"
#include "TestClassDef.h"
@@ -50,23 +51,7 @@ static SkData* create_data_from_bitmap(const SkBitmap& bm,
return NULL;
}
-/**
- * A simplified version of SkBitmapFactory
- */
-static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc,
- SkData* data,
- SkBitmap* dst) {
- SkImageInfo info;
- if (!proc(data->data(), data->size(), &info, NULL)) {
- return false;
- }
- dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
- info.fHeight, 0, info.fAlphaType);
- SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef,
- (data, proc, NULL)));
- dst->setPixelRef(ref);
- return true;
-}
+////////////////////////////////////////////////////////////////////////////////
static void compare_bitmaps(skiatest::Reporter* reporter,
const SkBitmap& b1, const SkBitmap& b2,
@@ -105,7 +90,6 @@ static void compare_bitmaps(skiatest::Reporter* reporter,
REPORTER_ASSERT(reporter, 0 == pixelErrors);
}
-
typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
/**
@@ -160,36 +144,169 @@ static void test_three_encodings(skiatest::Reporter* reporter,
}
}
-
////////////////////////////////////////////////////////////////////////////////
-/**
- * This checks to see that a SkLazyPixelRef works as advertised.
- */
-static bool install_skLazyPixelRef(SkData* encoded, SkBitmap* dst) {
- static const SkBitmapFactory::DecodeProc decoder =
- &(SkImageDecoder::DecodeMemoryToTarget);
- return simple_bitmap_factory(decoder, encoded, dst);
+static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
+ return SkCachingPixelRef::Install(
+ SkNEW_ARGS(SkDecodingImageGenerator, (encoded)), dst);
}
-DEF_TEST(LazyPixelRef, reporter) {
- test_three_encodings(reporter, install_skLazyPixelRef);
+static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
+ // Use system-default discardable memory.
+ return SkDecodingImageGenerator::Install(encoded, dst, NULL);
}
////////////////////////////////////////////////////////////////////////////////
/**
- * This checks to see that a SkCachingPixelRef works as advertised.
+ * This checks to see that a SkCachingPixelRef and a
+ * SkDiscardablePixelRef works as advertised with a
+ * SkDecodingImageGenerator.
*/
-static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
- return SkCachingPixelRef::Install(
- SkNEW_ARGS(SkDecodingImageGenerator, (encoded)), dst);
-}
-DEF_TEST(CachingPixelRef, reporter) {
+DEF_TEST(DecodingImageGenerator, reporter) {
test_three_encodings(reporter, install_skCachingPixelRef);
+ test_three_encodings(reporter, install_skDiscardablePixelRef);
}
////////////////////////////////////////////////////////////////////////////////
+namespace {
+class TestImageGenerator : public SkImageGenerator {
+public:
+ enum TestType {
+ kFailGetInfo_TestType,
+ kFailGetPixels_TestType,
+ kSucceedGetPixels_TestType,
+ kLast_TestType = kSucceedGetPixels_TestType
+ };
+ static int Width() { return 10; }
+ static int Height() { return 10; }
+ static SkColor Color() { return SK_ColorCYAN; }
+ TestImageGenerator(TestType type, skiatest::Reporter* reporter)
+ : fType(type), fReporter(reporter) {
+ SkASSERT((fType <= kLast_TestType) && (fType >= 0));
+ }
+ ~TestImageGenerator() { }
+ bool getInfo(SkImageInfo* info) SK_OVERRIDE {
+ REPORTER_ASSERT(fReporter, NULL != info);
+ if ((NULL == info) || (kFailGetInfo_TestType == fType)) {
+ return false;
+ }
+ info->fWidth = TestImageGenerator::Width();
+ info->fHeight = TestImageGenerator::Height();
+ info->fColorType = kPMColor_SkColorType;
+ info->fAlphaType = kOpaque_SkAlphaType;
+ return true;
+ }
+ bool getPixels(const SkImageInfo& info,
+ void* pixels,
+ size_t rowBytes) SK_OVERRIDE {
+ REPORTER_ASSERT(fReporter, pixels != NULL);
+ size_t minRowBytes
+ = static_cast<size_t>(info.fWidth * info.bytesPerPixel());
+ REPORTER_ASSERT(fReporter, rowBytes >= minRowBytes);
+ if ((NULL == pixels)
+ || (fType != kSucceedGetPixels_TestType)
+ || (info.fColorType != kPMColor_SkColorType)) {
+ return false;
+ }
+ char* bytePtr = static_cast<char*>(pixels);
+ for (int y = 0; y < info.fHeight; ++y) {
+ sk_memset32(reinterpret_cast<SkColor*>(bytePtr),
+ TestImageGenerator::Color(), info.fWidth);
+ bytePtr += rowBytes;
+ }
+ return true;
+ }
+private:
+ const TestType fType;
+ skiatest::Reporter* const fReporter;
+};
+void CheckTestImageGeneratorBitmap(skiatest::Reporter* reporter,
+ const SkBitmap& bm) {
+ REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
+ REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
+ SkAutoLockPixels autoLockPixels(bm);
+ REPORTER_ASSERT(reporter, NULL != bm.getPixels());
+ if (NULL == bm.getPixels()) {
+ return;
+ }
+ int errors = 0;
+ for (int y = 0; y < bm.height(); ++y) {
+ for (int x = 0; x < bm.width(); ++x) {
+ if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) {
+ ++errors;
+ }
+ }
+ }
+ REPORTER_ASSERT(reporter, 0 == errors);
+}
+
+enum PixelRefType {
+ kSkCaching_PixelRefType,
+ kSkDiscardable_PixelRefType,
+ kLast_PixelRefType = kSkDiscardable_PixelRefType
+};
+void CheckPixelRef(TestImageGenerator::TestType type,
+ skiatest::Reporter* reporter,
+ PixelRefType pixelRefType,
+ SkDiscardableMemory::Factory* factory) {
+ SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType));
+ SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator,
+ (type, reporter)));
+ REPORTER_ASSERT(reporter, gen.get() != NULL);
+ SkBitmap lazy;
+ bool success;
+ if (kSkCaching_PixelRefType == pixelRefType) {
+ // Ignore factory; use global SkScaledImageCache.
+ success = SkCachingPixelRef::Install(gen.detach(), &lazy);
+ } else {
+ success = SkDiscardablePixelRef::Install(gen.detach(), &lazy, factory);
+ }
+ REPORTER_ASSERT(reporter, success
+ == (TestImageGenerator::kFailGetInfo_TestType != type));
+ if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
+ CheckTestImageGeneratorBitmap(reporter, lazy);
+ } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
+ SkAutoLockPixels autoLockPixels(lazy);
+ REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
+ }
+}
+} // namespace
/**
- * This checks to see that a SkDecodingImageGenerator works as advertised.
+ * This tests the basic functionality of SkDiscardablePixelRef with a
+ * basic SkImageGenerator implementation and several
+ * SkDiscardableMemory::Factory choices.
*/
-DEF_TEST(DecodingImageGenerator, reporter) {
- test_three_encodings(reporter, SkDecodingImageGenerator::Install);
+DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
+ CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType,
+ reporter, kSkCaching_PixelRefType, NULL);
+ CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
+ reporter, kSkCaching_PixelRefType, NULL);
+ CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
+ reporter, kSkCaching_PixelRefType, NULL);
+
+ CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType,
+ reporter, kSkDiscardable_PixelRefType, NULL);
+ CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
+ reporter, kSkDiscardable_PixelRefType, NULL);
+ CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
+ reporter, kSkDiscardable_PixelRefType, NULL);
+
+ SkAutoTUnref<SkDiscardableMemoryPool> pool(
+ SkNEW_ARGS(SkDiscardableMemoryPool, (1, NULL)));
+ REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
+ CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
+ reporter, kSkDiscardable_PixelRefType, pool);
+ REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
+ CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
+ reporter, kSkDiscardable_PixelRefType, pool);
+ REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
+
+ SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
+ CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
+ reporter, kSkDiscardable_PixelRefType, globalPool);
+ CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
+ reporter, kSkDiscardable_PixelRefType, globalPool);
+
+ // TODO(halcanary): When ashmem-backed SkDiscardableMemory lands,
+ // test that here (on platforms where it is availible).
}
+////////////////////////////////////////////////////////////////////////////////
+