0

Currently I am trying to work with both textureView and OpenGL, and my target is to read the buffer from SurfaceTexture. After lots of searching, I found grafika, but fail to find a suitable example that using textureview.(and, for some reason I have to use textureView)

What I try was that I created a texture and tried to set the textureView to use it:

    @Override
protected void onCreate(final Bundle savedInstanceState) {

    super.onCreate(null);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_textureview);

    displayTextureView=(TextureView) findViewById(R.id.camera_textureview);
    mEglCore = new EglCore(null, EglCore.FLAG_RECORDABLE);
    mOffscreenSurface=new OffscreenSurface(mEglCore,VIDEO_WIDTH,VIDEO_HEIGHT);
    mOffscreenSurface.makeCurrent();
    mFullFrameBlit = new FullFrameRect(
            new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT));
    mTextureId = mFullFrameBlit.createTextureObject();
    mCameraTexture = new SurfaceTexture(false);
    mCameraTexture.attachToGLContext(mTextureId);
    displayTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
    displayTextureView.setSurfaceTexture(mCameraTexture);
    mHandler = new MainHandler(this);
    Initialized=true;
}

But then it gitves me the error:

GLConsumer is already attached to a context

I have also found this, but after I tried the method described here what I got from glReadPixels was totally black, so I guess the surfaceTexture must be attached to GLcontext to read the pixels.

Can anybody give me some help?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Michael.Z
  • 41
  • 7

1 Answers1

1

you should refer to this https://github.com/google/grafika/blob/master/app/src/main/java/com/android/grafika/ContinuousCaptureActivity.java

this example use surfaceView , you just need to replace to textureView.

the 386 row:"mDisplaySurface = new WindowSurface(mEglCore, holder.getSurface(), false);" just replace holder.getSurface() with surfacetexture from textureView.

And there is a method to read buffer https://github.com/google/grafika/blob/master/app/src/main/java/com/android/grafika/gles/EglSurfaceBase.java

look at the saveFrame method.

dragonfly
  • 1,151
  • 14
  • 35
  • Thanks for your answer! By the surfacetexture do I need to create one myself or use the one that textureview creates automatically? If I use the one that textureview creates automatically how do I get the texture id that the surfacetexture is bind to so that I can draw frame onto the windowsurface?(also, I looked into the android sdk, and looks like the surfacetexture is not bind to any GLcontext when it`s created), and if I use the one I created like in my code, there will be the error I mentioned and it doesn`t work.... – Michael.Z Mar 05 '19 at 17:51
  • Just created a demo of what I have now, could you have a look? https://github.com/MICHAEL-ZENGZF/Get_SurfaceTexture_Buffer/blob/master/getsurfacetexturebuffer/src/main/java/com/airlife/getsurfacetexturebuffer/CameraWithTextureView.java – Michael.Z Mar 05 '19 at 17:58
  • your demo is not right. the right way is that: 1、use the surfacetexture from textureView to create a screen windowsurface 2 . create an another surfacetexture with oes texture 3. bind camera to the new surfacetexture, so camera will give frames to the surfacetexture 4.draw the frame in the oes texture to screen windowsurface. – dragonfly Mar 06 '19 at 03:23
  • Fantastic!!! you perfectly solved my problem~ Also, I have another question, when I try to read pixels directly from the window surface, the camera preview was really slow, so instead I render the texture to an OffscreenSurface and then it solves the problem, could you tell me what`s the mechanism behind this? – Michael.Z Mar 06 '19 at 21:48
  • The opengl api readPixel will read the data from gpu ram to cpu ram, this is slow. So readPixel will block the rendering thread and the fps of camera preview will reduce. If you want more smooth effect, you should use the PBO technology which is introduced since opengl 3.0. – dragonfly Mar 07 '19 at 04:33
  • Interesting, Thank you so much! – Michael.Z Mar 07 '19 at 22:10