feat: use native memory allocation for direct byte buffers
This commit is contained in:
1 parent
ef72f1b9e5
commit
92e0b01e98
4 files changed
+109
-45
No files matched your search
@@ -2528,4 +2528,25 @@ Java_com_hinnka_mycamera_ml_DnCNNDenoiseEstimator_postprocessNative(
|
||||
AndroidBitmap_unlockPixels(env, dstBitmap);
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_com_hinnka_mycamera_utils_DirectBufferAllocator_allocateNative(
|
||||
JNIEnv *env, jobject, jlong capacity) {
|
||||
void* ptr = malloc(capacity);
|
||||
if (!ptr) {
|
||||
LOGE("Failed to allocate %lld bytes", (long long)capacity);
|
||||
return nullptr;
|
||||
}
|
||||
return env->NewDirectByteBuffer(ptr, capacity);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_hinnka_mycamera_utils_DirectBufferAllocator_freeNative(
|
||||
JNIEnv *env, jobject, jobject buffer) {
|
||||
if (!buffer) return;
|
||||
void* ptr = env->GetDirectBufferAddress(buffer);
|
||||
if (ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -536,19 +536,24 @@ class LutImageProcessor {
|
||||
drawQuad(nlmChromaProgram)
|
||||
|
||||
val pixelSize = width * height * 4
|
||||
val pixelBuffer = ByteBuffer.allocateDirect(pixelSize).order(ByteOrder.nativeOrder())
|
||||
GLES30.glReadPixels(
|
||||
0, 0, width, height,
|
||||
GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, pixelBuffer
|
||||
)
|
||||
pixelBuffer.position(0)
|
||||
|
||||
val tempBitmap = createBitmap(width, height, colorSpace = bitmap.colorSpace ?: ColorSpace.get(ColorSpace.Named.SRGB))
|
||||
tempBitmap.copyPixelsFromBuffer(pixelBuffer)
|
||||
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
|
||||
tempBitmap
|
||||
val pixelBuffer = com.hinnka.mycamera.utils.DirectBufferAllocator.allocateNative(pixelSize.toLong())?.order(ByteOrder.nativeOrder())
|
||||
?: throw OutOfMemoryError("Failed to allocate native direct buffer")
|
||||
try {
|
||||
GLES30.glReadPixels(
|
||||
0, 0, width, height,
|
||||
GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, pixelBuffer
|
||||
)
|
||||
pixelBuffer.position(0)
|
||||
|
||||
val tempBitmap = createBitmap(width, height, colorSpace = bitmap.colorSpace ?: ColorSpace.get(ColorSpace.Named.SRGB))
|
||||
tempBitmap.copyPixelsFromBuffer(pixelBuffer)
|
||||
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
|
||||
tempBitmap
|
||||
} finally {
|
||||
com.hinnka.mycamera.utils.DirectBufferAllocator.freeNative(pixelBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildLchAdjustmentArrays(params: ColorRecipeParams?): Triple<FloatArray, FloatArray, FloatArray> {
|
||||
@@ -782,34 +787,40 @@ class LutImageProcessor {
|
||||
if (texCoordHandle >= 0) GLES30.glDisableVertexAttribArray(texCoordHandle)
|
||||
|
||||
val pixelSize = width * height * 4
|
||||
val pixelBuffer = ByteBuffer.allocateDirect(pixelSize).order(ByteOrder.nativeOrder())
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glPixelStorei(GLES30.GL_PACK_ALIGNMENT, 4)
|
||||
GLES30.glReadPixels(
|
||||
0,
|
||||
0,
|
||||
width,
|
||||
height,
|
||||
GLES30.GL_RGBA,
|
||||
GLES30.GL_UNSIGNED_BYTE,
|
||||
pixelBuffer
|
||||
)
|
||||
pixelBuffer.position(0)
|
||||
val pixelBuffer = com.hinnka.mycamera.utils.DirectBufferAllocator.allocateNative(pixelSize.toLong())?.order(ByteOrder.nativeOrder())
|
||||
?: throw OutOfMemoryError("Failed to allocate native direct buffer")
|
||||
|
||||
try {
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glPixelStorei(GLES30.GL_PACK_ALIGNMENT, 4)
|
||||
GLES30.glReadPixels(
|
||||
0,
|
||||
0,
|
||||
width,
|
||||
height,
|
||||
GLES30.GL_RGBA,
|
||||
GLES30.GL_UNSIGNED_BYTE,
|
||||
pixelBuffer
|
||||
)
|
||||
pixelBuffer.position(0)
|
||||
|
||||
// 创建临时 Bitmap
|
||||
val tempBitmap = createBitmap(width, height, colorSpace = inputColorSpace)
|
||||
tempBitmap.copyPixelsFromBuffer(pixelBuffer)
|
||||
// 创建临时 Bitmap
|
||||
val tempBitmap = createBitmap(width, height, colorSpace = inputColorSpace)
|
||||
tempBitmap.copyPixelsFromBuffer(pixelBuffer)
|
||||
|
||||
// 翻转 Y 轴(glReadPixels 从左下角开始读取,需要翻转)
|
||||
// 翻转 Y 轴(glReadPixels 从左下角开始读取,需要翻转)
|
||||
// val matrix = android.graphics.Matrix()
|
||||
// matrix.preScale(1f, -1f)
|
||||
// val outputBitmap = Bitmap.createBitmap(tempBitmap, 0, 0, width, height, matrix, true)
|
||||
// tempBitmap.recycle()
|
||||
|
||||
// 解绑帧缓冲
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
// 解绑帧缓冲
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
|
||||
return tempBitmap
|
||||
return tempBitmap
|
||||
} finally {
|
||||
com.hinnka.mycamera.utils.DirectBufferAllocator.freeNative(pixelBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupFramebuffer(width: Int, height: Int) {
|
||||
|
||||
@@ -498,6 +498,7 @@ class RawDemosaicProcessor {
|
||||
}
|
||||
|
||||
// Check GL_MAX_TEXTURE_SIZE and downscale if needed
|
||||
var downscaledBuffer: ByteBuffer? = null
|
||||
if (actualWidth > maxTextureSize || actualHeight > maxTextureSize) {
|
||||
PLog.w(
|
||||
TAG,
|
||||
@@ -516,7 +517,8 @@ class RawDemosaicProcessor {
|
||||
srcBuf.position(0)
|
||||
val src = srcBuf.asShortBuffer()
|
||||
val dstSize = newWidth * newHeight * 3 * 2 // 3 channels * 2 bytes
|
||||
val dstByteBuf = ByteBuffer.allocateDirect(dstSize).order(java.nio.ByteOrder.nativeOrder())
|
||||
val dstByteBuf = com.hinnka.mycamera.utils.DirectBufferAllocator.allocateNative(dstSize.toLong())?.order(java.nio.ByteOrder.nativeOrder())
|
||||
?: throw OutOfMemoryError("Failed to allocate native direct buffer")
|
||||
val dst = dstByteBuf.asShortBuffer()
|
||||
|
||||
val srcChannels = 3
|
||||
@@ -546,6 +548,7 @@ class RawDemosaicProcessor {
|
||||
}
|
||||
}
|
||||
actualRawData = dstByteBuf
|
||||
downscaledBuffer = dstByteBuf
|
||||
actualRowStride = newWidth * 6
|
||||
PLog.d(TAG, "Downscaled to ${newWidth}x${newHeight}")
|
||||
}
|
||||
@@ -571,6 +574,9 @@ class RawDemosaicProcessor {
|
||||
)
|
||||
// GPU 已消费 rawData,立即释放 CPU 侧引用,帮助 GC 回收(超分时约 288 MB)
|
||||
actualRawData = null
|
||||
downscaledBuffer?.let {
|
||||
com.hinnka.mycamera.utils.DirectBufferAllocator.freeNative(it)
|
||||
}
|
||||
renderLinearPass(
|
||||
metadata = actualMetadata,
|
||||
rawExposureCompensation = rawExposureCompensation,
|
||||
@@ -2307,22 +2313,28 @@ class RawDemosaicProcessor {
|
||||
|
||||
// --- 降级路径:直接分配 ByteBuffer(Java 堆)---
|
||||
val pixelBuffer = try {
|
||||
ByteBuffer.allocateDirect(pixelSize).order(ByteOrder.nativeOrder())
|
||||
com.hinnka.mycamera.utils.DirectBufferAllocator.allocateNative(pixelSize.toLong())?.order(ByteOrder.nativeOrder())
|
||||
?: throw OutOfMemoryError("Failed to allocate native direct buffer")
|
||||
} catch (e: OutOfMemoryError) {
|
||||
PLog.e(TAG, "OOM allocating pixel buffer ($width x $height, ${pixelSize}B)", e)
|
||||
return null
|
||||
}
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glReadPixels(0, 0, width, height, GLES30.GL_RGBA, GLES30.GL_HALF_FLOAT, pixelBuffer)
|
||||
pixelBuffer.position(0)
|
||||
checkGlError("readPixels direct")
|
||||
return try {
|
||||
createBitmap(width, height, Bitmap.Config.RGBA_F16, colorSpace = colorSpace).also { bitmap ->
|
||||
bitmap.copyPixelsFromBuffer(pixelBuffer)
|
||||
|
||||
try {
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
GLES30.glReadPixels(0, 0, width, height, GLES30.GL_RGBA, GLES30.GL_HALF_FLOAT, pixelBuffer)
|
||||
pixelBuffer.position(0)
|
||||
checkGlError("readPixels direct")
|
||||
return try {
|
||||
createBitmap(width, height, Bitmap.Config.RGBA_F16, colorSpace = colorSpace).also { bitmap ->
|
||||
bitmap.copyPixelsFromBuffer(pixelBuffer)
|
||||
}
|
||||
} catch (e: OutOfMemoryError) {
|
||||
PLog.e(TAG, "OOM creating output bitmap ($width x $height)", e)
|
||||
null
|
||||
}
|
||||
} catch (e: OutOfMemoryError) {
|
||||
PLog.e(TAG, "OOM creating output bitmap ($width x $height)", e)
|
||||
null
|
||||
} finally {
|
||||
com.hinnka.mycamera.utils.DirectBufferAllocator.freeNative(pixelBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.hinnka.mycamera.utils
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
object DirectBufferAllocator {
|
||||
init {
|
||||
System.loadLibrary("my-native-lib")
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请大容量 native 内存并包装为 DirectByteBuffer,突破 dalvik 512MB 限制
|
||||
* capacity 必须小于 Int.MAX_VALUE (2GB)
|
||||
*/
|
||||
external fun allocateNative(capacity: Long): ByteBuffer?
|
||||
|
||||
/**
|
||||
* 释放由 allocateNative 分配的内存
|
||||
*/
|
||||
external fun freeNative(buffer: ByteBuffer)
|
||||
}
|
||||
Reference in new issue
Block a user