在排查渲染模块的渲染问题时,需要查看纹理数据是否正常,此时需要dump纹理数据来排查问题。
以 Android 平台为例,dump OES texture 和普通 texture 的代码
(1)C++ 代码
FILE* file_handler_{nullptr};
file_handler_ = fopen(yuv_file_name.c_str(), "wb");
static void save_texture(int texture_id, bool is_texture_2d, int width, int height,
FILE* file_handler) {
GLint old_fbo;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
GLuint tmp_fbo;
glGenFramebuffers(1, &tmp_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, tmp_fbo);
if (is_texture_2d) {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
texture_id, 0);
} else {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_EXTERNAL_OES,
texture_id, 0);
}
std::unique_ptr<uint8_t []> outRgbaBuf(new uint8_t[width*height*4]);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, outRgbaBuf.get());
{
fwrite(outRgbaBuf.get(), 1, width * height * 4, file_handler);
}
glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
glDeleteFramebuffers(1, &tmp_fbo);
}
(2)JAVA 代码
public FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(yuv_file_name);
} catch (FileNotFoundException e) {
//throw new RuntimeException(e);
e.printStackTrace();
}
public static void save_texture(int texture_id, boolean is_texture_2d, int width, int height,
FileOutputStream fileOutputStream) {
int[] old_fbo = new int[1];
GLES20.glGetIntegerv(GL_FRAMEBUFFER_BINDING, old_fbo, 0);
Log.d("TAG", "old_fbo " + old_fbo[0]);
int[] tmp_fbo = new int[1];
GLES20.glGenFramebuffers(1, tmp_fbo, 0);
Log.d("TAG", "tmp_fbo " + tmp_fbo[0]);
GLES20.glBindFramebuffer(GL_FRAMEBUFFER, tmp_fbo[0]);
if (is_texture_2d) {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
texture_id, 0);
} else {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_EXTERNAL_OES,
texture_id, 0);
}
ByteBuffer outRgbaBuf = ByteBuffer.allocate(width * height * 4);
GLES20.glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, outRgbaBuf);
byte[] bytes = new byte[width * height * 4];
outRgbaBuf.get(bytes);
{
try {
fileOutputStream.write(bytes);
} catch (IOException e) {
//throw new RuntimeException(e);
e.printStackTrace();
}
}
GLES20.glBindFramebuffer(GL_FRAMEBUFFER, old_fbo[0]);
GLES20.glDeleteFramebuffers(1, tmp_fbo, 0);
}
参考:https://blog.csdn.net/weixin_43931399/article/details/106638116