fix: LutImageProcessor release thread safety
This commit is contained in:
1 parent
cae7f9771b
commit
c7885d95a5
1 file changed
+27
-1
@@ -23,6 +23,7 @@ import com.hinnka.mycamera.utils.PLog
|
|||||||
import kotlinx.coroutines.asCoroutineDispatcher
|
import kotlinx.coroutines.asCoroutineDispatcher
|
||||||
import kotlinx.coroutines.currentCoroutineContext
|
import kotlinx.coroutines.currentCoroutineContext
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.nio.ByteOrder
|
import java.nio.ByteOrder
|
||||||
@@ -42,9 +43,18 @@ import kotlin.math.sqrt
|
|||||||
* 所有 GPU 操作在独立单线程完成,确保 EGL 上下文线程安全
|
* 所有 GPU 操作在独立单线程完成,确保 EGL 上下文线程安全
|
||||||
*/
|
*/
|
||||||
class LutImageProcessor {
|
class LutImageProcessor {
|
||||||
|
@Volatile
|
||||||
|
private var glThread: Thread? = null
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var isReleased = false
|
||||||
|
|
||||||
// 单线程调度器,确保所有 EGL 操作在同一线程
|
// 单线程调度器,确保所有 EGL 操作在同一线程
|
||||||
private val glDispatcher = Executors.newSingleThreadExecutor { r ->
|
private val glDispatcher = Executors.newSingleThreadExecutor { r ->
|
||||||
Thread(r, "LutImageProcessor-GL").apply { isDaemon = true }
|
Thread(r, "LutImageProcessor-GL").apply {
|
||||||
|
isDaemon = true
|
||||||
|
glThread = this
|
||||||
|
}
|
||||||
}.asCoroutineDispatcher()
|
}.asCoroutineDispatcher()
|
||||||
|
|
||||||
private var eglDisplay: EGLDisplay = EGL14.EGL_NO_DISPLAY
|
private var eglDisplay: EGLDisplay = EGL14.EGL_NO_DISPLAY
|
||||||
@@ -2853,9 +2863,25 @@ class LutImageProcessor {
|
|||||||
* 释放资源
|
* 释放资源
|
||||||
*/
|
*/
|
||||||
fun release() {
|
fun release() {
|
||||||
|
if (isReleased) return
|
||||||
|
|
||||||
|
if (Thread.currentThread() === glThread) {
|
||||||
|
releaseOnGlThread()
|
||||||
|
} else {
|
||||||
|
runBlocking(glDispatcher) {
|
||||||
|
releaseOnGlThread()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isReleased = true
|
||||||
|
glDispatcher.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun releaseOnGlThread() {
|
||||||
if (!isInitialized) return
|
if (!isInitialized) return
|
||||||
|
|
||||||
EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)
|
EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)
|
||||||
|
GLES30.glFinish()
|
||||||
|
|
||||||
if (shaderProgram != 0) {
|
if (shaderProgram != 0) {
|
||||||
GLES30.glDeleteProgram(shaderProgram)
|
GLES30.glDeleteProgram(shaderProgram)
|
||||||
|
|||||||
Reference in new issue
Block a user