Merge "Using surface texture as a default renderscript surface."
diff --git a/driver/rsdGL.cpp b/driver/rsdGL.cpp
index b6fc00e..5103878 100644
--- a/driver/rsdGL.cpp
+++ b/driver/rsdGL.cpp
@@ -481,3 +481,37 @@
     }
 
 }
+
+void rsdGLClearColor(const android::renderscript::Context *rsc,
+                     float r, float g, float b, float a) {
+    RSD_CALL_GL(glClearColor, r, g, b, a);
+    RSD_CALL_GL(glClear, GL_COLOR_BUFFER_BIT);
+}
+
+void rsdGLClearDepth(const android::renderscript::Context *rsc, float v) {
+    RSD_CALL_GL(glClearDepthf, v);
+    RSD_CALL_GL(glClear, GL_DEPTH_BUFFER_BIT);
+}
+
+void rsdGLFinish(const android::renderscript::Context *rsc) {
+    RSD_CALL_GL(glFinish);
+}
+
+void rsdGLDrawQuadTexCoords(const android::renderscript::Context *rsc,
+                            float x1, float y1, float z1, float u1, float v1,
+                            float x2, float y2, float z2, float u2, float v2,
+                            float x3, float y3, float z3, float u3, float v3,
+                            float x4, float y4, float z4, float u4, float v4) {
+
+    float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
+    const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
+
+    RsdVertexArray::Attrib attribs[2];
+    attribs[0].set(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position");
+    attribs[1].set(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0");
+
+    RsdVertexArray va(attribs, 2);
+    va.setup(rsc);
+
+    RSD_CALL_GL(glDrawArrays, GL_TRIANGLE_FAN, 0, 4);
+}
diff --git a/driver/rsdGL.h b/driver/rsdGL.h
index e015cb1..1e5b40f 100644
--- a/driver/rsdGL.h
+++ b/driver/rsdGL.h
@@ -84,6 +84,15 @@
                      const char *msg, bool isFatal = false);
 void rsdGLSetPriority(const android::renderscript::Context *rsc,
                       int32_t priority);
+void rsdGLClearColor(const android::renderscript::Context *rsc,
+                     float r, float g, float b, float a);
+void rsdGLClearDepth(const android::renderscript::Context *rsc, float v);
+void rsdGLFinish(const android::renderscript::Context *rsc);
+void rsdGLDrawQuadTexCoords(const android::renderscript::Context *rsc,
+                            float x1, float y1, float z1, float u1, float v1,
+                            float x2, float y2, float z2, float u2, float v2,
+                            float x3, float y3, float z3, float u3, float v3,
+                            float x4, float y4, float z4, float u4, float v4);
 
 #endif
 
diff --git a/driver/rsdRuntimeStubs.cpp b/driver/rsdRuntimeStubs.cpp
index 44bfb1c..aa9f159 100644
--- a/driver/rsdRuntimeStubs.cpp
+++ b/driver/rsdRuntimeStubs.cpp
@@ -257,17 +257,19 @@
 
 static void SC_Finish() {
     GET_TLS();
-    rsrFinish(rsc, sc);
+    rsdGLFinish(rsc);
 }
 
 static void SC_ClearColor(float r, float g, float b, float a) {
     GET_TLS();
-    rsrClearColor(rsc, sc, r, g, b, a);
+    rsrPrepareClear(rsc, sc);
+    rsdGLClearColor(rsc, r, g, b, a);
 }
 
 static void SC_ClearDepth(float v) {
     GET_TLS();
-    rsrClearDepth(rsc, sc, v);
+    rsrPrepareClear(rsc, sc);
+    rsdGLClearDepth(rsc, v);
 }
 
 static uint32_t SC_GetWidth() {
diff --git a/rsRuntime.h b/rsRuntime.h
index 3bded62..64f2de8 100644
--- a/rsRuntime.h
+++ b/rsRuntime.h
@@ -86,7 +86,6 @@
 
 
 void rsrColor(Context *, Script *, float r, float g, float b, float a);
-void rsrFinish(Context *, Script *);
 void rsrAllocationSyncAll(Context *, Script *, Allocation *);
 
 void rsrAllocationCopy1DRange(Context *, Allocation *dstAlloc,
@@ -103,8 +102,7 @@
                               uint32_t srcXoff, uint32_t srcYoff,
                               uint32_t srcMip, uint32_t srcFace);
 
-void rsrClearColor(Context *, Script *, float r, float g, float b, float a);
-void rsrClearDepth(Context *, Script *, float v);
+void rsrPrepareClear(Context *, Script *);
 uint32_t rsrGetWidth(Context *, Script *);
 uint32_t rsrGetHeight(Context *, Script *);
 void rsrDrawTextAlloc(Context *, Script *, Allocation *, int x, int y);
diff --git a/rsScriptC_LibGL.cpp b/rsScriptC_LibGL.cpp
index 97469d3..bda18fd 100644
--- a/rsScriptC_LibGL.cpp
+++ b/rsScriptC_LibGL.cpp
@@ -269,25 +269,9 @@
     pf->setConstantColor(rsc, r, g, b, a);
 }
 
-void rsrFinish(Context *rsc, Script *sc) {
-    RSD_CALL_GL(glFinish);
-}
-
-
-void rsrClearColor(Context *rsc, Script *sc, float r, float g, float b, float a) {
+void rsrPrepareClear(Context *rsc, Script *sc) {
     rsc->mFBOCache.setup(rsc);
     rsc->setupProgramStore();
-
-    RSD_CALL_GL(glClearColor, r, g, b, a);
-    RSD_CALL_GL(glClear, GL_COLOR_BUFFER_BIT);
-}
-
-void rsrClearDepth(Context *rsc, Script *sc, float v) {
-    rsc->mFBOCache.setup(rsc);
-    rsc->setupProgramStore();
-
-    RSD_CALL_GL(glClearDepthf, v);
-    RSD_CALL_GL(glClear, GL_DEPTH_BUFFER_BIT);
 }
 
 uint32_t rsrGetWidth(Context *rsc, Script *sc) {
diff --git a/scriptc/rs_allocation.rsh b/scriptc/rs_allocation.rsh
index a2f69d9..89696b8 100644
--- a/scriptc/rs_allocation.rsh
+++ b/scriptc/rs_allocation.rsh
@@ -298,5 +298,67 @@
 extern uint32_t __attribute__((overloadable))
     rsElementGetVectorSize(rs_element e);
 
+/**
+ * Fetch allocation in a way described by the sampler
+ * @param a 1D allocation to sample from
+ * @param s sampler state
+ * @param location to sample from
+ */
+extern const float4 __attribute__((overloadable))
+    rsSample(rs_allocation a, rs_sampler s, float location);
+/**
+ * Fetch allocation in a way described by the sampler
+ * @param a 1D allocation to sample from
+ * @param s sampler state
+ * @param location to sample from
+ * @param lod mip level to sample from, for fractional values
+ *            mip levels will be interpolated if
+ *            RS_SAMPLER_LINEAR_MIP_LINEAR is used
+ */
+extern const float4 __attribute__((overloadable))
+    rsSample(rs_allocation a, rs_sampler s, float location, float lod);
+
+/**
+ * Fetch allocation in a way described by the sampler
+ * @param a 2D allocation to sample from
+ * @param s sampler state
+ * @param location to sample from
+ */
+extern const float4 __attribute__((overloadable))
+    rsSample(rs_allocation a, rs_sampler s, float2 location);
+
+/**
+ * Fetch allocation in a way described by the sampler
+ * @param a 2D allocation to sample from
+ * @param s sampler state
+ * @param location to sample from
+ * @param lod mip level to sample from, for fractional values
+ *            mip levels will be interpolated if
+ *            RS_SAMPLER_LINEAR_MIP_LINEAR is used
+ */
+extern const float4 __attribute__((overloadable))
+    rsSample(rs_allocation a, rs_sampler s, float2 location, float lod);
+
+/**
+ * Fetch allocation in a way described by the sampler
+ * @param a 3D allocation to sample from
+ * @param s sampler state
+ * @param location to sample from
+ */
+extern const float4 __attribute__((overloadable))
+    rsSample(rs_allocation a, rs_sampler s, float3 location);
+
+/**
+ * Fetch allocation in a way described by the sampler
+ * @param a 3D allocation to sample from
+ * @param s sampler state
+ * @param location to sample from
+ * @param lod mip level to sample from, for fractional values
+ *            mip levels will be interpolated if
+ *            RS_SAMPLER_LINEAR_MIP_LINEAR is used
+ */
+extern const float4 __attribute__((overloadable))
+    rsSample(rs_allocation a, rs_sampler s, float3 location, float lod);
+
 #endif
 
diff --git a/scriptc/rs_graphics.rsh b/scriptc/rs_graphics.rsh
index 7fdebdc..e3fde82 100644
--- a/scriptc/rs_graphics.rsh
+++ b/scriptc/rs_graphics.rsh
@@ -23,65 +23,6 @@
 #ifndef __RS_GRAPHICS_RSH__
 #define __RS_GRAPHICS_RSH__
 
-// These are API 15 once it get official
-typedef enum {
-    RS_DEPTH_FUNC_ALWAYS,
-    RS_DEPTH_FUNC_LESS,
-    RS_DEPTH_FUNC_LEQUAL,
-    RS_DEPTH_FUNC_GREATER,
-    RS_DEPTH_FUNC_GEQUAL,
-    RS_DEPTH_FUNC_EQUAL,
-    RS_DEPTH_FUNC_NOTEQUAL,
-
-    RS_DEPTH_FUNC_INVALID = 100,
-} rs_depth_func;
-
-typedef enum {
-    RS_BLEND_SRC_ZERO,                  // 0
-    RS_BLEND_SRC_ONE,                   // 1
-    RS_BLEND_SRC_DST_COLOR,             // 2
-    RS_BLEND_SRC_ONE_MINUS_DST_COLOR,   // 3
-    RS_BLEND_SRC_SRC_ALPHA,             // 4
-    RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA,   // 5
-    RS_BLEND_SRC_DST_ALPHA,             // 6
-    RS_BLEND_SRC_ONE_MINUS_DST_ALPHA,   // 7
-    RS_BLEND_SRC_SRC_ALPHA_SATURATE,    // 8
-
-    RS_BLEND_SRC_INVALID = 100,
-} rs_blend_src_func;
-
-typedef enum {
-    RS_BLEND_DST_ZERO,                  // 0
-    RS_BLEND_DST_ONE,                   // 1
-    RS_BLEND_DST_SRC_COLOR,             // 2
-    RS_BLEND_DST_ONE_MINUS_SRC_COLOR,   // 3
-    RS_BLEND_DST_SRC_ALPHA,             // 4
-    RS_BLEND_DST_ONE_MINUS_SRC_ALPHA,   // 5
-    RS_BLEND_DST_DST_ALPHA,             // 6
-    RS_BLEND_DST_ONE_MINUS_DST_ALPHA,   // 7
-
-    RS_BLEND_DST_INVALID = 100,
-} rs_blend_dst_func;
-
-typedef enum {
-    RS_CULL_BACK,
-    RS_CULL_FRONT,
-    RS_CULL_NONE,
-
-    RS_CULL_INVALID = 100,
-} rs_cull_mode;
-
-typedef enum {
-    RS_SAMPLER_NEAREST,
-    RS_SAMPLER_LINEAR,
-    RS_SAMPLER_LINEAR_MIP_LINEAR,
-    RS_SAMPLER_WRAP,
-    RS_SAMPLER_CLAMP,
-    RS_SAMPLER_LINEAR_MIP_NEAREST,
-
-    RS_SAMPLER_INVALID = 100,
-} rs_sampler_value;
-
 #if (defined(RS_VERSION) && (RS_VERSION >= 14))
 /**
  * Set the color target used for all subsequent rendering calls
diff --git a/scriptc/rs_types.rsh b/scriptc/rs_types.rsh
index 5345a48..f8c2657 100644
--- a/scriptc/rs_types.rsh
+++ b/scriptc/rs_types.rsh
@@ -407,14 +407,14 @@
  *
  **/
 typedef enum {
-    RS_PRIMITIVE_POINT,
-    RS_PRIMITIVE_LINE,
-    RS_PRIMITIVE_LINE_STRIP,
-    RS_PRIMITIVE_TRIANGLE,
-    RS_PRIMITIVE_TRIANGLE_STRIP,
-    RS_PRIMITIVE_TRIANGLE_FAN,
+    RS_PRIMITIVE_POINT              = 0,
+    RS_PRIMITIVE_LINE               = 1,
+    RS_PRIMITIVE_LINE_STRIP         = 2,
+    RS_PRIMITIVE_TRIANGLE           = 3,
+    RS_PRIMITIVE_TRIANGLE_STRIP     = 4,
+    RS_PRIMITIVE_TRIANGLE_FAN       = 5,
 
-    RS_PRIMITIVE_INVALID = 100,
+    RS_PRIMITIVE_INVALID            = 100,
 } rs_primitive;
 
 /**
@@ -436,41 +436,41 @@
  * RS_* objects.  32 bit opaque handles.
  */
 typedef enum {
-    RS_TYPE_NONE,
+    RS_TYPE_NONE             = 0,
     //RS_TYPE_FLOAT_16,
-    RS_TYPE_FLOAT_32 = 2,
-    RS_TYPE_FLOAT_64,
-    RS_TYPE_SIGNED_8,
-    RS_TYPE_SIGNED_16,
-    RS_TYPE_SIGNED_32,
-    RS_TYPE_SIGNED_64,
-    RS_TYPE_UNSIGNED_8,
-    RS_TYPE_UNSIGNED_16,
-    RS_TYPE_UNSIGNED_32,
-    RS_TYPE_UNSIGNED_64,
+    RS_TYPE_FLOAT_32         = 2,
+    RS_TYPE_FLOAT_64         = 3,
+    RS_TYPE_SIGNED_8         = 4,
+    RS_TYPE_SIGNED_16        = 5,
+    RS_TYPE_SIGNED_32        = 6,
+    RS_TYPE_SIGNED_64        = 7,
+    RS_TYPE_UNSIGNED_8       = 8,
+    RS_TYPE_UNSIGNED_16      = 9,
+    RS_TYPE_UNSIGNED_32      = 10,
+    RS_TYPE_UNSIGNED_64      = 11,
 
-    RS_TYPE_BOOLEAN,
+    RS_TYPE_BOOLEAN          = 12,
 
-    RS_TYPE_UNSIGNED_5_6_5,
-    RS_TYPE_UNSIGNED_5_5_5_1,
-    RS_TYPE_UNSIGNED_4_4_4_4,
+    RS_TYPE_UNSIGNED_5_6_5   = 13,
+    RS_TYPE_UNSIGNED_5_5_5_1 = 14,
+    RS_TYPE_UNSIGNED_4_4_4_4 = 15,
 
-    RS_TYPE_MATRIX_4X4,
-    RS_TYPE_MATRIX_3X3,
-    RS_TYPE_MATRIX_2X2,
+    RS_TYPE_MATRIX_4X4       = 16,
+    RS_TYPE_MATRIX_3X3       = 17,
+    RS_TYPE_MATRIX_2X2       = 18,
 
-    RS_TYPE_ELEMENT = 1000,
-    RS_TYPE_TYPE,
-    RS_TYPE_ALLOCATION,
-    RS_TYPE_SAMPLER,
-    RS_TYPE_SCRIPT,
-    RS_TYPE_MESH,
-    RS_TYPE_PROGRAM_FRAGMENT,
-    RS_TYPE_PROGRAM_VERTEX,
-    RS_TYPE_PROGRAM_RASTER,
-    RS_TYPE_PROGRAM_STORE,
+    RS_TYPE_ELEMENT          = 1000,
+    RS_TYPE_TYPE             = 1001,
+    RS_TYPE_ALLOCATION       = 1002,
+    RS_TYPE_SAMPLER          = 1003,
+    RS_TYPE_SCRIPT           = 1004,
+    RS_TYPE_MESH             = 1005,
+    RS_TYPE_PROGRAM_FRAGMENT = 1006,
+    RS_TYPE_PROGRAM_VERTEX   = 1007,
+    RS_TYPE_PROGRAM_RASTER   = 1008,
+    RS_TYPE_PROGRAM_STORE    = 1009,
 
-    RS_TYPE_INVALID = 10000,
+    RS_TYPE_INVALID          = 10000,
 } rs_data_type;
 
 /**
@@ -482,16 +482,74 @@
  * representing texture formats.
  */
 typedef enum {
-    RS_KIND_USER,
+    RS_KIND_USER         = 0,
 
-    RS_KIND_PIXEL_L = 7,
-    RS_KIND_PIXEL_A,
-    RS_KIND_PIXEL_LA,
-    RS_KIND_PIXEL_RGB,
-    RS_KIND_PIXEL_RGBA,
-    RS_KIND_PIXEL_DEPTH,
+    RS_KIND_PIXEL_L      = 7,
+    RS_KIND_PIXEL_A      = 8,
+    RS_KIND_PIXEL_LA     = 9,
+    RS_KIND_PIXEL_RGB    = 10,
+    RS_KIND_PIXEL_RGBA   = 11,
+    RS_KIND_PIXEL_DEPTH  = 12,
 
-    RS_KIND_INVALID = 100,
+    RS_KIND_INVALID      = 100,
 } rs_data_kind;
 
+typedef enum {
+    RS_DEPTH_FUNC_ALWAYS        = 0,
+    RS_DEPTH_FUNC_LESS          = 1,
+    RS_DEPTH_FUNC_LEQUAL        = 2,
+    RS_DEPTH_FUNC_GREATER       = 3,
+    RS_DEPTH_FUNC_GEQUAL        = 4,
+    RS_DEPTH_FUNC_EQUAL         = 5,
+    RS_DEPTH_FUNC_NOTEQUAL      = 6,
+
+    RS_DEPTH_FUNC_INVALID       = 100,
+} rs_depth_func;
+
+typedef enum {
+    RS_BLEND_SRC_ZERO                   = 0,
+    RS_BLEND_SRC_ONE                    = 1,
+    RS_BLEND_SRC_DST_COLOR              = 2,
+    RS_BLEND_SRC_ONE_MINUS_DST_COLOR    = 3,
+    RS_BLEND_SRC_SRC_ALPHA              = 4,
+    RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA    = 5,
+    RS_BLEND_SRC_DST_ALPHA              = 6,
+    RS_BLEND_SRC_ONE_MINUS_DST_ALPHA    = 7,
+    RS_BLEND_SRC_SRC_ALPHA_SATURATE     = 8,
+
+    RS_BLEND_SRC_INVALID                = 100,
+} rs_blend_src_func;
+
+typedef enum {
+    RS_BLEND_DST_ZERO                   = 0,
+    RS_BLEND_DST_ONE                    = 1,
+    RS_BLEND_DST_SRC_COLOR              = 2,
+    RS_BLEND_DST_ONE_MINUS_SRC_COLOR    = 3,
+    RS_BLEND_DST_SRC_ALPHA              = 4,
+    RS_BLEND_DST_ONE_MINUS_SRC_ALPHA    = 5,
+    RS_BLEND_DST_DST_ALPHA              = 6,
+    RS_BLEND_DST_ONE_MINUS_DST_ALPHA    = 7,
+
+    RS_BLEND_DST_INVALID                = 100,
+} rs_blend_dst_func;
+
+typedef enum {
+    RS_CULL_BACK     = 0,
+    RS_CULL_FRONT    = 1,
+    RS_CULL_NONE     = 2,
+
+    RS_CULL_INVALID  = 100,
+} rs_cull_mode;
+
+typedef enum {
+    RS_SAMPLER_NEAREST              = 0,
+    RS_SAMPLER_LINEAR               = 1,
+    RS_SAMPLER_LINEAR_MIP_LINEAR    = 2,
+    RS_SAMPLER_WRAP                 = 3,
+    RS_SAMPLER_CLAMP                = 4,
+    RS_SAMPLER_LINEAR_MIP_NEAREST   = 5,
+
+    RS_SAMPLER_INVALID              = 100,
+} rs_sampler_value;
+
 #endif