Avoid ref-ing AndroidPixelRefs that wrap others in globalRef/Unref

bug:10691404

Only ref the wrapped pixel refs, so that they are the single source of
correct refcount, since the wrapping pixel ref can change out from
underneath an SkBitmap.

Change-Id: I253b8737d2afdcc567a26725fa40b1d9ef0e6a92
diff --git a/core/jni/android/graphics/Graphics.cpp b/core/jni/android/graphics/Graphics.cpp
index 1ff0d635..8cb152d 100644
--- a/core/jni/android/graphics/Graphics.cpp
+++ b/core/jni/android/graphics/Graphics.cpp
@@ -479,7 +479,13 @@
     if (fWrappedPixelRef) {
         // delegate java obj management to the wrapped ref
         fWrappedPixelRef->globalRef(localref);
-    } else if (fOnJavaHeap && sk_atomic_inc(&fGlobalRefCnt) == 0) {
+
+        // Note: we only ref and unref the wrapped AndroidPixelRef so that
+        // bitmap->pixelRef()->globalRef() and globalUnref() can be used in a pair, even if
+        // the bitmap has its underlying AndroidPixelRef swapped out/wrapped
+        return;
+    }
+    if (fOnJavaHeap && sk_atomic_inc(&fGlobalRefCnt) == 0) {
         JNIEnv *env = vm2env(fVM);
 
         // If JNI ref was passed, it is always used
@@ -506,7 +512,9 @@
     if (fWrappedPixelRef) {
         // delegate java obj management to the wrapped ref
         fWrappedPixelRef->globalUnref();
-    } else if (fOnJavaHeap && sk_atomic_dec(&fGlobalRefCnt) == 1) {
+        return;
+    }
+    if (fOnJavaHeap && sk_atomic_dec(&fGlobalRefCnt) == 1) {
         JNIEnv *env = vm2env(fVM);
         if (!fHasGlobalRef) {
             SkDebugf("We don't have a global ref!");