Allocation copy functions.

Change-Id: Idce6d44a4f4bb2e399284a40c0f90dc1bff912fd
diff --git a/RenderScriptDefines.h b/RenderScriptDefines.h
index ee9645c..d092520 100644
--- a/RenderScriptDefines.h
+++ b/RenderScriptDefines.h
@@ -110,12 +110,12 @@
 };
 
 enum RsAllocationCubemapFace {
-    RS_ALLOCATION_CUBMAP_FACE_POSITVE_X = 0,
-    RS_ALLOCATION_CUBMAP_FACE_NEGATIVE_X = 1,
-    RS_ALLOCATION_CUBMAP_FACE_POSITVE_Y = 2,
-    RS_ALLOCATION_CUBMAP_FACE_NEGATIVE_Y = 3,
-    RS_ALLOCATION_CUBMAP_FACE_POSITVE_Z = 4,
-    RS_ALLOCATION_CUBMAP_FACE_NEGATIVE_Z = 5
+    RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X = 0,
+    RS_ALLOCATION_CUBEMAP_FACE_NEGATIVE_X = 1,
+    RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_Y = 2,
+    RS_ALLOCATION_CUBEMAP_FACE_NEGATIVE_Y = 3,
+    RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_Z = 4,
+    RS_ALLOCATION_CUBEMAP_FACE_NEGATIVE_Z = 5
 };
 
 enum RsDataType {
diff --git a/driver/rsdAllocation.cpp b/driver/rsdAllocation.cpp
index 2e13e9d..8bfc185 100644
--- a/driver/rsdAllocation.cpp
+++ b/driver/rsdAllocation.cpp
@@ -378,6 +378,32 @@
 
 }
 
+void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
+                               const android::renderscript::Allocation *dstAlloc,
+                               uint32_t dstXoff, uint32_t dstLod, uint32_t count,
+                               const android::renderscript::Allocation *srcAlloc,
+                               uint32_t srcXoff, uint32_t srcLod){
+}
+
+void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
+                               const android::renderscript::Allocation *dstAlloc,
+                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
+                               RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
+                               const android::renderscript::Allocation *srcAlloc,
+                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
+                               RsAllocationCubemapFace srcFace) {
+}
+
+void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
+                               const android::renderscript::Allocation *dstAlloc,
+                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
+                               uint32_t dstLod, RsAllocationCubemapFace dstFace,
+                               uint32_t w, uint32_t h, uint32_t d,
+                               const android::renderscript::Allocation *srcAlloc,
+                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
+                               uint32_t srcLod, RsAllocationCubemapFace srcFace) {
+}
+
 void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
                                 uint32_t x,
                                 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
diff --git a/driver/rsdAllocation.h b/driver/rsdAllocation.h
index d7385ce..7555c4a 100644
--- a/driver/rsdAllocation.h
+++ b/driver/rsdAllocation.h
@@ -80,6 +80,27 @@
                          uint32_t lod, RsAllocationCubemapFace face,
                          uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
 
+void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
+                               const android::renderscript::Allocation *dstAlloc,
+                               uint32_t dstXoff, uint32_t dstLod, uint32_t count,
+                               const android::renderscript::Allocation *srcAlloc,
+                               uint32_t srcXoff, uint32_t srcLod);
+void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
+                               const android::renderscript::Allocation *dstAlloc,
+                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
+                               RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
+                               const android::renderscript::Allocation *srcAlloc,
+                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
+                               RsAllocationCubemapFace srcFace);
+void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
+                               const android::renderscript::Allocation *dstAlloc,
+                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
+                               uint32_t dstLod, RsAllocationCubemapFace dstFace,
+                               uint32_t w, uint32_t h, uint32_t d,
+                               const android::renderscript::Allocation *srcAlloc,
+                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
+                               uint32_t srcLod, RsAllocationCubemapFace srcFace);
+
 void rsdAllocationElementData1D(const android::renderscript::Context *rsc,
                                 const android::renderscript::Allocation *alloc,
                                 uint32_t x,
diff --git a/driver/rsdCore.cpp b/driver/rsdCore.cpp
index 01cc369..38f6895 100644
--- a/driver/rsdCore.cpp
+++ b/driver/rsdCore.cpp
@@ -74,6 +74,9 @@
         rsdAllocationData1D,
         rsdAllocationData2D,
         rsdAllocationData3D,
+        rsdAllocationData1D_alloc,
+        rsdAllocationData2D_alloc,
+        rsdAllocationData3D_alloc,
         rsdAllocationElementData1D,
         rsdAllocationElementData2D
     },
diff --git a/driver/rsdRuntimeStubs.cpp b/driver/rsdRuntimeStubs.cpp
index 9cbff95..bd8b3c3 100644
--- a/driver/rsdRuntimeStubs.cpp
+++ b/driver/rsdRuntimeStubs.cpp
@@ -88,6 +88,26 @@
     rsrAllocationSyncAll(rsc, sc, a, RS_ALLOCATION_USAGE_SCRIPT);
 }
 
+static void SC_AllocationCopy1DRange(Allocation *dstAlloc,
+                                     uint32_t dstOff,
+                                     uint32_t dstMip,
+                                     uint32_t count,
+                                     Allocation *srcAlloc,
+                                     uint32_t srcOff, uint32_t srcMip) {
+    GET_TLS();
+}
+
+static void SC_AllocationCopy2DRange(Allocation *dstAlloc,
+                                     uint32_t dstXoff, uint32_t dstYoff,
+                                     uint32_t dstMip, uint32_t dstFace,
+                                     uint32_t width, uint32_t height,
+                                     Allocation *srcAlloc,
+                                     uint32_t srcXoff, uint32_t srcYoff,
+                                     uint32_t srcMip, uint32_t srcFace) {
+    GET_TLS();
+}
+
+
 const Allocation * SC_getAllocation(const void *ptr) {
     GET_TLS();
     return rsrGetAllocation(rsc, sc, ptr);
@@ -566,8 +586,11 @@
     { "_Z21rsAllocationMarkDirty13rs_allocation", (void *)&SC_AllocationSyncAll, true },
     { "_Z20rsgAllocationSyncAll13rs_allocation", (void *)&SC_AllocationSyncAll, false },
     { "_Z20rsgAllocationSyncAll13rs_allocationj", (void *)&SC_AllocationSyncAll2, false },
+    { "_Z20rsgAllocationSyncAll13rs_allocation24rs_allocation_usage_type", (void *)&SC_AllocationSyncAll2, false },
     { "_Z15rsGetAllocationPKv", (void *)&SC_GetAllocation, true },
 
+    { "_Z23rsAllocationCopy1DRange13rs_allocationjjjS_jj", (void *)&SC_AllocationCopy1DRange, false },
+    { "_Z23rsAllocationCopy2DRange13rs_allocationjjj26rs_allocation_cubemap_facejjS_jjjS0_", (void *)&SC_AllocationCopy2DRange, false },
 
     // Messaging
 
diff --git a/rs.spec b/rs.spec
index 87d764d..963a6e7 100644
--- a/rs.spec
+++ b/rs.spec
@@ -209,6 +209,21 @@
 	param uint32_t dimY
 	}
 
+AllocationCopy2DRange {
+	param RsAllocation dest
+	param uint32_t destXoff
+	param uint32_t destYoff
+	param uint32_t destMip
+	param uint32_t destFace
+	param uint32_t width
+	param uint32_t height
+	param RsAllocation src
+	param uint32_t srcXoff
+	param uint32_t srcYoff
+	param uint32_t srcMip
+	param uint32_t srcFace
+	}
+
 SamplerCreate {
     param RsSamplerValue magFilter
     param RsSamplerValue minFilter
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index bff3660..f3e0c0a 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -532,6 +532,23 @@
     return texAlloc;
 }
 
+void rsi_AllocationCopy2DRange(Context *rsc,
+                               RsAllocation dstAlloc,
+                               uint32_t dstXoff, uint32_t dstYoff,
+                               uint32_t dstMip, uint32_t dstFace,
+                               uint32_t width, uint32_t height,
+                               RsAllocation srcAlloc,
+                               uint32_t srcXoff, uint32_t srcYoff,
+                               uint32_t srcMip, uint32_t srcFace) {
+    Allocation *dst = static_cast<Allocation *>(dstAlloc);
+    Allocation *src= static_cast<Allocation *>(srcAlloc);
+    rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
+                                           (RsAllocationCubemapFace)dstFace,
+                                           width, height,
+                                           src, srcXoff, srcYoff,srcMip,
+                                           (RsAllocationCubemapFace)srcFace);
+}
+
 }
 }
 
diff --git a/rsRuntime.h b/rsRuntime.h
index 6d45285..cb962a8 100644
--- a/rsRuntime.h
+++ b/rsRuntime.h
@@ -85,6 +85,21 @@
 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,
+                              uint32_t dstOff,
+                              uint32_t dstMip,
+                              uint32_t count,
+                              Allocation *srcAlloc,
+                              uint32_t srcOff, uint32_t srcMip);
+void rsrAllocationCopy2DRange(Context *, Allocation *dstAlloc,
+                              uint32_t dstXoff, uint32_t dstYoff,
+                              uint32_t dstMip, uint32_t dstFace,
+                              uint32_t width, uint32_t height,
+                              Allocation *srcAlloc,
+                              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);
 uint32_t rsrGetWidth(Context *, Script *);
diff --git a/rsScriptC_Lib.cpp b/rsScriptC_Lib.cpp
index 4ee0a3e..ec15bc0 100644
--- a/rsScriptC_Lib.cpp
+++ b/rsScriptC_Lib.cpp
@@ -164,6 +164,29 @@
     a->syncAll(rsc, usage);
 }
 
+void rsrAllocationCopy1DRange(Context *rsc, Allocation *dstAlloc,
+                              uint32_t dstOff,
+                              uint32_t dstMip,
+                              uint32_t count,
+                              Allocation *srcAlloc,
+                              uint32_t srcOff, uint32_t srcMip) {
+    rsi_AllocationCopy2DRange(rsc, dstAlloc, dstOff, 0,
+                              dstMip, 0, count, 1,
+                              srcAlloc, srcOff, 0, srcMip, 0);
+}
+
+void rsrAllocationCopy2DRange(Context *rsc, Allocation *dstAlloc,
+                              uint32_t dstXoff, uint32_t dstYoff,
+                              uint32_t dstMip, uint32_t dstFace,
+                              uint32_t width, uint32_t height,
+                              Allocation *srcAlloc,
+                              uint32_t srcXoff, uint32_t srcYoff,
+                              uint32_t srcMip, uint32_t srcFace) {
+    rsi_AllocationCopy2DRange(rsc, dstAlloc, dstXoff, dstYoff,
+                              dstMip, dstFace, width, height,
+                              srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
+}
+
 const Allocation * rsrGetAllocation(Context *rsc, Script *s, const void *ptr) {
     ScriptC *sc = (ScriptC *)s;
     return sc->ptrToAllocation(ptr);
diff --git a/rs_hal.h b/rs_hal.h
index 7bb09bb..928dca5 100644
--- a/rs_hal.h
+++ b/rs_hal.h
@@ -112,6 +112,27 @@
                        uint32_t lod, RsAllocationCubemapFace face,
                        uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
 
+        // Allocation to allocation copies
+        void (*allocData1D)(const Context *rsc,
+                            const Allocation *dstAlloc,
+                            uint32_t dstXoff, uint32_t dstLod, uint32_t count,
+                            const Allocation *srcAlloc, uint32_t srcXoff, uint32_t srcLod);
+        void (*allocData2D)(const Context *rsc,
+                            const Allocation *dstAlloc,
+                            uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
+                            RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
+                            const Allocation *srcAlloc,
+                            uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
+                            RsAllocationCubemapFace srcFace);
+        void (*allocData3D)(const Context *rsc,
+                            const Allocation *dstAlloc,
+                            uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
+                            uint32_t dstLod, RsAllocationCubemapFace dstFace,
+                            uint32_t w, uint32_t h, uint32_t d,
+                            const Allocation *srcAlloc,
+                            uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
+                            uint32_t srcLod, RsAllocationCubemapFace srcFace);
+
         void (*elementData1D)(const Context *rsc, const Allocation *alloc, uint32_t x,
                               const void *data, uint32_t elementOff, uint32_t sizeBytes);
         void (*elementData2D)(const Context *rsc, const Allocation *alloc, uint32_t x, uint32_t y,
diff --git a/scriptc/rs_graphics.rsh b/scriptc/rs_graphics.rsh
index d53bc95..9a8a4e6 100644
--- a/scriptc/rs_graphics.rsh
+++ b/scriptc/rs_graphics.rsh
@@ -144,6 +144,17 @@
     rsgAllocationSyncAll(rs_allocation alloc);
 
 /**
+ * Sync the contents of an allocation from memory space
+ * specified by source.
+ *
+ * @param alloc
+ * @param source
+ */
+extern void __attribute__((overloadable))
+    rsgAllocationSyncAll(rs_allocation alloc,
+                         rs_allocation_usage_type source);
+
+/**
  * Low performance utility function for drawing a simple rectangle.  Not
  * intended for drawing large quantities of geometry.
  *
diff --git a/scriptc/rs_math.rsh b/scriptc/rs_math.rsh
index 6e3cfdb..584317e 100644
--- a/scriptc/rs_math.rsh
+++ b/scriptc/rs_math.rsh
@@ -136,6 +136,58 @@
 extern uint32_t __attribute__((overloadable))
     rsAllocationGetDimFaces(rs_allocation);
 
+/**
+ * Copy part of an allocation from another allocation.
+ *
+ * @param dstAlloc Allocation to copy data into.
+ * @param dstOff The offset of the first element to be copied in
+ *               the destination allocation.
+ * @param dstMip Mip level in the destination allocation.
+ * @param count The number of elements to be copied.
+ * @param srcAlloc The source data allocation.
+ * @param srcOff The offset of the first element in data to be
+ *               copied in the source allocation.
+ * @param srcMip Mip level in the source allocation.
+ */
+extern void __attribute__((overloadable))
+    rsAllocationCopy1DRange(rs_allocation dstAlloc,
+                            uint32_t dstOff, uint32_t dstMip,
+                            uint32_t count,
+                            rs_allocation srcAlloc,
+                            uint32_t srcOff, uint32_t srcMip);
+
+/**
+ * Copy a rectangular region into the allocation from another
+ * allocation.
+ *
+ * @param dstAlloc allocation to copy data into.
+ * @param dstXoff X offset of the region to update in the
+ *                destination allocation.
+ * @param dstYoff Y offset of the region to update in the
+ *                destination allocation.
+ * @param dstMip Mip level in the destination allocation.
+ * @param dstFace Cubemap face of the destination allocation,
+ *                ignored for allocations that aren't cubemaps.
+ * @param width Width of the incoming region to update.
+ * @param height Height of the incoming region to update.
+ * @param srcAlloc The source data allocation.
+ * @param srcXoff X offset in data of the source allocation.
+ * @param srcYoff Y offset in data of the source allocation.
+ * @param srcMip Mip level in the source allocation.
+ * @param srcFace Cubemap face of the source allocation,
+ *                ignored for allocations that aren't cubemaps.
+ */
+extern void __attribute__((overloadable))
+    rsAllocationCopy2DRange(rs_allocation dstAlloc,
+                            uint32_t dstXoff, uint32_t dstYoff,
+                            uint32_t dstMip,
+                            rs_allocation_cubemap_face dstFace,
+                            uint32_t width, uint32_t height,
+                            rs_allocation srcAlloc,
+                            uint32_t srcXoff, uint32_t srcYoff,
+                            uint32_t srcMip,
+                            rs_allocation_cubemap_face srcFace);
+
 // Extract a single element from an allocation.
 extern const void * __attribute__((overloadable))
     rsGetElementAt(rs_allocation, uint32_t x);
diff --git a/scriptc/rs_types.rsh b/scriptc/rs_types.rsh
index d9f4b4b..536d1f0 100644
--- a/scriptc/rs_types.rsh
+++ b/scriptc/rs_types.rsh
@@ -88,4 +88,21 @@
 
 #define RS_PACKED __attribute__((packed, aligned(4)))
 
+typedef enum {
+    RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X = 0,
+    RS_ALLOCATION_CUBEMAP_FACE_NEGATIVE_X = 1,
+    RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_Y = 2,
+    RS_ALLOCATION_CUBEMAP_FACE_NEGATIVE_Y = 3,
+    RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_Z = 4,
+    RS_ALLOCATION_CUBEMAP_FACE_NEGATIVE_Z = 5
+} rs_allocation_cubemap_face;
+
+typedef enum {
+    RS_ALLOCATION_USAGE_SCRIPT = 0x0001,
+    RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE = 0x0002,
+    RS_ALLOCATION_USAGE_GRAPHICS_VERTEX = 0x0004,
+    RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS = 0x0008,
+    RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET = 0x0010
+} rs_allocation_usage_type;
+
 #endif