知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
android開(kāi)機(jī)動(dòng)畫(huà)啟動(dòng)流程
發(fā)表時(shí)間:2020-11-6
發(fā)布人:葵宇科技
瀏覽次數(shù):49
從android的Surface Flinger服務(wù)啟動(dòng)分析知道,開(kāi)機(jī)動(dòng)畫(huà)是在SurfaceFlinger實(shí)例通過(guò)調(diào)用startBootAnim()啟動(dòng)的。
下面我們就一起學(xué)習(xí)BootAnim是如何啟動(dòng)和結(jié)束的,我精讀代碼前都喜歡先描出框架圖,以此圖為基礎(chǔ)再去研讀會(huì)達(dá)到事半功倍的效果。好吧,直接上圖。
[img]http://img.blog.csdn.net/20141231162020380?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG92YW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
內(nèi)核起來(lái)后會(huì)啟動(dòng)第一個(gè)進(jìn)程,即init進(jìn)程。
init進(jìn)程會(huì)根據(jù)init.rc配置啟動(dòng)surfaceflinger進(jìn)程。
service surfaceflinger /system/bin/surfaceflinger class main user system group graphics drmrpc onrestart restart zygote
surfaceflinger進(jìn)程便啟動(dòng)了,跟著就會(huì)跑進(jìn)程的main()函數(shù)。
frameworks/native/services/surfaceflinger/main_surfaceflinger.cpp
int main(int argc, char** argv) { .... // instantiate surfaceflinger sp<SurfaceFlinger> flinger = new SurfaceFlinger();//創(chuàng)建surfaceflinger服務(wù)實(shí)例 .... flinger->init(); // publish surface flinger sp<IServiceManager> sm(defaultServiceManager()); sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false);//注冊(cè)到service manager里 // run in this thread flinger->run();//開(kāi)跑 return 0; }
首先new一個(gè)SurfaceFlinger實(shí)例,然后init,然后run
frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() { ALOGI( "SurfaceFlinger's main thread ready to run. " "Initializing graphics H/W..."); ..... // start boot animation startBootAnim();//開(kāi)始播放動(dòng)畫(huà) }
初始化graphics之后,就調(diào)用startBootAnim()播放開(kāi)機(jī)動(dòng)畫(huà)。
void SurfaceFlinger::startBootAnim() { // start boot animation mBootFinished = false; property_set("service.bootanim.exit", "0");//這個(gè)會(huì)有bootanimation進(jìn)程周期檢測(cè),=1退出動(dòng)畫(huà) property_set("ctl.start", "bootanim");//通過(guò)ctl.start命令啟動(dòng)bootanim }
把service.bootanim.exit屬性設(shè)為0,這個(gè)屬性bootanimation進(jìn)程里會(huì)周期檢查,=1時(shí)就退出動(dòng)畫(huà),這里=0表示要播放動(dòng)畫(huà)。
后面通過(guò)ctl.start的命令啟動(dòng)bootanim進(jìn)程,動(dòng)畫(huà)就開(kāi)始播放了。
下面來(lái)到bootanimation的實(shí)現(xiàn)
frameworks/base/cmds/bootanimation/bootanimation_main.cpp
int main(int argc, char** argv) { sp<ProcessState> proc(ProcessState::self()); ProcessState::self()->startThreadPool(); // create the boot animation object sp<BootAnimation> boot = new BootAnimation();//創(chuàng)建BootAnimation實(shí)例 IPCThreadState::self()->joinThreadPool();//binder線程池,與surfaceflinger通信用的。 } return 0; }
new一個(gè)BootAnimation實(shí)例,然后建個(gè)binder線程池,因?yàn)锽ootAnimation在顯示動(dòng)畫(huà)時(shí)要與SurfaceFlinger服務(wù)進(jìn)程通信,所以要啟個(gè)binder線程池。
frameworks/base/cmds/bootanimation/BootAnimation.cpp
BootAnimation::BootAnimation() : Thread(false) { mSession = new SurfaceComposerClient();//創(chuàng)建一個(gè)對(duì)象 }
創(chuàng)建實(shí)例時(shí),構(gòu)造函數(shù)就會(huì)被調(diào)用,new一個(gè)SurfaceComposerClient實(shí)例,他是用來(lái)與surfaceflinger通信的
void BootAnimation::onFirstRef() { status_t err = mSession->linkToComposerDeath(this);//注冊(cè)surfaceflinger死亡消息的通知書(shū) ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err)); if (err == NO_ERROR) { run("BootAnimation", PRIORITY_DISPLAY);//開(kāi)跑 } }
linkTocomposerDeath的作用是當(dāng)surfaceflinger死掉是,BootAnimation就會(huì)得到通知。
如下,收到通知后就退出動(dòng)畫(huà)了,因?yàn)閟urfaceflinger都掛掉了,播放不了了。
void BootAnimation::binderDied(const wp<IBinder>& who) { // woah, surfaceflinger died! ALOGD("SurfaceFlinger died, exiting..."); // calling requestExit() is not enough here because the Surface code // might be blocked on a condition variable that will never be updated. kill( getpid(), SIGKILL );//收到surfaceflinger死亡的消息,好吧自己也跟著去了。 requestExit(); }
另一個(gè)函數(shù)run()在BootAnimation的父類Thead里,用來(lái)創(chuàng)建一個(gè)線程并跑起來(lái)。
父類
system/core/libutils/Threads.cpp
status_t Thread::run(const char* name, int32_t priority, size_t stack) { ... if (mCanCallJava) { res = createThreadEtc(_threadLoop,//創(chuàng)建線程 this, name, priority, stack, &mThread); } else { res = androidCreateRawThreadEtc(_threadLoop, this, name, priority, stack, &mThread); } .... }
創(chuàng)建_threadLoop線程
int Thread::_threadLoop(void* user) { .... do { bool result; if (first) { first = false; self->mStatus = self->readyToRun();//這個(gè)函數(shù)被bootanimation重寫(xiě)了 result = (self->mStatus == NO_ERROR); if (result && !self->exitPending()) { ... result = self->threadLoop();//這個(gè)函數(shù)被bootanimation重寫(xiě)了 } } else { result = self->threadLoop(); } ... return 0; }
readyToRun函數(shù)實(shí)現(xiàn)
status_t BootAnimation::readyToRun() { mAssets.addDefaultAssets(); sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay( ISurfaceComposer::eDisplayIdMain)); DisplayInfo dinfo; status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo); if (status) return -1; char value[PROPERTY_VALUE_MAX]; property_get("persist.panel.orientation", value, "0"); int orient = atoi(value) / 90; if(orient == eOrientation90 || orient == eOrientation270) { int temp = dinfo.h; dinfo.h = dinfo.w; dinfo.w = temp; } Rect destRect(dinfo.w, dinfo.h); mSession->setDisplayProjection(dtoken, orient, destRect, destRect); // create the native surface sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"), dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565); SurfaceComposerClient::openGlobalTransaction(); control->setLayer(0x40000000); SurfaceComposerClient::closeGlobalTransaction(); sp<Surface> s = control->getSurface(); // initialize opengl and egl const EGLint attribs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, 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); eglChooseConfig(display, attribs, &config, 1, &numConfigs); 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 the device has encryption turned on or is in process // of being encrypted we show the encrypted boot animation. char decrypt[PROPERTY_VALUE_MAX]; property_get("vold.decrypt", decrypt, ""); bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt); if ((encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) && (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) || ((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; }
threadloop實(shí)現(xiàn)
bool BootAnimation::threadLoop() { bool r; if (mAndroidAnimation) { r = android();//顯示android默認(rèn)動(dòng)畫(huà) } else { r = movie();//顯示自定義的動(dòng)畫(huà) } // No need to force exit anymore property_set(EXIT_PROP_NAME, "0"); 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; }
movie()的實(shí)現(xiàn)
bool BootAnimation::movie() { //讀取bootanimation.zip文件并解釋 // clear screen //下面是循環(huán)顯示 for (int i=0 ; i<pcount ; i++) { const Animation::Part& part(animation.parts[i]); const size_t fcount = part.frames.size(); glBindTexture(GL_TEXTURE_2D, 0); for (int r=0 ; !part.count || r<part.count ; r++) { // Exit any non playuntil complete parts immediately if(exitPending() && !part.playUntilComplete) break; for (int j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) { const Animation::Frame& frame(part.frames[j]); nsecs_t lastFrame = systemTime(); if (r > 0) { glBindTexture(GL_TEXTURE_2D, frame.tid); } else { if (part.count != 1) { glGenTextures(1, &frame.tid); glBindTexture(GL_TEXTURE_2D, frame.tid); glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } initTexture( frame.map->getDataPtr(), frame.map->getDataLength()); } if (!clearReg.isEmpty()) { Region::const_iterator head(clearReg.begin()); Region::const_iterator tail(clearReg.end()); glEnable(GL_SCISSOR_TEST); while (head != tail) { const Rect& r(*head++); glScissor(r.left, mHeight - r.bottom, r.width(), r.height()); glClear(GL_COLOR_BUFFER_BIT); } glDisable(GL_SCISSOR_TEST); } glDrawTexiOES(xc, yc, 0, animation.width, animation.height); eglSwapBuffers(mDisplay, mSurface); nsecs_t now = systemTime(); nsecs_t delay = frameDuration - (now - lastFrame); //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay)); lastFrame = now; if (delay > 0) { struct timespec spec; spec.tv_sec = (now + delay) / 1000000000; spec.tv_nsec = (now + delay) % 1000000000; int err; do { err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL); } while (err<0 && errno == EINTR); } checkExit();//檢測(cè)是否退出動(dòng)畫(huà) } usleep(part.pause * ns2us(frameDuration)); // For infinite parts, we've now played them at least once, so perhaps exit if(exitPending() && !part.count) break; } // free the textures for this part if (part.count != 1) { for (int j=0 ; j<fcount ; j++) { const Animation::Frame& frame(part.frames[j]); glDeleteTextures(1, &frame.tid); } } } return false; }
那么到movie為止,動(dòng)畫(huà)是在播放了,而且還在循環(huán)檢測(cè)是否退出,即checkExit()
checkExit()的實(shí)現(xiàn)
void BootAnimation::checkExit() { // Allow surface flinger to gracefully request shutdown char value[PROPERTY_VALUE_MAX]; property_get(EXIT_PROP_NAME, value, "0");//屬性為1,說(shuō)明要退出了 int exitnow = atoi(value); if (exitnow) { requestExit(); } }
property_get(EXIT_PROP_NAME, value, "0");檢測(cè)這個(gè)屬性,=1就退出動(dòng)畫(huà)
#define EXIT_PROP_NAME "service.bootanim.exit"這個(gè)屬性就是上面講到的,等到launcher跑起來(lái)后就會(huì)置1
那動(dòng)畫(huà)是什么時(shí)候退出的?
當(dāng)launcher應(yīng)用程序主線程跑起來(lái)后,如果主線程處于空閑,就會(huì)向ActivityManagerService發(fā)送一個(gè)activityIdle的消息。
應(yīng)用程序主線程是ActivityThread.java來(lái)描述的,activityIdle是這個(gè)類來(lái)實(shí)現(xiàn)的
private class Idler implements MessageQueue.IdleHandler { ... IActivityManager am = ActivityManagerNative.getDefault(); ... try { am.activityIdle(a.token, a.createdConfig, stopProfiling); a.createdConfig = null; } catch (RemoteException ex) { // Ignore } .... }
上面的ActivityManagerNavtive.getDefault()得到am
來(lái)到frameworks/base/core/java/android/app/ActivityManagerNative.java
static public IActivityManager getDefault() { return gDefault.get();//getDefault的實(shí)現(xiàn) }
private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() { protected IActivityManager create() { IBinder b = ServiceManager.getService("activity"); if (false) { Log.v("ActivityManager", "default service binder = " + b); } IActivityManager am = asInterface(b); if (false) { Log.v("ActivityManager", "default service = " + am); } return am; } };
gDefault實(shí)際上是IActivityManager,往下看
class ActivityManagerProxy implements IActivityManager {
ActivityManagerProxy實(shí)現(xiàn)了IActivityManager
那么am.activityIdle()就是ActivityManagerProxy里的函數(shù),如下
public void activityIdle(IBinder token, Configuration config, boolean stopProfiling) throws RemoteException { ... mRemote.transact(ACTIVITY_IDLE_TRANSACTION, data, reply, IBinder.FLAG_ONEWAY);//發(fā)送ACTIVITY_IDLE_TRANSACTION .... }
發(fā)送了ACTIVITY_IDLE_TRANSACTION的進(jìn)程間通信,這個(gè)消息被ActivityManagerNative接收處理了。
case ACTIVITY_IDLE_TRANSACTION: {//收到消息 data.enforceInterface(IActivityManager.descriptor); IBinder token = data.readStrongBinder(); Configuration config = null; if (data.readInt() != 0) { config = Configuration.CREATOR.createFromParcel(data); } boolean stopProfiling = data.readInt() != 0; if (token != null) { activityIdle(token, config, stopProfiling);//這個(gè)函數(shù)在ActivityManagerService被重寫(xiě) } reply.writeNoException(); return true; }
收到消息后就調(diào)用了activityIdle函數(shù),這個(gè)函數(shù)被ActivityManagerService重寫(xiě)了,如下
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
@Override public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) { final long origId = Binder.clearCallingIdentity(); synchronized (this) { ActivityStack stack = ActivityRecord.getStackLocked(token); if (stack != null) { ActivityRecord r = mStackSupervisor.activityIdleInternalLocked(token, false, config); if (stopProfiling) { if ((mProfileProc == r.app) && (mProfileFd != null)) { try { mProfileFd.close(); } catch (IOException e) { } clearProfilerLocked(); } } } } Binder.restoreCallingIdentity(origId); }
調(diào)用activityIdleInternalLocked函數(shù),在下面實(shí)現(xiàn)
frameworks/base/services/java/com/android/server/am/ActivityStackSupervisor.java
final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout, Configuration config) { .... if (enableScreen) { mService.enableScreenAfterBoot();//調(diào)ActivityManagerService類的enableScreenAfterBoot()函數(shù) } .... if (activityRemoved) { resumeTopActivitiesLocked(); } return r; }
來(lái)到frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
void enableScreenAfterBoot() { EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, SystemClock.uptimeMillis()); mWindowManager.enableScreenAfterBoot();//調(diào)WindowManagerService類里的enableScreenAfterBoot()函數(shù) synchronized (this) { updateEventDispatchingLocked(); } }
來(lái)到frameworks/base/services/java/com/android/server/wm/WindowManagerService.java
public void enableScreenAfterBoot() { .... performEnableScreen(); }
performEnableScreen()實(shí)現(xiàn)
public void performEnableScreen() { ..... surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION, // BOOT_FINISHED data, null, 0); .... }
發(fā)送了FIRST_CALL_TRANSACTION的請(qǐng)求
因?yàn)閺南旅嬷繤IRST_CALL_TRANSACTION = BOOT_FINISHED
所以BnSurfaceComposer收到消息
frameworks/native/include/gui/ISurfaceComposer.h
class BnSurfaceComposer: public BnInterface<ISurfaceComposer> { public: enum { // Note: BOOT_FINISHED must remain this value, it is called from // Java by ActivityManagerService. BOOT_FINISHED = IBinder::FIRST_CALL_TRANSACTION, ... }; virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0); };
</pre></p><p><span style="color:#333333;">frameworks/native/libs/gui/ISurfaceComposer.cpp</span></p><p><span style="color:#333333;"></span><pre name="code" class="cpp">status_t BnSurfaceComposer::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { .... case BOOT_FINISHED: { CHECK_INTERFACE(ISurfaceComposer, data, reply); bootFinished();//調(diào)用 bootFinished() return NO_ERROR; } .... } // should be unreachable return NO_ERROR; }
bootFinished()函數(shù)BpSurfaceComposer里實(shí)現(xiàn),但發(fā)現(xiàn)沒(méi)有,他又發(fā)了一個(gè)BOOT_FINISHED,死循環(huán)了,其實(shí)沒(méi)有。bootFinished()被SurfaceFlinger類重寫(xiě)了
class BpSurfaceComposer : public BpInterface<ISurfaceComposer> { virtual void bootFinished() { Parcel data, reply; data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor()); remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply); }
重寫(xiě)
frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::bootFinished() { ... property_set("service.bootanim.exit", "1"); }
把service.bootanim.exit寫(xiě)成1,然后bootanimation進(jìn)程的checkExit()檢測(cè)到就退出進(jìn)程,停止播放。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南微信小程序開(kāi)發(fā)
- 百度小程序
- 云南網(wǎng)站建設(shè)外包
- 網(wǎng)站建設(shè)電話
- 云南網(wǎng)站建設(shè)百度官方
- 昆明小程序開(kāi)發(fā)
- typescript
- 網(wǎng)絡(luò)公司電話
- 網(wǎng)站維護(hù)
- 前端技術(shù)
- 海報(bào)插件
- 汽車(chē)報(bào)廢管理系統(tǒng)
- 昆明網(wǎng)站制作
- 開(kāi)發(fā)框架
- 云南小程序開(kāi)發(fā)公司
- 網(wǎng)站建設(shè)公司網(wǎng)站
- 汽車(chē)報(bào)廢回收軟件
- 網(wǎng)站建設(shè)百度官方
- 人口普查小程序
- 云南軟件設(shè)計(jì)
- 云南企業(yè)網(wǎng)站
- 云南小程序開(kāi)發(fā)
- 云南建設(shè)廳網(wǎng)站首頁(yè)
- 昆明網(wǎng)站建設(shè)公司
- 保山小程序開(kāi)發(fā)
- 小程序被騙退款成功
- 云南小程序商城
- 云南小程序開(kāi)發(fā)公司推薦
- 小程序制作
- Web開(kāi)發(fā)框架