Call .rs.dtor() when tearing down Scripts.

BUG=5186750

This allows us to properly reference count any globals (static or extern) that
need to potentially be cleaned up.

Change-Id: I03d2c38c1e7a4ca96c40003d2eeecb6f395d5835
diff --git a/driver/rsdBcc.cpp b/driver/rsdBcc.cpp
index 44ea79c..0755fb7 100644
--- a/driver/rsdBcc.cpp
+++ b/driver/rsdBcc.cpp
@@ -37,6 +37,7 @@
 struct DrvScript {
     int (*mRoot)();
     void (*mInit)();
+    void (*mFreeChildren)();
 
     BCCScriptRef mBccScript;
 
@@ -125,6 +126,7 @@
 
     drv->mRoot = reinterpret_cast<int (*)()>(bccGetFuncAddr(drv->mBccScript, "root"));
     drv->mInit = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, "init"));
+    drv->mFreeChildren = reinterpret_cast<void (*)()>(bccGetFuncAddr(drv->mBccScript, ".rs.dtor"));
 
     exportFuncCount = drv->ME->getExportFuncCount();
     if (exportFuncCount > 0) {
@@ -430,6 +432,13 @@
     }
 }
 
+void rsdScriptInvokeFreeChildren(const Context *dc, Script *script) {
+    DrvScript *drv = (DrvScript *)script->mHal.drv;
+
+    if (drv->mFreeChildren) {
+        drv->mFreeChildren();
+    }
+}
 
 void rsdScriptInvokeFunction(const Context *dc, Script *script,
                             uint32_t slot,
diff --git a/driver/rsdBcc.h b/driver/rsdBcc.h
index 67929bc..5f83ed2 100644
--- a/driver/rsdBcc.h
+++ b/driver/rsdBcc.h
@@ -43,6 +43,8 @@
                         android::renderscript::Script *script);
 void rsdScriptInvokeInit(const android::renderscript::Context *dc,
                          android::renderscript::Script *script);
+void rsdScriptInvokeFreeChildren(const android::renderscript::Context *dc,
+                                 android::renderscript::Script *script);
 
 void rsdScriptSetGlobalVar(const android::renderscript::Context *,
                            const android::renderscript::Script *,
diff --git a/driver/rsdCore.cpp b/driver/rsdCore.cpp
index 171d045..a38fff7 100644
--- a/driver/rsdCore.cpp
+++ b/driver/rsdCore.cpp
@@ -60,6 +60,7 @@
         rsdScriptInvokeRoot,
         rsdScriptInvokeForEach,
         rsdScriptInvokeInit,
+        rsdScriptInvokeFreeChildren,
         rsdScriptSetGlobalVar,
         rsdScriptSetGlobalBind,
         rsdScriptSetGlobalObj,
diff --git a/rsScript.cpp b/rsScript.cpp
index f62c72e..93513fe 100644
--- a/rsScript.cpp
+++ b/rsScript.cpp
@@ -72,6 +72,12 @@
     mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
 }
 
+bool Script::freeChildren() {
+    incSysRef();
+    mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
+    return decSysRef();
+}
+
 namespace android {
 namespace renderscript {
 
diff --git a/rsScript.h b/rsScript.h
index c0324dd..d645421 100644
--- a/rsScript.h
+++ b/rsScript.h
@@ -73,6 +73,8 @@
     void setVar(uint32_t slot, const void *val, size_t len);
     void setVarObj(uint32_t slot, ObjectBase *val);
 
+    virtual bool freeChildren();
+
     virtual void runForEach(Context *rsc,
                             const Allocation * ain,
                             Allocation * aout,
diff --git a/rs_hal.h b/rs_hal.h
index 21dff21..b8d7351 100644
--- a/rs_hal.h
+++ b/rs_hal.h
@@ -90,6 +90,7 @@
                               uint32_t usrLen,
                               const RsScriptCall *sc);
         void (*invokeInit)(const Context *rsc, Script *s);
+        void (*invokeFreeChildren)(const Context *rsc, Script *s);
 
         void (*setGlobalVar)(const Context *rsc, const Script *s,
                              uint32_t slot,