GLConsumer is already attached to a context for ne

2019-06-07 00:30发布

问题:

Is SurfaceTexture attached to GLContext by default when created manually? If so, how?

Here is an example, I'm trying to create my own SurfaceTexture and set it to TextureView:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.version);
        TextureView textureView = (TextureView) findViewById(R.id.texture);

        int[] arr = new int[1];
        GLES20.glGenTextures(1, arr, 0);
        int texName = arr[1];

        SurfaceTexture surfaceTexture = new SurfaceTexture(texName);
        textureView.setSurfaceTexture(surfaceTexture);
    }
}

I'm constantly getting:

E/GLConsumer: [unnamed-29058-0] attachToContext: GLConsumer is already attached to a context

And the exception:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.dkarmazin.openglesversion, PID: 29058 java.lang.RuntimeException: Error during attachToGLContext (see logcat for details) at android.graphics.SurfaceTexture.attachToGLContext(SurfaceTexture.java:215) at android.view.GLES20TextureLayer.setSurfaceTexture(GLES20TextureLayer.java:86) at android.view.HardwareRenderer$Gl20Renderer.setSurfaceTexture(HardwareRenderer.java:2228) at android.view.TextureView.getHardwareLayer(TextureView.java:401) at android.view.View.getDisplayList(View.java:13443) at android.view.View.getDisplayList(View.java:13519) at android.view.View.draw(View.java:14297) at android.view.ViewGroup.drawChild(ViewGroup.java:3115) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2952) at android.view.View.getDisplayList(View.java:13472) at android.view.View.getDisplayList(View.java:13519) at android.view.View.draw(View.java:14297) at android.view.ViewGroup.drawChild(ViewGroup.java:3115) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2952) at android.view.View.draw(View.java:14583) at android.widget.FrameLayout.draw(FrameLayout.java:472) at android.view.View.getDisplayList(View.java:13477) at android.view.View.getDisplayList(View.java:13519) at android.view.View.draw(View.java:14297) at android.view.ViewGroup.drawChild(ViewGroup.java:3115) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2952) at android.view.View.draw(View.java:14583) at android.support.v7.widget.ActionBarOverlayLayout.draw(ActionBarOverlayLayout.java:444) at android.view.View.getDisplayList(View.java:13477) at android.view.View.getDisplayList(View.java:13519) at android.view.View.draw(View.java:14297) at android.view.ViewGroup.drawChild(ViewGroup.java:3115) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2952) at android.view.View.getDisplayList(View.java:13472) at android.view.View.getDisplayList(View.java:13519) at android.view.View.draw(View.java:14297) at android.view.ViewGroup.drawChild(ViewGroup.java:3115) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2952) at android.view.View.getDisplayList(View.java:13472) at android.view.View.getDisplayList(View.java:13519) at android.view.View.draw(View.java:14297) at android.view.ViewGroup.drawChild(ViewGroup.java:3115) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2952) at android.view.View.draw(View.java:14583) at android.widget.FrameLayout.draw(FrameLayout.java:472) at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2326) at android.view.View.getDisplayList(View.java:13477) at android.view.View.getDisplayList(View.java:13519) at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1577) at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2402) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2019) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1079) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5948) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5102) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)

Calling detachFromGLContext on newly created SurfaceTexture object solves the problem, but that's rather confusing as I can confirm that attachToGLContext is never called in this scenario.

P.S. I know that TextureView has it's own SurfaceTexture by default. In this scenario I have to use own implementation that extends from SurfaceTexture.

回答1:

I'll post an answer to my own question just in case someone else is facing the same issue.

Presumably, a new SurfaceTexture is attached to GLContext by default due to:

  1. SurfaceTexture constructor calls to nativeInit

  2. nativeInit corresponds to SurfaceTexture_init: https://android.googlesource.com/platform/frameworks/base.git/+/android-4.4.4_r2.0.1/core/jni/android/graphics/SurfaceTexture.cpp#337

  3. SurfaceTexture_init creates a new GLConsumer here: https://android.googlesource.com/platform/frameworks/base.git/+/android-4.4.4_r2.0.1/core/jni/android/graphics/SurfaceTexture.cpp#239

Solution would be to manually detach this newly created SurfaceTexture from GLContext right after the call to a super constructor.

public class MySurfaceTexture extends SurfaceTexture {
    public MySurfaceTexture(int texName) {
        super(texName);
        init();
    }

    private void init() {
        super.detachFromGLContext();
    }
}

I looked up docs specifically for API-19, but you can follow the same path for other API levels.



回答2:

In Android O there is a new constructor which doesn't attach immediately. Sounds like that's what you want. As your self-answer says, the previous constructors automatically attach to the current thread's GL context.