ensure that we call onUnlock only when we onLock succeeded

BUG=
R=halcanary@google.com, scroggo@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk/src@12642 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/core/SkPixelRef.cpp b/core/SkPixelRef.cpp
index e93882a..da88ca5 100644
--- a/core/SkPixelRef.cpp
+++ b/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/core/SkScaledImageCache.cpp b/core/SkScaledImageCache.cpp
index b3956f4..1b8ad52 100644
--- a/core/SkScaledImageCache.cpp
+++ b/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,21 +233,16 @@
 void* SkOneShotDiscardablePixelRef::onLockPixels(SkColorTable** ctable) {
     if (fFirstTime) {
         // we're already locked
+        SkASSERT(fDM->data());
         fFirstTime = false;
         return fDM->data();
     }
-
-    SkASSERT(!fIsLocked);
-    fIsLocked = fDM->lock();
-    return fIsLocked ? fDM->data() : NULL;
+    return fDM->lock() ? fDM->data() : NULL;
 }
 
 void SkOneShotDiscardablePixelRef::onUnlockPixels() {
     SkASSERT(!fFirstTime);
-    if (fIsLocked) {
-        fIsLocked = false;
-        fDM->unlock();
-    }
+    fDM->unlock();
 }
 
 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const {
diff --git a/images/SkImageRef_ashmem.cpp b/images/SkImageRef_ashmem.cpp
index 0dba1d1..9933ca9 100644
--- a/images/SkImageRef_ashmem.cpp
+++ b/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/lazy/SkCachingPixelRef.cpp b/lazy/SkCachingPixelRef.cpp
index 667a949..b7eaf57 100644
--- a/lazy/SkCachingPixelRef.cpp
+++ b/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/lazy/SkDiscardablePixelRef.cpp b/lazy/SkDiscardablePixelRef.cpp
index 6a9507c..2528a99 100644
--- a/lazy/SkDiscardablePixelRef.cpp
+++ b/lazy/SkDiscardablePixelRef.cpp
@@ -58,9 +58,7 @@
     return pixels;
 }
 void SkDiscardablePixelRef::onUnlockPixels() {
-    if (fDiscardableMemory != NULL) {
-        fDiscardableMemory->unlock();
-    }
+    fDiscardableMemory->unlock();
 }
 
 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator,