Fix object logging

Change-Id: I1731637412cf9e894f2b73dc32ab3370f6467160
diff --git a/rsContext.cpp b/rsContext.cpp
index f8088b0..0bf9d9d 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -253,7 +253,6 @@
 
     rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
     rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
-    rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
     rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
     rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
     rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
diff --git a/rsContext.h b/rsContext.h
index dac276f..5965cae 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -216,7 +216,6 @@
     struct {
         bool mLogTimes;
         bool mLogScripts;
-        bool mLogObjects;
         bool mLogShaders;
         bool mLogShadersAttr;
         bool mLogShadersUniforms;
diff --git a/rsDebugHelper.h b/rsDebugHelper.h
index a70fe4d..f81e258 100644
--- a/rsDebugHelper.h
+++ b/rsDebugHelper.h
@@ -20,42 +20,36 @@
 #include "rsUtils.h"
 #include "rsInternalDefines.h"
 
-#ifndef RS_SERVER
-// This shouldn't ever be defined with RS_SERVER
-#define RS_OBJECT_DEBUG 0
-#endif
-
-#if RS_OBJECT_DEBUG
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
 #include <utils/CallStack.h>
 #endif
 
 namespace android {
 namespace renderscript {
 
-#if RS_OBJECT_DEBUG
 
 class DebugHelper {
 public:
     DebugHelper() {
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
         mStack.update(2);
+#endif
     }
 
     void dump() {
-        mStack.dump();
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
+        String8 s = mStack.toString();
+        ALOGV("%s", s.string());
+        //mStack.dump();
+#endif
     }
 
 private:
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
     CallStack mStack;
-};
-
-#else
-
-class DebugHelper {
-public:
-    DebugHelper() { }
-};
-
 #endif
+};
+
 
 }  // namespace renderscript
 }  // namespace android
diff --git a/rsObjectBase.cpp b/rsObjectBase.cpp
index dc94bcc..4992633 100644
--- a/rsObjectBase.cpp
+++ b/rsObjectBase.cpp
@@ -16,6 +16,7 @@
 
 #include "rsObjectBase.h"
 #include "rsContext.h"
+#include "rsDebugHelper.h"
 
 using namespace android;
 using namespace android::renderscript;
@@ -31,22 +32,30 @@
     mDH = nullptr;
     mName = nullptr;
 
-#if RS_OBJECT_DEBUG
-    mDH = new DebugHelper();
-#endif
+    if (gDebugStacks || gDebugReferences || gDebugLeaks) {
+        mDH = new DebugHelper();
+    }
 
     rsAssert(rsc);
     add();
-    //ALOGV("ObjectBase %p con", this);
+
+    if (gDebugLifetime || gDebugReferences) {
+        ALOGV("ObjectBase constructed %p", this);
+    }
 }
 
 ObjectBase::~ObjectBase() {
-    //ALOGE("~ObjectBase %p  ref %i,%i", this, mUserRefCount, mSysRefCount);
-#if RS_OBJECT_DEBUG
-    mDH->dump();
-    delete mDH;
-    mDH = nullptr;
-#endif
+    if (gDebugLifetime || gDebugReferences) {
+        ALOGV("ObjectBase destroyed %p   refs %i %i", this, mUserRefCount, mSysRefCount);
+    }
+
+    if (gDebugStacks || gDebugReferences || gDebugLeaks) {
+        if (gDebugStacks || gDebugReferences) {
+            mDH->dump();
+        }
+        delete mDH;
+        mDH = nullptr;
+    }
 
     free(const_cast<char *>(mName));
 
@@ -76,12 +85,16 @@
 
 void ObjectBase::incUserRef() const {
     __sync_fetch_and_add(&mUserRefCount, 1);
-    //ALOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
+    if (gDebugReferences) {
+        ALOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
+    }
 }
 
 void ObjectBase::incSysRef() const {
     __sync_fetch_and_add(&mSysRefCount, 1);
-    //ALOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
+    if (gDebugReferences) {
+        ALOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
+    }
 }
 
 void ObjectBase::preDestroy() const {
@@ -116,12 +129,12 @@
 
 bool ObjectBase::decUserRef() const {
     rsAssert(mUserRefCount > 0);
-#if RS_OBJECT_DEBUG
-    //ALOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
-    if (mUserRefCount <= 0) {
-        mDH->dump();
+    if (gDebugReferences) {
+        ALOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
+        if (mUserRefCount <= 0) {
+            mDH->dump();
+        }
     }
-#endif
 
 
     if ((__sync_fetch_and_sub(&mUserRefCount, 1) <= 1)) {
@@ -134,7 +147,10 @@
 }
 
 bool ObjectBase::zeroUserRef() const {
-    //ALOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
+    if (gDebugReferences) {
+        ALOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
+    }
+
     __sync_and_and_fetch(&mUserRefCount, 0);
     if (mSysRefCount <= 0) {
         return checkDelete(this);
@@ -143,7 +159,10 @@
 }
 
 bool ObjectBase::decSysRef() const {
-    //ALOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
+    if (gDebugReferences) {
+        ALOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
+    }
+
     rsAssert(mSysRefCount > 0);
     if ((__sync_fetch_and_sub(&mSysRefCount, 1) <= 1)) {
         __sync_synchronize();
@@ -208,7 +227,7 @@
 }
 
 void ObjectBase::zeroAllUserRef(Context *rsc) {
-    if (rsc->props.mLogObjects) {
+    if (gDebugReferences || gDebugLeaks) {
         ALOGV("Forcing release of all outstanding user refs.");
     }
 
@@ -226,14 +245,14 @@
         }
     }
 
-    if (rsc->props.mLogObjects) {
+    if (gDebugReferences || gDebugLeaks) {
         ALOGV("Objects remaining.");
         dumpAll(rsc);
     }
 }
 
 void ObjectBase::freeAllChildren(Context *rsc) {
-    if (rsc->props.mLogObjects) {
+    if (gDebugReferences) {
         ALOGV("Forcing release of all child objects.");
     }
 
@@ -248,7 +267,7 @@
         }
     }
 
-    if (rsc->props.mLogObjects) {
+    if (gDebugReferences) {
         ALOGV("Objects remaining.");
         dumpAll(rsc);
     }
@@ -262,6 +281,9 @@
     while (o) {
         ALOGV(" Object %p", o);
         o->dumpLOGV("  ");
+        if (o->mDH != nullptr) {
+            o->mDH->dump();
+        }
         o = o->mNext;
     }
 
diff --git a/rsObjectBase.h b/rsObjectBase.h
index cd1b16e..c51d85c 100644
--- a/rsObjectBase.h
+++ b/rsObjectBase.h
@@ -19,7 +19,7 @@
 
 #include "rsUtils.h"
 #include "rsDefines.h"
-#include "rsDebugHelper.h"
+#include "rsInternalDefines.h"
 
 namespace android {
 namespace renderscript {
@@ -30,6 +30,11 @@
 // An element is a group of Components that occupies one cell in a structure.
 class ObjectBase {
 public:
+    static const bool gDebugStacks = false;
+    static const bool gDebugReferences = false;
+    static const bool gDebugLeaks = false;
+    static const bool gDebugLifetime = false;
+
     ObjectBase(Context *rsc);
 
     void incSysRef() const;
@@ -89,7 +94,7 @@
     mutable const ObjectBase * mPrev;
     mutable const ObjectBase * mNext;
 
-    DebugHelper *mDH;
+    class DebugHelper *mDH;
 };
 
 template<class T>