fix: prevent curve editor crashes and offload metering tasks to background thread
This commit is contained in:
1 parent
07cfde312c
commit
5078f56188
2 files changed
+61
-27
No files matched your search
@@ -14,6 +14,8 @@ import com.hinnka.mycamera.video.VideoLogProfile
|
||||
import com.hinnka.mycamera.video.VideoRecorder
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import javax.microedition.khronos.egl.EGLConfig
|
||||
import javax.microedition.khronos.opengles.GL10
|
||||
import kotlin.math.hypot
|
||||
@@ -110,7 +112,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
private var meteringFboId: Int = 0
|
||||
private var meteringTextureId: Int = 0
|
||||
private val METERING_SIZE = 32
|
||||
private val lumaGrid = IntArray(METERING_SIZE * METERING_SIZE)
|
||||
private val meteringExecutor: ExecutorService = Executors.newSingleThreadExecutor()
|
||||
private var captureFboId: Int = 0
|
||||
private var captureTextureId: Int = 0
|
||||
private var recordFboId: Int = 0
|
||||
@@ -2273,8 +2275,6 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
private val meteringBuffer =
|
||||
ByteBuffer.allocateDirect(METERING_SIZE * METERING_SIZE * 4).order(ByteOrder.nativeOrder())
|
||||
private var lastRunMeteringTime = 0L
|
||||
|
||||
private fun runMeteringInternal(
|
||||
@@ -2332,17 +2332,32 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
) as? ByteBuffer
|
||||
|
||||
if (mappedBuffer != null) {
|
||||
meteringBuffer.rewind()
|
||||
meteringBuffer.put(mappedBuffer)
|
||||
val bytesCopy = ByteArray(pixelSize)
|
||||
mappedBuffer.rewind()
|
||||
mappedBuffer.get(bytesCopy)
|
||||
GLES30.glUnmapBuffer(GLES30.GL_PIXEL_PACK_BUFFER)
|
||||
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
GLES30.glViewport(0, 0, viewportWidth, viewportHeight)
|
||||
|
||||
val currentFocus = focusPoint
|
||||
val currentMode = meteringMode
|
||||
|
||||
try {
|
||||
if (!meteringExecutor.isShutdown) {
|
||||
meteringExecutor.execute {
|
||||
calculateMeteringResults(bytesCopy, currentFocus, currentMode)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to dispatch metering task", e)
|
||||
}
|
||||
} else {
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
GLES30.glViewport(0, 0, viewportWidth, viewportHeight)
|
||||
}
|
||||
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
GLES30.glViewport(0, 0, viewportWidth, viewportHeight)
|
||||
|
||||
// 3. 计算直方图和测光 (CPU)
|
||||
calculateMeteringResults()
|
||||
}
|
||||
|
||||
private fun runDepthInputCaptureInternal(sourceTextureId: Int) {
|
||||
@@ -2486,18 +2501,11 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
GLES30.glViewport(0, 0, viewportWidth, viewportHeight)
|
||||
}
|
||||
|
||||
private val meteringBytes = ByteArray(METERING_SIZE * METERING_SIZE * 4)
|
||||
|
||||
private fun calculateMeteringResults() {
|
||||
meteringBuffer.rewind()
|
||||
meteringBuffer.get(meteringBytes)
|
||||
|
||||
private fun calculateMeteringResults(meteringBytes: ByteArray, focus: PointF?, mode: MeteringMode) {
|
||||
val histogram = IntArray(256)
|
||||
var weightedSumLuminance = 0.0
|
||||
var totalWeight = 0.0
|
||||
|
||||
val focus = focusPoint
|
||||
val mode = meteringMode
|
||||
val lumaGrid = IntArray(METERING_SIZE * METERING_SIZE)
|
||||
|
||||
// 根据测光模式设置权重参数
|
||||
val (weightCenter, weightEdge, radiusSq) = when (mode) {
|
||||
@@ -2633,6 +2641,11 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
* 释放资源
|
||||
*/
|
||||
fun release() {
|
||||
try {
|
||||
meteringExecutor.shutdown()
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Error shutting down metering executor", e)
|
||||
}
|
||||
// 删除纹理
|
||||
if (cameraTextureId != 0) {
|
||||
GlUtils.deleteTexture(cameraTextureId)
|
||||
|
||||
@@ -188,9 +188,7 @@ private fun CurveCanvas(
|
||||
val ny = normalizeCurveY(change.position.y, size, graphInsetPx)
|
||||
|
||||
// x 轴:由相邻点边界夹紧,不排序——消除抖动
|
||||
val minX = if (dragIdx > 0) current[dragIdx - 1].first + 0.005f else 0f
|
||||
val maxX = if (dragIdx < current.size - 1) current[dragIdx + 1].first - 0.005f else 1f
|
||||
val nx = rawX.coerceIn(minX, maxX)
|
||||
val nx = safeCoerceX(rawX, dragIdx, current)
|
||||
|
||||
val updated = current.toMutableList()
|
||||
updated[dragIdx] = Pair(nx, ny)
|
||||
@@ -231,9 +229,7 @@ private fun CurveCanvas(
|
||||
val current = controlPointsState.value
|
||||
val rawX = normalizeCurveX(change.position.x, size, graphInsetPx)
|
||||
val ny = normalizeCurveY(change.position.y, size, graphInsetPx)
|
||||
val minX = if (dragIdx > 0) current[dragIdx - 1].first + 0.005f else 0f
|
||||
val maxX = if (dragIdx < current.size - 1) current[dragIdx + 1].first - 0.005f else 1f
|
||||
val nx = rawX.coerceIn(minX, maxX)
|
||||
val nx = safeCoerceX(rawX, dragIdx, current)
|
||||
val updated = current.toMutableList()
|
||||
updated[dragIdx] = Pair(nx, ny)
|
||||
controlPointsState.value = updated
|
||||
@@ -353,6 +349,31 @@ private fun DrawScope.drawCurveCanvas(
|
||||
|
||||
// ──────────────────────── 辅助函数 ────────────────────────
|
||||
|
||||
/**
|
||||
* 安全地限制控制点的 x 坐标,防止 minX > maxX 导致 coerceIn 崩溃。
|
||||
* 如果由于邻近点距离太近而导致 minX > maxX,则退而使用极小缓冲值 (0.0001f)。
|
||||
* 如果仍无法满足,则直接限制在两个相邻点之间,或返回相邻点的中点。
|
||||
*/
|
||||
private fun safeCoerceX(
|
||||
rawX: Float,
|
||||
dragIdx: Int,
|
||||
current: List<Pair<Float, Float>>
|
||||
): Float {
|
||||
val minX = if (dragIdx > 0) current[dragIdx - 1].first + 0.005f else 0f
|
||||
val maxX = if (dragIdx < current.size - 1) current[dragIdx + 1].first - 0.005f else 1f
|
||||
return if (minX <= maxX) {
|
||||
rawX.coerceIn(minX, maxX)
|
||||
} else {
|
||||
val leftBound = if (dragIdx > 0) current[dragIdx - 1].first + 0.0001f else 0f
|
||||
val rightBound = if (dragIdx < current.size - 1) current[dragIdx + 1].first - 0.0001f else 1f
|
||||
if (leftBound <= rightBound) {
|
||||
rawX.coerceIn(leftBound, rightBound)
|
||||
} else {
|
||||
(leftBound + rightBound) / 2f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 解析 FloatArray 为控制点列表 */
|
||||
private fun parsePoints(points: FloatArray): List<Pair<Float, Float>> {
|
||||
val n = points.size / 2
|
||||
|
||||
Reference in new issue
Block a user