How to distribute Renderscript selection using OpenGL in Android

I have a Renderscript that processes an image that is output in the output to Distribution. I want to use this distribution as a texture in my OpenGL program, but I don't know how to get the texture identifier from Allocation.

On the other hand, I know that I can use graphical Renderscript, but since it is deprecated, I think there must be a different way to achieve the same result.

+6
source share
2 answers

Specify USAGE_IO_OUTPUT when creating the Distribution. Assuming you are generating texture data in a script, you should also add USAGE_SCRIPT. Then you can call

Allocation.setSurface (theGLSurface)

to associate the selection with the texture. Every time you want to update a texture with the contents of a script, you need to call.

Allocation.ioSend ()

This will move the data without additional copies.

+6
source

You formulated the question in terms of transforming the distribution into a texture, but it’s easier to think about creating a texture and then providing belonging to that texture. That way, you know that the texture ID is before passing it to the list. Beware: distribution has the right to radically change texture properties.

Step by step:

  • Create a GL Texture

  • Create a SurfaceTexture using your id

  • Create a selection using Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT

  • Pass SurfaceTexture for selection with setSurface()

  • Put data in Distribution

  • Call ioSend() in the Distribution section to update the texture

  • Repeat steps 5 and 6 as often as you want to update the texture.

I am very far from the GL expert, so step 2 frankly guesses. Below is an adaptation of the HelloCompute sample, in which I replace the displayout with a TextureView , which is a view that conveniently creates a texture and SurfaceTexture for me. From now on, I follow the steps above.

 package com.example.android.rs.hellocompute; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; 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.Surface.OutOfResourcesException; import android.view.TextureView; import android.widget.ImageView; public class HelloCompute extends Activity { private Bitmap mBitmapIn; private RenderScript mRS; private ScriptC_mono mScript; private Allocation mSurfaceAllocation; private Allocation mCanvasAllocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBitmapIn = loadBitmap(R.drawable.data); int x = mBitmapIn.getWidth(); int y = mBitmapIn.getHeight(); ImageView in = (ImageView) findViewById(R.id.displayin); in.setImageBitmap(mBitmapIn); mRS = RenderScript.create(this); mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono); mCanvasAllocation = Allocation.createTyped(mRS, new Type.Builder(mRS, Element.RGBA_8888(mRS)) .setX(x).setY(y).create(), Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT); mSurfaceAllocation = Allocation.createTyped(mRS, new Type.Builder(mRS, Element.RGBA_8888(mRS)) .setX(x).setY(y).create(), Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT); TextureView mTextureView = (TextureView) findViewById(R.id.displayout); mTextureView.setSurfaceTextureListener( new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture s, int w, int h) { if(s != null) mSurfaceAllocation.setSurface(new Surface(s)); mScript.forEach_root(mCanvasAllocation, mSurfaceAllocation); mSurfaceAllocation.ioSend(); } @Override public void onSurfaceTextureUpdated(SurfaceTexture s) { } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture s, int w, int h) { if(s != null) mSurfaceAllocation.setSurface(new Surface(s)); } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture s) { mSurfaceAllocation.setSurface(null); return true; } }); try { Surface surface = mCanvasAllocation.getSurface(); Canvas canvas = surface.lockCanvas(new Rect(0,0,100,100)); canvas.drawBitmap(mBitmapIn, 0, 0, new Paint()); surface.unlockCanvasAndPost(canvas); mCanvasAllocation.ioReceive(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (OutOfResourcesException e) { e.printStackTrace(); } } private Bitmap loadBitmap(int resource) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; return BitmapFactory.decodeResource(getResources(), resource, options); } } 
+1
source

All Articles