Fix truncated webp images DO NOT MERGE am: 99740b3bb9
am: ffc7e57387

Change-Id: I189f273c58239a2ef55f6f5cb145b8f386dfbc26
diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h
index 9d1e5f1..0be2562 100644
--- a/include/core/SkRefCnt.h
+++ b/include/core/SkRefCnt.h
@@ -136,6 +136,28 @@
     null in on each side of the assignment, and ensuring that ref() is called
     before unref(), in case the two pointers point to the same object.
  */
+
+#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || defined(SK_DEBUG)
+// This version heuristically detects data races, since those otherwise result
+// in redundant reference count decrements, which are exceedingly
+// difficult to debug.
+
+#define SkRefCnt_SafeAssign(dst, src)   \
+    do {                                \
+        typedef typename std::remove_reference<decltype(dst)>::type \
+                SkRefCntPtrT;  \
+        SkRefCntPtrT old_dst = *const_cast<SkRefCntPtrT volatile *>(&dst); \
+        if (src) src->ref();            \
+        if (old_dst) old_dst->unref();          \
+        if (old_dst != *const_cast<SkRefCntPtrT volatile *>(&dst)) { \
+            SkDebugf("Detected racing Skia calls at %s:%d\n", \
+                    __FILE__, __LINE__); \
+        } \
+        dst = src;                      \
+    } while (0)
+
+#else /* !(SK_BUILD_FOR_ANDROID_FRAMEWORK || SK_DEBUG) */
+
 #define SkRefCnt_SafeAssign(dst, src)   \
     do {                                \
         if (src) src->ref();            \
@@ -143,6 +165,8 @@
         dst = src;                      \
     } while (0)
 
+#endif
+
 
 /** Call obj->ref() and return obj. The obj must not be nullptr.
  */