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,