Revert "Revert of https://codereview.chromium.org/112783004/"

This reverts commit 3293fe57a7507541e9040eab880dcd82c43881f4.

Conflicts:
	src/core/SkScaledImageCache.cpp

BUG=

Review URL: https://codereview.chromium.org/108613005

git-svn-id: http://skia.googlecode.com/svn/trunk@12663 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp
index e93882a..da88ca5 100644
--- a/src/core/SkPixelRef.cpp
+++ b/src/core/SkPixelRef.cpp
@@ -182,6 +182,10 @@
 
         if (1 == ++fLockCount) {
             fPixels = this->onLockPixels(&fColorTable);
+            // If onLockPixels failed, it will return NULL
+            if (NULL == fPixels) {
+                fColorTable = NULL;
+            }
         }
     }
 }
@@ -194,9 +198,14 @@
 
         SkASSERT(fLockCount > 0);
         if (0 == --fLockCount) {
-            this->onUnlockPixels();
-            fPixels = NULL;
-            fColorTable = NULL;
+            // don't call onUnlockPixels unless onLockPixels succeeded
+            if (fPixels) {
+                this->onUnlockPixels();
+                fPixels = NULL;
+                fColorTable = NULL;
+            } else {
+                SkASSERT(NULL == fColorTable);
+            }
         }
     }
 }
diff --git a/src/core/SkScaledImageCache.cpp b/src/core/SkScaledImageCache.cpp
index 7c8b664..2529b5f 100644
--- a/src/core/SkScaledImageCache.cpp
+++ b/src/core/SkScaledImageCache.cpp
@@ -209,7 +209,6 @@
     SkDiscardableMemory* fDM;
     size_t               fRB;
     bool                 fFirstTime;
-    bool                 fIsLocked;
 
     typedef SkPixelRef INHERITED;
 };
@@ -225,7 +224,6 @@
 
     SkASSERT(dm->data());
     fFirstTime = true;
-    fIsLocked = false;
 }
 
 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() {
@@ -235,6 +233,7 @@
 void* SkOneShotDiscardablePixelRef::onLockPixels(SkColorTable** ctable) {
     if (fFirstTime) {
         // we're already locked
+        SkASSERT(fDM->data());
         fFirstTime = false;
         return fDM->data();
     }
@@ -255,10 +254,7 @@
 
 void SkOneShotDiscardablePixelRef::onUnlockPixels() {
     SkASSERT(!fFirstTime);
-    if (fIsLocked) {
-        fIsLocked = false;
-        fDM->unlock();
-    }
+    fDM->unlock();
 }
 
 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const {
diff --git a/src/images/SkImageRef_ashmem.cpp b/src/images/SkImageRef_ashmem.cpp
index 0dba1d1..9933ca9 100644
--- a/src/images/SkImageRef_ashmem.cpp
+++ b/src/images/SkImageRef_ashmem.cpp
@@ -1,10 +1,10 @@
-
 /*
  * Copyright 2011 Google Inc.
  *
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file.
  */
+
 #include "SkImageRef_ashmem.h"
 #include "SkImageDecoder.h"
 #include "SkFlattenableBuffers.h"
diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp
index 667a949..b7eaf57 100644
--- a/src/lazy/SkCachingPixelRef.cpp
+++ b/src/lazy/SkCachingPixelRef.cpp
@@ -90,9 +90,7 @@
 }
 
 void SkCachingPixelRef::onUnlockPixels() {
-    if (fScaledCacheId != NULL) {
-        SkScaledImageCache::Unlock(
-            static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
-        fScaledCacheId = NULL;
-    }
+    SkASSERT(fScaledCacheId != NULL);
+    SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
+    fScaledCacheId = NULL;
 }
diff --git a/src/lazy/SkDiscardablePixelRef.cpp b/src/lazy/SkDiscardablePixelRef.cpp
index 0b193ad..f551436 100644
--- a/src/lazy/SkDiscardablePixelRef.cpp
+++ b/src/lazy/SkDiscardablePixelRef.cpp
@@ -56,14 +56,15 @@
     }
     void* pixels = fDiscardableMemory->data();
     if (!fGenerator->getPixels(fInfo, pixels, fRowBytes)) {
-        return NULL;  // TODO(halcanary) Find out correct thing to do.
+        fDiscardableMemory->unlock();
+        SkDELETE(fDiscardableMemory);
+        fDiscardableMemory = NULL;
+        return NULL;
     }
     return pixels;
 }
 void SkDiscardablePixelRef::onUnlockPixels() {
-    if (fDiscardableMemory != NULL) {
-        fDiscardableMemory->unlock();
-    }
+    fDiscardableMemory->unlock();
 }
 
 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator,