title: RenderScript Stopped Working in Android Lollilop
body: <p>I was developing an app in KitKat (4.4) and I recently flashed Lollipop on my device (5.1). My RenderScript Code seems that is has 'broken'. I mean it compiles fine, but when I launch the app it is not doing what it is supposed to do. I use it to do edge detection in parallel but it is not doing anything. Previously on KitKat I could see the edges. Now it renders what the camera sees, no effect, no edge detection.</p>
  
  <p>Here is my Application Manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="foo.bar"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk android:minSdkVersion="9" /&gt;
  
      &lt;uses-feature android:name="android.hardware.camera" /&gt;
  
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;uses-permission android:name="android.hardware.camera.autofocus" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;!--uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /--&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:label="@string/app_name"
              android:name=".LiveCameraActivity"
              android:screenOrientation="sensorLandscape" &gt;
              &lt;intent-filter &gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Here is my app gradle <code>build.gradle</code> file:</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 19
      buildToolsVersion '19.1.0'
  
      defaultConfig {
          applicationId "foo.bar"
          minSdkVersion 15
          targetSdkVersion 19
          versionCode 1
          versionName "1.0"
  
          renderscriptTargetApi 19
          renderscriptSupportModeEnabled true //not applicable for rs targetapi 21+
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  </code></pre>
  
  <p>And more or less this is how I call my RenderScript Code:</p>
  
  <p>I have all my renderscript (.rs) files under the <code>rs</code> folder under <code>main</code>.</p>
  
  <p>And in Java:</p>
  
  <pre><code>bmp = mTextureView.getBitmap();
  bmpCopy = bmp.copy(bmp.getConfig(),true); //create a copy of the original
  bitmapProcessor.processBmpEdgeDetect(bmp, bmpCopy); //I think this is where it breaks
  mImageView.setImageBitmap(bmpCopy); // render result
  </code></pre>
  
  <p>Now <code>bitmapProcessor.processBmpEdgeDetect()</code> has this:</p>
  
  <pre><code>renderScriptEdgeDetectWrapper = new RenderScriptEdgeDetectWrapper(ctx);
  renderScriptAvgOperWrapper.setInAllocation(bmp);
  renderScriptAvgOperWrapper.setOutAllocation(bmpCopy);
  renderScriptAvgOperWrapper.setScriptWidth(bmp.getWidth()-1);
  renderScriptAvgOperWrapper.setScriptHeight(bmp.getHeight()-1);
  renderScriptAvgOperWrapper.forEach_root();
  </code></pre>
  
  <p>And my <code>renderScriptAvgOperWrapper</code> is basically this:</p>
  
  <pre><code>public class RenderScriptEdgeDetectWrapper {
  
      private Allocation inAllocation;
      private Allocation outAllocation;
      private RenderScript rs;
      private ScriptC_edgedetect edgeDetectScript;
      private Context ctx;
  
  
      public RenderScriptEdgeDetectWrapper(Context context){
          ctx = context;
          rs = RenderScript.create(ctx);
          edgeDetectScript = new ScriptC_edgedetect(rs, ctx.getResources(), R.raw.edgedetect);
      };
  
      public void setInAllocation(Bitmap bmp){
          inAllocation = Allocation.createFromBitmap(rs,bmp);
          edgeDetectScript.set_inPixels(inAllocation);
      };
  
      public void setOutAllocation(Bitmap bmp){
          outAllocation = Allocation.createFromBitmap(rs,bmp);
      };
  
      public void setScriptWidth(int scriptWidth) {
          edgeDetectScript.set_width(scriptWidth);
      }
  
      public void setScriptHeight(int scriptHeight) {
          edgeDetectScript.set_height(scriptHeight);
      }
  
      public void forEach_root(){
          edgeDetectScript.forEach_root(inAllocation,outAllocation);
      }
  }
  </code></pre>
  
  <p>It is just a simple wrapper.</p>
  
  <p>Finally my renderScript <code>edgedetect.rs</code> file is this:</p>
  
  <pre><code>#pragma version(1)
  #pragma rs java_package_name(foo.bar)
  
  rs_allocation inPixels;
  int height;
  int width;
  
  void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
      float3 pixel = convert_float4(in[0]).rgb;
  
      if(x==0 || x==width || y==0 || y==height){
          pixel.r = 0;
          pixel.g = 191;
          pixel.b = 255;
      }else{ //do image processing here
  
          float3 pixelNH = convert_float4(rsGetElementAt_uchar4(inPixels, x+1, y)).rgb;
          float3 pixelNV = convert_float4(rsGetElementAt_uchar4(inPixels, x, y+1)).rgb;
  
          int grayAvg = (pixel.r + pixel.g + pixel.b)/3;
          int grayAvgNH = (pixelNH.r + pixelNH.g + pixelNH.b)/3;
          int grayAvgNV = (pixelNV.r + pixelNV.g + pixelNV.b)/3;
  
          int edgeOperatorValue = 2*grayAvg - grayAvgNH - grayAvgNV;
  
          if(edgeOperatorValue &lt; 0){
              edgeOperatorValue = -1 * edgeOperatorValue;
          };
  
          pixel.r = edgeOperatorValue;
          pixel.g = edgeOperatorValue;
          pixel.b = edgeOperatorValue;
      };
  
      out-&gt;xyz = convert_uchar3(pixel);
  }
  </code></pre>
  
  <p>Now I do not understand why this code worked fine in KitKat but fails completely in Lollipop. If you could guide in what is wrong that would be great. Thanks.</p>