Merge "Separate libRS.so into public and internal libs"
diff --git a/Android.mk b/Android.mk
index 3488f73..3b4194d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -45,7 +45,7 @@
 	driver/rsdVertexArray.cpp
 
 
-LOCAL_SHARED_LIBRARIES += libRS libRSCpuRef
+LOCAL_SHARED_LIBRARIES += libRS_internal libRSCpuRef
 LOCAL_SHARED_LIBRARIES += liblog libcutils libutils libEGL libGLESv1_CM libGLESv2
 LOCAL_SHARED_LIBRARIES += libui libgui libsync
 
@@ -88,7 +88,7 @@
 
 include $(CLEAR_VARS)
 LOCAL_CLANG := true
-LOCAL_MODULE := libRS
+LOCAL_MODULE := libRS_internal
 LOCAL_MODULE_TARGET_ARCH_WARN := arm mips mips64 x86 x86_64 arm64
 
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
@@ -114,7 +114,6 @@
 # Generate custom source files
 
 GEN := $(addprefix $(generated_sources)/, \
-            rsgApi.cpp \
             rsgApiReplay.cpp \
         )
 
@@ -190,6 +189,73 @@
 
 include $(BUILD_SHARED_LIBRARY)
 
+include $(CLEAR_VARS)
+LOCAL_CLANG := true
+LOCAL_MODULE := libRS
+LOCAL_MODULE_TARGET_ARCH_WARN := arm mips mips64 x86 x86_64 arm64
+
+LOCAL_MODULE_CLASS := SHARED_LIBRARIES
+generated_sources:= $(local-generated-sources-dir)
+
+# Generate custom headers
+
+GEN := $(addprefix $(generated_sources)/, \
+            rsgApiStructs.h \
+            rsgApiFuncDecl.h \
+        )
+
+$(GEN) : PRIVATE_PATH := $(LOCAL_PATH)
+$(GEN) : PRIVATE_CUSTOM_TOOL = cat $(PRIVATE_PATH)/rs.spec $(PRIVATE_PATH)/rsg.spec | $(RSG_GENERATOR) $< $@
+$(GEN) : $(RSG_GENERATOR) $(LOCAL_PATH)/rs.spec $(LOCAL_PATH)/rsg.spec
+$(GEN): $(generated_sources)/%.h : $(LOCAL_PATH)/%.h.rsg
+	$(transform-generated-source)
+
+# used in jni/Android.mk
+rs_generated_source += $(GEN)
+LOCAL_GENERATED_SOURCES += $(GEN)
+
+# Generate custom source files
+
+GEN := $(addprefix $(generated_sources)/, \
+            rsgApi.cpp \
+        )
+
+$(GEN) : PRIVATE_PATH := $(LOCAL_PATH)
+$(GEN) : PRIVATE_CUSTOM_TOOL = cat $(PRIVATE_PATH)/rs.spec $(PRIVATE_PATH)/rsg.spec | $(RSG_GENERATOR) $< $@
+$(GEN) : $(RSG_GENERATOR) $(LOCAL_PATH)/rs.spec $(LOCAL_PATH)/rsg.spec
+$(GEN): $(generated_sources)/%.cpp : $(LOCAL_PATH)/%.cpp.rsg
+	$(transform-generated-source)
+
+# used in jni/Android.mk
+rs_generated_source += $(GEN)
+
+LOCAL_GENERATED_SOURCES += $(GEN)
+
+LOCAL_SRC_FILES:= \
+	rsApiAllocation.cpp \
+	rsApiContext.cpp \
+	rsApiDevice.cpp \
+	rsApiElement.cpp \
+	rsApiFileA3D.cpp \
+	rsApiMesh.cpp \
+	rsApiType.cpp \
+
+LOCAL_SHARED_LIBRARIES += libRS_internal
+LOCAL_SHARED_LIBRARIES += liblog
+
+LOCAL_CFLAGS += $(rs_base_CFLAGS)
+# TODO: external/freetype still uses the register keyword
+# Bug: 17163086
+LOCAL_CFLAGS += -Wno-deprecated-register
+
+LOCAL_CPPFLAGS += -fno-exceptions
+
+LOCAL_LDFLAGS += -Wl,--version-script,${LOCAL_PATH}/libRS.map
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
+
 # Now build a host version for serialization
 include $(CLEAR_VARS)
 LOCAL_MODULE:= libRS
diff --git a/cpu_ref/Android.mk b/cpu_ref/Android.mk
index 750bf11..ad5d097 100644
--- a/cpu_ref/Android.mk
+++ b/cpu_ref/Android.mk
@@ -76,7 +76,7 @@
     rsCpuIntrinsics_x86.cpp
 endif
 
-LOCAL_SHARED_LIBRARIES += libRS libcutils libutils liblog libsync libc++ libdl libz
+LOCAL_SHARED_LIBRARIES += libRS_internal libcutils libutils liblog libsync libc++ libdl libz
 
 LOCAL_SHARED_LIBRARIES += libbcinfo libblas
 LOCAL_STATIC_LIBRARIES := libbnnmlowp
diff --git a/libRS.map b/libRS.map
new file mode 100644
index 0000000..4440ad9
--- /dev/null
+++ b/libRS.map
@@ -0,0 +1,121 @@
+libRS {
+  global:
+    rsaAllocationGetType;
+    rsaElementGetNativeData;
+    rsaElementGetSubElements;
+    rsaGetName;
+    rsAllocation1DData;
+    rsAllocation1DElementData;
+    rsAllocation1DRead;
+    rsAllocation2DData;
+    rsAllocation2DRead;
+    rsAllocation3DData;
+    rsAllocation3DRead;
+    rsAllocationAdapterCreate;
+    rsAllocationAdapterOffset;
+    rsAllocationCopy2DRange;
+    rsAllocationCopy3DRange;
+    rsAllocationCopyToBitmap;
+    rsAllocationCreateFromBitmap;
+    rsAllocationCreateTyped;
+    rsAllocationCubeCreateFromBitmap;
+    rsAllocationElementData;
+    rsAllocationElementRead;
+    rsAllocationGenerateMipmaps;
+    rsAllocationGetPointer;
+    rsAllocationGetSurface;
+    rsAllocationIoReceive;
+    rsAllocationIoSend;
+    rsAllocationRead;
+    rsAllocationResize1D;
+    rsAllocationSetSurface;
+    rsAllocationSetupBufferQueue;
+    rsAllocationShareBufferQueue;
+    rsAllocationSyncAll;
+    rsAssignName;
+    rsaTypeGetNativeData;
+    rsClosureCreate;
+    rsClosureSetArg;
+    rsClosureSetGlobal;
+    rsContextBindProgramFragment;
+    rsContextBindProgramRaster;
+    rsContextBindProgramStore;
+    rsContextBindProgramVertex;
+    rsContextBindRootScript;
+    rsContextCreate;
+    rsContextDeinitToClient;
+    rsContextDestroy;
+    rsContextDestroyWorker;
+    rsContextDump;
+    rsContextFinish;
+    rsContextGetMessage;
+    rsContextInitToClient;
+    rsContextPause;
+    rsContextPeekMessage;
+    rsContextResume;
+    rsContextSendMessage;
+    rsContextSetCacheDir;
+    rsContextSetPriority;
+    rsContextSetSurface;
+    rsDeviceCreate;
+    rsDeviceDestroy;
+    rsDeviceSetConfig;
+    rsElementCreate;
+    rsElementCreate2;
+    rsFontCreateFromFile;
+    rsFontCreateFromMemory;
+    rsInvokeClosureCreate;
+    rsMeshCreate;
+    rsObjDestroy;
+    rsProgramBindConstants;
+    rsProgramBindSampler;
+    rsProgramBindTexture;
+    rsProgramFragmentCreate;
+    rsProgramRasterCreate;
+    rsProgramStoreCreate;
+    rsProgramVertexCreate;
+    rsSamplerCreate;
+    rsScriptBindAllocation;
+    rsScriptCCreate;
+    rsScriptFieldIDCreate;
+    rsScriptForEach;
+    rsScriptForEachMulti;
+    rsScriptGetVarV;
+    rsScriptGroup2Create;
+    rsScriptGroup2Create;
+    rsScriptGroupCreate;
+    rsScriptGroupExecute;
+    rsScriptGroupSetInput;
+    rsScriptGroupSetOutput;
+    rsScriptIntrinsicCreate;
+    rsScriptInvoke;
+    rsScriptInvokeIDCreate;
+    rsScriptInvokeV;
+    rsScriptKernelIDCreate;
+    rsScriptReduce;
+    rsScriptReduceNew;
+    rsScriptSetTimeZone;
+    rsScriptSetVarD;
+    rsScriptSetVarF;
+    rsScriptSetVarI;
+    rsScriptSetVarJ;
+    rsScriptSetVarObj;
+    rsScriptSetVarV;
+    rsScriptSetVarVE;
+    rsTypeCreate;
+    rsTypeCreate2;
+    _Z24rsaFileA3DCreateFromFilePvPKc;
+    _Z25rsaFileA3DCreateFromAssetPvS_;
+    _Z25rsaFileA3DGetEntryByIndexPvjS_;
+    _Z25rsaFileA3DGetIndexEntriesPvP16RsFileIndexEntryjS_;
+    _Z26rsaFileA3DCreateFromMemoryPvPKvj;
+    _Z28rsaFileA3DGetNumIndexEntriesPvPiS_;
+    _Z17rsaMeshGetIndicesPvS_PS_Pjj;
+    _Z18rsaMeshGetVerticesPvS_PS_j;
+    _Z20rsaMeshGetIndexCountPvS_Pi;
+    _Z27rsaMeshGetVertexBufferCountPvS_Pi;
+    _Z17rsContextCreateGLPvjj15RsSurfaceConfigj;
+
+  local:
+    *;
+};
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index 13a3848..8b6de66 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -984,10 +984,3 @@
 
 }
 }
-
-extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
-    Allocation *a = static_cast<Allocation *>(va);
-    a->getType()->incUserRef();
-
-    return a->getType();
-}
diff --git a/rsApiAllocation.cpp b/rsApiAllocation.cpp
new file mode 100644
index 0000000..84d5692
--- /dev/null
+++ b/rsApiAllocation.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsContext.h"
+#include "rsAllocation.h"
+
+using namespace android;
+using namespace android::renderscript;
+
+extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
+    Allocation *a = static_cast<Allocation *>(va);
+    a->getType()->incUserRef();
+
+    return a->getType();
+}
diff --git a/rsApiContext.cpp b/rsApiContext.cpp
new file mode 100644
index 0000000..a5c7c3f
--- /dev/null
+++ b/rsApiContext.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsDevice.h"
+#include "rsContext.h"
+#include "rsThreadIO.h"
+#include "rsgApiStructs.h"
+#include "rsgApiFuncDecl.h"
+#include "rsFifo.h"
+
+using namespace android;
+using namespace android::renderscript;
+
+extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
+                                      RsContextType ct, uint32_t flags) {
+    //ALOGV("rsContextCreate dev=%p", vdev);
+    Device * dev = static_cast<Device *>(vdev);
+    Context *rsc = Context::createContext(dev, nullptr, ct, flags);
+    if (rsc) {
+        rsc->setTargetSdkVersion(sdkVersion);
+    }
+    return rsc;
+}
+
+extern "C" void rsaContextSetNativeLibDir(RsContext con, char *libDir, size_t length) {
+#ifdef RS_COMPATIBILITY_LIB
+    Context *rsc = static_cast<Context *>(con);
+    rsc->setNativeLibDir(libDir, length);
+#endif
+}
+
+#ifndef RS_COMPATIBILITY_LIB
+RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
+                            uint32_t sdkVersion, RsSurfaceConfig sc,
+                            uint32_t dpi) {
+    //ALOGV("rsContextCreateGL dev=%p", vdev);
+    Device * dev = static_cast<Device *>(vdev);
+    Context *rsc = Context::createContext(dev, &sc);
+    if (rsc) {
+        rsc->setTargetSdkVersion(sdkVersion);
+        rsc->setDPI(dpi);
+    }
+    //ALOGV("%p rsContextCreateGL ret", rsc);
+    return rsc;
+}
+#endif
+
+// Only to be called at a3d load time, before object is visible to user
+// not thread safe
+extern "C" void rsaGetName(RsContext con, void * obj, const char **name) {
+    ObjectBase *ob = static_cast<ObjectBase *>(obj);
+    (*name) = ob->getName();
+}
diff --git a/rsApiDevice.cpp b/rsApiDevice.cpp
new file mode 100644
index 0000000..0dc1270
--- /dev/null
+++ b/rsApiDevice.cpp
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsDevice.h"
+#include "rsContext.h"
+#include "rsThreadIO.h"
+#include "rsgApiStructs.h"
+#include "rsgApiFuncDecl.h"
+#include "rsFifo.h"
+
+using namespace android;
+using namespace android::renderscript;
+
+extern "C" RsDevice rsDeviceCreate() {
+    Device * d = new Device();
+    return d;
+}
+
+extern "C" void rsDeviceDestroy(RsDevice dev) {
+    Device * d = static_cast<Device *>(dev);
+    delete d;
+}
+
+extern "C" void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value) {
+    Device * d = static_cast<Device *>(dev);
+    if (p == RS_DEVICE_PARAM_FORCE_SOFTWARE_GL) {
+        d->mForceSW = value != 0;
+        return;
+    }
+    rsAssert(0);
+}
diff --git a/rsApiElement.cpp b/rsApiElement.cpp
new file mode 100644
index 0000000..868716b
--- /dev/null
+++ b/rsApiElement.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsContext.h"
+
+using namespace android;
+using namespace android::renderscript;
+
+extern "C" void rsaElementGetNativeData(RsContext con, RsElement elem,
+                             uint32_t *elemData, uint32_t elemDataSize) {
+    rsAssert(elemDataSize == 5);
+    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
+    Element *e = static_cast<Element *>(elem);
+
+    (*elemData++) = (uint32_t)e->getType();
+    (*elemData++) = (uint32_t)e->getKind();
+    (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
+    (*elemData++) = e->getComponent().getVectorSize();
+    (*elemData++) = e->getFieldCount();
+}
+
+extern "C" void rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids,
+                              const char **names, size_t *arraySizes, uint32_t dataSize) {
+    Element *e = static_cast<Element *>(elem);
+    rsAssert(e->getFieldCount() == dataSize);
+
+    for (uint32_t i = 0; i < dataSize; i ++) {
+        e->getField(i)->incUserRef();
+        ids[i] = (uintptr_t)e->getField(i);
+        names[i] = e->getFieldName(i);
+        arraySizes[i] = e->getFieldArraySize(i);
+    }
+}
diff --git a/rsApiFileA3D.cpp b/rsApiFileA3D.cpp
new file mode 100644
index 0000000..d6eee8a
--- /dev/null
+++ b/rsApiFileA3D.cpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsContext.h"
+#include "rsFileA3D.h"
+
+using namespace android;
+using namespace android::renderscript;
+
+RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) {
+    FileA3D *fa3d = static_cast<FileA3D *>(file);
+    if (!fa3d) {
+        ALOGE("Can't load entry. No valid file");
+        return nullptr;
+    }
+
+    ObjectBase *obj = fa3d->initializeFromEntry(index);
+    //ALOGV("Returning object with name %s", obj->getName());
+
+    return obj;
+}
+
+
+void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) {
+    FileA3D *fa3d = static_cast<FileA3D *>(file);
+
+    if (fa3d) {
+        *numEntries = fa3d->getNumIndexEntries();
+    } else {
+        *numEntries = 0;
+    }
+}
+
+void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) {
+    FileA3D *fa3d = static_cast<FileA3D *>(file);
+
+    if (!fa3d) {
+        ALOGE("Can't load index entries. No valid file");
+        return;
+    }
+
+    uint32_t numFileEntries = fa3d->getNumIndexEntries();
+    if (numFileEntries != numEntries || numEntries == 0 || fileEntries == nullptr) {
+        ALOGE("Can't load index entries. Invalid number requested");
+        return;
+    }
+
+    for (uint32_t i = 0; i < numFileEntries; i ++) {
+        const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i);
+        fileEntries[i].classID = entry->getType();
+        fileEntries[i].objectName = rsuCopyString(entry->getObjectName());
+    }
+}
+
+RsFile rsaFileA3DCreateFromMemory(RsContext con, const void *data, uint32_t len) {
+    if (data == nullptr) {
+        ALOGE("File load failed. Asset stream is nullptr");
+        return nullptr;
+    }
+
+    Context *rsc = static_cast<Context *>(con);
+    FileA3D *fa3d = new FileA3D(rsc);
+    fa3d->incUserRef();
+
+    fa3d->load(data, len);
+    return fa3d;
+}
+
+RsFile rsaFileA3DCreateFromAsset(RsContext con, void *_asset) {
+#if !defined(__RS_PDK__)
+    Context *rsc = static_cast<Context *>(con);
+    Asset *asset = static_cast<Asset *>(_asset);
+    FileA3D *fa3d = new FileA3D(rsc);
+    fa3d->incUserRef();
+
+    fa3d->load(asset);
+    return fa3d;
+#else
+    return nullptr;
+#endif
+}
+
+RsFile rsaFileA3DCreateFromFile(RsContext con, const char *path) {
+    if (path == nullptr) {
+        ALOGE("File load failed. Path is nullptr");
+        return nullptr;
+    }
+
+    Context *rsc = static_cast<Context *>(con);
+    FileA3D *fa3d = nullptr;
+
+    FILE *f = fopen(path, "rb");
+    if (f) {
+        fa3d = new FileA3D(rsc);
+        fa3d->incUserRef();
+        fa3d->load(f);
+        fclose(f);
+    } else {
+        ALOGE("Could not open file %s", path);
+    }
+
+    return fa3d;
+}
diff --git a/rsApiMesh.cpp b/rsApiMesh.cpp
new file mode 100644
index 0000000..a5ef5f1
--- /dev/null
+++ b/rsApiMesh.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsContext.h"
+#include "rsMesh.h"
+
+using namespace android;
+using namespace android::renderscript;
+
+void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) {
+    Mesh *sm = static_cast<Mesh *>(mv);
+    *numVtx = sm->mHal.state.vertexBuffersCount;
+}
+
+void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) {
+    Mesh *sm = static_cast<Mesh *>(mv);
+    *numIdx = sm->mHal.state.primitivesCount;
+}
+
+void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) {
+    Mesh *sm = static_cast<Mesh *>(mv);
+    rsAssert(vtxDataCount == sm->mHal.state.vertexBuffersCount);
+
+    for (uint32_t ct = 0; ct < vtxDataCount; ct ++) {
+        vtxData[ct] = sm->mHal.state.vertexBuffers[ct];
+        sm->mHal.state.vertexBuffers[ct]->incUserRef();
+    }
+}
+
+void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) {
+    Mesh *sm = static_cast<Mesh *>(mv);
+    rsAssert(idxDataCount == sm->mHal.state.primitivesCount);
+
+    for (uint32_t ct = 0; ct < idxDataCount; ct ++) {
+        va[ct] = sm->mHal.state.indexBuffers[ct];
+        primType[ct] = sm->mHal.state.primitives[ct];
+        if (sm->mHal.state.indexBuffers[ct]) {
+            sm->mHal.state.indexBuffers[ct]->incUserRef();
+        }
+    }
+}
diff --git a/rsApiType.cpp b/rsApiType.cpp
new file mode 100644
index 0000000..856a520
--- /dev/null
+++ b/rsApiType.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rsContext.h"
+
+#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
+#include "system/graphics.h"
+#endif
+
+#ifdef RS_COMPATIBILITY_LIB
+#include "rsCompatibilityLib.h"
+#endif
+
+using namespace android;
+using namespace android::renderscript;
+
+extern "C" void rsaTypeGetNativeData(RsContext con, RsType type, uintptr_t *typeData, uint32_t typeDataSize) {
+    rsAssert(typeDataSize == 6);
+    // Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
+    // mHal.state.lodCount; mHal.state.faces; mElement; into typeData
+    Type *t = static_cast<Type *>(type);
+
+    (*typeData++) = t->getDimX();
+    (*typeData++) = t->getDimY();
+    (*typeData++) = t->getDimZ();
+    (*typeData++) = t->getDimLOD() ? 1 : 0;
+    (*typeData++) = t->getDimFaces() ? 1 : 0;
+    (*typeData++) = (uintptr_t)t->getElement();
+    t->getElement()->incUserRef();
+}
diff --git a/rsContext.cpp b/rsContext.cpp
index dba9767..3eb7216 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -876,7 +876,7 @@
 
 void rsi_ContextDestroy(Context *rsc) {
     //ALOGE("%p rsContextDestroy", rsc);
-    rsContextDestroyWorker(rsc);
+    rsc->destroyWorkerThreadResources();
     delete rsc;
     //ALOGV("%p rsContextDestroy done", rsc);
 }
@@ -935,44 +935,3 @@
 
 }
 }
-
-extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
-                                     RsContextType ct, uint32_t flags) {
-    //ALOGV("rsContextCreate dev=%p", vdev);
-    Device * dev = static_cast<Device *>(vdev);
-    Context *rsc = Context::createContext(dev, nullptr, ct, flags);
-    if (rsc) {
-        rsc->setTargetSdkVersion(sdkVersion);
-    }
-    return rsc;
-}
-
-extern "C" void rsaContextSetNativeLibDir(RsContext con, char *libDir, size_t length) {
-#ifdef RS_COMPATIBILITY_LIB
-    Context *rsc = static_cast<Context *>(con);
-    rsc->setNativeLibDir(libDir, length);
-#endif
-}
-
-#ifndef RS_COMPATIBILITY_LIB
-RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
-                            uint32_t sdkVersion, RsSurfaceConfig sc,
-                            uint32_t dpi) {
-    //ALOGV("rsContextCreateGL dev=%p", vdev);
-    Device * dev = static_cast<Device *>(vdev);
-    Context *rsc = Context::createContext(dev, &sc);
-    if (rsc) {
-        rsc->setTargetSdkVersion(sdkVersion);
-        rsc->setDPI(dpi);
-    }
-    //ALOGV("%p rsContextCreateGL ret", rsc);
-    return rsc;
-}
-#endif
-
-// Only to be called at a3d load time, before object is visible to user
-// not thread safe
-void rsaGetName(RsContext con, void * obj, const char **name) {
-    ObjectBase *ob = static_cast<ObjectBase *>(obj);
-    (*name) = ob->getName();
-}
diff --git a/rsDevice.cpp b/rsDevice.cpp
index 2688890..cda71a4 100644
--- a/rsDevice.cpp
+++ b/rsDevice.cpp
@@ -39,23 +39,3 @@
         }
     }
 }
-
-extern "C" RsDevice rsDeviceCreate() {
-    Device * d = new Device();
-    return d;
-}
-
-extern "C" void rsDeviceDestroy(RsDevice dev) {
-    Device * d = static_cast<Device *>(dev);
-    delete d;
-}
-
-extern "C" void rsDeviceSetConfig(RsDevice dev, RsDeviceParam p, int32_t value) {
-    Device * d = static_cast<Device *>(dev);
-    if (p == RS_DEVICE_PARAM_FORCE_SOFTWARE_GL) {
-        d->mForceSW = value != 0;
-        return;
-    }
-    rsAssert(0);
-}
-
diff --git a/rsElement.cpp b/rsElement.cpp
index a734400..4d831ac 100644
--- a/rsElement.cpp
+++ b/rsElement.cpp
@@ -435,29 +435,3 @@
 
 }
 }
-
-extern "C" void rsaElementGetNativeData(RsContext con, RsElement elem,
-                             uint32_t *elemData, uint32_t elemDataSize) {
-    rsAssert(elemDataSize == 5);
-    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
-    Element *e = static_cast<Element *>(elem);
-
-    (*elemData++) = (uint32_t)e->getType();
-    (*elemData++) = (uint32_t)e->getKind();
-    (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
-    (*elemData++) = e->getComponent().getVectorSize();
-    (*elemData++) = e->getFieldCount();
-}
-
-extern "C" void rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids,
-                              const char **names, size_t *arraySizes, uint32_t dataSize) {
-    Element *e = static_cast<Element *>(elem);
-    rsAssert(e->getFieldCount() == dataSize);
-
-    for (uint32_t i = 0; i < dataSize; i ++) {
-        e->getField(i)->incUserRef();
-        ids[i] = (uintptr_t)e->getField(i);
-        names[i] = e->getFieldName(i);
-        arraySizes[i] = e->getFieldArraySize(i);
-    }
-}
diff --git a/rsFileA3D.cpp b/rsFileA3D.cpp
index a7b1e27..91e92f1 100644
--- a/rsFileA3D.cpp
+++ b/rsFileA3D.cpp
@@ -391,97 +391,3 @@
     mWriteStream->align(4);
 }
 
-RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file) {
-    FileA3D *fa3d = static_cast<FileA3D *>(file);
-    if (!fa3d) {
-        ALOGE("Can't load entry. No valid file");
-        return nullptr;
-    }
-
-    ObjectBase *obj = fa3d->initializeFromEntry(index);
-    //ALOGV("Returning object with name %s", obj->getName());
-
-    return obj;
-}
-
-
-void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file) {
-    FileA3D *fa3d = static_cast<FileA3D *>(file);
-
-    if (fa3d) {
-        *numEntries = fa3d->getNumIndexEntries();
-    } else {
-        *numEntries = 0;
-    }
-}
-
-void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file) {
-    FileA3D *fa3d = static_cast<FileA3D *>(file);
-
-    if (!fa3d) {
-        ALOGE("Can't load index entries. No valid file");
-        return;
-    }
-
-    uint32_t numFileEntries = fa3d->getNumIndexEntries();
-    if (numFileEntries != numEntries || numEntries == 0 || fileEntries == nullptr) {
-        ALOGE("Can't load index entries. Invalid number requested");
-        return;
-    }
-
-    for (uint32_t i = 0; i < numFileEntries; i ++) {
-        const FileA3D::A3DIndexEntry *entry = fa3d->getIndexEntry(i);
-        fileEntries[i].classID = entry->getType();
-        fileEntries[i].objectName = rsuCopyString(entry->getObjectName());
-    }
-}
-
-RsFile rsaFileA3DCreateFromMemory(RsContext con, const void *data, uint32_t len) {
-    if (data == nullptr) {
-        ALOGE("File load failed. Asset stream is nullptr");
-        return nullptr;
-    }
-
-    Context *rsc = static_cast<Context *>(con);
-    FileA3D *fa3d = new FileA3D(rsc);
-    fa3d->incUserRef();
-
-    fa3d->load(data, len);
-    return fa3d;
-}
-
-RsFile rsaFileA3DCreateFromAsset(RsContext con, void *_asset) {
-#if !defined(__RS_PDK__)
-    Context *rsc = static_cast<Context *>(con);
-    Asset *asset = static_cast<Asset *>(_asset);
-    FileA3D *fa3d = new FileA3D(rsc);
-    fa3d->incUserRef();
-
-    fa3d->load(asset);
-    return fa3d;
-#else
-    return nullptr;
-#endif
-}
-
-RsFile rsaFileA3DCreateFromFile(RsContext con, const char *path) {
-    if (path == nullptr) {
-        ALOGE("File load failed. Path is nullptr");
-        return nullptr;
-    }
-
-    Context *rsc = static_cast<Context *>(con);
-    FileA3D *fa3d = nullptr;
-
-    FILE *f = fopen(path, "rb");
-    if (f) {
-        fa3d = new FileA3D(rsc);
-        fa3d->incUserRef();
-        fa3d->load(f);
-        fclose(f);
-    } else {
-        ALOGE("Could not open file %s", path);
-    }
-
-    return fa3d;
-}
diff --git a/rsMesh.cpp b/rsMesh.cpp
index 08d85fb..f6fbaf6 100644
--- a/rsMesh.cpp
+++ b/rsMesh.cpp
@@ -288,36 +288,3 @@
 }
 
 }}
-
-void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) {
-    Mesh *sm = static_cast<Mesh *>(mv);
-    *numVtx = sm->mHal.state.vertexBuffersCount;
-}
-
-void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) {
-    Mesh *sm = static_cast<Mesh *>(mv);
-    *numIdx = sm->mHal.state.primitivesCount;
-}
-
-void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) {
-    Mesh *sm = static_cast<Mesh *>(mv);
-    rsAssert(vtxDataCount == sm->mHal.state.vertexBuffersCount);
-
-    for (uint32_t ct = 0; ct < vtxDataCount; ct ++) {
-        vtxData[ct] = sm->mHal.state.vertexBuffers[ct];
-        sm->mHal.state.vertexBuffers[ct]->incUserRef();
-    }
-}
-
-void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) {
-    Mesh *sm = static_cast<Mesh *>(mv);
-    rsAssert(idxDataCount == sm->mHal.state.primitivesCount);
-
-    for (uint32_t ct = 0; ct < idxDataCount; ct ++) {
-        va[ct] = sm->mHal.state.indexBuffers[ct];
-        primType[ct] = sm->mHal.state.primitives[ct];
-        if (sm->mHal.state.indexBuffers[ct]) {
-            sm->mHal.state.indexBuffers[ct]->incUserRef();
-        }
-    }
-}
diff --git a/rsType.cpp b/rsType.cpp
index d45de2d..25473eb 100644
--- a/rsType.cpp
+++ b/rsType.cpp
@@ -383,18 +383,3 @@
 
 }
 }
-
-extern "C" void rsaTypeGetNativeData(RsContext con, RsType type, uintptr_t *typeData, uint32_t typeDataSize) {
-    rsAssert(typeDataSize == 6);
-    // Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
-    // mHal.state.lodCount; mHal.state.faces; mElement; into typeData
-    Type *t = static_cast<Type *>(type);
-
-    (*typeData++) = t->getDimX();
-    (*typeData++) = t->getDimY();
-    (*typeData++) = t->getDimZ();
-    (*typeData++) = t->getDimLOD() ? 1 : 0;
-    (*typeData++) = t->getDimFaces() ? 1 : 0;
-    (*typeData++) = (uintptr_t)t->getElement();
-    t->getElement()->incUserRef();
-}