glReadPixels 很慢

Jak*_*ade 6 android opengl-es-2.0 android-camera2

我正在使用 GLSurfaceView 创建自定义相机预览,使用 OpenGl 渲染相机提供给我的帧。我已经完全实现了相机,并按照我期望的方式工作,没有 fps 损失和正确的纵横比等。但是当我需要捕获来自相机源的帧时,问题出现了,我的第一个想法是使用 glReadPixles ()

使用 GLES20.glReadPixels() 我发现某些设备会出现 fps 损失,主要是屏幕分辨率较高的设备,这是有道理的,因为 glReadPixels 需要以更高分辨率读取更多像素。

我做了一些挖掘,发现其他人对 glReadPixels 也有类似的问题,许多人建议使用 PBO,最好使用其中两个作为双缓冲区,这将允许我读取像素数据,而不会阻塞/停止当前的渲染过程。我完全理解双缓冲的概念,我对 OpenGL 相当陌生,需要一些关于如何让双缓冲 PBO 工作的指导。

我找到了一些 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);
Run Code Online (Sandbox Code Playgroud)

Jak*_*ade 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);
}
Run Code Online (Sandbox Code Playgroud)

然后我们需要将 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)
Run Code Online (Sandbox Code Playgroud)

现在打开您的应用程序/模块 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将其粘贴到 Gradle 文件的 android 部分

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

        // Provides a relative path to your CMake build script.
        path "CMakeLists.txt"
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是所有 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);
}
Run Code Online (Sandbox Code Playgroud)

因为我们已经将本机代码添加到项目中,所以我们需要使用 System.loadLibrary("native-lib") 来加载我们的新方法。

在我们开始下一步之前,将这些成员变量添加到您的渲染器中

/**
 * 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;
Run Code Online (Sandbox Code Playgroud)

所以现在我们需要初始化我们的 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);
Run Code Online (Sandbox Code Playgroud)

然后在我们开始绘图之前调用此方法,这会将像素数据读入 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);
}
Run Code Online (Sandbox Code Playgroud)

回到我最初的问题,我们的 Redner/onDrawMethod 看起来像这样。

    // 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()
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助那些在 glReadPixels 的性能方面遇到类似问题或至少在实现 PBO 方面遇到困难的人