EGLImage 替换glreadpixels glTexImage2D 效率提升不明显

1、EGLImage替换glTexImage2D


	GLuint nIndex = 0;
	static GraphicBuffer* buffer = new GraphicBuffer(width, height,
											  PIXEL_FORMAT_RGBA_8888,
											  GraphicBuffer::USAGE_HW_TEXTURE |
											  GraphicBuffer::USAGE_HW_2D |
											  GraphicBuffer::USAGE_SW_READ_OFTEN |
											  GraphicBuffer::USAGE_SW_WRITE_OFTEN);


	int nWidth = buffer->getStride();
	unsigned char* bits = NULL;
	buffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN, (void**)&bits);

	for( int i = 0; i < height; i++ )
	{
		memcpy( bits + nWidth*4*i, data + width*4*i, width*4);
	}
// Write bitmap data into 'bits' here
	buffer->unlock();


// Create the EGLImageKHR from the native buffer
	EGLint eglImgAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE, EGL_NONE };
	EGLImageKHR img = eglCreateImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), EGL_NO_CONTEXT,
										EGL_NATIVE_BUFFER_ANDROID,
										(EGLClientBuffer)buffer->getNativeBuffer(),
										eglImgAttrs);


	glGenTextures(1, &nIndex);
	glBindTexture(GL_TEXTURE_2D, nIndex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, img);
	*textureId = nIndex;

2、EGLImage替换glreadpixels

a、替换FBO

//分配graphicbuffer
	outputBuffer = new GraphicBuffer(width,height,PIXEL_FORMAT_RGBA_8888, GraphicBuffer :: USAGE_SW_READ_OFTEN |
	GraphicBuffer :: USAGE_HW_RENDER |
	GraphicBuffer :: USAGE_HW_TEXTURE);


	//从graphicbuffer创建EGLImage
	EGLint eglImageAttributes[] = {EGL_WIDTH, width, EGL_HEIGHT, height, EGL_MATCH_FORMAT_KHR,
								   PIXEL_FORMAT_RGBA_8888, EGL_IMAGE_PRESERVED_KHR, EGL_FALSE, EGL_NONE};

	EGLClientBuffer nativeBuffer = outputBuffer->getNativeBuffer();

	EGLImageKHR eglImage = eglCreateImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, nativeBuffer, eglImageAttributes);


	//创建输出纹理
	glGenTextures(1, &textureId);
	glBindTexture(GL_TEXTURE_2D, textureId);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglImage);

	//Create target fbo
	glGenFramebuffers(1, &frameBuffer);
	glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

b、读取FBO的rgba数据

glFinish();
	unsigned char* result = NULL;
	int nWidth = outputBuffer->getStride();
    status_t statusgb = outputBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, (void**)(&result));
    for( int i = 0; i < height; i++ )
	{
		memcpy( data + width*4*i, result + nWidth*4*i, width*4);
	}
	outputBuffer->unlock();

 

注:目前 网上的说法是替换之后会快 但是目前已知测试glTexImage2D 效率比EGLImage快 , glReadpixels效率差不多 还在继续研究是否用错

你可能感兴趣的:(OpenGL,ES)