EGLImage与纹理
来源:互联网 发布:中南大学网络考试答案 编辑:程序博客网 时间:2024/06/02 04:28
Using the EGL* Image Extension
The conventional way to copy an image into a texture is with either the glTexImage2D()
orglTexSubImage2D()
methods, but these methods are slow because of how they convert the format of the image data as it is copied. These are really intended for loading static images, not dynamic ones. Moving images between OpenGL* ES textures and another graphics API quickly requires direct access to the memory in which the texture image is stored. Ideally, the image should be copied by an accelerated 2D BitBlt, but that requires the physical address of the image. Otherwise, you can use amemcpy()
method instead, which only requires the virtual address of the image.
The EGL* image extension is an extension to the EGL* standard defined by the Khronos Group that provides the virtual or physical addresses of an OpenGL* ES texture. With these addresses, images can be copied to or from OpenGL* ES textures quickly. This technique is so fast that it is possible to stream uncompressed video into OpenGL* ES, but doing so typically requires converting the pixels from the YUV to RGB color space, which is beyond the scope of this article.
The official name of the EGL* image extension is GL_OES_EGL_image. It is widely supported on most platforms, including Android. To confirm which extensions are available on any platform, use the functions provided in Listing 4 to return strings that list all of the available extensions by name for your OpenGL* ES and EGL* drivers.
Listing 4. Checking for available OpenGL* ES and EGL* extensions
glGetString(GL_EXTENSIONS);
eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS);
The header file eglext.h
defines the names of the rendering surface types that the EGL* and OpenGL* ES drivers for your platform support. Table 1 provides a summary of the EGL* image surface types that are available for Android. Note that Android lists support for the EGL_KHR_image_pixmap extension, but it is actually the EGL_NATIVE_BUFFER_ANDROID
surface type that you must use, notEGL_NATIVE_PIXMAP_KHR
.
Table 1. Surface types for EGL* images on Android
ExtensionSurface typeEGL_NATIVE_PIXMAP_KHRPixmap surface (not available on Android)EGL_GL_TEXTURE_2D_KHRConventional 2D textureEGL_GL_TEXTURE_3D_KHRConventional 3D textureEGL_GL_RENDERBUFFER_KHRRender buffer surface forglReadPixels()
EGL_NATIVE_BUFFER_ANDROIDFor Android’s native graphics APIThe code in Listing 5 shows how to use the EGL* image extension in two ways. First, on the Android platform, a native GraphicBuffer
surface is created and locked. This buffer can be accessed for rendering while it is locked. When this buffer is unlocked, it can be imported into a new EGL* image with the ClientBufferAddress parameter to eglCreateImageKHR()
. This EGL* image is then bound to GL_TEXTURE_2D with glEGLImageTargetTexture2DOES()
, to be used as any texture can be used in OpenGL* ES. This is accomplished without ever copying the image, as the native GraphicBuffer
and the OpenGL* ES texture are actually sharing the same image data. This example demonstrates how images can be exchanged quickly between OpenGL* ES and Android or any 2D API on the Android platform. Note that the GraphicBuffer class is only available in the Android framework API, not the NDK.
If you are not using Android, you can still import images into OpenGL* ES textures in the same way. Set the ClientBufferAddress
to point to your image data, and set the SurfaceType asEGL_GL_TEXTURE_2D_KHR
. Refer to your eglext.h include file for a complete list of the surface types that are available on your platform. Use eglQuerySurface()
to obtain the address, pitch (stride), and origin of the new EGL* image buffer after it is created. Be sure to use eglGetError()
after each call to the EGL* to check for any returned errors.
Listing 5. Example of using the EGL* image extension with Android
#include <EGL/eglext.h>
#include <GLES2/gl2ext.h>
#ifdef ANDROID
GraphicBuffer * pGraphicBuffer = new GraphicBuffer(ImageWidth, ImageHeight, PIXEL_FORMAT_RGB_565, GraphicBuffer::USAGE_SW_WRITE_OFTEN | GraphicBuffer::USAGE_HW_TEXTURE);
// Lock the buffer to get a pointer
unsigned char * pBitmap = NULL;
pGraphicBuffer->lock(GraphicBuffer::USAGE_SW_WRITE_OFTEN,(void **)&pBitmap);
// Write 2D image to pBitmap
// Unlock to allow OpenGL ES to use it
pGraphicBuffer->unlock();
EGLClientBuffer ClientBufferAddress = pGraphicBuffer->getNativeBuffer();
EGLint SurfaceType = EGL_NATIVE_BUFFER_ANDROID;
#else
EGLint SurfaceType = EGL_GL_TEXTURE_2D_KHR;
#endif
// Make an EGL Image at the same address of the native client buffer
EGLDisplay eglDisplayHandle = eglGetDisplay(EGL_DEFAULT_DISPLAY);
// Create an EGL Image with these attributes
EGLint eglImageAttributes[] = {EGL_WIDTH, ImageWidth, EGL_HEIGHT, ImageHeight, EGL_MATCH_FORMAT_KHR, EGL_FORMAT_RGB_565_KHR, EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
EGLImageKHR eglImageHandle = eglCreateImageKHR(eglDisplayHandle, EGL_NO_CONTEXT, SurfaceType, ClientBufferAddress, eglImageAttributes);
// Create a texture and bind it to GL_TEXTURE_2D
EGLint TextureHandle;
glGenTextures(1, &TextureHandle);
glBindTexture(GL_TEXTURE_2D, TextureHandle);
// Attach the EGL Image to the same texture
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglImageHandle);
// Get the address and pitch (stride) of the new texture image
eglQuerySurface(eglDisplayHandle, eglImageHandle, EGL_BITMAP_POINTER_KHR, &BitmapAddress);
eglQuerySurface(eglDisplayHandle, eglImageHandle, EGL_BITMAP_PITCH_KHR, &BitmapPitch);
eglQuerySurface(eglDisplayHandle, eglImageHandle, EGL_BITMAP_ORIGIN_KHR, &BitmapOrigin);
// Check for errors after each call to the EGL
if (eglGetError() != EGL_SUCCESS)
break;
// Delete the EGL Image to free the memory when done
eglDestroyImageKHR(eglDisplayHandle, eglImageHandle);
Conclusion
One of the best ways to update an application with a tired 2D GUI is to exploit the accelerated OpenGL* ES features of Android on the Intel® Atom™ platform. Even though 2D and 3D are really different paradigms, the combination of the two is powerful. The trick is to make them cooperate by either sharing the frame buffer or sharing images through textures and the EGL* image extension. Use of this extension with OpenGL* ES is essential for achieving a good user experience, because the conventional method of loading textures with glTexImage2D()
is too slow for dynamic images. Fortunately, this extension is well supported on most embedded platforms today, including Android.
https://software.intel.com/en-us/articles/using-opengl-es-to-accelerate-apps-with-legacy-2d-guis
- EGLImage与纹理
- EGLImage与纹理
- EGLImage与纹理
- EGLImage与纹理
- EGLImage in SurfaceFlinger
- OpenGL ES 理解纹理与纹理过滤
- opengles之多重纹理与过程纹理
- 纹理与材质
- [OpenGL]茶壶与纹理
- Mipmap与纹理过滤
- 纹理与表面间复制
- 显存与纹理内存详解
- 显存与纹理内存详解 .
- Cube Texture与环境纹理
- 图像纹理识别与检索
- WebGL入门 颜色与纹理
- 纹理与图片的区别
- Android OpenGL ES(七)----理解纹理与纹理过滤
- MySQL:Duplicate entry '1-2' for key 'PRIMARY' 错误
- Shell脚本常用符号
- Objective C中数组排序几种情况的总结
- rtorrent编译
- 用shell写的jboss自动部署脚本
- EGLImage与纹理
- Algorithm---LCA(倍增算法)
- 深入浅出游戏算法(4)-unity3d算法(3)-移动摄像机
- 推导和理解正交投影变换
- poj3007
- oracle访问另1个库的表的共享创建。
- 深入C++的拷贝构造和赋值函数 (深拷贝,浅拷贝)
- HDU 2822 BFS+优先队列
- Spring MVC 3学习笔记+教程 在controller和视图之间传递参数