[RenderScript] better handle edge cases for Incremental Intrinsic
Support.

   bug: 21902810

(cherry picked from commit 47c6b0d9d617a74fdfc4a23a523157760b6632ef)

Change-Id: I37ff71f9002c591785143aa14dd825be7de60c78
diff --git a/v8/renderscript/java/src/android/support/v8/renderscript/RenderScript.java b/v8/renderscript/java/src/android/support/v8/renderscript/RenderScript.java
index f6fc27e..ce18c8d 100644
--- a/v8/renderscript/java/src/android/support/v8/renderscript/RenderScript.java
+++ b/v8/renderscript/java/src/android/support/v8/renderscript/RenderScript.java
@@ -48,6 +48,7 @@
     static final boolean DEBUG  = false;
     @SuppressWarnings({"UnusedDeclaration", "deprecation"})
     static final boolean LOG_ENABLED = false;
+    static final int SUPPORT_LIB_API = 23;
 
     static private ArrayList<RenderScript> mProcessContextList = new ArrayList<RenderScript>();
     private boolean mIsProcessContext = false;
@@ -731,6 +732,11 @@
     synchronized long nScriptIntrinsicCreate(int id, long eid, boolean mUseInc) {
         validate();
         if (mUseInc) {
+            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
+                Log.e(LOG_TAG, "Incremental Intrinsics are not supported, please change targetSdkVersion to >= 21");
+                throw new RSRuntimeException("Incremental Intrinsics are not supported before Lollipop (API 21)");
+            }
+
             if (!mIncLoaded) {
                 try {
                     System.loadLibrary("RSSupport");
@@ -738,7 +744,7 @@
                     Log.e(LOG_TAG, "Error loading RS Compat library for Incremental Intrinsic Support: " + e);
                     throw new RSRuntimeException("Error loading RS Compat library for Incremental Intrinsic Support: " + e);
                 }
-                if (!nIncLoadSO(sSdkVersion)) {
+                if (!nIncLoadSO(SUPPORT_LIB_API)) {
                     throw new RSRuntimeException("Error loading libRSSupport library for Incremental Intrinsic Support");
                 }
                 mIncLoaded = true;
@@ -1349,7 +1355,13 @@
         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
             useIOlib = true;
         }
-        if (!rs.nLoadSO(useNative, sdkVersion)) {
+
+        int dispatchAPI = sdkVersion;
+        if (sdkVersion < android.os.Build.VERSION.SDK_INT) {
+            // If the device API is higher than target API level, init dispatch table based on device API.
+            dispatchAPI = android.os.Build.VERSION.SDK_INT;
+        }
+        if (!rs.nLoadSO(useNative, dispatchAPI)) {
             if (useNative) {
                 android.util.Log.v(LOG_TAG, "Unable to load libRS.so, falling back to compat mode");
                 useNative = false;
@@ -1360,7 +1372,7 @@
                 Log.e(LOG_TAG, "Error loading RS Compat library: " + e);
                 throw new RSRuntimeException("Error loading RS Compat library: " + e);
             }
-            if (!rs.nLoadSO(false, sdkVersion)) {
+            if (!rs.nLoadSO(false, dispatchAPI)) {
                 throw new RSRuntimeException("Error loading libRSSupport library");
             }
         }