Support for cubemaps.

Change-Id: Iaf6087f614451a8e233b3e5bc49c834ab0ad08ee
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 0de53f2..bad1208 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -34,6 +34,18 @@
     Type mType;
     Bitmap mBitmap;
 
+    public enum CubemapLayout {
+        VERTICAL_FACE_LIST (0),
+        HORIZONTAL_FACE_LIST (1),
+        VERTICAL_CROSS (2),
+        HORIZONTAL_CROSS (3);
+
+        int mID;
+        CubemapLayout(int id) {
+            mID = id;
+        }
+    }
+
     Allocation(int id, RenderScript rs, Type t) {
         super(id, rs);
         mType = t;
@@ -355,18 +367,21 @@
         throw new RSInvalidStateException("Bad bitmap type: " + bc);
     }
 
-    static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
+    static private Type typeFromBitmap(RenderScript rs, Bitmap b, boolean mip) {
         Element e = elementFromBitmap(rs, b);
         Type.Builder tb = new Type.Builder(rs, e);
         tb.add(Dimension.X, b.getWidth());
         tb.add(Dimension.Y, b.getHeight());
+        if (mip) {
+            tb.add(Dimension.LOD, 1);
+        }
         return tb.create();
     }
 
-    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) {
-
+    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
+                                              Element dstFmt, boolean genMips) {
         rs.validate();
-        Type t = typeFromBitmap(rs, b);
+        Type t = typeFromBitmap(rs, b, genMips);
 
         int id = rs.nAllocationCreateFromBitmap(dstFmt.getID(), genMips, b);
         if(id == 0) {
@@ -375,10 +390,49 @@
         return new Allocation(id, rs, t);
     }
 
+    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
+                                                     Element dstFmt,
+                                                     boolean genMips,
+                                                     CubemapLayout layout) {
+        rs.validate();
+        int height = b.getHeight();
+        int width = b.getWidth();
+
+        if (layout != CubemapLayout.VERTICAL_FACE_LIST) {
+            throw new RSIllegalArgumentException("Only vertical face list supported");
+        }
+        if (height % 6 != 0) {
+            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
+        }
+        if (height / 6 != width) {
+            throw new RSIllegalArgumentException("Only square cobe map faces supported");
+        }
+        boolean isPow2 = (width & (width - 1)) == 0;
+        if (!isPow2) {
+            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
+        }
+
+        Element e = elementFromBitmap(rs, b);
+        Type.Builder tb = new Type.Builder(rs, e);
+        tb.add(Dimension.X, width);
+        tb.add(Dimension.Y, width);
+        tb.add(Dimension.FACE, 1);
+        if (genMips) {
+            tb.add(Dimension.LOD, 1);
+        }
+        Type t = tb.create();
+
+        int id = rs.nAllocationCubeCreateFromBitmap(dstFmt.getID(), genMips, b);
+        if(id == 0) {
+            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
+        }
+        return new Allocation(id, rs, t);
+    }
+
     static public Allocation createBitmapRef(RenderScript rs, Bitmap b) {
 
         rs.validate();
-        Type t = typeFromBitmap(rs, b);
+        Type t = typeFromBitmap(rs, b, false);
 
         int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
         if(id == 0) {
@@ -404,7 +458,9 @@
             if (aId == 0) {
                 throw new RSRuntimeException("Load failed.");
             }
-            return new Allocation(aId, rs, null);
+            Allocation alloc = new Allocation(aId, rs, null);
+            alloc.updateFromNative();
+            return alloc;
         } finally {
             if (is != null) {
                 try {
diff --git a/graphics/java/android/renderscript/Program.java b/graphics/java/android/renderscript/Program.java
index 83c3601..22f3fc5 100644
--- a/graphics/java/android/renderscript/Program.java
+++ b/graphics/java/android/renderscript/Program.java
@@ -36,9 +36,32 @@
     public static final int MAX_CONSTANT = 8;
     public static final int MAX_TEXTURE = 8;
 
+    public enum TextureType {
+        TEXTURE_2D (0),
+        TEXTURE_CUBE (1);
+
+        int mID;
+        TextureType(int id) {
+            mID = id;
+        }
+    }
+
+    enum ProgramParam {
+        INPUT (0),
+        OUTPUT (1),
+        CONSTANT (2),
+        TEXTURE_TYPE (3);
+
+        int mID;
+        ProgramParam(int id) {
+            mID = id;
+        }
+    };
+
     Element mInputs[];
     Element mOutputs[];
     Type mConstants[];
+    TextureType mTextures[];
     int mTextureCount;
     String mShader;
 
@@ -54,27 +77,34 @@
             a.getType().getID() != mConstants[slot].getID()) {
             throw new IllegalArgumentException("Allocation type does not match slot type.");
         }
-        mRS.nProgramBindConstants(getID(), slot, a.getID());
+        int id = a != null ? a.getID() : 0;
+        mRS.nProgramBindConstants(getID(), slot, id);
     }
 
     public void bindTexture(Allocation va, int slot)
         throws IllegalArgumentException {
         mRS.validate();
-        if((slot < 0) || (slot >= mTextureCount)) {
+        if ((slot < 0) || (slot >= mTextureCount)) {
             throw new IllegalArgumentException("Slot ID out of range.");
         }
+        if (va != null && va.getType().getFaces() &&
+            mTextures[slot] != TextureType.TEXTURE_CUBE) {
+            throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
+        }
 
-        mRS.nProgramBindTexture(getID(), slot, va.getID());
+        int id = va != null ? va.getID() : 0;
+        mRS.nProgramBindTexture(getID(), slot, id);
     }
 
     public void bindSampler(Sampler vs, int slot)
         throws IllegalArgumentException {
         mRS.validate();
-        if((slot < 0) || (slot >= mTextureCount)) {
+        if ((slot < 0) || (slot >= mTextureCount)) {
             throw new IllegalArgumentException("Slot ID out of range.");
         }
 
-        mRS.nProgramBindSampler(getID(), slot, vs.getID());
+        int id = vs != null ? vs.getID() : 0;
+        mRS.nProgramBindSampler(getID(), slot, id);
     }
 
 
@@ -84,6 +114,7 @@
         Element mOutputs[];
         Type mConstants[];
         Type mTextures[];
+        TextureType mTextureTypes[];
         int mInputCount;
         int mOutputCount;
         int mConstantCount;
@@ -100,6 +131,7 @@
             mOutputCount = 0;
             mConstantCount = 0;
             mTextureCount = 0;
+            mTextureTypes = new TextureType[MAX_TEXTURE];
         }
 
         public BaseProgramBuilder setShader(String s) {
@@ -192,6 +224,17 @@
                 throw new IllegalArgumentException("Max texture count exceeded.");
             }
             mTextureCount = count;
+            for (int i = 0; i < mTextureCount; i ++) {
+                mTextureTypes[i] = TextureType.TEXTURE_2D;
+            }
+            return this;
+        }
+
+        public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
+            if(mTextureCount >= MAX_TEXTURE) {
+                throw new IllegalArgumentException("Max texture count exceeded.");
+            }
+            mTextureTypes[mTextureCount ++] = texType;
             return this;
         }
 
@@ -203,6 +246,8 @@
             p.mConstants = new Type[mConstantCount];
             System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
             p.mTextureCount = mTextureCount;
+            p.mTextures = new TextureType[mTextureCount];
+            System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
         }
     }
 
diff --git a/graphics/java/android/renderscript/ProgramFragment.java b/graphics/java/android/renderscript/ProgramFragment.java
index d30e483..faaf980 100644
--- a/graphics/java/android/renderscript/ProgramFragment.java
+++ b/graphics/java/android/renderscript/ProgramFragment.java
@@ -37,23 +37,25 @@
 
         public ProgramFragment create() {
             mRS.validate();
-            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + 1) * 2];
+            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
             int idx = 0;
 
             for (int i=0; i < mInputCount; i++) {
-                tmp[idx++] = 0;
+                tmp[idx++] = ProgramParam.INPUT.mID;
                 tmp[idx++] = mInputs[i].getID();
             }
             for (int i=0; i < mOutputCount; i++) {
-                tmp[idx++] = 1;
+                tmp[idx++] = ProgramParam.OUTPUT.mID;
                 tmp[idx++] = mOutputs[i].getID();
             }
             for (int i=0; i < mConstantCount; i++) {
-                tmp[idx++] = 2;
+                tmp[idx++] = ProgramParam.CONSTANT.mID;
                 tmp[idx++] = mConstants[i].getID();
             }
-            tmp[idx++] = 3;
-            tmp[idx++] = mTextureCount;
+            for (int i=0; i < mTextureCount; i++) {
+                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
+                tmp[idx++] = mTextureTypes[i].mID;
+            }
 
             int id = mRS.nProgramFragmentCreate(mShader, tmp);
             ProgramFragment pf = new ProgramFragment(id, mRS);
diff --git a/graphics/java/android/renderscript/ProgramVertex.java b/graphics/java/android/renderscript/ProgramVertex.java
index 13f017a..998e05e 100644
--- a/graphics/java/android/renderscript/ProgramVertex.java
+++ b/graphics/java/android/renderscript/ProgramVertex.java
@@ -46,23 +46,25 @@
 
         public ProgramVertex create() {
             mRS.validate();
-            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount +1) * 2];
+            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
             int idx = 0;
 
             for (int i=0; i < mInputCount; i++) {
-                tmp[idx++] = 0;
+                tmp[idx++] = ProgramParam.INPUT.mID;
                 tmp[idx++] = mInputs[i].getID();
             }
             for (int i=0; i < mOutputCount; i++) {
-                tmp[idx++] = 1;
+                tmp[idx++] = ProgramParam.OUTPUT.mID;
                 tmp[idx++] = mOutputs[i].getID();
             }
             for (int i=0; i < mConstantCount; i++) {
-                tmp[idx++] = 2;
+                tmp[idx++] = ProgramParam.CONSTANT.mID;
                 tmp[idx++] = mConstants[i].getID();
             }
-            tmp[idx++] = 3;
-            tmp[idx++] = mTextureCount;
+            for (int i=0; i < mTextureCount; i++) {
+                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
+                tmp[idx++] = mTextureTypes[i].mID;
+            }
 
             int id = mRS.nProgramVertexCreate(mShader, tmp);
             ProgramVertex pv = new ProgramVertex(id, mRS);
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index dcf86e3..0660441 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -204,6 +204,10 @@
     synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
         return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
     }
+    native int  rsnAllocationCubeCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
+    synchronized int nAllocationCubeCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
+        return rsnAllocationCubeCreateFromBitmap(mContext, dstFmt, genMips, bmp);
+    }
     native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
     synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
         return rsnAllocationCreateBitmapRef(mContext, type, bmp);