Add fatal error check

Prevent launching additional commands if the process is dying
This increases the chance the developer will get the error message
before we segfault somewhere.

Change-Id: I575906b22364c0d03859140570ca29bf8f336c01
diff --git a/rsContext.cpp b/rsContext.cpp
index 75f927e..233926c 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -459,6 +459,7 @@
     mForceCpu = false;
     mContextType = RS_CONTEXT_TYPE_NORMAL;
     mSynchronous = false;
+    mFatalErrorOccured = false;
 }
 
 Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
@@ -725,6 +726,13 @@
 
 void Context::setError(RsError e, const char *msg) const {
     mError = e;
+
+    if (mError >= RS_ERROR_FATAL_DEBUG) {
+        // If a FATAL error occurred, set the flag to indicate the process
+        // will be goign down
+        mFatalErrorOccured = true;
+    }
+
     sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
 }
 
diff --git a/rsContext.h b/rsContext.h
index 459550c..1c6fc58 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -246,6 +246,12 @@
     RsContextType getContextType() const { return mContextType; }
     void setContextType(RsContextType ct) { mContextType = ct; }
 
+    // Check for Fatal errors
+    // Should be used to prevent work from being launched
+    // which could take the process down.  Maximizes the chance
+    // the process lives long enough to get the error to the developer
+    bool hadFatalError() {return mFatalErrorOccured;}
+
     Device *mDev;
 
 #ifdef RS_COMPATIBILITY_LIB
@@ -273,8 +279,10 @@
     bool mRunning;
     bool mExit;
     bool mPaused;
+    mutable bool mFatalErrorOccured;
     mutable RsError mError;
 
+
     pthread_t mThreadId;
     pid_t mNativeThreadId;
 
diff --git a/rsScript.cpp b/rsScript.cpp
index b89c96e..cb611af 100644
--- a/rsScript.cpp
+++ b/rsScript.cpp
@@ -48,6 +48,8 @@
         return;
     }
 
+    if (mRSC->hadFatalError()) return;
+
     mSlots[slot].set(a);
     mHasObjectSlots = true;
     mRSC->mHal.funcs.script.setGlobalBind(mRSC, this, slot, a);
@@ -59,6 +61,8 @@
         ALOGE("Script::setVar unable to set allocation, invalid slot index");
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mRSC->mHal.funcs.script.setGlobalVar(mRSC, this, slot, (void *)val, len);
 }
 
@@ -69,6 +73,8 @@
               "%u >= %zu", slot, mHal.info.exportedVariableCount);
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mRSC->mHal.funcs.script.getGlobalVar(mRSC, this, slot, (void *)val, len);
 }
 
@@ -79,6 +85,8 @@
               "%u >= %zu", slot, mHal.info.exportedVariableCount);
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mRSC->mHal.funcs.script.setGlobalVarWithElemDims(mRSC, this, slot,
             (void *)val, len, e, dims, dimLen);
 }
@@ -90,6 +98,8 @@
               "%u >= %zu", slot, mHal.info.exportedVariableCount);
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     mHasObjectSlots = true;
     mRSC->mHal.funcs.script.setGlobalObj(mRSC, this, slot, val);
 }
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index 4b204d3..4d791f7 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -204,6 +204,7 @@
     }
     ATRACE_NAME(String);
     (void)String;
+    if (mRSC->hadFatalError()) return;
 
     Context::PushState ps(rsc);
 
@@ -235,6 +236,8 @@
         rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
         return;
     }
+    if (mRSC->hadFatalError()) return;
+
     setupScript(rsc);
 
     if (rsc->props.mLogScripts) {