Create a separate static lib that provides a dispatch table to both fw/support/v8/renderscript/jni and libRScpp to reduce code duplication.

 - This CL contains related changes to libRSSupport.

Change-Id: I9f243d319eb29ddc048feb3cea5866c813d9f622
diff --git a/v8/renderscript/jni/Android.mk b/v8/renderscript/jni/Android.mk
index 4ee8a25..3c921bb 100644
--- a/v8/renderscript/jni/Android.mk
+++ b/v8/renderscript/jni/Android.mk
@@ -11,7 +11,8 @@
         libjnigraphics
 
 LOCAL_STATIC_LIBRARIES := \
-        libcutils
+        libcutils \
+        libRSDispatch
 
 rs_generated_include_dir := $(call generated-sources-dir-for,SHARED_LIBRARIES,libRSSupport,,)
 
diff --git a/v8/renderscript/jni/android_renderscript_RenderScript.cpp b/v8/renderscript/jni/android_renderscript_RenderScript.cpp
index 593cdfe..068e8eb 100644
--- a/v8/renderscript/jni/android_renderscript_RenderScript.cpp
+++ b/v8/renderscript/jni/android_renderscript_RenderScript.cpp
@@ -92,346 +92,8 @@
 
 
 // ---------------------------------------------------------------------------
-
 static dispatchTable dispatchTab;
 
-static bool loadSymbols(void* handle) {
-
-    dispatchTab.AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType");
-    if (dispatchTab.AllocationGetType == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationGetType");
-        return false;
-    }
-    dispatchTab.TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData");
-    if (dispatchTab.TypeGetNativeData == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.TypeGetNativeData");
-        return false;
-    }
-    dispatchTab.ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData");
-    if (dispatchTab.ElementGetNativeData == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ElementGetNativeData");
-        return false;
-    }
-    dispatchTab.ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements");
-    if (dispatchTab.ElementGetSubElements == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ElementGetSubElements");
-        return false;
-    }
-    dispatchTab.DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate");
-    if (dispatchTab.DeviceCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.DeviceCreate");
-        return false;
-    }
-    dispatchTab.DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy");
-    if (dispatchTab.DeviceDestroy == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.DeviceDestroy");
-        return false;
-    }
-    dispatchTab.DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig");
-    if (dispatchTab.DeviceSetConfig == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.DeviceSetConfig");
-        return false;
-    }
-    dispatchTab.ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");;
-    if (dispatchTab.ContextCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextCreate");
-        return false;
-    }
-    dispatchTab.GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");;
-    if (dispatchTab.GetName == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.GetName");
-        return false;
-    }
-    dispatchTab.ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy");
-    if (dispatchTab.ContextDestroy == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextDestroy");
-        return false;
-    }
-    dispatchTab.ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage");
-    if (dispatchTab.ContextGetMessage == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextGetMessage");
-        return false;
-    }
-    dispatchTab.ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage");
-    if (dispatchTab.ContextPeekMessage == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextPeekMessage");
-        return false;
-    }
-    dispatchTab.ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage");
-    if (dispatchTab.ContextSendMessage == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextSendMessage");
-        return false;
-    }
-    dispatchTab.ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient");
-    if (dispatchTab.ContextInitToClient == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextInitToClient");
-        return false;
-    }
-    dispatchTab.ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient");
-    if (dispatchTab.ContextDeinitToClient == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextDeinitToClient");
-        return false;
-    }
-    dispatchTab.TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate");
-    if (dispatchTab.TypeCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.TypeCreate");
-        return false;
-    }
-    dispatchTab.AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped");
-    if (dispatchTab.AllocationCreateTyped == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationCreateTyped");
-        return false;
-    }
-    dispatchTab.AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap");
-    if (dispatchTab.AllocationCreateFromBitmap == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationCreateFromBitmap");
-        return false;
-    }
-    dispatchTab.AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap");
-    if (dispatchTab.AllocationCubeCreateFromBitmap == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationCubeCreateFromBitmap");
-        return false;
-    }
-    dispatchTab.AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface");
-    if (dispatchTab.AllocationGetSurface == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationGetSurface");
-        return false;
-    }
-    dispatchTab.AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface");
-    if (dispatchTab.AllocationSetSurface == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationSetSurface");
-        return false;
-    }
-    dispatchTab.ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish");
-    if (dispatchTab.ContextFinish == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextFinish");
-        return false;
-    }
-    dispatchTab.ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump");
-    if (dispatchTab.ContextDump == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextDump");
-        return false;
-    }
-    dispatchTab.ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority");
-    if (dispatchTab.ContextSetPriority == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ContextSetPriority");
-        return false;
-    }
-    dispatchTab.AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName");
-    if (dispatchTab.AssignName == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AssignName");
-        return false;
-    }
-    dispatchTab.ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy");
-    if (dispatchTab.ObjDestroy == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ObjDestroy");
-        return false;
-    }
-    dispatchTab.ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate");
-    if (dispatchTab.ElementCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ElementCreate");
-        return false;
-    }
-    dispatchTab.ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2");
-    if (dispatchTab.ElementCreate2 == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ElementCreate2");
-        return false;
-    }
-    dispatchTab.AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap");
-    if (dispatchTab.AllocationCopyToBitmap == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationCopyToBitmap");
-        return false;
-    }
-    dispatchTab.Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData");
-    if (dispatchTab.Allocation1DData == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.Allocation1DData");
-        return false;
-    }
-    dispatchTab.Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData");
-    if (dispatchTab.Allocation1DElementData == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.Allocation1DElementData");
-        return false;
-    }
-    dispatchTab.Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData");
-    if (dispatchTab.Allocation2DData == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.Allocation2DData");
-        return false;
-    }
-    dispatchTab.Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData");
-    if (dispatchTab.Allocation3DData == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.Allocation3DData");
-        return false;
-    }
-    dispatchTab.AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps");
-    if (dispatchTab.AllocationGenerateMipmaps == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationGenerateMipmaps");
-        return false;
-    }
-    dispatchTab.AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead");
-    if (dispatchTab.AllocationRead == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationRead");
-        return false;
-    }
-    dispatchTab.Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead");
-    if (dispatchTab.Allocation1DRead == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.Allocation1DRead");
-        return false;
-    }
-    dispatchTab.Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead");
-    if (dispatchTab.Allocation2DRead == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.Allocation2DRead");
-        return false;
-    }
-    dispatchTab.AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll");
-    if (dispatchTab.AllocationSyncAll == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationSyncAll");
-        return false;
-    }
-    dispatchTab.AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D");
-    if (dispatchTab.AllocationResize1D == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationResize1D");
-        return false;
-    }
-    dispatchTab.AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange");
-    if (dispatchTab.AllocationCopy2DRange == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationCopy2DRange");
-        return false;
-    }
-    dispatchTab.AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange");
-    if (dispatchTab.AllocationCopy3DRange == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationCopy3DRange");
-        return false;
-    }
-    dispatchTab.SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate");
-    if (dispatchTab.SamplerCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.SamplerCreate");
-        return false;
-    }
-    dispatchTab.ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation");
-    if (dispatchTab.ScriptBindAllocation == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptBindAllocation");
-        return false;
-    }
-    dispatchTab.ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone");
-    if (dispatchTab.ScriptSetTimeZone == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetTimeZone");
-        return false;
-    }
-    dispatchTab.ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke");
-    if (dispatchTab.ScriptInvoke == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptInvoke");
-        return false;
-    }
-    dispatchTab.ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV");
-    if (dispatchTab.ScriptInvokeV == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptInvokeV");
-        return false;
-    }
-    dispatchTab.ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach");
-    if (dispatchTab.ScriptForEach == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptForEach");
-        return false;
-    }
-    dispatchTab.ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI");
-    if (dispatchTab.ScriptSetVarI == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarI");
-        return false;
-    }
-    dispatchTab.ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj");
-    if (dispatchTab.ScriptSetVarObj == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarObj");
-        return false;
-    }
-    dispatchTab.ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ");
-    if (dispatchTab.ScriptSetVarJ == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarJ");
-        return false;
-    }
-    dispatchTab.ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF");
-    if (dispatchTab.ScriptSetVarF == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarF");
-        return false;
-    }
-    dispatchTab.ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD");
-    if (dispatchTab.ScriptSetVarD == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarD");
-        return false;
-    }
-    dispatchTab.ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV");
-    if (dispatchTab.ScriptSetVarV == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarV");
-        return false;
-    }
-    dispatchTab.ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV");
-    if (dispatchTab.ScriptGetVarV == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptGetVarV");
-        return false;
-    }
-    dispatchTab.ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE");
-    if (dispatchTab.ScriptSetVarVE == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarVE");
-        return false;
-    }
-    dispatchTab.ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate");
-    if (dispatchTab.ScriptCCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptCCreate");
-        return false;
-    }
-    dispatchTab.ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate");
-    if (dispatchTab.ScriptIntrinsicCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptIntrinsicCreate");
-        return false;
-    }
-    dispatchTab.ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate");
-    if (dispatchTab.ScriptKernelIDCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptKernelIDCreate");
-        return false;
-    }
-    dispatchTab.ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate");
-    if (dispatchTab.ScriptFieldIDCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptFieldIDCreate");
-        return false;
-    }
-    dispatchTab.ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate");
-    if (dispatchTab.ScriptGroupCreate == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptGroupCreate");
-        return false;
-    }
-    dispatchTab.ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput");
-    if (dispatchTab.ScriptGroupSetOutput == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptGroupSetOutput");
-        return false;
-    }
-    dispatchTab.ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput");
-    if (dispatchTab.ScriptGroupSetInput == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptGroupSetInput");
-        return false;
-    }
-    dispatchTab.ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute");
-    if (dispatchTab.ScriptGroupExecute == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.ScriptGroupExecute");
-        return false;
-    }
-    dispatchTab.AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend");
-    if (dispatchTab.AllocationIoSend == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationIoSend");
-        return false;
-    }
-    dispatchTab.AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive");
-    if (dispatchTab.AllocationIoReceive == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationIoReceive");
-        return false;
-    }
-    dispatchTab.AllocationGetPointer = (AllocationGetPointerFnPtr)dlsym(handle, "rsAllocationGetPointer");
-    if (dispatchTab.AllocationGetPointer == NULL) {
-        LOG_API("Couldn't initialize dispatchTab.AllocationGetPointer");
-        return false;
-    }
-
-    return true;
-}
-
-
 static jboolean nLoadSO(JNIEnv *_env, jobject _this, jboolean useNative) {
     void* handle = NULL;
     if (useNative) {
@@ -444,7 +106,7 @@
         return false;
     }
 
-    if (loadSymbols(handle) == false) {
+    if (loadSymbols(handle, dispatchTab) == false) {
         LOG_API("%s init failed!", filename);
         return false;
     }