Add Sampler support to C++ API.

Change-Id: I9a6ada9cf16b5e9f92bcecc12e87d66585df1d33
diff --git a/cpp/Android.mk b/cpp/Android.mk
index 75a8ae7..3613eae 100644
--- a/cpp/Android.mk
+++ b/cpp/Android.mk
@@ -21,7 +21,8 @@
 	Allocation.cpp \
 	Script.cpp \
 	ScriptC.cpp \
-	ScriptIntrinsics.cpp
+	ScriptIntrinsics.cpp \
+	Sampler.cpp
 
 LOCAL_SHARED_LIBRARIES := \
 	libz \
diff --git a/cpp/Element.cpp b/cpp/Element.cpp
index 7c58516..1289c6e 100644
--- a/cpp/Element.cpp
+++ b/cpp/Element.cpp
@@ -65,8 +65,12 @@
 
 
 #define CREATE_USER(N, T) sp<const Element> Element::N(sp<RS> rs) { \
-    return createUser(rs, RS_TYPE_##T); \
-}
+    if (rs->mElements.N == NULL) {                                  \
+        rs->mElements.N = (createUser(rs, RS_TYPE_##T)).get();      \
+    }                                                               \
+    return rs->mElements.N;                                         \
+    }
+
 CREATE_USER(BOOLEAN, BOOLEAN);
 CREATE_USER(U8, UNSIGNED_8);
 CREATE_USER(I8, SIGNED_8);
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index 6acfc9e..e2c8bc0 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -48,6 +48,7 @@
     mInit = false;
 
     memset(&mElements, 0, sizeof(mElements));
+    memset(&mSamplers, 0, sizeof(mSamplers));
 }
 
 RS::~RS() {
diff --git a/cpp/Sampler.cpp b/cpp/Sampler.cpp
new file mode 100644
index 0000000..fa6b2fd
--- /dev/null
+++ b/cpp/Sampler.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2008-2012 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 "RenderScript.h"
+#include <rs.h>
+
+using namespace android;
+using namespace RSC;
+
+Sampler::Sampler(sp<RS> rs, void* id):
+    BaseObj(id, rs)
+{
+    RsSamplerValue mMin = RS_SAMPLER_INVALID;
+    RsSamplerValue mMag = RS_SAMPLER_INVALID;
+    RsSamplerValue mWrapS = RS_SAMPLER_INVALID;;
+    RsSamplerValue mWrapT = RS_SAMPLER_INVALID;;
+    float mAniso = 0.f;
+}
+
+RsSamplerValue Sampler::getMinification() {
+    return mMin;
+}
+
+RsSamplerValue Sampler::getMagnification() {
+    return mMag;
+}
+
+RsSamplerValue Sampler::getWrapS() {
+    return mWrapS;
+}
+
+RsSamplerValue Sampler::getWrapT() {
+    return mWrapT;
+}
+
+float Sampler::getAnisotropy() {
+    return mAniso;
+}
+
+sp<Sampler> Sampler::create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy) {
+    // we aren't supporting wrapR in C++ API atm, so always pass wrap for that
+    void* id = RS::dispatch->SamplerCreate(rs.get(), min, mag, wrapS, wrapT, RS_SAMPLER_WRAP, anisotropy);
+    return new Sampler(rs, id);
+}
+
+#define CREATE_SAMPLER(N, MIN, MAG, WRAPS, WRAPT) sp<const Sampler> Sampler::N(sp<RS> rs) { \
+        if (rs->mSamplers.N == NULL) {                                  \
+            rs->mSamplers.N = (create(rs, MIN, MAG, WRAPS, WRAPT, 0.f)).get(); \
+        }                                                               \
+        return rs->mSamplers.N;                                         \
+    }
+
+CREATE_SAMPLER(CLAMP_NEAREST, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST);
+CREATE_SAMPLER(CLAMP_LINEAR, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR);
+CREATE_SAMPLER(CLAMP_LINEAR_MIP_LINEAR, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR);
+CREATE_SAMPLER(WRAP_NEAREST, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST);
+CREATE_SAMPLER(WRAP_LINEAR, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR);
+CREATE_SAMPLER(WRAP_LINEAR_MIP_LINEAR, RS_SAMPLER_WRAP, RS_SAMPLER_WRAP, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR);
+CREATE_SAMPLER(MIRRORED_REPEAT_NEAREST, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST);
+CREATE_SAMPLER(MIRRORED_REPEAT_LINEAR, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_LINEAR, RS_SAMPLER_LINEAR);
+CREATE_SAMPLER(MIRRORED_REPEAT_LINEAR_MIP_LINEAR, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_MIRRORED_REPEAT, RS_SAMPLER_LINEAR_MIP_LINEAR, RS_SAMPLER_LINEAR_MIP_LINEAR);
diff --git a/cpp/ScriptC.cpp b/cpp/ScriptC.cpp
index 30aa380..70d7ba5 100644
--- a/cpp/ScriptC.cpp
+++ b/cpp/ScriptC.cpp
@@ -14,8 +14,6 @@
  * limitations under the License.
  */
 
-#include <malloc.h>
-
 #include "RenderScript.h"
 #include <rs.h>
 
diff --git a/cpp/rsCppStructs.h b/cpp/rsCppStructs.h
index 7a876b3..b948c91 100644
--- a/cpp/rsCppStructs.h
+++ b/cpp/rsCppStructs.h
@@ -43,6 +43,7 @@
 class Allocation;
 class Script;
 class ScriptC;
+class Sampler;
 
 class RS : public android::LightRefBase<RS> {
 
@@ -88,81 +89,94 @@
     bool mInit;
 
     struct {
-        Element *U8;
-        Element *I8;
-        Element *U16;
-        Element *I16;
-        Element *U32;
-        Element *I32;
-        Element *U64;
-        Element *I64;
-        Element *F32;
-        Element *F64;
-        Element *BOOLEAN;
+        const Element *U8;
+        const Element *I8;
+        const Element *U16;
+        const Element *I16;
+        const Element *U32;
+        const Element *I32;
+        const Element *U64;
+        const Element *I64;
+        const Element *F32;
+        const Element *F64;
+        const Element *BOOLEAN;
 
-        Element *ELEMENT;
-        Element *TYPE;
-        Element *ALLOCATION;
-        Element *SAMPLER;
-        Element *SCRIPT;
-        Element *MESH;
-        Element *PROGRAM_FRAGMENT;
-        Element *PROGRAM_VERTEX;
-        Element *PROGRAM_RASTER;
-        Element *PROGRAM_STORE;
+        const Element *ELEMENT;
+        const Element *TYPE;
+        const Element *ALLOCATION;
+        const Element *SAMPLER;
+        const Element *SCRIPT;
+        const Element *MESH;
+        const Element *PROGRAM_FRAGMENT;
+        const Element *PROGRAM_VERTEX;
+        const Element *PROGRAM_RASTER;
+        const Element *PROGRAM_STORE;
 
-        Element *A_8;
-        Element *RGB_565;
-        Element *RGB_888;
-        Element *RGBA_5551;
-        Element *RGBA_4444;
-        Element *RGBA_8888;
+        const Element *A_8;
+        const Element *RGB_565;
+        const Element *RGB_888;
+        const Element *RGBA_5551;
+        const Element *RGBA_4444;
+        const Element *RGBA_8888;
 
-        Element *FLOAT_2;
-        Element *FLOAT_3;
-        Element *FLOAT_4;
+        const Element *FLOAT_2;
+        const Element *FLOAT_3;
+        const Element *FLOAT_4;
 
-        Element *DOUBLE_2;
-        Element *DOUBLE_3;
-        Element *DOUBLE_4;
+        const Element *DOUBLE_2;
+        const Element *DOUBLE_3;
+        const Element *DOUBLE_4;
 
-        Element *UCHAR_2;
-        Element *UCHAR_3;
-        Element *UCHAR_4;
+        const Element *UCHAR_2;
+        const Element *UCHAR_3;
+        const Element *UCHAR_4;
 
-        Element *CHAR_2;
-        Element *CHAR_3;
-        Element *CHAR_4;
+        const Element *CHAR_2;
+        const Element *CHAR_3;
+        const Element *CHAR_4;
 
-        Element *USHORT_2;
-        Element *USHORT_3;
-        Element *USHORT_4;
+        const Element *USHORT_2;
+        const Element *USHORT_3;
+        const Element *USHORT_4;
 
-        Element *SHORT_2;
-        Element *SHORT_3;
-        Element *SHORT_4;
+        const Element *SHORT_2;
+        const Element *SHORT_3;
+        const Element *SHORT_4;
 
-        Element *UINT_2;
-        Element *UINT_3;
-        Element *UINT_4;
+        const Element *UINT_2;
+        const Element *UINT_3;
+        const Element *UINT_4;
 
-        Element *INT_2;
-        Element *INT_3;
-        Element *INT_4;
+        const Element *INT_2;
+        const Element *INT_3;
+        const Element *INT_4;
 
-        Element *ULONG_2;
-        Element *ULONG_3;
-        Element *ULONG_4;
+        const Element *ULONG_2;
+        const Element *ULONG_3;
+        const Element *ULONG_4;
 
-        Element *LONG_2;
-        Element *LONG_3;
-        Element *LONG_4;
+        const Element *LONG_2;
+        const Element *LONG_3;
+        const Element *LONG_4;
 
-        Element *MATRIX_4X4;
-        Element *MATRIX_3X3;
-        Element *MATRIX_2X2;
+        const Element *MATRIX_4X4;
+        const Element *MATRIX_3X3;
+        const Element *MATRIX_2X2;
     } mElements;
 
+    struct {
+        const Sampler* CLAMP_NEAREST;
+        const Sampler* CLAMP_LINEAR;
+        const Sampler* CLAMP_LINEAR_MIP_LINEAR;
+        const Sampler* WRAP_NEAREST;
+        const Sampler* WRAP_LINEAR;
+        const Sampler* WRAP_LINEAR_MIP_LINEAR;
+        const Sampler* MIRRORED_REPEAT_NEAREST;
+        const Sampler* MIRRORED_REPEAT_LINEAR;
+        const Sampler* MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
+    } mSamplers;
+    friend class Sampler;
+    friend class Element;
 };
 
 class BaseObj : public android::LightRefBase<BaseObj> {
@@ -676,6 +690,37 @@
     void setRadius(float radius);
 };
 
+ class Sampler : public BaseObj {
+ private:
+    Sampler(sp<RS> rs, void* id);
+    RsSamplerValue mMin;
+    RsSamplerValue mMag;
+    RsSamplerValue mWrapS;
+    RsSamplerValue mWrapT;
+    RsSamplerValue mWrapR;
+    float mAniso;
+
+ public:
+    static sp<Sampler> create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
+
+    RsSamplerValue getMinification();
+    RsSamplerValue getMagnification();
+    RsSamplerValue getWrapS();
+    RsSamplerValue getWrapT();
+    float getAnisotropy();
+
+    sp<const Sampler> CLAMP_NEAREST(sp<RS> rs);
+    sp<const Sampler> CLAMP_LINEAR(sp<RS> rs);
+    sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR(sp<RS> rs);
+    sp<const Sampler> WRAP_NEAREST(sp<RS> rs);
+    sp<const Sampler> WRAP_LINEAR(sp<RS> rs);
+    sp<const Sampler> WRAP_LINEAR_MIP_LINEAR(sp<RS> rs);
+    sp<const Sampler> MIRRORED_REPEAT_NEAREST(sp<RS> rs);
+    sp<const Sampler> MIRRORED_REPEAT_LINEAR(sp<RS> rs);
+    sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR(sp<RS> rs);
+
+};
+
 }
 
 }