Skip to content

Instantly share code, notes, and snippets.

@niusounds
Last active October 29, 2015 15:59
Show Gist options
  • Select an option

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

Select an option

Save niusounds/532f46eb3ab4764226dc to your computer and use it in GitHub Desktop.
Generate content with RenderScript and show result to TextureView.
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.Type;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import java.util.Random;
public class RenderScriptTest extends Activity implements TextureView.SurfaceTextureListener {
public static final Random RANDOM = new Random();
private RenderScript rs;
private ScriptC_simple simple;
private Allocation output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rs = RenderScript.create(this);
simple = new ScriptC_simple(rs);
TextureView view = new TextureView(this);
view.setSurfaceTextureListener(this);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int t = RANDOM.nextInt();
simple.set_t(t);
simple.forEach_process(output);
output.ioSend();
}
});
setContentView(view);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
output = Allocation.createTyped(rs,
new Type.Builder(rs, Element.RGBA_8888(rs))
.setX(width)
.setY(height).create(),
Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
output.setSurface(new Surface(surfaceTexture));
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}
#pragma version(1)
#pragma rs java_package_name(com.eje_c.renderscripttest)
#pragma rs_fp_relaxed
int t = 0;
uchar4 __attribute__((kernel)) process(uint32_t x, uint32_t y) {
uchar4 out = {(x + t) % 255, y % 255, t % 255, 255};
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment