Seperate ProgramRaster.
Cleanup ProgramRaster and ProgramStore creation.

Change-Id: If25ea74355238d405340f0ccfb8117ad6e1307b7
diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk
index a07955a..3362c7d 100644
--- a/libs/rs/Android.mk
+++ b/libs/rs/Android.mk
@@ -117,6 +117,7 @@
 	rsVertexArray.cpp \
 	driver/rsdBcc.cpp \
 	driver/rsdCore.cpp \
+	driver/rsdProgramRaster.cpp \
 	driver/rsdProgramStore.cpp
 
 
diff --git a/libs/rs/driver/rsdCore.cpp b/libs/rs/driver/rsdCore.cpp
index 448be6a..00d62ba 100644
--- a/libs/rs/driver/rsdCore.cpp
+++ b/libs/rs/driver/rsdCore.cpp
@@ -17,6 +17,7 @@
 #include "rsdCore.h"
 #include "rsdBcc.h"
 #include "rsdProgramStore.h"
+#include "rsdProgramRaster.h"
 
 #include <malloc.h>
 #include "rsContext.h"
@@ -56,6 +57,12 @@
         rsdProgramStoreInit,
         rsdProgramStoreSetActive,
         rsdProgramStoreDestroy
+    },
+
+    {
+        rsdProgramRasterInit,
+        rsdProgramRasterSetActive,
+        rsdProgramRasterDestroy
     }
 
 };
diff --git a/libs/rs/driver/rsdProgramRaster.cpp b/libs/rs/driver/rsdProgramRaster.cpp
new file mode 100644
index 0000000..65995be
--- /dev/null
+++ b/libs/rs/driver/rsdProgramRaster.cpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "rsdCore.h"
+#include "rsdProgramStore.h"
+
+#include "rsContext.h"
+#include "rsProgramStore.h"
+
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
+
+using namespace android;
+using namespace android::renderscript;
+
+bool rsdProgramRasterInit(const Context *, const ProgramRaster *) {
+    return true;
+}
+
+void rsdProgramRasterSetActive(const Context *, const ProgramRaster *pr) {
+    switch (pr->mHal.state.cull) {
+        case RS_CULL_BACK:
+            glEnable(GL_CULL_FACE);
+            glCullFace(GL_BACK);
+            break;
+        case RS_CULL_FRONT:
+            glEnable(GL_CULL_FACE);
+            glCullFace(GL_FRONT);
+            break;
+        case RS_CULL_NONE:
+            glDisable(GL_CULL_FACE);
+            break;
+    }
+
+}
+
+void rsdProgramRasterDestroy(const Context *, const ProgramRaster *) {
+}
+
+
diff --git a/libs/rs/driver/rsdProgramRaster.h b/libs/rs/driver/rsdProgramRaster.h
new file mode 100644
index 0000000..20adaad
--- /dev/null
+++ b/libs/rs/driver/rsdProgramRaster.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RSD_PROGRAM_RASTER_H
+#define RSD_PROGRAM_RASTER_H
+
+#include <rs_hal.h>
+
+
+bool rsdProgramRasterInit(const android::renderscript::Context *rsc,
+                         const android::renderscript::ProgramRaster *);
+void rsdProgramRasterSetActive(const android::renderscript::Context *rsc,
+                              const android::renderscript::ProgramRaster *);
+void rsdProgramRasterDestroy(const android::renderscript::Context *rsc,
+                            const android::renderscript::ProgramRaster *);
+
+
+#endif
diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec
index bbb6200..a7f473c 100644
--- a/libs/rs/rs.spec
+++ b/libs/rs/rs.spec
@@ -251,36 +251,16 @@
 	}
 
 
-ProgramStoreBegin {
-	param RsElement in
-	param RsElement out
-	}
-
-ProgramStoreColorMask {
-	param bool r
-	param bool g
-	param bool b
-	param bool a
-	}
-
-ProgramStoreBlendFunc {
+ProgramStoreCreate {
+	param bool colorMaskR
+	param bool colorMaskG
+	param bool colorMaskB
+	param bool colorMaskA
+        param bool depthMask
+        param bool ditherEnable
 	param RsBlendSrcFunc srcFunc
 	param RsBlendDstFunc destFunc
-	}
-
-ProgramStoreDepthMask {
-	param bool enable
-}
-
-ProgramStoreDither {
-	param bool enable
-}
-
-ProgramStoreDepthFunc {
-	param RsDepthFunc func
-}
-
-ProgramStoreCreate {
+        param RsDepthFunc depthFunc
 	ret RsProgramStore
 	}
 
@@ -288,19 +268,11 @@
 	param bool pointSmooth
 	param bool lineSmooth
 	param bool pointSprite
+	param float lineWidth
+	param RsCullMode cull
 	ret RsProgramRaster
 }
 
-ProgramRasterSetLineWidth {
-	param RsProgramRaster pr
-	param float lw
-}
-
-ProgramRasterSetCullMode {
-	param RsProgramRaster pr
-	param RsCullMode mode
-}
-
 ProgramBindConstants {
 	param RsProgram vp
 	param uint32_t slot
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index d727ba1..c9a7060 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -405,16 +405,16 @@
         return false;
     }
 
-    mFragmentStore->setupGL2(this, &mStateFragmentStore);
+    mFragmentStore->setup(this, &mStateFragmentStore);
     mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
-    mRaster->setupGL2(this, &mStateRaster);
+    mRaster->setup(this, &mStateRaster);
     mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
     mFBOCache.setupGL2(this);
     return true;
 }
 
 void Context::setupProgramStore() {
-    mFragmentStore->setupGL2(this, &mStateFragmentStore);
+    mFragmentStore->setup(this, &mStateFragmentStore);
 }
 
 static bool getProp(const char *str) {
diff --git a/libs/rs/rsFont.cpp b/libs/rs/rsFont.cpp
index 0871d2f7..cb83435 100644
--- a/libs/rs/rsFont.cpp
+++ b/libs/rs/rsFont.cpp
@@ -507,12 +507,11 @@
     mFontSampler.set(sampler);
     mFontShaderF->bindSampler(mRSC, 0, sampler);
 
-    ProgramStore *fontStore = new ProgramStore(mRSC);
+    ProgramStore *fontStore = new ProgramStore(mRSC, true, true, true, true,
+                                               false, false,
+                                               RS_BLEND_SRC_SRC_ALPHA, RS_BLEND_DST_ONE_MINUS_SRC_ALPHA,
+                                               RS_DEPTH_FUNC_ALWAYS);
     mFontProgramStore.set(fontStore);
-    mFontProgramStore->setDepthFunc(RS_DEPTH_FUNC_ALWAYS);
-    mFontProgramStore->setBlendFunc(RS_BLEND_SRC_SRC_ALPHA, RS_BLEND_DST_ONE_MINUS_SRC_ALPHA);
-    mFontProgramStore->setDitherEnable(false);
-    mFontProgramStore->setDepthMask(false);
     mFontProgramStore->init();
 }
 
diff --git a/libs/rs/rsProgramRaster.cpp b/libs/rs/rsProgramRaster.cpp
index ace1572..9617c4d 100644
--- a/libs/rs/rsProgramRaster.cpp
+++ b/libs/rs/rsProgramRaster.cpp
@@ -15,11 +15,6 @@
  */
 
 #include "rsContext.h"
-#ifndef ANDROID_RS_SERIALIZE
-#include <GLES/gl.h>
-#include <GLES/glext.h>
-#endif //ANDROID_RS_SERIALIZE
-
 #include "rsProgramRaster.h"
 
 using namespace android;
@@ -27,49 +22,33 @@
 
 
 ProgramRaster::ProgramRaster(Context *rsc, bool pointSmooth,
-                             bool lineSmooth, bool pointSprite)
+                             bool lineSmooth, bool pointSprite,
+                             float lineWidth, RsCullMode cull)
     : Program(rsc) {
 
-    mPointSmooth = pointSmooth;
-    mLineSmooth = lineSmooth;
-    mPointSprite = pointSprite;
-    mLineWidth = 1.0f;
-    mCull = RS_CULL_BACK;
+    memset(&mHal, 0, sizeof(mHal));
+
+    mHal.state.pointSmooth = pointSmooth;
+    mHal.state.lineSmooth = lineSmooth;
+    mHal.state.pointSprite = pointSprite;
+    mHal.state.lineWidth = lineWidth;
+    mHal.state.cull = cull;
+
+    rsc->mHal.funcs.raster.init(rsc, this);
 }
 
 ProgramRaster::~ProgramRaster() {
+    mRSC->mHal.funcs.raster.destroy(mRSC, this);
 }
 
-void ProgramRaster::setLineWidth(float s) {
-    mLineWidth = s;
-    mDirty = true;
-}
-
-void ProgramRaster::setCullMode(RsCullMode mode) {
-    mCull = mode;
-    mDirty = true;
-}
-
-void ProgramRaster::setupGL2(const Context *rsc, ProgramRasterState *state) {
+void ProgramRaster::setup(const Context *rsc, ProgramRasterState *state) {
     if (state->mLast.get() == this && !mDirty) {
         return;
     }
     state->mLast.set(this);
     mDirty = false;
 
-    switch (mCull) {
-        case RS_CULL_BACK:
-            glEnable(GL_CULL_FACE);
-            glCullFace(GL_BACK);
-            break;
-        case RS_CULL_FRONT:
-            glEnable(GL_CULL_FACE);
-            glCullFace(GL_FRONT);
-            break;
-        case RS_CULL_NONE:
-            glDisable(GL_CULL_FACE);
-            break;
-    }
+    rsc->mHal.funcs.raster.setActive(rsc, this);
 }
 
 void ProgramRaster::serialize(OStream *stream) const {
@@ -86,7 +65,7 @@
 }
 
 void ProgramRasterState::init(Context *rsc) {
-    ProgramRaster *pr = new ProgramRaster(rsc, false, false, false);
+    ProgramRaster *pr = new ProgramRaster(rsc, false, false, false, 1.f, RS_CULL_BACK);
     mDefault.set(pr);
 }
 
@@ -101,27 +80,15 @@
 RsProgramRaster rsi_ProgramRasterCreate(Context * rsc,
                                       bool pointSmooth,
                                       bool lineSmooth,
-                                      bool pointSprite) {
+                                      bool pointSprite,
+                                      float lineWidth,
+                                      RsCullMode cull) {
     ProgramRaster *pr = new ProgramRaster(rsc, pointSmooth,
-                                          lineSmooth, pointSprite);
+                                          lineSmooth, pointSprite, lineWidth, cull);
     pr->incUserRef();
     return pr;
 }
 
-void rsi_ProgramRasterSetLineWidth(Context * rsc,
-                                   RsProgramRaster vpr,
-                                   float s) {
-    ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
-    pr->setLineWidth(s);
-}
-
-void rsi_ProgramRasterSetCullMode(Context * rsc,
-                                  RsProgramRaster vpr,
-                                  RsCullMode mode) {
-    ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
-    pr->setCullMode(mode);
-}
-
 }
 }
 
diff --git a/libs/rs/rsProgramRaster.h b/libs/rs/rsProgramRaster.h
index 7958af9..045a7c1 100644
--- a/libs/rs/rsProgramRaster.h
+++ b/libs/rs/rsProgramRaster.h
@@ -30,23 +30,31 @@
     ProgramRaster(Context *rsc,
                   bool pointSmooth,
                   bool lineSmooth,
-                  bool pointSprite);
+                  bool pointSprite,
+                  float lineWidth,
+                  RsCullMode cull);
     virtual ~ProgramRaster();
 
-    virtual void setupGL2(const Context *, ProgramRasterState *);
+    virtual void setup(const Context *, ProgramRasterState *);
     virtual void serialize(OStream *stream) const;
     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_RASTER; }
     static ProgramRaster *createFromStream(Context *rsc, IStream *stream);
 
-    void setLineWidth(float w);
-    void setCullMode(RsCullMode mode);
+    struct Hal {
+        mutable void *drv;
+
+        struct State {
+            bool pointSmooth;
+            bool lineSmooth;
+            bool pointSprite;
+            float lineWidth;
+            RsCullMode cull;
+        };
+        State state;
+    };
+    Hal mHal;
 
 protected:
-    bool mPointSmooth;
-    bool mLineSmooth;
-    bool mPointSprite;
-    float mLineWidth;
-    RsCullMode mCull;
 };
 
 class ProgramRasterState {
diff --git a/libs/rs/rsProgramStore.cpp b/libs/rs/rsProgramStore.cpp
index 087414a..2ad65e9 100644
--- a/libs/rs/rsProgramStore.cpp
+++ b/libs/rs/rsProgramStore.cpp
@@ -21,27 +21,31 @@
 using namespace android::renderscript;
 
 
-ProgramStore::ProgramStore(Context *rsc) : Program(rsc) {
+ProgramStore::ProgramStore(Context *rsc,
+                           bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
+                           bool depthMask, bool ditherEnable,
+                           RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
+                           RsDepthFunc depthFunc) : Program(rsc) {
     memset(&mHal, 0, sizeof(mHal));
 
-    mHal.state.ditherEnable = true;
+    mHal.state.ditherEnable = ditherEnable;
 
-    mHal.state.colorRWriteEnable = true;
-    mHal.state.colorGWriteEnable = true;
-    mHal.state.colorBWriteEnable = true;
-    mHal.state.colorAWriteEnable = true;
-    mHal.state.blendSrc = RS_BLEND_SRC_ONE;
-    mHal.state.blendDst = RS_BLEND_DST_ZERO;
+    mHal.state.colorRWriteEnable = colorMaskR;
+    mHal.state.colorGWriteEnable = colorMaskG;
+    mHal.state.colorBWriteEnable = colorMaskB;
+    mHal.state.colorAWriteEnable = colorMaskA;
+    mHal.state.blendSrc = srcFunc;
+    mHal.state.blendDst = destFunc;
 
-    mHal.state.depthWriteEnable = true;
-    mHal.state.depthFunc = RS_DEPTH_FUNC_LESS;
+    mHal.state.depthWriteEnable = depthMask;
+    mHal.state.depthFunc = depthFunc;
 }
 
 ProgramStore::~ProgramStore() {
     mRSC->mHal.funcs.store.destroy(mRSC, this);
 }
 
-void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state) {
+void ProgramStore::setup(const Context *rsc, ProgramStoreState *state) {
     if (state->mLast.get() == this) {
         return;
     }
@@ -50,10 +54,6 @@
     rsc->mHal.funcs.store.setActive(rsc, this);
 }
 
-void ProgramStore::setDitherEnable(bool enable) {
-    mHal.state.ditherEnable = enable;
-}
-
 void ProgramStore::serialize(OStream *stream) const {
 }
 
@@ -61,43 +61,24 @@
     return NULL;
 }
 
-void ProgramStore::setDepthFunc(RsDepthFunc func) {
-    mHal.state.depthFunc = func;
-}
-
-void ProgramStore::setDepthMask(bool mask) {
-    mHal.state.depthWriteEnable = mask;
-}
-
-void ProgramStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst) {
-    mHal.state.blendSrc = src;
-    mHal.state.blendDst = dst;
-}
-
-void ProgramStore::setColorMask(bool r, bool g, bool b, bool a) {
-    mHal.state.colorRWriteEnable = r;
-    mHal.state.colorGWriteEnable = g;
-    mHal.state.colorBWriteEnable = b;
-    mHal.state.colorAWriteEnable = a;
-}
-
 void ProgramStore::init() {
     mRSC->mHal.funcs.store.init(mRSC, this);
 }
 
 ProgramStoreState::ProgramStoreState() {
-    mPFS = NULL;
 }
 
 ProgramStoreState::~ProgramStoreState() {
-    ObjectBase::checkDelete(mPFS);
-    mPFS = NULL;
 }
 
 void ProgramStoreState::init(Context *rsc) {
-    ProgramStore *pfs = new ProgramStore(rsc);
-    pfs->init();
-    mDefault.set(pfs);
+    ProgramStore *ps = new ProgramStore(rsc,
+                                        true, true, true, true,
+                                        true, true,
+                                        RS_BLEND_SRC_ONE, RS_BLEND_DST_ZERO,
+                                        RS_DEPTH_FUNC_LESS);
+    ps->init();
+    mDefault.set(ps);
 }
 
 void ProgramStoreState::deinit(Context *rsc) {
@@ -109,38 +90,20 @@
 namespace android {
 namespace renderscript {
 
-void rsi_ProgramStoreBegin(Context * rsc, RsElement in, RsElement out) {
-    ObjectBase::checkDelete(rsc->mStateFragmentStore.mPFS);
-    rsc->mStateFragmentStore.mPFS = new ProgramStore(rsc);
-}
+RsProgramStore rsi_ProgramStoreCreate(Context *rsc,
+                                      bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
+                                      bool depthMask, bool ditherEnable,
+                                      RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
+                                      RsDepthFunc depthFunc) {
 
-void rsi_ProgramStoreDepthFunc(Context *rsc, RsDepthFunc func) {
-    rsc->mStateFragmentStore.mPFS->setDepthFunc(func);
-}
-
-void rsi_ProgramStoreDepthMask(Context *rsc, bool mask) {
-    rsc->mStateFragmentStore.mPFS->setDepthMask(mask);
-}
-
-void rsi_ProgramStoreColorMask(Context *rsc, bool r, bool g, bool b, bool a) {
-    rsc->mStateFragmentStore.mPFS->setColorMask(r, g, b, a);
-}
-
-void rsi_ProgramStoreBlendFunc(Context *rsc, RsBlendSrcFunc src, RsBlendDstFunc dst) {
-    rsc->mStateFragmentStore.mPFS->setBlendFunc(src, dst);
-}
-
-RsProgramStore rsi_ProgramStoreCreate(Context *rsc) {
-    ProgramStore *pfs = rsc->mStateFragmentStore.mPFS;
+    ProgramStore *pfs = new ProgramStore(rsc,
+                                         colorMaskR, colorMaskG, colorMaskB, colorMaskA,
+                                         depthMask, ditherEnable,
+                                         srcFunc, destFunc, depthFunc);
     pfs->init();
     pfs->incUserRef();
-    rsc->mStateFragmentStore.mPFS = 0;
     return pfs;
 }
 
-void rsi_ProgramStoreDither(Context *rsc, bool enable) {
-    rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
-}
-
 }
 }
diff --git a/libs/rs/rsProgramStore.h b/libs/rs/rsProgramStore.h
index 38e88e3..bfe276d 100644
--- a/libs/rs/rsProgramStore.h
+++ b/libs/rs/rsProgramStore.h
@@ -28,18 +28,14 @@
 
 class ProgramStore : public Program {
 public:
-    ProgramStore(Context *);
+    ProgramStore(Context *,
+                 bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
+                 bool depthMask, bool ditherEnable,
+                 RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
+                 RsDepthFunc depthFunc);
     virtual ~ProgramStore();
 
-    virtual void setupGL2(const Context *, ProgramStoreState *);
-
-    void setDepthFunc(RsDepthFunc);
-    void setDepthMask(bool);
-
-    void setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst);
-    void setColorMask(bool, bool, bool, bool);
-
-    void setDitherEnable(bool);
+    virtual void setup(const Context *, ProgramStoreState *);
 
     virtual void serialize(OStream *stream) const;
     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_STORE; }
@@ -83,9 +79,6 @@
 
     ObjectBaseRef<ProgramStore> mDefault;
     ObjectBaseRef<ProgramStore> mLast;
-
-
-    ProgramStore *mPFS;
 };
 
 }
diff --git a/libs/rs/rs_hal.h b/libs/rs/rs_hal.h
index 6d7a5b1..93d7476 100644
--- a/libs/rs/rs_hal.h
+++ b/libs/rs/rs_hal.h
@@ -30,6 +30,7 @@
 class Script;
 class ScriptC;
 class ProgramStore;
+class ProgramRaster;
 
 typedef void *(*RsHalSymbolLookupFunc)(void *usrptr, char const *symbolName);
 
@@ -93,6 +94,11 @@
         void (*destroy)(const Context *rsc, const ProgramStore *ps);
     } store;
 
+    struct {
+        bool (*init)(const Context *rsc, const ProgramRaster *ps);
+        void (*setActive)(const Context *rsc, const ProgramRaster *ps);
+        void (*destroy)(const Context *rsc, const ProgramRaster *ps);
+    } raster;
 
 
 } RsdHalFunctions;