glReadPixels 很慢

6
我正在使用GLSurfaceView创建自定义相机预览,使用OpenGL来渲染相机给我的帧。我已经完全实现了相机,并按照预期工作,没有fps损失和正确的纵横比等。但当我需要捕获来自相机的帧时,问题就出现了。我的第一个想法是使用glReadPixles()。
使用GLES20.glReadPixels(),我发现一些设备会出现fps损失,尤其是具有更高屏幕分辨率的设备,这很合理,因为glReadPixels需要读取更多像素。
我进行了一些调查,发现其他人也遇到了glReadPixels的类似问题,许多人建议使用PBO,使用两个PBO充当双缓冲区,这将使我能够在不阻塞/停止当前渲染过程的情况下读取像素数据。我完全理解双缓冲的概念,但我对如何使双缓冲PBO正常工作需要一些指导,因为我还是OpenGL的新手。
我找到了一些关于PBO双缓冲的解决方案,但我从未找到一个完整的解决方案来充分理解它如何与GLES交互。
我的GLSurfaceView.Renderer.onDrawFrame()的实现。
    // mBuffer and mBitmap are declared and allocated outside of the onDrawFrame Method

    // Buffer is used to store pixel data from glReadPixels
    mBuffer.rewind();


    GLES20.glUseProgram(hProgram);
    if (tex_matrix != null)
    {
        GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, tex_matrix, 0);
    }
    GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mMvpMatrix, 0);

    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, tex_id);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, GLConstants.VERTEX_NUM);
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);

    // Read pixels from the current GLES context
    GLES10.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, mBuffer);

    // Copy the Pixels from the buffer
    mBitmap.copyPixelsFromBuffer(mBuffer);

    GLES20.glUseProgram(0);

1个回答

6

经过相当多的研究和挖掘,我已找到了解决glReadPixels并如何使用PBO缓冲图像/帧以供后续处理的方法。

所以我们需要做的第一件事是在GLES2中公开一个附加功能。 在您的应用程序模块中添加一个名为cpp的新目录,然后创建一个名为GlesHelper(或任何您想要的名称)的新c文件。

然后粘贴以下代码:

#include <jni.h>
#include <GLES2/gl2.h>
JNIEXPORT void JNICALL


// Change
Java_com_your_full_package_name_helper_GlesHelper_glReadPixels(JNIEnv *env, jobject instance, jint x,
                                                        jint y, jint width, jint height,
                                                        jint format, jint type) {
    // TODO
    glReadPixels(x, y, width, height, format, type, 0);
}

接下来,您需要在项目根目录中添加一个CMakeFile。右键点击,新建文件,输入CMakeLists.txt。

然后将以下代码粘贴进去:

cmake_minimum_required(VERSION 3.4.1)

add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main//cpp//GlesHelper.c )

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        GLESv2)

现在打开你的应用程序模块 build.gradle 文件,将以下内容粘贴到 Gradle 文件的 android.defaultConfig 部分。
externalNativeBuild {
    // Encapsulates your CMake build configurations.
    cmake {
        // Provides a relative path to your CMake build script.
        cppFlags "-std=c++11 -fexceptions"
        arguments "-DANDROID_STL=c++_shared"
    }
}

然后将此粘贴到Gradle文件的Android部分

externalNativeBuild {
// Encapsulates your CMake build configurations.
    cmake {

        // Provides a relative path to your CMake build script.
        path "CMakeLists.txt"
    }
}

现在我们已经完成了MakeFile和C语言的设置,接下来转向Java。

在您的项目中创建一个与C文件中的包相匹配的新文件,例如com_your_full_package_name_helper = com.your.full.package.name.helper。

确保这些匹配正确,同样适用于类名和函数名。

因此,您的类应该像这样:

package com.your.full.package.name.helper;

public class GlesHelper
{
    public static native void glReadPixels(int x, int y, int width, int height, int format, int type);
}

由于我们在项目中添加了原生代码,因此需要使用System.loadLibrary("native-lib")加载我们的新方法。

在开始下一步之前,请将以下成员变量添加到您的Renderer中。

/**
 * The PBO Ids, increase the allocate amount for more PBO's
 * The more PBO's the smoother the frame rate (to an extent)
 * Side affect of having more PBO's the frames you get from the PBO's will lag behind by the amount of pbo's
 */
private IntBuffer mPboIds = IntBuffer.allocate(2);;

/**
 * The current PBO Index
 */
private int mCurrentPboIndex = 0;

/**
 * The next PBO Index
 */
private int mNextPboIndex = 1;

现在我们需要初始化PBO,这非常简单。

    // Generate the buffers for the pbo's
    GLES30.glGenBuffers(mPboIds.capacity(), mPboIds);

    // Loop for how many pbo's we have
    for (int i = 0; i < mPboIds.capacity(); i++)
    {
        // Bind the Pixel_Pack_Buffer to the current pbo id
        GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, mPboIds.get(i));

        // Buffer empty data, capacity is the width * height * 4
        GLES30.glBufferData(GLES30.GL_PIXEL_PACK_BUFFER, capacity, null, GLES30.GL_STATIC_READ);
    }

    // Reset the current buffer so we can draw properly
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0);

在开始绘制之前,请调用此方法,它将读取像素数据到PBO中,交换缓冲区并使您可以访问像素数据。

/**
 * Reads the pixels from the PBO and swaps the buffers
 */
private void readPixelsFromPBO()
{
    // Bind the current buffer
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, mPboIds.get(mCurrentPboIndex));

    // Read pixels into the bound buffer
    GlesHelper.glReadPixels(0, 0, mViewWidth, mViewHeight, GLES20.GL_RGBA, GLES30.GL_UNSIGNED_BYTE);

    // Bind the next buffer
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, mPboIds.get(mNextPboIndex));

    // Map to buffer to a byte buffer, this is our pixel data
    ByteBuffer pixelsBuffer = (ByteBuffer) GLES30.glMapBufferRange(GLES30.GL_PIXEL_PACK_BUFFER, 0, mViewWidth * mViewHeight * 4, GLES30.GL_MAP_READ_BIT);

    if(mSkipFirstFrame)
    {
        // Skip the first frame as the PBO's have nothing in them until the second render cycle
    }
    // Set skip first frame to true so we can begin frame processing
    mSkipFirstFrame = true;

    // Swap the buffer index
    mCurrentPboIndex = (mCurrentPboIndex + 1) % mPboIds.capacity();
    mNextPboIndex = (mNextPboIndex + 1) % mPboIds.capacity();

    // Unmap the buffers
    GLES30.glUnmapBuffer(GLES30.GL_PIXEL_PACK_BUFFER);
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, GLES20.GL_NONE);
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, GLES20.GL_NONE);
}

所以回到我的最初问题,我们的Redner/onDraw方法应该是这样的。
    // Use the OpenGL Program for rendering
    GLES20.glUseProgram(mProgram);

    // If the Texture Matrix is not null
    if (textureMatrix != null)
    {
        // Apply the Matrix
        GLES20.glUniformMatrix4fv(mTexMatrixLoc, 1, false, textureMatrix, 0);
    }


    // Apply the Matrix
    GLES20.glUniformMatrix4fv(mMVPMatrixLoc, 1, false, mMvpMatrix, 0);

    // Bind the Texture
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureID);

    // Draw the texture
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, GLConstants.VERTEX_NUM);

    // Unbind the Texture
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0);

    // Read from PBO
    readPixelsFromPBO()

我希望这篇文章能够帮助那些遇到与glReadPixels性能问题类似的人,或者正在努力实现PBO。


嗨,谢谢你的解决方案,我已经尝试过了,但速度不够快,你能用那个解决方案解决这个问题吗? - Mohammad Elsayed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接