Android系统的开机画面显示过程分析(10)
BootAnimation类的成员函数readyToRun的实现如下所示:
- status_t BootAnimation::readyToRun() {
- mAssets.addDefaultAssets();
- DisplayInfo dinfo;
- status_t status = session()->getDisplayInfo(0, &dinfo);
- if (status)
- return -1;
- // create the native surface
- sp<SurfaceControl> control = session()->createSurface(
- getpid(), 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
- session()->openTransaction();
- control->setLayer(0x40000000);
- session()->closeTransaction();
- sp<Surface> s = control->getSurface();
- // initialize opengl and egl
- const EGLint attribs[] = {
- EGL_DEPTH_SIZE, 0,
- EGL_NONE
- };
- EGLint w, h, dummy;
- EGLint numConfigs;
- EGLConfig config;
- EGLSurface surface;
- EGLContext context;
- EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
- eglInitialize(display, 0, 0);
- EGLUtils::selectConfigForNativeWindow(display, attribs, s.get(), &config);
- surface = eglCreateWindowSurface(display, config, s.get(), NULL);
- context = eglCreateContext(display, config, NULL, NULL);
- eglQuerySurface(display, surface, EGL_WIDTH, &w);
- eglQuerySurface(display, surface, EGL_HEIGHT, &h);
- if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
- return NO_INIT;
- mDisplay = display;
- mContext = context;
- mSurface = surface;
- mWidth = w;
- mHeight = h;
- mFlingerSurfaceControl = control;
- mFlingerSurface = s;
- mAndroidAnimation = true;
- if ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
- (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR) ||
- (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
- (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))
- mAndroidAnimation = false;
- return NO_ERROR;
- }
- #define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
- #define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
这一步执行完成之后,用来显示第三个开机画面的线程的初始化工作就执行完成了,接下来,就会执行这个线程的主体函数,即BootAnimation类的成员函数threadLoop。
- bool BootAnimation::threadLoop()
- {
- bool r;
- if (mAndroidAnimation) {
- r = android();
- } else {
- r = movie();
- }
- eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
- eglDestroyContext(mDisplay, mContext);
- eglDestroySurface(mDisplay, mSurface);
- mFlingerSurface.clear();
- mFlingerSurfaceControl.clear();
- eglTerminate(mDisplay);
- IPCThreadState::self()->stopProcess();
- return r;
- }
- bool BootAnimation::android()
- {
- initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
- initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
- // clear screen
- glShadeModel(GL_FLAT);
- glDisable(GL_DITHER);
- glDisable(GL_SCISSOR_TEST);
- glClear(GL_COLOR_BUFFER_BIT);
- eglSwapBuffers(mDisplay, mSurface);
- glEnable(GL_TEXTURE_2D);
- glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- const GLint xc = (mWidth - mAndroid[0].w) / 2;
- const GLint yc = (mHeight - mAndroid[0].h) / 2;
- const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
- // draw and update only what we need
- mFlingerSurface->setSwapRectangle(updateRect);
- glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
- updateRect.height());
- // Blend state
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- const nsecs_t startTime = systemTime();
- do {
- nsecs_t now = systemTime();
- double time = now - startTime;
- float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
- GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
- GLint x = xc - offset;
- glDisable(GL_SCISSOR_TEST);
- glClear(GL_COLOR_BUFFER_BIT);
- glEnable(GL_SCISSOR_TEST);
- glDisable(GL_BLEND);
- glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
- glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
- glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
- glEnable(GL_BLEND);
- glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
- glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
- EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
- if (res == EGL_FALSE) {
- break;
- }
- // 12fps: don't animate too fast to preserve CPU
- const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
- if (sleepTime > 0)
- usleep(sleepTime);
- } while (!exitPending());
- glDeleteTextures(1, &mAndroid[0].name);
- glDeleteTextures(1, &mAndroid[1].name);
- return false;
- }