简体   繁体   English

在 android 中的 Dispatchers.Default 上运行 EGL 命令可以吗?

[英]Is it okay to run EGL commands on Dispatchers.Default in android?

As I understand, the EGL14.makeCurrent() function maps the thread to the EGLContext we created.据我了解,EGL14.makeCurrent() 函数将线程映射到我们创建的 EGLContext。 And every EGL operations we execute will be executed on the EGLContext tied to the thread.我们执行的每个 EGL 操作都将在绑定到线程的 EGLContext 上执行。

Is it safe to perform EGL operations wrapped with Dispatchers.Default?执行用 Dispatchers.Default 包装的 EGL 操作是否安全? According to docs, ""Dispatchers.Default is backed by a shared pool of threads on JVM.根据文档,“”Dispatchers.Default 由 JVM 上的共享线程池支持。 By default, the maximum number of threads used by this dispatcher is equal to the number of CPU cores, but is at least two.""默认情况下,此调度程序使用的最大线程数等于 CPU 核心数,但至少为两个。""

So will my EGL operations switch thread in Dispatchers.Default and the EGLContext be unavailable for the thread I am in?那么我的 EGL 操作是否会在 Dispatchers.Default 中切换线程并且 EGLContext 对于我所在的线程不可用?

It's not safe you have to call EGL function from the same thread which was linked to a EGL context.必须从链接到 EGL 上下文的同一线程调用 EGL 函数是不安全的。

You can create a dedicated thread to handle EGL calls and construct Dispatcher for them.您可以创建一个专用线程来处理 EGL 调用并为它们构造 Dispatcher。

Something like this:是这样的:

val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()

suspend fun init() {
    withContext(dispatcher) {
        EGL14.makeCurrent() 
    }
}

suspend fun render() {
    withContext(dispatcher) {
        EGLFun()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM