Android boot animation source code analysis

Source: Internet
Author: User

1.1 Android boot Animation: currently, Android boot animation is mainly implemented by frame-by-frame animation and OpenGL animation.
? Frame-By-Frame animation is a common form of animation (Frame By Frame). Its principle is to break down the animation action in "continuous key frames, that is, different content is drawn frame by frame on each frame of the timeline so that it can be played continuously as an animation. Because the frame sequence content of frame-by-frame animation is different, it not only adds a burden to production, but also produces a large amount of final output files, but its advantages are also obvious: frame-by-frame animation has great flexibility and can express almost any content you want to express. It is similar to the playing mode of a movie and is suitable for performing delicate animations.
Frame-by-frame animation is a widely used method on the Internet. The actual principle is to pack a series of images into bootanimation.zip and put them into the/system/media/directory. The system will play the images one frame at a time to form an animation effect. Theoretically, this kind of animation needs to be implemented, but after practice, you will find that when bootanimation.zip is larger than 5 MB, the animation will obviously lag, and the larger the file, the less smooth the animation. As a result, I learned carefully that most of the boot animations on the mobile phone are only a small part of the changes, and all around them are black, so that 100*50 (or even smaller) can be used) the resolution of the image, so that the size of 100 frames is only a few MB.
? OpenGL animation OpenGL (English: Open Graphics Library) is a specification that defines a cross-programming language and cross-platform application interface (API). It is used to generate two-dimensional and three-dimensional images. This interface is composed of nearly three hundred and fifty different function calls, used to draw a complex 3D scene from a simple graphical bit.

Comparing the two methods, we can easily find that the TV platform is different from the mobile phone, especially the boot advertisement. The picture with a resolution of 1920*1080 is several hundred KB. Due to the size limit, the number of animation frames is very small, therefore, TV platforms cannot make complex and smooth animations using frame-by-frame animations. This article will mainly discuss the implementation of OpenGL.

1.2 native boot animation source code analysis before making your own boot animation, let's first analyze the native Android boot animation source code. The source code of the Android system boot animation is in framework/base/cmds/bootanimation (table 1 ).

Bootanimation Directory: android-x.x/framework/base/cmds/bootanimation/

Android. mk

Android compilation Definition

Bootanimation_main.cpp

Portal File

BootAnimation. h

BootAnimation class declaration

BootAnimation. cpp

Definition and Implementation of the BootAnimation class


Let's take a look at bootanimation_main.cpp. This file defines the main function, and the code is as follows:

[Bootanimation_main.cpp]
Int main (int argc, char ** argv)
{
......
Sp Proc (ProcessState: self ());
ProcessState: self ()-> startThreadPool ();

// Create the boot animation object
Sp Boot = new BootAnimation ();

IPCThreadState: self ()-> joinThreadPool ();
......
}

The code of the Main function is very simple. First, start the thread pool of Process, create a BootAnimation object, and add the BootAnimation object to the thread pool, startThreadPool () and joinThreadPool () for usage instructions, refer to the binder Mechanism of android. Next let's take a look at the BootAnimation class:

[BootAnimation. h]
Class BootAnimation: public Thread, public IBinder: DeathRecipient
{
......
Private:
Virtual bool threadLoop ();
Virtual status_t readyToRun ();
Virtual void onFirstRef ();
Virtual void binderDied (const wp & Who );
......
Status_t initTexture (Texture * texture, AssetManager & asset, const char * name );
Status_t initTexture (void * buffer, size_t len );
Bool android ();
Bool movie ();

Void checkExit ();
......
}

The BootAnimation class inherits the Thread class and IBinder: DeathRecipient class. The functions of several override functions are as follows:
? OnFirstRef () belongs to its parent class RefBase. This function is called when a strong reference sp adds a reference count, that is, it is called when a class packaged by sp is initialized.
? BinderDied (): The binderDied () method is called back when the object is dead or otherwise the Binder ends;
? ReadyToRun () defines the initialization before the Thread is executed;
? ThreadLoop () is implemented by every thread class. The thread execution content is defined here. If this function returns true, the function will continuously execute the content in threadloop, if this function returns false, the content in threadloop will exit after only one thread is executed;

[BootAnimation: onFirstRef ()]
Void BootAnimation: onFirstRef ()
{
Status_t err = mSession-> linkToComposerDeath (this );
ALOGE_IF (err, "linkToComposerDeath failed (% s)", strerror (-err ));
If (err = NO_ERROR ){
Run ("BootAnimation", PRIORITY_DISPLAY );
}
}

Void BootAnimation: binderDied (const wp & Who)
{
Kill (getpid (), SIGKILL );
RequestExit ();
}

The onFirstRef function executes the run BootAnimation thread.

[BootAnimation: readyToRun ()]
Status_t BootAnimation: readyToRun (){
......
// Create the native surface
......
// Initialize opengl and egl
......
EGLDisplay display = eglGetDisplay (EGL_DEFAULT_DISPLAY );
......
Surface = eglCreateWindowSurface (display, config, s. get (), NULL );
......
MDisplay = display;
......
MSurface = surface;
......
}

The readyToRun function is mainly used to obtain the EGLDisplay mDisplay and EGLDisplay msurfaceobjects. These two objects will be introduced later, and the last part will query the existence of bootanimation.zip and initialize the value of mAndroidAnimation.

[BootAnimation: threadLoop ()]
# Define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
# Define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
# Define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
......
Bool BootAnimation: threadLoop ()
{
Bool r;
If (mAndroidAnimation ){
R = android ();
} Else {
R = movie ();
}
......
}

BootAnimation: readytorunchecks whether the producer has bootanimation.zip. if bootanimation.zip exists, Run movie (). Otherwise, run android (). Movie( androandroandroandroandroandroandroandroandromomomomomomomo.

[BootAnimation: android ()]
Bool BootAnimation: android ()
{
InitTexture (& mAndroid [0], mAssets, "images/android-logo-mask.png ");
InitTexture (& mAndroid [1], mAssets, "images/android-logo-shine.png ");
......
Do {
......
GlBindTexture (GL_TEXTURE_2D, mAndroid [0]. name );
GlDrawTexiOES (xc, yc, 0, mAndroid [0]. w, mAndroid [0]. h );

EGLBoolean res = eglSwapBuffers (mDisplay, mSurface );
......
CheckExit ();
} While (! ExitPending ());
......
}

Finally, the plotting step is reached. BootAnimation: The android function contains the entire opengl plotting process, because android uses the standard opengl es api, therefore, the initialization and Drawing Process of opengl will not be detailed here. Please note that eglSwapBuffers (mDisplay, mSurface) is used to ship mSruface to the screen. Two EGLDisplay objects are used here, this is because the dual-buffer mechanism is used to achieve stable flushing rate with sleep. Finally, exit the program when the system initialization is detected.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.