gpt4 book ai didi

android - GLConsumer 已附加到新 SurfaceTexture 的上下文

转载 作者:行者123 更新时间:2023-11-29 15:39:38 27 4
gpt4 key购买 nike

SurfaceTexture 是否在手动创建时默认附加到 GLContext?如果是,怎么办?

这是一个示例,我正在尝试创建自己的 SurfaceTexture 并将其设置为 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);
}
}

我不断得到:

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

异常(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)

在新创建的 SurfaceTexture 对象上调用 detachFromGLContext 解决了问题,但这相当令人困惑,因为我可以确认 attachToGLContext 从未在此调用场景。

附言我知道 TextureView 默认有自己的 SurfaceTexture。在这种情况下,我必须使用从 SurfaceTexture 扩展而来的自己的实现。

最佳答案

我会发布我自己的问题的答案,以防其他人遇到同样的问题。

据推测,由于以下原因,一个新的 SurfaceTexture 默认附加到 GLContext:

  1. SurfaceTexture 构造函数调用 nativeInit

  2. nativeInit对应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 在此处创建一个新的 GLConsumer:https://android.googlesource.com/platform/frameworks/base.git/+/android-4.4.4_r2.0.1/core/jni/android/graphics/SurfaceTexture.cpp#239

解决方案是手动分离这个新创建的 SurfaceTexture在调用 super 构造函数之后立即从 GLContext 中获取。

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

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

我查找了专门针对 API-19 的文档,但您可以按照相同的路径查找其他 API 级别。

关于android - GLConsumer 已附加到新 SurfaceTexture 的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42988060/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com