perf: optimize AI focus with dual PBO asynchronous capture and busy state tracking
This commit is contained in:
1 parent
13968d7759
commit
5464fa4cb6
7 files changed
+75
-28
No files matched your search
@@ -137,7 +137,10 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
var onDepthInputAvailable: ((Bitmap) -> Unit)? = null
|
||||
private var aiFocusInputFboId: Int = 0
|
||||
private var aiFocusInputTextureId: Int = 0
|
||||
private var aiFocusInputPboId: Int = 0
|
||||
private val aiFocusInputPboIds = IntArray(2)
|
||||
private var aiFocusInputPboIndex = 0
|
||||
@Volatile
|
||||
var isAiFocusBusy = false
|
||||
private val AI_FOCUS_INPUT_SIZE = 640
|
||||
private val aiFocusInputBuffer = ByteBuffer.allocateDirect(AI_FOCUS_INPUT_SIZE * AI_FOCUS_INPUT_SIZE * 4)
|
||||
private var lastRunAiFocusInputTime: Long = 0
|
||||
@@ -585,7 +588,10 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
depthInputPboId = 0
|
||||
aiFocusInputFboId = 0
|
||||
aiFocusInputTextureId = 0
|
||||
aiFocusInputPboId = 0
|
||||
aiFocusInputPboIds[0] = 0
|
||||
aiFocusInputPboIds[1] = 0
|
||||
aiFocusInputPboIndex = 0
|
||||
isAiFocusBusy = false
|
||||
fboId = 0
|
||||
fboTextureId = 0
|
||||
stackFboId = 0
|
||||
@@ -954,7 +960,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
val halationEnabled = redHalation > 0.001f
|
||||
val postProcessEffectEnabled = hdfEnabled || halationEnabled
|
||||
val bokehNeeded = aperture > 0f && depthMap != null
|
||||
val aiFocusInputNeeded = onAiFocusInputAvailable != null && isAutoFocus
|
||||
val aiFocusInputNeeded = onAiFocusInputAvailable != null && isAutoFocus && !isAiFocusBusy
|
||||
val suppressBaselineLayerForVideoLog = videoLogProfile.isEnabled
|
||||
val hasBaselineLayer = hasBaselineLayer() && !suppressBaselineLayerForVideoLog
|
||||
val hasCreativeLayer = hasCreativeLayer()
|
||||
@@ -2437,7 +2443,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
private fun runAiFocusInputCaptureInternal(sourceTextureId: Int) {
|
||||
if (onAiFocusInputAvailable == null || sourceTextureId == 0) return
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - lastRunAiFocusInputTime < 200) return
|
||||
if (now - lastRunAiFocusInputTime < 300) return
|
||||
lastRunAiFocusInputTime = now
|
||||
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, aiFocusInputFboId)
|
||||
@@ -2470,32 +2476,42 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
|
||||
GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, indexBufferId)
|
||||
GLES30.glDrawElements(GLES30.GL_TRIANGLES, 6, GLES30.GL_UNSIGNED_SHORT, 0)
|
||||
GLES30.glFinish()
|
||||
|
||||
val pixelSize = AI_FOCUS_INPUT_SIZE * AI_FOCUS_INPUT_SIZE * 4
|
||||
if (aiFocusInputPboId == 0) {
|
||||
val pbos = IntArray(1)
|
||||
GLES30.glGenBuffers(1, pbos, 0)
|
||||
aiFocusInputPboId = pbos[0]
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, aiFocusInputPboId)
|
||||
GLES30.glBufferData(GLES30.GL_PIXEL_PACK_BUFFER, pixelSize, null, GLES30.GL_STREAM_READ)
|
||||
} else {
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, aiFocusInputPboId)
|
||||
val writeIndex = aiFocusInputPboIndex % 2
|
||||
val readIndex = (aiFocusInputPboIndex + 1) % 2
|
||||
|
||||
if (aiFocusInputPboIds[0] == 0) {
|
||||
GLES30.glGenBuffers(2, aiFocusInputPboIds, 0)
|
||||
for (i in 0 until 2) {
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, aiFocusInputPboIds[i])
|
||||
GLES30.glBufferData(GLES30.GL_PIXEL_PACK_BUFFER, pixelSize, null, GLES30.GL_STREAM_READ)
|
||||
}
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
}
|
||||
|
||||
// 异步将当前帧读取到 writeIndex PBO
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, aiFocusInputPboIds[writeIndex])
|
||||
GLES30.glReadPixels(0, 0, AI_FOCUS_INPUT_SIZE, AI_FOCUS_INPUT_SIZE, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, 0)
|
||||
val mappedBuffer = GLES30.glMapBufferRange(GLES30.GL_PIXEL_PACK_BUFFER, 0, pixelSize, GLES30.GL_MAP_READ_BIT) as? ByteBuffer
|
||||
if (mappedBuffer != null) {
|
||||
aiFocusInputBuffer.rewind()
|
||||
aiFocusInputBuffer.put(mappedBuffer)
|
||||
GLES30.glUnmapBuffer(GLES30.GL_PIXEL_PACK_BUFFER)
|
||||
|
||||
val bitmap = Bitmap.createBitmap(AI_FOCUS_INPUT_SIZE, AI_FOCUS_INPUT_SIZE, Bitmap.Config.ARGB_8888)
|
||||
aiFocusInputBuffer.rewind()
|
||||
bitmap.copyPixelsFromBuffer(aiFocusInputBuffer)
|
||||
onAiFocusInputAvailable?.invoke(bitmap)
|
||||
// 提取上一帧已经传输完成的 readIndex PBO 的像素
|
||||
if (aiFocusInputPboIndex > 0) {
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, aiFocusInputPboIds[readIndex])
|
||||
val mappedBuffer = GLES30.glMapBufferRange(GLES30.GL_PIXEL_PACK_BUFFER, 0, pixelSize, GLES30.GL_MAP_READ_BIT) as? ByteBuffer
|
||||
if (mappedBuffer != null) {
|
||||
aiFocusInputBuffer.rewind()
|
||||
aiFocusInputBuffer.put(mappedBuffer)
|
||||
GLES30.glUnmapBuffer(GLES30.GL_PIXEL_PACK_BUFFER)
|
||||
|
||||
val bitmap = Bitmap.createBitmap(AI_FOCUS_INPUT_SIZE, AI_FOCUS_INPUT_SIZE, Bitmap.Config.ARGB_8888)
|
||||
aiFocusInputBuffer.rewind()
|
||||
bitmap.copyPixelsFromBuffer(aiFocusInputBuffer)
|
||||
onAiFocusInputAvailable?.invoke(bitmap)
|
||||
}
|
||||
}
|
||||
|
||||
aiFocusInputPboIndex++
|
||||
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
GLES30.glViewport(0, 0, viewportWidth, viewportHeight)
|
||||
@@ -2705,9 +2721,10 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
GLES30.glDeleteBuffers(1, intArrayOf(depthInputPboId), 0)
|
||||
depthInputPboId = 0
|
||||
}
|
||||
if (aiFocusInputPboId != 0) {
|
||||
GLES30.glDeleteBuffers(1, intArrayOf(aiFocusInputPboId), 0)
|
||||
aiFocusInputPboId = 0
|
||||
if (aiFocusInputPboIds[0] != 0) {
|
||||
GLES30.glDeleteBuffers(2, aiFocusInputPboIds, 0)
|
||||
aiFocusInputPboIds[0] = 0
|
||||
aiFocusInputPboIds[1] = 0
|
||||
}
|
||||
|
||||
if (meteringFboId != 0) {
|
||||
|
||||
@@ -49,6 +49,7 @@ class YoloXObjectDetector(context: Context) {
|
||||
private val scoresBuffer = ByteBuffer.allocateDirect(DETECTION_COUNT).order(ByteOrder.nativeOrder())
|
||||
private val classIdxBuffer = ByteBuffer.allocateDirect(DETECTION_COUNT).order(ByteOrder.nativeOrder())
|
||||
private val pixels = IntArray(INPUT_SIZE * INPUT_SIZE)
|
||||
private val byteBufferArray = ByteArray(INPUT_SIZE * INPUT_SIZE * 3)
|
||||
private val outputs = mapOf(0 to boxesBuffer, 1 to scoresBuffer, 2 to classIdxBuffer)
|
||||
|
||||
@Volatile
|
||||
@@ -169,11 +170,13 @@ class YoloXObjectDetector(context: Context) {
|
||||
}
|
||||
scaled.getPixels(pixels, 0, INPUT_SIZE, 0, 0, INPUT_SIZE, INPUT_SIZE)
|
||||
inputBuffer.rewind()
|
||||
var byteIdx = 0
|
||||
for (pixel in pixels) {
|
||||
inputBuffer.put(((pixel shr 16) and 0xFF).toByte())
|
||||
inputBuffer.put(((pixel shr 8) and 0xFF).toByte())
|
||||
inputBuffer.put((pixel and 0xFF).toByte())
|
||||
byteBufferArray[byteIdx++] = ((pixel shr 16) and 0xFF).toByte()
|
||||
byteBufferArray[byteIdx++] = ((pixel shr 8) and 0xFF).toByte()
|
||||
byteBufferArray[byteIdx++] = (pixel and 0xFF).toByte()
|
||||
}
|
||||
inputBuffer.put(byteBufferArray)
|
||||
inputBuffer.rewind()
|
||||
return inputBuffer
|
||||
}
|
||||
|
||||
@@ -55,6 +55,16 @@ class PreviewAiFocusProcessor(private val context: Context) {
|
||||
private var isPrewarmed = false
|
||||
@Volatile
|
||||
private var isProcessing = false
|
||||
set(value) {
|
||||
field = value
|
||||
onBusyStateChanged?.invoke(value)
|
||||
}
|
||||
|
||||
var onBusyStateChanged: ((Boolean) -> Unit)? = null
|
||||
|
||||
fun isBusy(): Boolean {
|
||||
return isProcessing
|
||||
}
|
||||
@Volatile
|
||||
private var processingStartTimeMs = 0L
|
||||
@Volatile
|
||||
|
||||
@@ -195,6 +195,11 @@ class CameraGLSurfaceView @JvmOverloads constructor(
|
||||
requestRender()
|
||||
}
|
||||
|
||||
fun setAiFocusBusy(busy: Boolean) {
|
||||
renderer.isAiFocusBusy = busy
|
||||
requestRender()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 LUT 强度
|
||||
*/
|
||||
|
||||
@@ -63,6 +63,7 @@ fun CameraPreviewGL(
|
||||
videoRecorder: VideoRecorder? = null,
|
||||
videoLogProfile: VideoLogProfile = VideoLogProfile.OFF,
|
||||
isHlgInput: Boolean = false,
|
||||
isAiFocusBusy: Boolean = false,
|
||||
onGLSurfaceViewReady: ((CameraGLSurfaceView) -> Unit)? = null,
|
||||
aperture: Float = 0f,
|
||||
isAutoFocus: Boolean = true,
|
||||
@@ -252,6 +253,7 @@ fun CameraPreviewGL(
|
||||
glSurfaceView.setIsHlgInput(isHlgInput)
|
||||
glSurfaceView.setAutoFocus(isAutoFocus)
|
||||
glSurfaceView.setFocusPeakingEnabled(focusPeakingEnabled)
|
||||
glSurfaceView.setAiFocusBusy(isAiFocusBusy)
|
||||
},
|
||||
modifier = Modifier.fillMaxSize()
|
||||
)
|
||||
|
||||
@@ -752,6 +752,7 @@ fun CameraScreen(
|
||||
|
||||
// 相机预览
|
||||
CameraPreviewGL(
|
||||
isAiFocusBusy = viewModel.isAiFocusBusy,
|
||||
aspectRatio = previewAspectRatio,
|
||||
previewSize = previewSize,
|
||||
captureMode = state.captureMode,
|
||||
|
||||
@@ -201,6 +201,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
|
||||
var isExpanded by mutableStateOf(false)
|
||||
|
||||
var isAiFocusBusy by mutableStateOf(false)
|
||||
|
||||
// 新增设置项 StateFlow
|
||||
val showLevelIndicator: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.showLevelIndicator }
|
||||
val focusPeakingEnabled: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.focusPeakingEnabled }
|
||||
@@ -735,6 +737,12 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
cameraController.previewAiFocusProcessor.onBusyStateChanged = { busy ->
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
isAiFocusBusy = busy
|
||||
}
|
||||
}
|
||||
|
||||
StartupTrace.mark("CameraViewModel.init end")
|
||||
}
|
||||
|
||||
@@ -2035,6 +2043,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
|
||||
fun handleAiFocusInputUpdate(bitmap: android.graphics.Bitmap) {
|
||||
if (isAiFocusBusy) return
|
||||
if (!state.value.isAutoFocus || state.value.isFocusing) return
|
||||
cameraController.previewAiFocusProcessor.targetMode = aiFocusTargetMode.value
|
||||
cameraController.previewAiFocusProcessor.scoreThreshold = aiFocusScoreThreshold.value
|
||||
|
||||
Reference in new issue
Block a user