Support LinkRuntimeCallback() with RS compiler.

Change-Id: I28ada4e7c462cb9673de6886d934dce855fac339
diff --git a/cpu_ref/rsCpuCore.cpp b/cpu_ref/rsCpuCore.cpp
index 75fc3f1..06e5ce7 100644
--- a/cpu_ref/rsCpuCore.cpp
+++ b/cpu_ref/rsCpuCore.cpp
@@ -46,8 +46,8 @@
 }
 
 RsdCpuReference * RsdCpuReference::create(Context *rsc, uint32_t version_major,
-                                          uint32_t version_minor, sym_lookup_t lfn,
-                                          script_lookup_t slfn) {
+        uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn,
+        bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
 
     RsdCpuReferenceImpl *cpu = new RsdCpuReferenceImpl(rsc);
     if (!cpu) {
@@ -57,6 +57,9 @@
         delete cpu;
         return NULL;
     }
+
+    cpu->setLinkRuntimeCallback(pLinkRuntimeCallback);
+
     return cpu;
 }
 
@@ -71,6 +74,7 @@
     return tls->mScript;
 }
 
+pthread_key_t RsdCpuReference::getThreadTLSKey(){ return gThreadTLSKey; }
 
 ////////////////////////////////////////////////////////////
 ///
@@ -84,6 +88,7 @@
     memset(&mWorkers, 0, sizeof(mWorkers));
     memset(&mTlsStruct, 0, sizeof(mTlsStruct));
     mExit = false;
+    mLinkRuntimeCallback = NULL;
 
 }
 
diff --git a/cpu_ref/rsCpuCore.h b/cpu_ref/rsCpuCore.h
index 79b9681..4c43a8d 100644
--- a/cpu_ref/rsCpuCore.h
+++ b/cpu_ref/rsCpuCore.h
@@ -110,6 +110,14 @@
         return mScriptLookupFn(mRSC, s);
     }
 
+    void setLinkRuntimeCallback(
+            bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
+        mLinkRuntimeCallback = pLinkRuntimeCallback;
+    }
+    bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
+        return mLinkRuntimeCallback;
+    }
+    virtual bool getInForEach() { return mInForEach; }
 
 protected:
     Context *mRSC;
@@ -135,6 +143,8 @@
     script_lookup_t mScriptLookupFn;
 
     ScriptTLSStruct mTlsStruct;
+
+    bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
 };
 
 
diff --git a/cpu_ref/rsCpuScript.cpp b/cpu_ref/rsCpuScript.cpp
index e79a675..257655a 100644
--- a/cpu_ref/rsCpuScript.cpp
+++ b/cpu_ref/rsCpuScript.cpp
@@ -91,7 +91,8 @@
     mCompilerDriver->setRSRuntimeLookupContext(this);
 
     exec = mCompilerDriver->build(*mCompilerContext, cacheDir, resName,
-                                  (const char *)bitcode, bitcodeSize, NULL);
+                                  (const char *)bitcode, bitcodeSize, NULL,
+                                  mCtx->getLinkRuntimeCallback());
 
     if (exec == NULL) {
         ALOGE("bcc: FAILS to prepare executable for '%s'", resName);
diff --git a/cpu_ref/rsCpuScript.h b/cpu_ref/rsCpuScript.h
index 2197a20..53835bd 100644
--- a/cpu_ref/rsCpuScript.h
+++ b/cpu_ref/rsCpuScript.h
@@ -78,6 +78,7 @@
 
     virtual Allocation * getAllocationForPointer(const void *ptr) const;
 
+    virtual  void * getRSExecutable() { return mExecutable; }
 
 protected:
     RsdCpuReferenceImpl *mCtx;
diff --git a/cpu_ref/rsd_cpu.h b/cpu_ref/rsd_cpu.h
index d96d2d1..c9b8a71 100644
--- a/cpu_ref/rsd_cpu.h
+++ b/cpu_ref/rsd_cpu.h
@@ -19,6 +19,18 @@
 
 #include "rsAllocation.h"
 
+namespace llvm {
+
+class Module;
+
+}  // end namespace llvm
+
+namespace bcc {
+
+class RSScript;
+typedef llvm::Module* (*RSLinkRuntimeCallback) (bcc::RSScript *, llvm::Module *, llvm::Module *);
+
+}  // end namespace bcc;
 
 namespace android {
 namespace renderscript {
@@ -66,6 +78,9 @@
 
         virtual Allocation * getAllocationForPointer(const void *ptr) const = 0;
         virtual ~CpuScript() {}
+
+        virtual  void * getRSExecutable()  = 0;
+
     };
     typedef CpuScript * (* script_lookup_t)(Context *, const Script *s);
 
@@ -79,9 +94,11 @@
 
     static Context * getTlsContext();
     static const Script * getTlsScript();
+    static pthread_key_t getThreadTLSKey();
 
     static RsdCpuReference * create(Context *c, uint32_t version_major,
-                                    uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn);
+                                    uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn,
+                                    bcc::RSLinkRuntimeCallback pLinkRuntimeCallback = NULL);
     virtual ~RsdCpuReference();
     virtual void setPriority(int32_t priority) = 0;
 
@@ -90,6 +107,8 @@
                                      uint32_t flags) = 0;
     virtual CpuScript * createIntrinsic(const Script *s, RsScriptIntrinsicID iid, Element *e) = 0;
     virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg) = 0;
+    virtual bool getInForEach() = 0;
+
 };