Android系统采用OpenGL绘制3D图形,使用skia来绘制二维图形;OpenGL源码位于:
frameworks/native/opengl
frameworks/base/opengl
本文简单介绍OpenGL库的加载过程。OpenGL以动态库的方式提供,因此在使用OpenGL的接口函数绘图前,需要加载OpenGL库,并得到接口函数指针。函数EGLBoolean egl_init_drivers()就是负责OpenGL库的加载。
EGLBoolean egl_init_drivers() { EGLBoolean res; pthread_mutex_lock(&sInitDriverMutex); res = egl_init_drivers_locked(); pthread_mutex_unlock(&sInitDriverMutex); return res;}为保证多线程访问的安全性,使用线程锁来放完另一个接口函数egl_init_drivers_locked
static EGLBoolean egl_init_drivers_locked() { if (sEarlyInitState) { // initialized by static ctor. should be set here. return EGL_FALSE; } // 得到Loader对象实例 Loader& loader(Loader::getInstance()); //加载EGL库 egl_connection_t* cnx = &gEGLImpl; if (cnx->dso == 0) { cnx->hooks[egl_connection_t::GLESv1_INDEX] =&gHooks[egl_connection_t::GLESv1_INDEX]; cnx->hooks[egl_connection_t::GLESv2_INDEX] =&gHooks[egl_connection_t::GLESv2_INDEX]; cnx->dso = loader.open(cnx); } return cnx->dso ? EGL_TRUE : EGL_FALSE;}函数首先定义指针cnx指向全局变量gEGLImpl,并且将cnx的域hooks指向gHooks,最后通过loader对象的open函数打开EGL动态库,因此最后从EGL库中加载的接口函数指针都保存到了变量gEGLImpl和gHooks中了。
frameworks\native\opengl\libs\EGL\Loader.cpp
Loader::Loader(){ char line[256]; char tag[256]; /* Special case for GLES emulation 针对模拟器处理*/ if (checkGlesEmulationStatus() == 0) { ALOGD("Emulator without GPU support detected. ""Fallback to software renderer."); mDriverTag.setTo("android"); return; } /*打开/system/lib/egl/egl.cfg文件 */ FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r"); if (cfg == NULL) { //如果打开失败,mDriverTag ="android" ALOGD("egl.cfg not found, using default config"); mDriverTag.setTo("android"); } else {//否则读取文件内容 while (fgets(line, 256, cfg)) { int dpy, impl; if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) { // /system/lib/egl/egl.cfg文件内容为:0 1 mali if (tag != String8("android")) { mDriverTag = tag; //mDriverTag = "mali" } } } fclose(cfg); }}如果/system/lib/egl/egl.cfg文件不存在,则默认配置为0 0 android,否则就读取/system/lib/egl/egl.cfg文件内容,内容定义如下:
因此变量dpy=0, impl = 1, tag = mali
void* Loader::open(egl_connection_t* cnx){ void* dso; driver_t* hnd = 0; char const* tag = mDriverTag.string();//tag="mali" if (tag) { //加载GLES库函数 dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2); if (dso) { hnd = new driver_t(dso); } else {//如果加载GLES库失败,则加载EGL,GLESv1_CM,GLESv2三个库 dso = load_driver("EGL", tag, cnx, EGL); if (dso) {//只有EGL库加载成功,才加载GLESv1_CM,GLESv2库 hnd = new driver_t(dso); // TODO: make this more automated hnd->set(load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM ); hnd->set(load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 ); } } } return (void*)hnd;}这个函数首先去加载/system/lib/egl/libGLES_mali.so,如果加载成功,那么对EGL | GLESv1_CM | GLESv2三个函数库,进行初始化。如果加载不成功,那么就加载libEGL_mali.so,libGLESv1_CM_mali.so,libGLESv2_mali.so这三个库,/system/lib/egl目录下的库如下:
由于libGLES_mali.so库不存在,因此最终加载的是libEGL_mali.so,libGLESv1_CM_mali.so,libGLESv2_mali.so三个库
void *Loader::load_driver(const char* kind, const char *tag, egl_connection_t* cnx, uint32_t mask){ char driver_absolute_path[PATH_MAX]; const char* const search1 = "/vendor/lib/egl/lib%s_%s.so"; const char* const search2 = "/system/lib/egl/lib%s_%s.so"; //首先从/vendor/lib/egl/目录下查找对应的库,如果该目录下没有,则查找/system/lib/egl/目录 snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag); //driver_absolute_path="/vendor/lib/egl/libEGL_mali.so" //driver_absolute_path="/vendor/lib/egl/libGLESv1_CM_mali.so" //driver_absolute_path="/vendor/lib/egl/libGLESv2_mali.so" //判断该路径下的动态库是否可以访问 if (access(driver_absolute_path, R_OK)) { snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag); //driver_absolute_path="/system/lib/egl/libEGL_mali.so" //driver_absolute_path="/system/lib/egl/libGLESv1_CM_mali.so" //driver_absolute_path="/system/lib/egl/libGLESv2_mali.so" if (access(driver_absolute_path, R_OK)) { return 0; } } //打开so动态库到cnx->egl中 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); if (dso == 0) { const char* err = dlerror(); ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); return 0; } ALOGD("loaded %s", driver_absolute_path); //从动态库中加载EGL函数库到cnx->egl中,EGL库函数定义在egl_names数组中 if (mask & EGL) { //读取动态库中eglGetProcAddress函数指针 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); ALOGE_IF(!getProcAddress, "can't find eglGetProcAddress() in %s", driver_absolute_path);#ifdef SYSTEMUI_PBSIZE_HACK#warning "SYSTEMUI_PBSIZE_HACK enabled" /* * Here we adjust the PB size from its default value to 512KB which * is the minimum acceptable for the systemui process. */ const char *cmdline = getProcessCmdline(); if (strstr(cmdline, "systemui")) { void *imgegl = dlopen("/vendor/lib/libIMGegl.so", RTLD_LAZY); if (imgegl) { unsigned int *PVRDefaultPBS =(unsigned int *)dlsym(imgegl, "PVRDefaultPBS"); if (PVRDefaultPBS) { ALOGD("setting default PBS to 512KB, was %d KB", *PVRDefaultPBS / 1024); *PVRDefaultPBS = 512*1024; } } }#endif egl_t* egl = &cnx->egl; __eglMustCastToProperFunctionPointerType* curr = (__eglMustCastToProperFunctionPointerType*)egl; /*定义指向egl_names数组的指针 * char const * const egl_names[] = { * #include "egl_entries.in" * NULL * }; * 文件frameworks/native/opengl/libs/EGL/egl_entries.in声明了EGL库中的所有函数 */ char const * const * api = egl_names; //遍历数组egl_names,将数组中定义的函数指针保存到cnx->egl中 while (*api) { char const * name = *api; //根据函数名称从动态库EGL中取得函数指针 __eglMustCastToProperFunctionPointerType f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); if (f == NULL) { //如果在动态库中查找不到该函数指针,则使用getProcAddress函数来获取 f = getProcAddress(name); if (f == NULL) {//如果依然得不到该函数指针,则设置为0 f = (__eglMustCastToProperFunctionPointerType)0; } } *curr++ = f; api++; } } //char const * const gl_names[] = { // #include "entries.in" // NULL //}; //从动态库中加载GLESv1_CM函数库到cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl中,GLESv1_CM库函数定义在gl_names数组中 if (mask & GLESv1_CM) { init_api(dso, gl_names,(__eglMustCastToProperFunctionPointerType*) &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, getProcAddress); } //从动态库中加载GLESv2函数库到cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl中,GLESv2库函数定义在gl_names数组中 if (mask & GLESv2) { init_api(dso, gl_names,(__eglMustCastToProperFunctionPointerType*) &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, getProcAddress); } return dso;}该函数就是从动态库libEGL_mali.so中查询egl_names数组,即frameworks/native/opengl/libs/EGL/egl_entries.in文件中声明的接口函数,及从动态库libGLESv1_CM_mali.so,libGLESv2_mali.so中查询gl_names数组,即frameworks/native/opengl/libs/entries.in文件中声明的接口函数,函数声明如下:
void Loader::init_api(void* dso, char const * const * api, __eglMustCastToProperFunctionPointerType* curr, getProcAddressType getProcAddress) { const ssize_t SIZE = 256; char scrap[SIZE]; while (*api) { char const * name = *api; //根据函数名称从动态库中查找函数指针 __eglMustCastToProperFunctionPointerType f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); if (f == NULL) { //使用eglGetProcAddress()得到指定函数指针 f = getProcAddress(name); } if (f == NULL) { //将函数名称去掉后缀OES ssize_t index = ssize_t(strlen(name)) - 3; if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { strncpy(scrap, name, index); scrap[index] = 0; f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); } } if (f == NULL) { //将函数名称增加后缀OES ssize_t index = ssize_t(strlen(name)) - 3; if (index>0 && strcmp(name+index, "OES")) { snprintf(scrap, SIZE, "%sOES", name); f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); } } if (f == NULL) { f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; if (!strcmp(name, "glInsertEventMarkerEXT")) { f = (__eglMustCastToProperFunctionPointerType)gl_noop; } else if (!strcmp(name, "glPushGroupMarkerEXT")) { f = (__eglMustCastToProperFunctionPointerType)gl_noop; } else if (!strcmp(name, "glPopGroupMarkerEXT")) { f = (__eglMustCastToProperFunctionPointerType)gl_noop; } } *curr++ = f; api++; }}load_driver函数所做的工作:首先通过dlopen加载/system/lib/egl/libGLES_mali.so库,然后从/system/lib/egl/libGLES_mali.so库中提取EGL的各个API函数的地址放到cnx->egl中,从libGLES_mali.so获取GLESv1_CM的API保存到cnx->hooks[GLESv1_INDEX]->gl中,从libGLES_mali.so获取GLESv1_CM的API保存到cnx->hooks[GLESv2_INDEX]->gl。提取EGLAPI地址的方法是首先通过dlsym函数获得一个获取函数地址的函数eglGetProcAddress的地址,然后遍历EGL的API所在文件frameworks/base/opengl/libs/EGL/egl_entries.in。先通过dlsym获取各个API地址,如果返回NULL再利用eglGetProcAddress去获得,如果依旧为空就把函数地址赋值为0;提取GLESv1_CM和GLESv1_CM库中函数地址方法和提取EGL差不多,只是他们的函数文件保存在frameworks/base/opengl/libs/entries.in中。还有它们把函数地址复制给了cnx->hooks[GLESv1_INDEX]->gl和cnx->hooks[GLESv2_INDEX]->gl。