EGLImage置換glreadpixels glTexImage 2 D効率の向上は顕著ではない

3307 ワード

1、EGLImage置換glTexImage 2 D

	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();

 
注:現在、ネット上では置き換えが速いと言われていますが、glTexImage 2 Dの効率はEGLImageよりも速いことが知られています.glReadpixelsの効率が悪いのは多くありませんが、間違っているかどうかを研究し続けています.