Support reference operations in RS scripts.

Change-Id: I9cb5f3cb71823ab24ca51bf0167a0f52cf0691f9
diff --git a/libs/rs/rsScriptC_Lib.cpp b/libs/rs/rsScriptC_Lib.cpp
index ac32810..5a2b6ba 100644
--- a/libs/rs/rsScriptC_Lib.cpp
+++ b/libs/rs/rsScriptC_Lib.cpp
@@ -239,7 +239,7 @@
     return a->getType()->getDimFaces();
 }
 
-const void * SC_getElementAtX(RsAllocation va, uint32_t x)
+static const void * SC_getElementAtX(RsAllocation va, uint32_t x)
 {
     const Allocation *a = static_cast<const Allocation *>(va);
     const Type *t = a->getType();
@@ -247,7 +247,7 @@
     return &p[t->getElementSizeBytes() * x];
 }
 
-const void * SC_getElementAtXY(RsAllocation va, uint32_t x, uint32_t y)
+static const void * SC_getElementAtXY(RsAllocation va, uint32_t x, uint32_t y)
 {
     const Allocation *a = static_cast<const Allocation *>(va);
     const Type *t = a->getType();
@@ -255,7 +255,7 @@
     return &p[t->getElementSizeBytes() * (x + y*t->getDimX())];
 }
 
-const void * SC_getElementAtXYZ(RsAllocation va, uint32_t x, uint32_t y, uint32_t z)
+static const void * SC_getElementAtXYZ(RsAllocation va, uint32_t x, uint32_t y, uint32_t z)
 {
     const Allocation *a = static_cast<const Allocation *>(va);
     const Type *t = a->getType();
@@ -263,6 +263,20 @@
     return &p[t->getElementSizeBytes() * (x + y*t->getDimX())];
 }
 
+static void SC_setObject(void **vdst, void * vsrc) {
+    static_cast<ObjectBase *>(vsrc)->incSysRef();
+    static_cast<ObjectBase *>(vdst[0])->decSysRef();
+    *vdst = vsrc;
+}
+static void SC_clearObject(void **vdst) {
+    static_cast<ObjectBase *>(vdst[0])->decSysRef();
+    *vdst = NULL;
+}
+static bool SC_isObject(RsAllocation vsrc) {
+    return vsrc != NULL;
+}
+
+
 
 static void SC_debugF(const char *s, float f) {
     LOGE("%s %f, 0x%08x", s, f, *((int *) (&f)));
@@ -405,6 +419,11 @@
     { "_Z14rsGetElementAt13rs_allocationjj", (void *)&SC_getElementAtXY },
     { "_Z14rsGetElementAt13rs_allocationjjj", (void *)&SC_getElementAtXYZ },
 
+    { "_Z11rsSetObjectP13rs_allocation13rs_allocation", (void *)&SC_setObject },
+    { "_Z13rsClearObjectP13rs_allocation", (void *)&SC_clearObject },
+    { "_Z10rsIsObject13rs_allocation", (void *)&SC_isObject },
+
+
     // Debug
     { "_Z7rsDebugPKcf", (void *)&SC_debugF },
     { "_Z7rsDebugPKcff", (void *)&SC_debugFv2 },
diff --git a/libs/rs/scriptc/rs_math.rsh b/libs/rs/scriptc/rs_math.rsh
index bb4aafb..c842ef1 100644
--- a/libs/rs/scriptc/rs_math.rsh
+++ b/libs/rs/scriptc/rs_math.rsh
@@ -29,6 +29,77 @@
 #include "rs_cl.rsh"
 #include "rs_core.rsh"
 
+extern void __attribute__((overloadable))
+    rsSetObject(rs_element *dst, rs_element src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_type *dst, rs_type src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_allocation *dst, rs_allocation src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_sampler *dst, rs_sampler src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_script *dst, rs_script src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_mesh *dst, rs_mesh src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_program_fragment *dst, rs_program_fragment src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_program_vertex *dst, rs_program_vertex src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_program_raster *dst, rs_program_raster src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_program_store *dst, rs_program_store src);
+extern void __attribute__((overloadable))
+    rsSetObject(rs_font *dst, rs_font src);
+
+extern void __attribute__((overloadable))
+    rsClearObject(rs_element *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_type *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_allocation *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_sampler *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_script *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_mesh *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_program_fragment *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_program_vertex *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_program_raster *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_program_store *dst);
+extern void __attribute__((overloadable))
+    rsClearObject(rs_font *dst);
+
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_element);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_type);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_allocation);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_sampler);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_script);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_mesh);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_program_fragment);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_program_vertex);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_program_raster);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_program_store);
+extern bool __attribute__((overloadable))
+    rsIsObject(rs_font);
+
+
+
 // Allocations
 
 // Return the rs_allocation associated with a bound data
diff --git a/libs/rs/scriptc/rs_types.rsh b/libs/rs/scriptc/rs_types.rsh
index fd9ac16..dd42972 100644
--- a/libs/rs/scriptc/rs_types.rsh
+++ b/libs/rs/scriptc/rs_types.rsh
@@ -14,17 +14,18 @@
 typedef uint32_t uint;
 typedef uint64_t ulong;
 
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_element;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_type;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_allocation;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_sampler;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_script;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_mesh;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_fragment;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_vertex;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_raster;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_program_store;
-typedef struct { int* p; } __attribute__((packed, aligned(4))) rs_font;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_element;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_type;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_allocation;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_sampler;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_script;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_mesh;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_program_fragment;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_program_vertex;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_program_raster;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_program_store;
+typedef struct { const int* const p; } __attribute__((packed, aligned(4))) rs_font;
+
 
 typedef float float2 __attribute__((ext_vector_type(2)));
 typedef float float3 __attribute__((ext_vector_type(3)));