Skip to content

Instantly share code, notes, and snippets.

@niusounds
Created September 4, 2014 05:41
Show Gist options
  • Select an option

  • Save niusounds/ade0a28f7172cc12e253 to your computer and use it in GitHub Desktop.

Select an option

Save niusounds/ade0a28f7172cc12e253 to your computer and use it in GitHub Desktop.
RenderScript filter shows output to TextureView.
#pragma version(1)
#pragma rs java_package_name(com.eje_c.renderscripttest)
#pragma rs_fp_relaxed
uchar4 __attribute__((kernel)) process(uchar4 in) {
in.r = in.g = in.b = in.r * 0.299f + in.g * 0.587f + in.b * 0.114f;
return in;
}
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
public class RenderScriptFilterTest extends Activity implements TextureView.SurfaceTextureListener {
private RenderScript rs;
private ScriptC_mono filter;
private Allocation input;
private Allocation output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rs = RenderScript.create(this);
filter = new ScriptC_mono(rs);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.testphoto);
input = Allocation.createFromBitmap(rs, bmp);
output = Allocation.createTyped(rs, input.getType(), Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
TextureView view = new TextureView(this);
view.setSurfaceTextureListener(this);
setContentView(view);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
output.setSurface(new Surface(surfaceTexture));
filter.forEach_process(input, output);
output.ioSend();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment