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
diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp
index 0706ab1..f92b1de 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 @@
     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 @@
     REPORTER_ASSERT(reporter, 0 == pixelErrors);
 }
 
-
 typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
 
 /**
@@ -160,36 +144,169 @@
     }
 }
 
-
 ////////////////////////////////////////////////////////////////////////////////
-/**
- *  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);
-}
-DEF_TEST(LazyPixelRef, reporter) {
-    test_three_encodings(reporter, install_skLazyPixelRef);
-}
-
-////////////////////////////////////////////////////////////////////////////////
-/**
- *  This checks to see that a SkCachingPixelRef works as advertised.
- */
 static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
     return SkCachingPixelRef::Install(
         SkNEW_ARGS(SkDecodingImageGenerator, (encoded)), dst);
 }
-DEF_TEST(CachingPixelRef, reporter) {
-    test_three_encodings(reporter, install_skCachingPixelRef);
+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 SkDecodingImageGenerator works as advertised.
+ *  This checks to see that a SkCachingPixelRef and a
+ *  SkDiscardablePixelRef works as advertised with a
+ *  SkDecodingImageGenerator.
  */
 DEF_TEST(DecodingImageGenerator, reporter) {
-    test_three_encodings(reporter, SkDecodingImageGenerator::Install);
+    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 tests the basic functionality of SkDiscardablePixelRef with a
+ *  basic SkImageGenerator implementation and several
+ *  SkDiscardableMemory::Factory choices.
+ */
+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).
+}
+////////////////////////////////////////////////////////////////////////////////
+