More API updates.

Change-Id: I754dc645ac08fa25019eed8fd8b7b3c47f178ff2
diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h
index 9e30799..43d4291 100644
--- a/libs/rs/RenderScript.h
+++ b/libs/rs/RenderScript.h
@@ -105,10 +105,10 @@
     RS_ALLOCATION_USAGE_ALL = 0x000F
 };
 
-enum RsAllocationMipmapGenerationControl {
-    RS_MIPMAP_NONE = 0,
-    RS_MIPMAP_FULL = 1,
-    RS_MIPMAP_TEXTURE_ONLY = 2
+enum RsAllocationMipmapControl {
+    RS_ALLOCATION_MIPMAP_NONE = 0,
+    RS_ALLOCATION_MIPMAP_FULL = 1,
+    RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE = 2
 };
 
 enum RsDataType {
@@ -345,13 +345,13 @@
 RsType rsaTypeCreate(RsContext, RsElement, uint32_t dimX, uint32_t dimY,
                      uint32_t dimZ, bool mips, bool faces);
 RsAllocation rsaAllocationCreateTyped(RsContext rsc, RsType vtype,
-                                      RsAllocationMipmapGenerationControl mips,
+                                      RsAllocationMipmapControl mips,
                                       uint32_t usages);
 RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
-                                           RsAllocationMipmapGenerationControl mips,
+                                           RsAllocationMipmapControl mips,
                                            const void *data, uint32_t usages);
 RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
-                                               RsAllocationMipmapGenerationControl mips,
+                                               RsAllocationMipmapControl mips,
                                                const void *data, uint32_t usages);
 
 #ifndef NO_RS_FUNCS
diff --git a/libs/rs/java/Balls/src/com/android/balls/BallsRS.java b/libs/rs/java/Balls/src/com/android/balls/BallsRS.java
index 4338f33..0a06394 100644
--- a/libs/rs/java/Balls/src/com/android/balls/BallsRS.java
+++ b/libs/rs/java/Balls/src/com/android/balls/BallsRS.java
@@ -34,11 +34,12 @@
     private ProgramFragment mPFPoints;
     private ProgramVertex mPV;
     private ScriptField_Point mPoints;
-    private ScriptField_Point mArcs;
     private ScriptField_VpConsts mVpConsts;
 
     void updateProjectionMatrices() {
-        mVpConsts = new ScriptField_VpConsts(mRS, 1);
+        mVpConsts = new ScriptField_VpConsts(mRS, 1,
+                                             Allocation.USAGE_SCRIPT |
+                                             Allocation.USAGE_GRAPHICS_CONSTANTS);
         ScriptField_VpConsts.Item i = new ScriptField_VpConsts.Item();
         Matrix4f mvp = new Matrix4f();
         mvp.loadOrtho(0, mRS.getWidth(), mRS.getHeight(), 0, -1, 1);
@@ -67,9 +68,10 @@
     }
 
     private Allocation loadTexture(int id) {
-        final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes,
-                id, Element.RGB_565(mRS), false);
-        allocation.uploadToTexture(0);
+        final Allocation allocation =
+            Allocation.createFromBitmapResource(mRS, mRes,
+                id, Allocation.MipmapControl.MIPMAP_NONE,
+                Allocation.USAGE_GRAPHICS_TEXTURE);
         return allocation;
     }
 
@@ -88,31 +90,24 @@
         pfb.setVaryingColor(true);
         mPFLines = pfb.create();
 
+        android.util.Log.e("rs", "Load texture");
         mPFPoints.bindTexture(loadTexture(R.drawable.flares), 0);
 
-        mPoints = new ScriptField_Point(mRS, PART_COUNT);
-        mArcs = new ScriptField_Point(mRS, PART_COUNT * 2);
+        mPoints = new ScriptField_Point(mRS, PART_COUNT, Allocation.USAGE_SCRIPT);
 
         Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
         smb.addVertexAllocation(mPoints.getAllocation());
         smb.addIndexType(Primitive.POINT);
         Mesh smP = smb.create();
 
-        smb = new Mesh.AllocationBuilder(mRS);
-        smb.addVertexAllocation(mArcs.getAllocation());
-        smb.addIndexType(Primitive.LINE);
-        Mesh smA = smb.create();
-
         mPhysicsScript = new ScriptC_ball_physics(mRS, mRes, R.raw.ball_physics);
 
         mScript = new ScriptC_balls(mRS, mRes, R.raw.balls);
         mScript.set_partMesh(smP);
-        mScript.set_arcMesh(smA);
         mScript.set_physics_script(mPhysicsScript);
         mScript.bind_point(mPoints);
-        mScript.bind_arc(mArcs);
-        mScript.bind_balls1(new ScriptField_Ball(mRS, PART_COUNT));
-        mScript.bind_balls2(new ScriptField_Ball(mRS, PART_COUNT));
+        mScript.bind_balls1(new ScriptField_Ball(mRS, PART_COUNT, Allocation.USAGE_SCRIPT));
+        mScript.bind_balls2(new ScriptField_Ball(mRS, PART_COUNT, Allocation.USAGE_SCRIPT));
 
         mScript.set_gPFLines(mPFLines);
         mScript.set_gPFPoints(mPFPoints);
diff --git a/libs/rs/java/Balls/src/com/android/balls/balls.rs b/libs/rs/java/Balls/src/com/android/balls/balls.rs
index c41ed0f..fed9963 100644
--- a/libs/rs/java/Balls/src/com/android/balls/balls.rs
+++ b/libs/rs/java/Balls/src/com/android/balls/balls.rs
@@ -10,18 +10,14 @@
 rs_program_fragment gPFPoints;
 rs_program_fragment gPFLines;
 rs_mesh partMesh;
-rs_mesh arcMesh;
 
 typedef struct __attribute__((packed, aligned(4))) Point {
     float2 position;
-    //uchar4 color;
     float size;
 } Point_t;
 Point_t *point;
-Point_t *arc;
 
 typedef struct VpConsts {
-    //rs_matrix4x4 Proj;
     rs_matrix4x4 MVP;
 } VpConsts_t;
 VpConsts_t *vpConstants;
@@ -42,8 +38,6 @@
         balls1[ct].position.y = rsRand(0.f, (float)h);
         balls1[ct].delta.x = 0.f;
         balls1[ct].delta.y = 0.f;
-        //balls1[ct].arcID = -1;
-        //balls1[ct].color = 0.f;
         balls1[ct].size = 1.f;
 
         float r = rsRand(100.f);
@@ -76,28 +70,12 @@
 
     rsForEach(physics_script, bc.ain, bc.aout, &bc);
 
-    uint32_t arcIdx = 0;
     for (uint32_t ct=0; ct < bc.dimX; ct++) {
         point[ct].position = bout[ct].position;
-        ///point[ct].color = 0xff;//rsPackColorTo8888(bout[ct].color);
         point[ct].size = 6.f /*+ bout[ct].color.g * 6.f*/ * bout[ct].size;
-/*
-        if (bout[ct].arcID >= 0) {
-            arc[arcIdx].position = bout[ct].position;
-            arc[arcIdx].color.r = min(bout[ct].arcStr, 1.f) * 0xff;
-            arc[arcIdx].color.g = 0;
-            arc[arcIdx].color.b = 0;
-            arc[arcIdx].color.a = 0xff;
-            arc[arcIdx+1].position = bout[bout[ct].arcID].position;
-            arc[arcIdx+1].color = arc[arcIdx].color;
-            arcIdx += 2;
-        }
-        */
     }
 
     frame++;
-    //rsgBindProgramFragment(gPFLines);
-    //rsgDrawMesh(arcMesh, 0, 0, arcIdx);
     rsgBindProgramFragment(gPFPoints);
     rsgDrawMesh(partMesh);
     rsClearObject(&bc.ain);
diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec
index 14094c4..9f817b6 100644
--- a/libs/rs/rs.spec
+++ b/libs/rs/rs.spec
@@ -89,14 +89,6 @@
 	param size_t dataLen
 	}
 
-AllocationCreateBitmapRef {
-	param RsType type
-	param RsAsyncVoidPtr bmpPtr
-	param RsAsyncVoidPtr callbackData
-	param RsBitmapCallback_t callback
-	ret RsAllocation
-	}
-
 AllocationUploadToTexture {
 	param RsAllocation alloc
 	param bool genMipMaps
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp
index 10a5caf..aab789a 100644
--- a/libs/rs/rsAllocation.cpp
+++ b/libs/rs/rsAllocation.cpp
@@ -45,17 +45,6 @@
     }
 }
 
-Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
-                       void *callbackData, RsBitmapCallback_t callback)
-                       : ObjectBase(rsc) {
-    init(rsc, type);
-
-    mUsageFlags = RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
-
-    mPtr = bmp;
-    mUserBitmapCallback = callback;
-    mUserBitmapCallbackData = callbackData;
-}
 
 void Allocation::init(Context *rsc, const Type *type) {
     mPtr = NULL;
@@ -67,10 +56,10 @@
 
     mReadWriteRatio = 0;
     mUpdateSize = 0;
+    mUsageFlags = 0;
+    mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
 
-    mIsTexture = false;
     mTextureID = 0;
-    mIsVertexBuffer = false;
     mBufferID = 0;
     mUploadDefered = false;
 
@@ -121,21 +110,21 @@
 
 void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) {
     rsAssert(lodOffset < mType->getLODCount());
-    mIsTexture = true;
+    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
     mTextureLOD = lodOffset;
     mUploadDefered = true;
     mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
 }
 
 uint32_t Allocation::getGLTarget() const {
-    if (mIsTexture) {
+    if (getIsTexture()) {
         if (mType->getDimFaces()) {
             return GL_TEXTURE_CUBE_MAP;
         } else {
             return GL_TEXTURE_2D;
         }
     }
-    if (mIsVertexBuffer) {
+    if (getIsBufferObject()) {
         return GL_ARRAY_BUFFER;
     }
     return 0;
@@ -144,10 +133,10 @@
 void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
     rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
 
-    if (mIsTexture) {
+    if (getIsTexture()) {
         uploadToTexture(rsc);
     }
-    if (mIsVertexBuffer) {
+    if (getIsBufferObject()) {
         uploadToBufferObject(rsc);
     }
 
@@ -156,7 +145,7 @@
 
 void Allocation::uploadToTexture(const Context *rsc) {
 
-    mIsTexture = true;
+    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
     GLenum type = mType->getElement()->getComponent().getGLType();
     GLenum format = mType->getElement()->getComponent().getGLFormat();
 
@@ -257,7 +246,7 @@
 }
 
 void Allocation::deferedUploadToBufferObject(const Context *rsc) {
-    mIsVertexBuffer = true;
+    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
     mUploadDefered = true;
 }
 
@@ -265,7 +254,7 @@
     rsAssert(!mType->getDimY());
     rsAssert(!mType->getDimZ());
 
-    mIsVertexBuffer = true;
+    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
 
     if (!mBufferID) {
         glGenBuffers(1, &mBufferID);
@@ -470,8 +459,8 @@
     LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
           prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
 
-    LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
-          prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
+    LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
+          prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
 }
 
 void Allocation::serialize(OStream *stream) const {
@@ -517,7 +506,7 @@
         return NULL;
     }
 
-    Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_ALL);
+    Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
     alloc->setName(name.string(), name.size());
 
     // Read in all of our allocation data
@@ -672,81 +661,6 @@
     }
 }
 
-typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
-
-static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) {
-    memcpy(dst, src, count * 2);
-}
-static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) {
-    memcpy(dst, src, count);
-}
-static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) {
-    memcpy(dst, src, count * 4);
-}
-
-static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) {
-    uint16_t *d = static_cast<uint16_t *>(dst);
-    const uint8_t *s = static_cast<const uint8_t *>(src);
-
-    while (count--) {
-        *d = rs888to565(s[0], s[1], s[2]);
-        d++;
-        s+= 3;
-    }
-}
-
-static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) {
-    uint16_t *d = static_cast<uint16_t *>(dst);
-    const uint8_t *s = static_cast<const uint8_t *>(src);
-
-    while (count--) {
-        *d = rs888to565(s[0], s[1], s[2]);
-        d++;
-        s+= 4;
-    }
-}
-
-static ElementConverter_t pickConverter(const Element *dst, const Element *src) {
-    GLenum srcGLType = src->getComponent().getGLType();
-    GLenum srcGLFmt = src->getComponent().getGLFormat();
-    GLenum dstGLType = dst->getComponent().getGLType();
-    GLenum dstGLFmt = dst->getComponent().getGLFormat();
-
-    if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
-        switch (dst->getSizeBytes()) {
-        case 4:
-            return elementConverter_cpy_32;
-        case 2:
-            return elementConverter_cpy_16;
-        case 1:
-            return elementConverter_cpy_8;
-        }
-    }
-
-    if (srcGLType == GL_UNSIGNED_BYTE &&
-        srcGLFmt == GL_RGB &&
-        dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
-        dstGLFmt == GL_RGB) {
-
-        return elementConverter_888_to_565;
-    }
-
-    if (srcGLType == GL_UNSIGNED_BYTE &&
-        srcGLFmt == GL_RGBA &&
-        dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
-        dstGLFmt == GL_RGB) {
-
-        return elementConverter_8888_to_565;
-    }
-
-    LOGE("pickConverter, unsuported combo, src %p,  dst %p", src, dst);
-    LOGE("pickConverter, srcGLType = %x,  srcGLFmt = %x", srcGLType, srcGLFmt);
-    LOGE("pickConverter, dstGLType = %x,  dstGLFmt = %x", dstGLType, dstGLFmt);
-    src->dumpLOGV("SRC ");
-    dst->dumpLOGV("DST ");
-    return 0;
-}
-
 #ifndef ANDROID_RS_BUILD_FOR_HOST
 
 void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
@@ -754,15 +668,6 @@
     a->syncAll(rsc, src);
 }
 
-RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
-                                           void *bmp, void *callbackData,
-                                           RsBitmapCallback_t callback) {
-    const Type * type = static_cast<const Type *>(vtype);
-    Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
-    alloc->incUserRef();
-    return alloc;
-}
-
 void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
     Allocation *texAlloc = static_cast<Allocation *>(va);
     const Type * t = texAlloc->getType();
@@ -854,7 +759,7 @@
 }
 
 RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
-                                      RsAllocationMipmapGenerationControl mips,
+                                      RsAllocationMipmapControl mips,
                                       uint32_t usages) {
     Context *rsc = static_cast<Context *>(con);
     Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages);
@@ -864,7 +769,7 @@
 
 
 RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
-                                           RsAllocationMipmapGenerationControl mips,
+                                           RsAllocationMipmapControl mips,
                                            const void *data, uint32_t usages) {
     Context *rsc = static_cast<Context *>(con);
     Type *t = static_cast<Type *>(vtype);
@@ -877,7 +782,7 @@
     }
 
     memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
-    if (mips == RS_MIPMAP_FULL) {
+    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
         Adapter2D adapt(rsc, texAlloc);
         Adapter2D adapt2(rsc, texAlloc);
         for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
@@ -887,11 +792,12 @@
         }
     }
 
+    texAlloc->deferedUploadToTexture(rsc, false, 0);
     return texAlloc;
 }
 
 RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
-                                               RsAllocationMipmapGenerationControl mips,
+                                               RsAllocationMipmapControl mips,
                                                const void *data, uint32_t usages) {
     Context *rsc = static_cast<Context *>(con);
     Type *t = static_cast<Type *>(vtype);
@@ -917,7 +823,7 @@
         // Move the data pointer to the next cube face
         sourcePtr += cpySize;
 
-        if (mips == RS_MIPMAP_FULL) {
+        if (mips == RS_ALLOCATION_MIPMAP_FULL) {
             Adapter2D adapt(rsc, texAlloc);
             Adapter2D adapt2(rsc, texAlloc);
             adapt.setFace(face);
@@ -930,5 +836,6 @@
         }
     }
 
+    texAlloc->deferedUploadToTexture(rsc, false, 0);
     return texAlloc;
 }
diff --git a/libs/rs/rsAllocation.h b/libs/rs/rsAllocation.h
index e63c7ab..d1dcb73 100644
--- a/libs/rs/rsAllocation.h
+++ b/libs/rs/rsAllocation.h
@@ -30,7 +30,6 @@
 
 public:
     Allocation(Context *rsc, const Type *, uint32_t usages);
-    Allocation(Context *rsc, const Type *, void *bmp, void *callbackData, RsBitmapCallback_t callback);
 
     virtual ~Allocation();
 
@@ -88,8 +87,12 @@
 
     virtual void uploadCheck(Context *rsc);
 
-    bool getIsTexture() const {return mIsTexture;}
-    bool getIsBufferObject() const {return mIsVertexBuffer;}
+    bool getIsTexture() const {
+        return (mUsageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
+    }
+    bool getIsBufferObject() const {
+        return (mUsageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
+    }
 
     void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
     void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
@@ -115,6 +118,7 @@
     bool mGpuRead;
 
     uint32_t mUsageFlags;
+    RsAllocationMipmapControl mMipmapControl;
 
     // more usage hint data from the application
     // which can be used by a driver to pick the best memory type.
@@ -125,7 +129,6 @@
 
     // Is this a legal structure to be used as a texture source.
     // Initially this will require 1D or 2D and color data
-    bool mIsTexture;
     bool mTextureGenMipmap;
     uint32_t mTextureLOD;
     uint32_t mTextureID;
@@ -133,7 +136,6 @@
     // Is this a legal structure to be used as a vertex source.
     // Initially this will require 1D and x(yzw).  Additional per element data
     // is allowed.
-    bool mIsVertexBuffer;
     uint32_t mBufferID;
 
     bool mUploadDefered;