EGL是OpenGL ES與系統原始窗口的適配層: android
Display:用於與原生窗口創建鏈接。 Surface:用於渲染的區域。 Context:建立渲染上下文。指的是OpenGL ES項目運行須要的全部數據結構。如:定點,着色器,頂點數據矩陣。頂點着色器針對每一個頂點執行一次,用於肯定頂點的位置。數組
片元着色器針對每一個片元(像素)執行一次,用於肯定片元(像素)的顏色。bash
public class XPlay extends GLSurfaceView implements Runnable,SurfaceHolder.Callback
{
public XPlay(Context context, AttributeSet attrs) {
super( context, attrs );
}
@Override
public void run() {
Open("/sdcard/outyuv",getHolder().getSurface());
}
@Override
public void surfaceCreated(SurfaceHolder var1)
{
new Thread( this ).start();
}
@Override
public void surfaceChanged(SurfaceHolder var1, int var2, int var3, int var4)
{
}
@Override
public void surfaceDestroyed(SurfaceHolder var1)
{
}
public native void Open(String url,Object surface);
}
複製代碼
#include <android/native_window_jni.h>
複製代碼
extern "C"
JNIEXPORT void JNICALL
Java_aplay_testopengles_XPlay_Open(JNIEnv *env, jobject instance, jstring url_, jobject surface) {
//1 獲取原始窗口
ANativeWindow *nwin = ANativeWindow_fromSurface(env,surface);
}
複製代碼
ANativeWindow_fromSurface():返回與Java Surface對象關聯的ANativeWindow,經過本地代碼與之交互。這得到了返回的ANativeWindow的引用; 使用結束以後要使用ANativeWindow_release(),這樣它纔不會泄漏。數據結構
ANativeWindow * ANativeWindow_fromSurface(
JNIEnv *env,
jobject surface
)
複製代碼
ANativeWindow:不透明類型,提供對本地窗口的訪問。ide
struct ANativeWindow
{
#ifdef __cplusplus
ANativeWindow()
: flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
{
common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
common.version = sizeof(ANativeWindow);
memset(common.reserved, 0, sizeof(common.reserved));
}
/* Implement the methods that sp<ANativeWindow> expects so that it
can be used to automatically refcount ANativeWindow's. */ void incStrong(const void* id) const { common.incRef(const_cast<android_native_base_t*>(&common)); } void decStrong(const void* id) const { common.decRef(const_cast<android_native_base_t*>(&common)); } #endif struct android_native_base_t common; /* flags describing some attributes of this surface or its updater */ const uint32_t flags; /* min swap interval supported by this updated */ const int minSwapInterval; /* max swap interval supported by this updated */ const int maxSwapInterval; /* horizontal and vertical resolution in DPI */ const float xdpi; const float ydpi; /* Some storage reserved for the OEM's driver. */
intptr_t oem[4];
/*
* Set the swap interval for this surface.
*
* Returns 0 on success or -errno on error.
*/
int (*setSwapInterval)(struct ANativeWindow* window,
int interval);
/*
* Hook called by EGL to acquire a buffer. After this call, the buffer
* is not locked, so its content cannot be modified. This call may block if
* no buffers are available.
*
* The window holds a reference to the buffer between dequeueBuffer and
* either queueBuffer or cancelBuffer, so clients only need their own
* reference if they might use the buffer after queueing or canceling it.
* Holding a reference to a buffer after queueing or canceling it is only
* allowed if a specific buffer count has been set.
*
* Returns 0 on success or -errno on error.
*/
int (*dequeueBuffer)(struct ANativeWindow* window,
struct ANativeWindowBuffer** buffer);
/*
* hook called by EGL to lock a buffer. This MUST be called before modifying
* the content of a buffer. The buffer must have been acquired with
* dequeueBuffer first.
*
* Returns 0 on success or -errno on error.
*/
int (*lockBuffer)(struct ANativeWindow* window,
struct ANativeWindowBuffer* buffer);
/*
* Hook called by EGL when modifications to the render buffer are done.
* This unlocks and post the buffer.
*
* The window holds a reference to the buffer between dequeueBuffer and
* either queueBuffer or cancelBuffer, so clients only need their own
* reference if they might use the buffer after queueing or canceling it.
* Holding a reference to a buffer after queueing or canceling it is only
* allowed if a specific buffer count has been set.
*
* Buffers MUST be queued in the same order than they were dequeued.
*
* Returns 0 on success or -errno on error.
*/
int (*queueBuffer)(struct ANativeWindow* window,
struct ANativeWindowBuffer* buffer);
/*
* hook used to retrieve information about the native window.
*
* Returns 0 on success or -errno on error.
*/
int (*query)(const struct ANativeWindow* window,
int what, int* value);
/*
* hook used to perform various operations on the surface.
* (*perform)() is a generic mechanism to add functionality to
* ANativeWindow while keeping backward binary compatibility.
*
* DO NOT CALL THIS HOOK DIRECTLY. Instead, use the helper functions
* defined below.
*
* (*perform)() returns -ENOENT if the 'what' parameter is not supported
* by the surface's implementation. * * The valid operations are: * NATIVE_WINDOW_SET_USAGE * NATIVE_WINDOW_CONNECT (deprecated) * NATIVE_WINDOW_DISCONNECT (deprecated) * NATIVE_WINDOW_SET_CROP (private) * NATIVE_WINDOW_SET_BUFFER_COUNT * NATIVE_WINDOW_SET_BUFFERS_GEOMETRY (deprecated) * NATIVE_WINDOW_SET_BUFFERS_TRANSFORM * NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP * NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS * NATIVE_WINDOW_SET_BUFFERS_FORMAT * NATIVE_WINDOW_SET_SCALING_MODE (private) * NATIVE_WINDOW_LOCK (private) * NATIVE_WINDOW_UNLOCK_AND_POST (private) * NATIVE_WINDOW_API_CONNECT (private) * NATIVE_WINDOW_API_DISCONNECT (private) * NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS (private) * NATIVE_WINDOW_SET_POST_TRANSFORM_CROP (private) * */ int (*perform)(struct ANativeWindow* window, int operation, ... ); /* * Hook used to cancel a buffer that has been dequeued. * No synchronization is performed between dequeue() and cancel(), so * either external synchronization is needed, or these functions must be * called from the same thread. * * The window holds a reference to the buffer between dequeueBuffer and * either queueBuffer or cancelBuffer, so clients only need their own * reference if they might use the buffer after queueing or canceling it. * Holding a reference to a buffer after queueing or canceling it is only * allowed if a specific buffer count has been set. */ int (*cancelBuffer)(struct ANativeWindow* window, struct ANativeWindowBuffer* buffer); void* reserved_proc[2]; }; 複製代碼
#include <EGL/egl.h>
複製代碼
///EGL
//1 EGL display建立和初始化
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if(display == EGL_NO_DISPLAY)
{
LOGD("eglGetDisplay failed!");
return;
}
if(EGL_TRUE != eglInitialize(display,0,0))
{
LOGD("eglInitialize failed!");
return;
}
//2 surface
//2-1 surface窗口配置
//輸出配置
EGLConfig config;
EGLint configNum;
EGLint configSpec[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_NONE // 屬性表以該常量爲結束符
};
if(EGL_TRUE != eglChooseConfig(display,configSpec,&config,1,&configNum))
{
LOGD("eglChooseConfig failed!");
return;
}
//建立surface
EGLSurface winsurface = eglCreateWindowSurface(display,config,nwin,0);
if(winsurface == EGL_NO_SURFACE)
{
LOGD("eglCreateWindowSurface failed!");
return;
}
//3 context 建立關聯的上下文
const EGLint ctxAttr[] = {
EGL_CONTEXT_CLIENT_VERSION,2,EGL_NONE
};
EGLContext context = eglCreateContext(display,config,EGL_NO_CONTEXT,ctxAttr);
if(context == EGL_NO_CONTEXT)
{
LOGD("eglCreateContext failed!");
return;
}
if(EGL_TRUE != eglMakeCurrent(display,winsurface,winsurface,context))
{
LOGD("eglMakeCurrent failed!");
return;
}
LOGD("EGL Init Success!");
複製代碼
EGLDisplay eglGetDisplay(EGLNativeDisplayType display_id):返回須要顯示內容的對象的句柄。post
EGLDisplay:EGL把這些不一樣平臺的顯示系統抽象爲一個獨立的類型:EGLDisplay。ui
eglInitialize():初始化EGLDisplay。this
EGLBoolean eglInitialize(EGLDisplay display, EGLint* majorVersion, EGLint* minorVersion);
複製代碼
EGLConfig:初始化EGLDisplay事後,要選擇一個合適的「繪圖表面」。url
EGLBoolean eglChooseConfig(EGLDisplay display,
const EGLint* attribs, // 你想要的屬性事先定義到這個數組裏
EGLConfig* configs, // 圖形系統將返回若干知足條件的配置到該數組
EGLint maxConfigs, // 上面數組的容量
EGLint* numConfigs); // 圖形系統返回的可用的配置個數
複製代碼
eglCreateWindowSurface():爲建立好的EGLDisplay建立窗口。spa
EGLSurface eglCreateWindowSurface(EGLDisplay display,
EGLConfig config,
EGLNativeWindowType window, // 在Windows上就是HWND類型
const EGLint* attribs); // 此屬性表非彼屬性表
複製代碼
EGLSurface:EGLSurface 能夠是由 EGL 分配的離屏緩衝區(稱爲「pbuffer」),或由操做系統分配的窗口。EGL 窗口 Surface 經過 eglCreateWindowSurface() 調用被建立。該調用將「窗口對象」做爲參數,在 Android 上,該對象能夠是 SurfaceView、SurfaceTexture、SurfaceHolder 或 Surface,全部這些對象下面都有一個 BufferQueue。當您進行此調用時,EGL 將建立一個新的 EGLSurface 對象,並將其鏈接到窗口對象的 BufferQueue 的生產方接口。此後,渲染到該 EGLSurface 會致使一個緩衝區離開隊列、進行渲染,而後排隊等待消耗方使用。
eglCreateContext():eglCreateContext爲當前渲染API(使用eglBindAPI設置)建立EGL渲染context,並返回context的句柄。 context而後能夠用於渲染到EGL繪圖表面。 若是eglCreateContext沒法建立渲染context,則返回EGL_NO_CONTEXT。
EGLContext eglCreateContext(EGLDisplay display,
EGLConfig config,
EGLContext share_context,
EGLint const * attrib_list);
複製代碼
display:指定EGL顯示鏈接。
config:指定定義可用於渲染context的幀緩衝區資源的EGL幀緩衝區配置。
share_context:指定用於共享數據的另外一個EGL渲染context,由與context相對應的客戶端API定義。 數據也與share_context共享數據的全部其餘context共享。 EGL_NO_CONTEXT表示不會發生共享。
attrib_list:爲正在建立的context指定屬性和屬性值。 只能指定屬性EGL_CONTEXT_CLIENT_VERSION。
EGLContext:OpenGL ES 圖形上下文,它表明了OpenGL狀態機;若是沒有它,OpenGL指令就沒有執行的環境。
eglMakeCurrent():eglMakeCurrent將context綁定到當前渲染線程以及繪製和讀取表面。 繪製用於全部GL操做,除了讀取的任何像素數據(glReadPixels,glCopyTexImage2D和glCopyTexSubImage2D),它取自讀取的幀緩衝區值。
若是調用線程已經有當前的渲染context,那麼該context將被刷新並標記爲再也不是最新的。
當context首次生成時,viewport和scissor尺寸被設置爲繪製表面的大小。 當context隨後變爲當前時,viewport和scissor不會被修改。
EGLBoolean eglMakeCurrent( EGLDisplay display,
EGLSurface draw,
EGLSurface read,
EGLContext context);
複製代碼
#include <GLES2/gl2.h>
複製代碼
//頂點和片元shader初始化
//頂點shader初始化
GLint vsh = InitShader(vertexShader,GL_VERTEX_SHADER);
//片元yuv420 shader初始化
GLint fsh = InitShader(fragYUV420P,GL_FRAGMENT_SHADER);
複製代碼
//頂點着色器glsl
#define GET_STR(x) #x
static const char *vertexShader = GET_STR(
attribute vec4 aPosition; //頂點座標
attribute vec2 aTexCoord; //材質頂點座標
varying vec2 vTexCoord; //輸出的材質座標
void main(){
vTexCoord = vec2(aTexCoord.x,1.0-aTexCoord.y);//以左上角爲座標原點
gl_Position = aPosition;
}
);
//片元着色器,軟解碼和部分x86硬解碼
static const char *fragYUV420P = GET_STR(
precision mediump float; //精度
varying vec2 vTexCoord; //頂點着色器傳遞的座標
uniform sampler2D yTexture; //輸入的材質(不透明灰度,單像素)
uniform sampler2D uTexture;
uniform sampler2D vTexture;
void main(){
vec3 yuv;//承載輸入的信息
vec3 rgb;//承載輸出的信息
yuv.r = texture2D(yTexture,vTexCoord).r;//獲取指定座標的材質顏色
yuv.g = texture2D(uTexture,vTexCoord).r - 0.5;
yuv.b = texture2D(vTexture,vTexCoord).r - 0.5;
rgb = mat3(1.0, 1.0, 1.0,
0.0,-0.39465,2.03211,
1.13983,-0.58060,0.0)*yuv;//指定的yuv轉換爲rgb的公式
//輸出像素顏色
gl_FragColor = vec4(rgb,1.0);
}
);
GLint InitShader(const char *code,GLint type)
{
//建立shader
GLint sh = glCreateShader(type);
if(sh == 0)
{
LOGD("glCreateShader %d failed!",type);
return 0;
}
//加載shader
glShaderSource(sh,
1, //shader數量
&code, //shader代碼
0); //代碼長度
//編譯shader
glCompileShader(sh);
//獲取編譯狀況
GLint status;
glGetShaderiv(sh,GL_COMPILE_STATUS,&status);
if(status == 0)
{
LOGD("glCompileShader failed!");
return 0;
}
LOGD("glCompileShader success!");
return sh;
}
複製代碼
//建立渲染程序
GLint program = glCreateProgram();
if(program == 0)
{
LOGD("glCreateProgram failed!");
return;
}
//渲染程序中加入着色器代碼
glAttachShader(program,vsh);
glAttachShader(program,fsh);
//連接程序
glLinkProgram(program);
GLint status = 0;
glGetProgramiv(program,GL_LINK_STATUS,&status);
if(status != GL_TRUE)
{
LOGD("glLinkProgram failed!");
return;
}
glUseProgram(program);
LOGD("glLinkProgram success!");
複製代碼
//加入三維頂點數據 兩個三角形組成正方形
static float vers[] = {
1.0f,-1.0f,0.0f,
-1.0f,-1.0f,0.0f,
1.0f,1.0f,0.0f,
-1.0f,1.0f,0.0f,
};
GLuint apos = (GLuint)glGetAttribLocation(program,"aPosition");
glEnableVertexAttribArray(apos);
//傳遞頂點
glVertexAttribPointer(apos,3,GL_FLOAT,GL_FALSE,12,vers);
複製代碼
//加入材質座標數據
static float txts[] = {
1.0f,0.0f , //右下
0.0f,0.0f,
1.0f,1.0f,
0.0f,1.0f
};
GLuint atex = (GLuint)glGetAttribLocation(program,"aTexCoord");
glEnableVertexAttribArray(atex);
glVertexAttribPointer(atex,2,GL_FLOAT,GL_FALSE,8,txts);
複製代碼
int width = 424;
int height = 240;
//材質紋理初始化
//設置紋理層
glUniform1i( glGetUniformLocation(program,"yTexture"),0); //對於紋理第1層
glUniform1i( glGetUniformLocation(program,"uTexture"),1); //對於紋理第2層
glUniform1i( glGetUniformLocation(program,"vTexture"),2); //對於紋理第3層
//建立opengl紋理
GLuint texts[3] = {0};
//建立三個紋理
glGenTextures(3,texts);
//設置紋理屬性
glBindTexture(GL_TEXTURE_2D,texts[0]);
//縮小的過濾器
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//設置紋理的格式和大小
glTexImage2D(GL_TEXTURE_2D,
0, //細節基本 0默認
GL_LUMINANCE,//gpu內部格式 亮度,灰度圖
width,height, //拉昇到全屏
0, //邊框
GL_LUMINANCE,//數據的像素格式 亮度,灰度圖 要與上面一致
GL_UNSIGNED_BYTE, //像素的數據類型
NULL //紋理的數據
);
//設置紋理屬性
glBindTexture(GL_TEXTURE_2D,texts[1]);
//縮小的過濾器
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//設置紋理的格式和大小
glTexImage2D(GL_TEXTURE_2D,
0, //細節基本 0默認
GL_LUMINANCE,//gpu內部格式 亮度,灰度圖
width/2,height/2, //拉昇到全屏
0, //邊框
GL_LUMINANCE,//數據的像素格式 亮度,灰度圖 要與上面一致
GL_UNSIGNED_BYTE, //像素的數據類型
NULL //紋理的數據
);
//設置紋理屬性
glBindTexture(GL_TEXTURE_2D,texts[2]);
//縮小的過濾器
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//設置紋理的格式和大小
glTexImage2D(GL_TEXTURE_2D,
0, //細節基本 0默認
GL_LUMINANCE,//gpu內部格式 亮度,灰度圖
width/2,height/2, //拉昇到全屏
0, //邊框
GL_LUMINANCE,//數據的像素格式 亮度,灰度圖 要與上面一致
GL_UNSIGNED_BYTE, //像素的數據類型
NULL //紋理的數據
);
複製代碼
////紋理的修改和顯示
FILE *fp = fopen(url,"rb");
if(!fp)
{
LOGD("open file %s failed!",url);
return;
}
unsigned char *buf[3] = {0};//將顯示的材質存放到buf中
buf[0] = new unsigned char[width*height];
buf[1] = new unsigned char[width*height/4];
buf[2] = new unsigned char[width*height/4];
for(int i = 0; i<10000;i++)
{
//420p yyyyyyyy uu vv
if(feof(fp) == 0)
{
//yyyyyyyy
fread(buf[0],1,width*height,fp);
fread(buf[1],1,width*height/4,fp);
fread(buf[2],1,width*height/4,fp);
}
//激活第1層紋理,綁定到建立的opengl紋理
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texts[0]);
//替換紋理內容
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_LUMINANCE,GL_UNSIGNED_BYTE,buf[0]);
//激活第2層紋理,綁定到建立的opengl紋理
glActiveTexture(GL_TEXTURE0+1);
glBindTexture(GL_TEXTURE_2D,texts[1]);
//替換紋理內容
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width/2,height/2,GL_LUMINANCE,GL_UNSIGNED_BYTE,buf[1]);
//激活第2層紋理,綁定到建立的opengl紋理
glActiveTexture(GL_TEXTURE0+2);
glBindTexture(GL_TEXTURE_2D,texts[2]);
//替換紋理內容
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width/2,height/2,GL_LUMINANCE,GL_UNSIGNED_BYTE,buf[2]);
//三維繪製
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
//窗口顯示
eglSwapBuffers(display,winsurface);
}
複製代碼