Added in wait for attach context flag.

Added in a small flag which is  passed to the context from the Java/C++ API
which causes the context to delay execution until a debugger is a attached.
This gives time to the developer to attach a debugger to the process before
the kernel is executed.

Change-Id: I5bb8e75100436920f9e1a0f414f3b0bcf0d2b439
Signed-off-by: Stephen McGroarty <[email protected]>
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index c05e0f9..aa7dbd7 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -165,7 +165,7 @@
     }
 
     if (flags & ~(RS_CONTEXT_SYNCHRONOUS | RS_CONTEXT_LOW_LATENCY |
-                  RS_CONTEXT_LOW_POWER)) {
+                  RS_CONTEXT_LOW_POWER | RS_CONTEXT_WAIT_FOR_ATTACH)) {
         ALOGE("Invalid flags passed");
         return false;
     }
diff --git a/cpp/rsCppStructs.h b/cpp/rsCppStructs.h
index f3f7f31..e7acbed 100644
--- a/cpp/rsCppStructs.h
+++ b/cpp/rsCppStructs.h
@@ -80,7 +80,9 @@
  enum RSInitFlags {
      RS_INIT_SYNCHRONOUS = 1, ///< All RenderScript calls will be synchronous. May reduce latency.
      RS_INIT_LOW_LATENCY = 2, ///< Prefer low latency devices over potentially higher throughput devices.
-     RS_INIT_MAX = 4
+     // Bitflag 4 is reserved for the context flag low power
+     RS_INIT_WAIT_FOR_ATTACH = 8,   ///< Kernel execution will hold to give time for a debugger to be attached
+     RS_INIT_MAX = 16
  };
 
  /**
diff --git a/rsContext.cpp b/rsContext.cpp
index fba06cd..c21542f 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -45,6 +45,8 @@
 #include "rsCompatibilityLib.h"
 #endif
 
+int gDebuggerPresent = 0;
+
 #ifdef RS_SERVER
 // Android exposes gettid(), standard Linux does not
 static pid_t gettid() {
@@ -478,6 +480,11 @@
     }
 }
 
+void Context::waitForDebugger() {
+    while (!gDebuggerPresent) {
+        sleep(0);
+    }
+}
 
 Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
                                  RsContextType ct, uint32_t flags) {
@@ -496,6 +503,11 @@
         delete rsc;
         return nullptr;
     }
+
+    if (flags & RS_CONTEXT_WAIT_FOR_ATTACH) {
+        rsc->waitForDebugger();
+    }
+
     return rsc;
 }
 
diff --git a/rsContext.h b/rsContext.h
index c6b7079..918e5e3 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -45,6 +45,13 @@
 
 #endif
 
+/*
+ * This global will be found by the debugger and will have its value flipped.
+ * It's independent of the Context class to allow the debugger to do the above
+ * without knowing the type makeup. This allows the debugger to be attached at
+ * an earlier stage.
+*/
+extern "C" int gDebuggerPresent;
 
 // ---------------------------------------------------------------------------
 namespace android {
@@ -321,7 +328,7 @@
 private:
     Context();
     bool initContext(Device *, const RsSurfaceConfig *sc);
-
+    void waitForDebugger();
     bool mSynchronous;
     bool initGLThread();
     void deinitEGL();
diff --git a/rsDefines.h b/rsDefines.h
index fdb0720..a8957a8 100644
--- a/rsDefines.h
+++ b/rsDefines.h
@@ -252,7 +252,8 @@
 enum RsContextFlags {
     RS_CONTEXT_SYNCHRONOUS      = 0x0001,
     RS_CONTEXT_LOW_LATENCY      = 0x0002,
-    RS_CONTEXT_LOW_POWER        = 0x0004
+    RS_CONTEXT_LOW_POWER        = 0x0004,
+    RS_CONTEXT_WAIT_FOR_ATTACH  = 0x0008
 };
 
 enum RsBlasTranspose {