Fix mipmap bug introduced with Allocation cleanup.
Add syncAll to rsg headers.

Change-Id: I02c953f3fddab3c6f4e98570cdad45a59711bbde
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 30475bd..1bddbae 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -452,13 +452,9 @@
         return new Allocation(id, rs, t, usage);
     }
 
-    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
-                                              Element dstFmt, boolean genMips) {
-        MipmapControl mc = MipmapControl.MIPMAP_NONE;
-        if (genMips) {
-            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
-        }
-        return createFromBitmap(rs, b, mc, USAGE_GRAPHICS_TEXTURE);
+    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
+        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
+                                USAGE_GRAPHICS_TEXTURE);
     }
 
     static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
@@ -500,14 +496,9 @@
     }
 
     static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
-                                                     Element dstFmt,
-                                                     boolean genMips,
                                                      CubemapLayout layout) {
-        MipmapControl mc = MipmapControl.MIPMAP_NONE;
-        if (genMips) {
-            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
-        }
-        return createCubemapFromBitmap(rs, b, mc, layout, USAGE_GRAPHICS_TEXTURE);
+        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
+                                       layout, USAGE_GRAPHICS_TEXTURE);
     }
 
     static public Allocation createFromBitmapResource(RenderScript rs,
@@ -525,6 +516,15 @@
 
     static public Allocation createFromBitmapResource(RenderScript rs,
                                                       Resources res,
+                                                      int id) {
+        return createFromBitmapResource(rs, res, id,
+                                        MipmapControl.MIPMAP_NONE,
+                                        USAGE_GRAPHICS_TEXTURE);
+    }
+
+/*
+    static public Allocation createFromBitmapResource(RenderScript rs,
+                                                      Resources res,
                                                       int id,
                                                       Element dstFmt,
                                                       boolean genMips) {
@@ -534,7 +534,7 @@
         }
         return createFromBitmapResource(rs, res, id, mc, USAGE_GRAPHICS_TEXTURE);
     }
-
+*/
     static public Allocation createFromString(RenderScript rs,
                                               String str,
                                               int usage) {
diff --git a/libs/rs/java/ModelViewer/src/com/android/modelviewer/SceneGraphRS.java b/libs/rs/java/ModelViewer/src/com/android/modelviewer/SceneGraphRS.java
index f61cf25..6cb50b8 100644
--- a/libs/rs/java/ModelViewer/src/com/android/modelviewer/SceneGraphRS.java
+++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/SceneGraphRS.java
@@ -128,9 +128,9 @@
     }
 
     private void loadImage() {
-        mGridImage = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot, Element.RGB_565(mRS), true);
-        mGridImage.uploadToTexture(0);
-
+        mGridImage = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot,
+                                                         Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                                                         Allocation.USAGE_GRAPHICS_TEXTURE);
         mScript.set_gTGrid(mGridImage);
     }
 
diff --git a/libs/rs/java/ModelViewer/src/com/android/modelviewer/SimpleModelRS.java b/libs/rs/java/ModelViewer/src/com/android/modelviewer/SimpleModelRS.java
index 22b3fff..747463a 100644
--- a/libs/rs/java/ModelViewer/src/com/android/modelviewer/SimpleModelRS.java
+++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/SimpleModelRS.java
@@ -124,9 +124,9 @@
     }
 
     private void loadImage() {
-        mGridImage = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot, Element.RGB_565(mRS), true);
-        mGridImage.uploadToTexture(0);
-
+        mGridImage = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot,
+                                                         Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                                                         Allocation.USAGE_GRAPHICS_TEXTURE);
         mScript.set_gTGrid(mGridImage);
     }
 
diff --git a/libs/rs/java/Samples/src/com/android/samples/RsBenchRS.java b/libs/rs/java/Samples/src/com/android/samples/RsBenchRS.java
index 212e7a8..a47c308 100644
--- a/libs/rs/java/Samples/src/com/android/samples/RsBenchRS.java
+++ b/libs/rs/java/Samples/src/com/android/samples/RsBenchRS.java
@@ -23,6 +23,7 @@
 import android.graphics.BitmapFactory;
 import android.renderscript.*;
 import android.renderscript.Allocation.CubemapLayout;
+import android.renderscript.Allocation.MipmapControl;
 import android.renderscript.Program.TextureType;
 import android.renderscript.ProgramStore.DepthFunc;
 import android.renderscript.Sampler.Value;
@@ -284,17 +285,16 @@
     }
 
     private Allocation loadTextureRGB(int id) {
-        final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes,
-                id, Element.RGB_565(mRS), true);
-        allocation.uploadToTexture(0);
-        return allocation;
+        return Allocation.createFromBitmapResource(mRS, mRes, id,
+                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                Allocation.USAGE_GRAPHICS_TEXTURE);
     }
 
     private Allocation loadTextureARGB(int id) {
         Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
-        final Allocation allocation = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888(mRS), true);
-        allocation.uploadToTexture(0);
-        return allocation;
+        return Allocation.createFromBitmap(mRS, b,
+                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                Allocation.USAGE_GRAPHICS_TEXTURE);
     }
 
     private void loadImages() {
@@ -303,9 +303,8 @@
         mTexTransparent = loadTextureARGB(R.drawable.leaf);
         mTexChecker = loadTextureRGB(R.drawable.checker);
         Bitmap b = BitmapFactory.decodeResource(mRes, R.drawable.cubemap_test);
-        mTexCube = Allocation.createCubemapFromBitmap(mRS, b, Element.RGB_565(mRS), false,
+        mTexCube = Allocation.createCubemapFromBitmap(mRS, b,
                                                       Allocation.CubemapLayout.VERTICAL_FACE_LIST);
-        mTexCube.uploadToTexture(0);
 
         mScript.set_gTexTorus(mTexTorus);
         mScript.set_gTexOpaque(mTexOpaque);
diff --git a/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java
index 6258c9b..75e8d99 100644
--- a/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java
+++ b/libs/rs/java/Samples/src/com/android/samples/RsRenderStatesRS.java
@@ -274,17 +274,16 @@
     }
 
     private Allocation loadTextureRGB(int id) {
-        final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes,
-                id, Element.RGB_565(mRS), true);
-        allocation.uploadToTexture(0);
-        return allocation;
+        return Allocation.createFromBitmapResource(mRS, mRes, id,
+                                                   Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                                                   Allocation.USAGE_GRAPHICS_TEXTURE);
     }
 
     private Allocation loadTextureARGB(int id) {
         Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
-        final Allocation allocation = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888(mRS), true);
-        allocation.uploadToTexture(0);
-        return allocation;
+        return Allocation.createFromBitmap(mRS, b,
+                                           Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
+                                           Allocation.USAGE_GRAPHICS_TEXTURE);
     }
 
     private void loadImages() {
@@ -293,9 +292,8 @@
         mTexTransparent = loadTextureARGB(R.drawable.leaf);
         mTexChecker = loadTextureRGB(R.drawable.checker);
         Bitmap b = BitmapFactory.decodeResource(mRes, R.drawable.cubemap_test);
-        mTexCube = Allocation.createCubemapFromBitmap(mRS, b, Element.RGB_565(mRS), false,
+        mTexCube = Allocation.createCubemapFromBitmap(mRS, b,
                                                       Allocation.CubemapLayout.VERTICAL_FACE_LIST);
-        mTexCube.uploadToTexture(0);
 
         mScript.set_gTexTorus(mTexTorus);
         mScript.set_gTexOpaque(mTexOpaque);
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp
index 78b570a..cb00223 100644
--- a/libs/rs/rsAllocation.cpp
+++ b/libs/rs/rsAllocation.cpp
@@ -191,13 +191,6 @@
         uploadCubeTexture(isFirstUpload);
     }
 
-    if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
-#ifndef ANDROID_RS_BUILD_FOR_HOST
-        glGenerateMipmap(target);
-#endif //ANDROID_RS_BUILD_FOR_HOST
-    }
-
-
     if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
         freeScriptMemory();
     }
@@ -227,6 +220,12 @@
                             format, type, p);
         }
     }
+
+    if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
+#ifndef ANDROID_RS_BUILD_FOR_HOST
+        glGenerateMipmap(target);
+#endif //ANDROID_RS_BUILD_FOR_HOST
+    }
 }
 
 void Allocation::uploadCubeTexture(bool isFirstUpload) {
@@ -266,6 +265,12 @@
             }
         }
     }
+
+    if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
+#ifndef ANDROID_RS_BUILD_FOR_HOST
+        glGenerateMipmap(target);
+#endif //ANDROID_RS_BUILD_FOR_HOST
+    }
 }
 
 void Allocation::deferedUploadToBufferObject(const Context *rsc) {
diff --git a/libs/rs/rsFont.cpp b/libs/rs/rsFont.cpp
index 3f9a9d6..2fa1f0a 100644
--- a/libs/rs/rsFont.cpp
+++ b/libs/rs/rsFont.cpp
@@ -463,7 +463,7 @@
 
     // This will dirty the texture and the shader so next time
     // we draw it will upload the data
-    mTextTexture->deferedUploadToTexture(mRSC);
+    mTextTexture->syncAll(mRSC, RS_ALLOCATION_USAGE_SCRIPT);
     mFontShaderF->bindTexture(mRSC, 0, mTextTexture.get());
 
     // Some debug code
@@ -529,7 +529,7 @@
 
     Allocation *cacheAlloc = new Allocation(mRSC, texType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE);
     mTextTexture.set(cacheAlloc);
-    mTextTexture->deferedUploadToTexture(mRSC);
+    mTextTexture->syncAll(mRSC, RS_ALLOCATION_USAGE_SCRIPT);
 
     // Split up our cache texture into lines of certain widths
     int32_t nextLine = 0;
diff --git a/libs/rs/rsScriptC_LibGL.cpp b/libs/rs/rsScriptC_LibGL.cpp
index 0f84e4b..fb5980a 100644
--- a/libs/rs/rsScriptC_LibGL.cpp
+++ b/libs/rs/rsScriptC_LibGL.cpp
@@ -275,6 +275,18 @@
     pf->setConstantColor(rsc, r, g, b, a);
 }
 
+static void SC_allocationSyncAll(RsAllocation va) {
+    CHECK_OBJ(va);
+    GET_TLS();
+    static_cast<Allocation *>(va)->syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
+}
+
+static void SC_allocationSyncAll2(RsAllocation va, RsAllocationUsageType source) {
+    CHECK_OBJ(va);
+    GET_TLS();
+    static_cast<Allocation *>(va)->syncAll(rsc, source);
+}
+
 static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel) {
     CHECK_OBJ(va);
     GET_TLS();
@@ -425,6 +437,8 @@
     { "_Z11rsgGetWidthv", (void *)&SC_getWidth, false },
     { "_Z12rsgGetHeightv", (void *)&SC_getHeight, false },
 
+    { "_Z20rsgAllocationSyncAll13rs_allocation", (void *)&SC_allocationSyncAll, false },
+
     { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2, false },
     { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture, false },
     { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject, false },
diff --git a/libs/rs/scriptc/rs_graphics.rsh b/libs/rs/scriptc/rs_graphics.rsh
index 54e6328..3e708aa 100644
--- a/libs/rs/scriptc/rs_graphics.rsh
+++ b/libs/rs/scriptc/rs_graphics.rsh
@@ -35,6 +35,9 @@
     rsgGetHeight(void);
 
 extern void __attribute__((overloadable))
+    rsgAllocationSyncAll(rs_allocation);
+
+extern void __attribute__((overloadable))
     rsgUploadToTexture(rs_allocation);
 extern void __attribute__((overloadable))
     rsgUploadToTexture(rs_allocation, uint mipLevel);