Merge "Fix 3140348: Signature was wrong for atan2() in RS."
diff --git a/RenderScript.h b/RenderScript.h
index 1d67329..8c2081d 100644
--- a/RenderScript.h
+++ b/RenderScript.h
@@ -286,6 +286,25 @@
 
 } RsScriptCall;
 
+// A3D loading and object update code.
+// Should only be called at object creation, not thread safe
+RsObjectBase rsaFileA3DGetEntryByIndex(RsContext, uint32_t idx, RsFile);
+RsFile rsaFileA3DCreateFromAssetStream(RsContext, const void *data, uint32_t len);
+void rsaFileA3DGetNumIndexEntries(RsContext, int32_t *numEntries, RsFile);
+void rsaFileA3DGetIndexEntries(RsContext, RsFileIndexEntry *fileEntries,uint32_t numEntries, RsFile);
+void rsaGetName(RsContext, void * obj, const char **name);
+// Mesh update functions
+void rsaMeshGetVertexBufferCount(RsContext, RsMesh, int32_t *vtxCount);
+void rsaMeshGetIndexCount(RsContext, RsMesh, int32_t *idxCount);
+void rsaMeshGetVertices(RsContext, RsMesh, RsAllocation *vtxData, uint32_t vtxDataCount);
+void rsaMeshGetIndices(RsContext, RsMesh, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount);
+// Allocation update
+const void* rsaAllocationGetType(RsContext con, RsAllocation va);
+// Type update
+void rsaTypeGetNativeData(RsContext, RsType, uint32_t *typeData, uint32_t typeDataSize);
+// Element update
+void rsaElementGetNativeData(RsContext, RsElement, uint32_t *elemData, uint32_t elemDataSize);
+void rsaElementGetSubElements(RsContext, RsElement, uint32_t *ids, const char **names, uint32_t dataSize);
 
 // Async commands for returning new IDS
 RsType rsaTypeCreate(RsContext, RsElement, uint32_t dimCount,
@@ -293,7 +312,6 @@
 RsAllocation rsaAllocationCreateTyped(RsContext rsc, RsType vtype);
 RsAllocation rsaAllocationCreateFromBitmap(RsContext con, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data);
 
-
 #ifndef NO_RS_FUNCS
 #include "rsgApiFuncDecl.h"
 #endif
diff --git a/rs.spec b/rs.spec
index 3e131b2..14809e9 100644
--- a/rs.spec
+++ b/rs.spec
@@ -58,11 +58,6 @@
 	param size_t len
 	}
 
-GetName {
-	param void *obj
-	param const char **name
-	}
-
 ObjDestroy {
 	param RsAsyncVoidPtr objPtr
 	}
@@ -84,26 +79,6 @@
 	ret RsElement
 	}
 
-ElementGetNativeData {
-	param RsElement elem
-	param uint32_t *elemData
-	param uint32_t elemDataSize
-	}
-
-ElementGetSubElements {
-	param RsElement elem
-	param uint32_t *ids
-	param const char **names
-	param uint32_t dataSize
-	}
-
-
-TypeGetNativeData {
-	param RsType type
-	param uint32_t * typeData
-	param uint32_t typeDataSize
-	}
-
 AllocationUpdateFromBitmap {
 	param RsAllocation alloc
 	param RsElement srcFmt
@@ -237,11 +212,6 @@
 	param const void *data
 	}
 
-AllocationGetType {
-	param RsAllocation va
-	ret const void*
-	}
-
 AllocationResize1D {
 	param RsAllocation va
 	param uint32_t dimX
@@ -433,35 +403,12 @@
 	ret RsProgramVertex
 	}
 
-FileA3DCreateFromAssetStream {
-	param const void * data
-	param size_t len
-	ret RsFile
-	}
-
 FileOpen {
 	ret RsFile
 	param const char *name
 	param size_t len
 	}
 
-FileA3DGetNumIndexEntries {
-	param int32_t * numEntries
-	param RsFile file
-	}
-
-FileA3DGetIndexEntries {
-	param RsFileIndexEntry * fileEntries
-	param uint32_t numEntries
-	param RsFile fileA3D
-	}
-
-FileA3DGetEntryByIndex {
-	param uint32_t index
-	param RsFile file
-	ret RsObjectBase
-	}
-
 FontCreateFromFile {
 	param const char *name
 	param uint32_t fontSize
@@ -488,29 +435,6 @@
 	param uint32_t slot
 	}
 
-MeshGetVertexBufferCount {
-	param RsMesh mesh
-	param int32_t *numVtx
-	}
-
-MeshGetIndexCount {
-	param RsMesh mesh
-	param int32_t *numIdx
-	}
-
-MeshGetVertices {
-	param RsMesh mv
-	param RsAllocation *vtxData
-	param uint32_t vtxDataCount
-	}
-
-MeshGetIndices {
-	param RsMesh mv
-	param RsAllocation *va
-	param uint32_t *primType
-	param uint32_t idxDataCount
-	}
-
 AnimationCreate {
 	param const float *inValues
 	param const float *outValues
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index b4872e3..b74fa8e 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -816,7 +816,12 @@
     a->resize2D(rsc, dimX, dimY);
 }
 
-const void* rsi_AllocationGetType(Context *rsc, RsAllocation va)
+#endif //ANDROID_RS_BUILD_FOR_HOST
+
+}
+}
+
+const void * rsaAllocationGetType(RsContext con, RsAllocation va)
 {
     Allocation *a = static_cast<Allocation *>(va);
     a->getType()->incUserRef();
@@ -824,11 +829,6 @@
     return a->getType();
 }
 
-#endif //ANDROID_RS_BUILD_FOR_HOST
-
-}
-}
-
 RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype)
 {
     Context *rsc = static_cast<Context *>(con);
@@ -873,4 +873,3 @@
 
     return texAlloc;
 }
-
diff --git a/rsContext.cpp b/rsContext.cpp
index 944cd86..3f04585 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -222,6 +222,7 @@
     glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
 
     mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
+    mGL.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)mGL.mExtensions, "GL_NV_texture_npot_2D_mipmap");
     mGL.EXT_texture_max_aniso = 1.0f;
     bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
     if(hasAniso) {
@@ -970,12 +971,6 @@
     rsc->assignName(ob, name, len);
 }
 
-void rsi_GetName(Context *rsc, void * obj, const char **name)
-{
-    ObjectBase *ob = static_cast<ObjectBase *>(obj);
-    (*name) = ob->getName();
-}
-
 void rsi_ObjDestroy(Context *rsc, void *optr)
 {
     ObjectBase *ob = static_cast<ObjectBase *>(optr);
@@ -1062,3 +1057,10 @@
     rsc->deinitToClient();
 }
 
+// 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/rsContext.h b/rsContext.h
index dbe2c79..e269d4e 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -190,6 +190,7 @@
     mutable const ObjectBase * mObjHead;
 
     bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
+    bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
     float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
     uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
     uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
@@ -232,6 +233,7 @@
         int32_t mMaxVertexTextureUnits;
 
         bool OES_texture_npot;
+        bool GL_NV_texture_npot_2D_mipmap;
         float EXT_texture_max_aniso;
     } mGL;
 
diff --git a/rsElement.cpp b/rsElement.cpp
index 096115c..d207dcf 100644
--- a/rsElement.cpp
+++ b/rsElement.cpp
@@ -379,7 +379,10 @@
     return (RsElement)e;
 }
 
-void rsi_ElementGetNativeData(Context *rsc, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
+}
+}
+
+void rsaElementGetNativeData(RsContext con, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
 {
     rsAssert(elemDataSize == 5);
     // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
@@ -393,7 +396,7 @@
 
 }
 
-void rsi_ElementGetSubElements(Context *rsc, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
+void rsaElementGetSubElements(RsContext con, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
 {
     Element *e = static_cast<Element *>(elem);
     rsAssert(e->getFieldCount() == dataSize);
@@ -405,7 +408,3 @@
     }
 
 }
-
-
-}
-}
diff --git a/rsFileA3D.cpp b/rsFileA3D.cpp
index c90edc2..e4c6dbd 100644
--- a/rsFileA3D.cpp
+++ b/rsFileA3D.cpp
@@ -115,19 +115,10 @@
         return false;
     }
 
-    uint8_t *headerData = (uint8_t *)malloc(headerSize);
-    if(!headerData) {
-        return false;
-    }
-
-    memcpy(headerData, localData, headerSize);
-
     // Now open the stream to parse the header
-    IStream headerStream(headerData, false);
+    IStream headerStream(localData, false);
     parseHeader(&headerStream);
 
-    free(headerData);
-
     localData += headerSize;
     lengthRemaining -= headerSize;
 
@@ -145,13 +136,7 @@
     }
 
     // We should know enough to read the file in at this point.
-    mAlloc = malloc(mDataSize);
-    if (!mAlloc) {
-        return false;
-    }
-    mData = (uint8_t *)mAlloc;
-    memcpy(mAlloc, localData, mDataSize);
-
+    mData = (uint8_t *)localData;
     mReadStream = new IStream(mData, mUse64BitOffsets);
 
     return true;
@@ -383,7 +368,41 @@
 namespace android {
 namespace renderscript {
 
-void rsi_FileA3DGetNumIndexEntries(Context *rsc, int32_t *numEntries, RsFile file)
+RsFile rsi_FileOpen(Context *rsc, char const *path, unsigned int len)
+{
+    FileA3D *fa3d = new FileA3D(rsc);
+
+    FILE *f = fopen("/sdcard/test.a3d", "rb");
+    if (f) {
+        fa3d->load(f);
+        fclose(f);
+        fa3d->incUserRef();
+        return fa3d;
+    }
+    delete fa3d;
+    return NULL;
+}
+
+
+}
+}
+
+RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file)
+{
+    FileA3D *fa3d = static_cast<FileA3D *>(file);
+    if(!fa3d) {
+        LOGE("Can't load entry. No valid file");
+        return NULL;
+    }
+
+    ObjectBase *obj = fa3d->initializeFromEntry(index);
+    LOGV("Returning object with name %s", obj->getName());
+
+    return obj;
+}
+
+
+void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file)
 {
     FileA3D *fa3d = static_cast<FileA3D *>(file);
 
@@ -395,7 +414,7 @@
     }
 }
 
-void rsi_FileA3DGetIndexEntries(Context *rsc, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file)
+void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file)
 {
     FileA3D *fa3d = static_cast<FileA3D *>(file);
 
@@ -418,51 +437,17 @@
 
 }
 
-RsObjectBase rsi_FileA3DGetEntryByIndex(Context *rsc, uint32_t index, RsFile file)
-{
-    FileA3D *fa3d = static_cast<FileA3D *>(file);
-    if(!fa3d) {
-        LOGE("Can't load entry. No valid file");
-        return NULL;
-    }
-
-    ObjectBase *obj = fa3d->initializeFromEntry(index);
-    LOGV("Returning object with name %s", obj->getName());
-
-    return obj;
-}
-
-RsFile rsi_FileA3DCreateFromAssetStream(Context *rsc, const void *data, uint32_t len)
+RsFile rsaFileA3DCreateFromAssetStream(RsContext con, const void *data, uint32_t len)
 {
     if (data == NULL) {
         LOGE("File load failed. Asset stream is NULL");
         return NULL;
     }
 
+    Context *rsc = static_cast<Context *>(con);
     FileA3D *fa3d = new FileA3D(rsc);
-
-    fa3d->load(data, len);
     fa3d->incUserRef();
 
+    fa3d->load(data, len);
     return fa3d;
 }
-
-
-RsFile rsi_FileOpen(Context *rsc, char const *path, unsigned int len)
-{
-    FileA3D *fa3d = new FileA3D(rsc);
-
-    FILE *f = fopen("/sdcard/test.a3d", "rb");
-    if (f) {
-        fa3d->load(f);
-        fclose(f);
-        fa3d->incUserRef();
-        return fa3d;
-    }
-    delete fa3d;
-    return NULL;
-}
-
-
-}
-}
diff --git a/rsMesh.cpp b/rsMesh.cpp
index 761be93..fd604e5 100644
--- a/rsMesh.cpp
+++ b/rsMesh.cpp
@@ -310,19 +310,21 @@
     sm->updateGLPrimitives();
 }
 
-void rsi_MeshGetVertexBufferCount(Context *rsc, RsMesh mv, int32_t *numVtx)
+}}
+
+void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     *numVtx = sm->mVertexBufferCount;
 }
 
-void rsi_MeshGetIndexCount(Context *rsc, RsMesh mv, int32_t *numIdx)
+void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     *numIdx = sm->mPrimitivesCount;
 }
 
-void rsi_MeshGetVertices(Context *rsc, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount)
+void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     rsAssert(vtxDataCount == sm->mVertexBufferCount);
@@ -333,7 +335,7 @@
     }
 }
 
-void rsi_MeshGetIndices(Context *rsc, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount)
+void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     rsAssert(idxDataCount == sm->mPrimitivesCount);
@@ -347,8 +349,3 @@
     }
 
 }
-
-
-
-
-}}
diff --git a/rsSampler.cpp b/rsSampler.cpp
index cfae7b2..cbdc407 100644
--- a/rsSampler.cpp
+++ b/rsSampler.cpp
@@ -76,7 +76,11 @@
     };
 
     if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
+        if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
+        } else {
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
+        }
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
diff --git a/rsType.cpp b/rsType.cpp
index 7ef2464..caaa9f5 100644
--- a/rsType.cpp
+++ b/rsType.cpp
@@ -374,23 +374,6 @@
 namespace android {
 namespace renderscript {
 
-void rsi_TypeGetNativeData(Context *rsc, RsType type, uint32_t *typeData, uint32_t typeDataSize)
-{
-    rsAssert(typeDataSize == 6);
-    // Pack the data in the follofing way mDimX; mDimY; mDimZ;
-    // mDimLOD; mDimFaces; mElement; into typeData
-    Type *t = static_cast<Type *>(type);
-
-    (*typeData++) = t->getDimX();
-    (*typeData++) = t->getDimY();
-    (*typeData++) = t->getDimZ();
-    (*typeData++) = t->getDimLOD();
-    (*typeData++) = t->getDimFaces() ? 1 : 0;
-    (*typeData++) = (uint32_t)t->getElement();
-    t->getElement()->incUserRef();
-}
-
-
 }
 }
 
@@ -424,3 +407,18 @@
     return Type::getType(rsc, e, dimX, dimY, dimZ, dimLOD, dimFaces);
 }
 
+void rsaTypeGetNativeData(RsContext con, RsType type, uint32_t *typeData, uint32_t typeDataSize)
+{
+    rsAssert(typeDataSize == 6);
+    // Pack the data in the follofing way mDimX; mDimY; mDimZ;
+    // mDimLOD; mDimFaces; mElement; into typeData
+    Type *t = static_cast<Type *>(type);
+
+    (*typeData++) = t->getDimX();
+    (*typeData++) = t->getDimY();
+    (*typeData++) = t->getDimZ();
+    (*typeData++) = t->getDimLOD();
+    (*typeData++) = t->getDimFaces() ? 1 : 0;
+    (*typeData++) = (uint32_t)t->getElement();
+    t->getElement()->incUserRef();
+}