Removing fixed size arrays.

Change-Id: I5c65b29a197013de2517cfb6dbe7abb9e24a688b
diff --git a/libs/rs/rsScript.cpp b/libs/rs/rsScript.cpp
index 43bb09e..0e76dae 100644
--- a/libs/rs/rsScript.cpp
+++ b/libs/rs/rsScript.cpp
@@ -24,10 +24,37 @@
     mAllocFile = __FILE__;
     mAllocLine = __LINE__;
     memset(&mEnviroment, 0, sizeof(mEnviroment));
+
+    mSlots = NULL;
+    mTypes = NULL;
 }
 
 Script::~Script()
 {
+    if(mSlots) {
+        delete [] mSlots;
+        mSlots = NULL;
+    }
+    if(mTypes) {
+        delete [] mTypes;
+        mTypes = NULL;
+    }
+}
+
+void Script::initSlots() {
+    if(mEnviroment.mFieldCount > 0) {
+        mSlots = new ObjectBaseRef<Allocation>[mEnviroment.mFieldCount];
+        mTypes = new ObjectBaseRef<const Type>[mEnviroment.mFieldCount];
+    }
+}
+
+void Script::setSlot(uint32_t slot, Allocation *a) {
+    if(slot >= mEnviroment.mFieldCount) {
+        LOGE("Script::setSlot unable to set allocation, invalid slot index");
+        return;
+    }
+
+    mSlots[slot].set(a);
 }
 
 void Script::setVar(uint32_t slot, const void *val, uint32_t len)
@@ -51,7 +78,7 @@
 {
     Script *s = static_cast<Script *>(vs);
     Allocation *a = static_cast<Allocation *>(va);
-    s->mSlots[slot].set(a);
+    s->setSlot(slot, a);
     //LOGE("rsi_ScriptBindAllocation %i  %p  %p", slot, a, a->getPtr());
 }
 
@@ -61,15 +88,6 @@
     s->mEnviroment.mTimeZone = timeZone;
 }
 
-void rsi_ScriptSetType(Context * rsc, RsType vt, uint32_t slot, bool writable, const char *name)
-{
-    ScriptCState *ss = &rsc->mScriptC;
-    const Type *t = static_cast<const Type *>(vt);
-    ss->mConstantBufferTypes[slot].set(t);
-    ss->mSlotWritable[slot] = writable;
-    LOGE("rsi_ScriptSetType");
-}
-
 void rsi_ScriptInvoke(Context *rsc, RsScript vs, uint32_t slot)
 {
     Script *s = static_cast<Script *>(vs);
diff --git a/libs/rs/rsScript.h b/libs/rs/rsScript.h
index 0a20344..c73bb5e 100644
--- a/libs/rs/rsScript.h
+++ b/libs/rs/rsScript.h
@@ -29,8 +29,6 @@
 class ProgramRaster;
 class ProgramStore;
 
-#define MAX_SCRIPT_BANKS 32
-
 class Script : public ObjectBase
 {
 public:
@@ -61,10 +59,8 @@
     };
     Enviroment_t mEnviroment;
 
-    ObjectBaseRef<Allocation> mSlots[MAX_SCRIPT_BANKS];
-    ObjectBaseRef<const Type> mTypes[MAX_SCRIPT_BANKS];
-    bool mSlotWritable[MAX_SCRIPT_BANKS];
-
+    void initSlots();
+    void setSlot(uint32_t slot, Allocation *a);
     void setVar(uint32_t slot, const void *val, uint32_t len);
 
     virtual void runForEach(Context *rsc,
@@ -76,6 +72,10 @@
     virtual void Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) = 0;
     virtual void setupScript(Context *rsc) = 0;
     virtual uint32_t run(Context *) = 0;
+protected:
+    ObjectBaseRef<Allocation> *mSlots;
+    ObjectBaseRef<const Type> *mTypes;
+
 };
 
 
diff --git a/libs/rs/rsScriptC.cpp b/libs/rs/rsScriptC.cpp
index c6418be..d961fed 100644
--- a/libs/rs/rsScriptC.cpp
+++ b/libs/rs/rsScriptC.cpp
@@ -365,11 +365,6 @@
 void ScriptCState::clear(Context *rsc)
 {
     rsAssert(rsc);
-    for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
-        mConstantBufferTypes[ct].clear();
-        mSlotWritable[ct] = false;
-    }
-
     mScript.clear();
     mScript.set(new ScriptC(rsc));
 }
@@ -428,6 +423,7 @@
     else {
         s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
         bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
+        s->initSlots();
     }
 
     s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
@@ -532,10 +528,6 @@
     ss->runCompiler(rsc, s.get());
     s->incUserRef();
     s->setContext(rsc);
-    for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
-        s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get());
-        s->mSlotWritable[ct] = ss->mSlotWritable[ct];
-    }
 
     ss->clear(rsc);
     return s.get();
diff --git a/libs/rs/rsScriptC.h b/libs/rs/rsScriptC.h
index 7ec80aa..e5b5ba9 100644
--- a/libs/rs/rsScriptC.h
+++ b/libs/rs/rsScriptC.h
@@ -81,11 +81,6 @@
 
     ObjectBaseRef<ScriptC> mScript;
 
-    ObjectBaseRef<const Type> mConstantBufferTypes[MAX_SCRIPT_BANKS];
-    //String8 mSlotNames[MAX_SCRIPT_BANKS];
-    bool mSlotWritable[MAX_SCRIPT_BANKS];
-    //String8 mInvokableNames[MAX_SCRIPT_BANKS];
-
     void init(Context *rsc);
 
     void clear(Context *rsc);
diff --git a/libs/rs/rsType.cpp b/libs/rs/rsType.cpp
index 8cdb48a..33776c3 100644
--- a/libs/rs/rsType.cpp
+++ b/libs/rs/rsType.cpp
@@ -31,6 +31,8 @@
     mAllocLine = __LINE__;
     mLODs = 0;
     mLODCount = 0;
+    mAttribs = NULL;
+    mAttribsSize = 0;
     clear();
 }
 
@@ -44,6 +46,11 @@
     }
     if (mLODs) {
         delete [] mLODs;
+        mLODs = NULL;
+    }
+    if(mAttribs) {
+        delete [] mAttribs;
+        mAttribs = NULL;
     }
 }
 
@@ -145,6 +152,21 @@
 
 void Type::makeGLComponents()
 {
+    // Count the number of gl attrs to initialize
+    mAttribsSize = 0;
+    for (uint32_t ct=0; ct < getElement()->getFieldCount(); ct++) {
+        if(getElement()->getFieldName(ct)[0] != '#') {
+            mAttribsSize ++;
+        }
+    }
+    if(mAttribs) {
+        delete [] mAttribs;
+        mAttribs = NULL;
+    }
+    if(mAttribsSize) {
+        mAttribs = new VertexArray::Attrib[mAttribsSize];
+    }
+
     uint32_t userNum = 0;
     for (uint32_t ct=0; ct < getElement()->getFieldCount(); ct++) {
         const Component &c = getElement()->getField(ct)->getComponent();
@@ -160,11 +182,8 @@
         String8 tmp(RS_SHADER_ATTR);
         tmp.append(getElement()->getFieldName(ct));
         mAttribs[userNum].name.setTo(tmp.string());
-        userNum ++;
 
-        if(userNum == RS_MAX_ATTRIBS) {
-            return;
-        }
+        userNum ++;
     }
 }
 
@@ -172,7 +191,13 @@
 void Type::enableGLVertexBuffer(VertexArray *va) const
 {
     uint32_t stride = mElement->getSizeBytes();
-    for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
+    for (uint32_t ct=0; ct < mAttribsSize; ct++) {
+        // Load up to RS_MAX_ATTRIBS inputs
+        // TODO: grow vertexarray dynamically
+        if(ct >= RS_MAX_ATTRIBS) {
+            LOGE("More GL attributes than we can handle");
+            break;
+        }
         if (mAttribs[ct].size) {
             va->add(mAttribs[ct], stride);
         }
diff --git a/libs/rs/rsType.h b/libs/rs/rsType.h
index b5548c0..9099bf1 100644
--- a/libs/rs/rsType.h
+++ b/libs/rs/rsType.h
@@ -119,7 +119,8 @@
     LOD *mLODs;
     uint32_t mLODCount;
 
-    VertexArray::Attrib mAttribs[RS_MAX_ATTRIBS];
+    VertexArray::Attrib *mAttribs;
+    uint32_t mAttribsSize;
     void makeGLComponents();
 
 private: