Handle user message ID 0
Pass RS runtime errors back to java.
throw exceptions for runtime errors.

Change-Id: Ifcf16cbbf9b98137971dced5076f8a5563eb016c
diff --git a/RenderScript.h b/RenderScript.h
index 8c2081d..20e289d 100644
--- a/RenderScript.h
+++ b/RenderScript.h
@@ -79,13 +79,24 @@
 RsContext rsContextCreateGL(RsDevice, uint32_t version, RsSurfaceConfig sc);
 void rsContextDestroy(RsContext);
 
-uint32_t rsContextGetMessage(RsContext, void *data, size_t *receiveLen, size_t bufferLen, bool wait);
+enum RsMessageToClientType {
+    RS_MESSAGE_TO_CLIENT_NONE = 0,
+    RS_MESSAGE_TO_CLIENT_EXCEPTION = 1,
+    RS_MESSAGE_TO_CLIENT_RESIZE = 2,
+    RS_MESSAGE_TO_CLIENT_ERROR = 3,
+    RS_MESSAGE_TO_CLIENT_USER = 4
+};
+
+RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
+RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait);
 void rsContextInitToClient(RsContext);
 void rsContextDeinitToClient(RsContext);
 
 #define RS_MAX_TEXTURE 2
 #define RS_MAX_ATTRIBS 16
 
+
+
 enum RsDataType {
     RS_TYPE_NONE,
     RS_TYPE_FLOAT_16,
diff --git a/rsContext.cpp b/rsContext.cpp
index 678d327..9d766b7 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -846,53 +846,69 @@
     }
 }
 
-uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
+RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait)
+{
+    *receiveLen = 0;
+    if (!wait && mIO.mToClient.isEmpty()) {
+        return RS_MESSAGE_TO_CLIENT_NONE;
+    }
+
+    uint32_t bytesData = 0;
+    uint32_t commandID = 0;
+    const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
+    *receiveLen = bytesData - sizeof(uint32_t);
+    if (bytesData) {
+        *subID = d[0];
+    }
+    return (RsMessageToClientType)commandID;
+}
+
+RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait)
 {
     //LOGE("getMessageToClient %i %i", bufferLen, wait);
     *receiveLen = 0;
-    if (!wait) {
-        if (mIO.mToClient.isEmpty()) {
-            // No message to get and not going to wait for one.
-            return 0;
-        }
+    if (!wait && mIO.mToClient.isEmpty()) {
+        return RS_MESSAGE_TO_CLIENT_NONE;
     }
 
     //LOGE("getMessageToClient 2 con=%p", this);
     uint32_t bytesData = 0;
     uint32_t commandID = 0;
-    const void *d = mIO.mToClient.get(&commandID, &bytesData);
+    const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
     //LOGE("getMessageToClient 3    %i  %i", commandID, bytesData);
 
-    *receiveLen = bytesData;
+    *receiveLen = bytesData - sizeof(uint32_t);
+    *subID = d[0];
+
+    //LOGE("getMessageToClient  %i %i", commandID, *subID);
     if (bufferLen >= bytesData) {
-        memcpy(data, d, bytesData);
+        memcpy(data, d+1, *receiveLen);
         mIO.mToClient.next();
-        return commandID;
+        return (RsMessageToClientType)commandID;
     }
-    return 0;
+    return RS_MESSAGE_TO_CLIENT_RESIZE;
 }
 
-bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
+bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace)
 {
-    //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
+    //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace);
     if (cmdID == 0) {
         LOGE("Attempting to send invalid command 0 to client.");
         return false;
     }
     if (!waitForSpace) {
-        if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
+        if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) {
             // Not enough room, and not waiting.
             return false;
         }
     }
     //LOGE("sendMessageToClient 2");
+    uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID));
+    p[0] = subID;
     if (len > 0) {
-        void *p = mIO.mToClient.reserve(len);
-        memcpy(p, data, len);
-        mIO.mToClient.commit(cmdID, len);
-    } else {
-        mIO.mToClient.commit(cmdID, 0);
+        memcpy(p+1, data, len);
     }
+    mIO.mToClient.commit(cmdID, len + sizeof(subID));
     //LOGE("sendMessageToClient 3");
     return true;
 }
@@ -923,6 +939,7 @@
 {
     mError = e;
     mErrorMsg = msg;
+    sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
 }
 
 
@@ -1080,10 +1097,16 @@
     delete rsc;
 }
 
-uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
+RsMessageToClientType rsContextPeekMessage(RsContext vrsc, size_t *receiveLen, uint32_t *subID, bool wait)
 {
     Context * rsc = static_cast<Context *>(vrsc);
-    return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
+    return rsc->peekMessageToClient(receiveLen, subID, wait);
+}
+
+RsMessageToClientType rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait)
+{
+    Context * rsc = static_cast<Context *>(vrsc);
+    return rsc->getMessageToClient(data, receiveLen, subID, bufferLen, wait);
 }
 
 void rsContextInitToClient(RsContext vrsc)
diff --git a/rsContext.h b/rsContext.h
index 2017ceb..2b9e57a 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -128,8 +128,9 @@
     void assignName(ObjectBase *obj, const char *name, uint32_t len);
     void removeName(ObjectBase *obj);
 
-    uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
-    bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
+    RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
+    RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
+    bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace);
     uint32_t runScript(Script *s);
 
     void initToClient();
diff --git a/rsScriptC_Lib.cpp b/rsScriptC_Lib.cpp
index ecae306..bb8e6a7 100644
--- a/rsScriptC_Lib.cpp
+++ b/rsScriptC_Lib.cpp
@@ -347,28 +347,28 @@
 {
     GET_TLS();
     //LOGE("SC_toClient %i %i %i", cmdID, len);
-    return rsc->sendMessageToClient(data, cmdID, len, false);
+    return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, false);
 }
 
 static uint32_t SC_toClient(int cmdID)
 {
     GET_TLS();
     //LOGE("SC_toClient %i", cmdID);
-    return rsc->sendMessageToClient(NULL, cmdID, 0, false);
+    return rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_USER, cmdID, 0, false);
 }
 
 static uint32_t SC_toClientBlocking2(int cmdID, void *data, int len)
 {
     GET_TLS();
     //LOGE("SC_toClientBlocking %i %i", cmdID, len);
-    return rsc->sendMessageToClient(data, cmdID, len, true);
+    return rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, cmdID, len, true);
 }
 
 static uint32_t SC_toClientBlocking(int cmdID)
 {
     GET_TLS();
     //LOGE("SC_toClientBlocking %i", cmdID);
-    return rsc->sendMessageToClient(NULL, cmdID, 0, true);
+    return rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_USER, cmdID, 0, true);
 }
 
 int SC_divsi3(int a, int b)