由于目前跟踪代码只跟到了surfaceflinger这一层,下面先从surfaceflinger说起:
在frameworks\base\services\surfaceflinger\surfaceflinger.cpp这个文件中有如下代码片断:
status_t SurfaceFlinger::readyToRun()
{
LOGI("SurfaceFlinger's main thread ready to run. Initializing graphics H/W...\n");//we only support one display currentlyintdpy=0;
{//initialize the main displayGraphicPlane&plane(graphicPlane(dpy));
DisplayHardware*consthw=newDisplayHardware(this, dpy);
DisplayHardware*consthw1=newDisplayHardware(this,1); //这个是我加入测试HAL层用的,下面将介绍到
plane.setDisplayHardware(hw);
}//create the shared control-blockmServerHeap=newMemoryHeapBase(4096,MemoryHeapBase::READ_ONLY,"SurfaceFlinger read-only heap");
LOGE_IF(mServerHeap==0,"can't create shared memory dealer");
mServerCblk=static_cast(mServerHeap->getBase());
LOGE_IF(mServerCblk==0,"can't get to shared control block's address");new(mServerCblk) surface_flinger_cblk_t;//initialize primary screen//(other display should be initialized in the same manner, but//asynchronously, as they could come and go. None of this is supported//yet).constGraphicPlane&plane(graphicPlane(dpy));constDisplayHardware&hw=plane.displayHardware();constuint32_t w=hw.getWidth();constuint32_t h=hw.getHeight();constuint32_t f=hw.getFormat();
hw.makeCurrent();//initialize the shared control blockmServerCblk->connected|=1<
display_cblk_t*dcblk=mServerCblk->displays+dpy;
memset(dcblk,0,sizeof(display_cblk_t));
dcblk->w=plane.getWidth();
dcblk->h=plane.getHeight();
dcblk->format=f;
dcblk->orientation=ISurfaceComposer::eOrientationDefault;
dcblk->xdpi=hw.getDpiX();
dcblk->ydpi=hw.getDpiY();
dcblk->fps=hw.getRefreshRate();
dcblk->density=hw.getDensity();//Initialize OpenGL|ESglPixelStorei(GL_UNPACK_ALIGNMENT,4);
glPixelStorei(GL_PACK_ALIGNMENT,4);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_SCISSOR_TEST);
glShadeModel(GL_FLAT);
glDisable(GL_DITHER);
glDisable(GL_CULL_FACE);constuint16_t g0=pack565(0x0F,0x1F,0x0F);constuint16_t g1=pack565(0x17,0x2f,0x17);constuint16_t textureData[4]={ g0, g1, g1, g0 };
glGenTextures(1,&mWormholeTexName);
glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB,2,2,0,GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, w, h,0,0,1);
LayerDim::initDimmer(this, w, h);
mReadyToRunBarrier.open();/** We're now ready to accept clients...*///start boot animationproperty_set("ctl.start","bootanim");returnNO_ERROR;
}
重点看上面用黄色标出的那两行,其中DisplayHardware* const hw1 = new DisplayHardware(this, 1);是我加入用来测试HAL层的buffer分配用的,DisplayHardware这个是在frameworks\base\services\surfaceflinger\DisplayHardware\DisplayHardware.cpp这个文件中定义的,如以下代码片断:
/** Initialize the display to the specified values.
**/DisplayHardware::DisplayHardware(constsp&flinger,uint32_t dpy)
: DisplayHardwareBase(flinger, dpy),mFlags(0)
{
init(dpy);
}
而在同文件中有如下代码片断:
voidDisplayHardware::init(uint32_t dpy)
{
mNativeWindow=newFramebufferNativeWindow(dpy);
framebuffer_device_tconst*fbDev=mNativeWindow->getDevice();
mDpiX=mNativeWindow->xdpi;
mDpiY=mNativeWindow->ydpi;
mRefreshRate=fbDev->fps;
mOverlayEngine=NULL;
hw_module_tconst*module;if(hw_get_module(OVERLAY_HARDWARE_MODULE_ID,&module)==0)
{
overlay_control_open(module,&mOverlayEngine);
}
EGLint w, h, dummy;
EGLint numConfigs=0;
EGLSurface surface;
EGLContext context;//initialize EGLEGLint attribs[]={
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_NONE,0,
EGL_NONE
};//debug: disable h/w renderingcharproperty[PROPERTY_VALUE_MAX];if(property_get("debug.sf.hw", property, NULL)>0)
{if(atoi(property)==0)
{
LOGW("H/W composition disabled");
attribs[2]=EGL_CONFIG_CAVEAT;
attribs[3]=EGL_SLOW_CONFIG;
}
}//TODO: all the extensions below should be queried through//eglGetProcAddress().//set to EGL wrapper to load SW OpenGLES onlyif(property_get("debug.sf.enable_hgl", property,"1")>0)
{if(atoi(property)==0)
{
eglSetImplementationAndroid(EGL_TRUE);
}
}/*下面这些都是和egl相关的,目前正在研究中,由于代码量太大,一起不好发上来,这里简单介绍一下,frameworks\base\opengl\libagl目录下是agl相关的一些代码,这些代码会生成libGLES_android.so 这个动态库,这点可以从此目录下的Android.mk文件里看出:
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/egl
LOCAL_MODULE:= libGLES_android
在frameworks\base\opengl\libs目录下是egl相关的一些代码,这些代码主要把agl这个库作了个封装,并向外提供相关接口*/EGLDisplay display=eglGetDisplay((NativeDisplayType)dpy);//(EGL_DEFAULT_DISPLAY);LOGI("******DisplayHardware.cpp display:%d******\n",(int)display);
eglInitialize(display, NULL, NULL);
eglGetConfigs(display, NULL,0,&numConfigs);
EGLConfig config;
status_t err=EGLUtils::selectConfigForNativeWindow(display, attribs, mNativeWindow.get(),&config);
LOGE_IF(err,"couldn't find an EGLConfig matching the screen format");
EGLint r,g,b,a;
eglGetConfigAttrib(display, config, EGL_RED_SIZE,&r);
eglGetConfigAttrib(display, config, EGL_GREEN_SIZE,&g);
eglGetConfigAttrib(display, config, EGL_BLUE_SIZE,&b);
eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE,&a);if(mNativeWindow->isUpdateOnDemand())
mFlags|=PARTIAL_UPDATES;if(eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT,&dummy)==EGL_TRUE)
{if(dummy==EGL_SLOW_CONFIG)
mFlags|=SLOW_CONFIG;
}/**