电竞比分网-中国电竞赛事及体育赛事平台

分享

Android 4.0.3 顯示系統(tǒng)深入理解

 leon0821 2013-08-15

1. 簡介      

        網(wǎng)上已經(jīng)有很多兄弟對(duì)Android的顯示系統(tǒng)做了深入解剖,很是佩服??勺罱〉茉谘芯緼ndroid4.0時(shí)發(fā)現(xiàn)出入比較大,也許是Android4.0的修改比較多吧!因?yàn)樾〉軟]有看Android4.0以前的代碼。

       面對(duì)這么復(fù)雜一個(gè)Android顯示系統(tǒng),如何入手呢? 根據(jù)以前的經(jīng)驗(yàn),不管它有多么復(fù)雜,其功能不就是以下三步曲嗎?

  1)顯示系統(tǒng)的創(chuàng)建及初始化

       2)畫圖

       3)銷毀

       哪我的分析就從顯示系統(tǒng)的創(chuàng)建及初始化開始吧!由于小弟對(duì)Java沒有什么研究興趣,所有重點(diǎn)就分析Native部分。當(dāng)然Native的入口就在android_view_Surface.cpp中,此文件主要包含以下兩部分給Java層調(diào)用:

       1)gSurfaceSessionMethods: 操作SurfaceSession的方法

       2)gSurfaceMethods:操作Surface的方法

2. android_view_Surface.cpp

2.1 SurfaceSession操作方法

  1. static JNINativeMethod gSurfaceSessionMethods[] = {    
  2.     {"init",     "()V",  (void*)SurfaceSession_init }, //創(chuàng)建SurfaceComposerClient    
  3.     {"destroy",  "()V",  (void*)SurfaceSession_destroy }, //直接銷毀SurfaceComposerClient    
  4.     {"kill",     "()V",  (void*)SurfaceSession_kill },//先clear,再銷毀SurfaceComposerClient   
  5. };    

2.1.1 SurfaceSession_init

        其功能如下:

        1)創(chuàng)建SurfaceComposerClient對(duì)象

        2)調(diào)用SurfaceComposerClient::onFirstRef方法

        現(xiàn)在已經(jīng)進(jìn)入到SurfaceComposerClient的地盤,根據(jù)其名字含義,它應(yīng)該是一個(gè)進(jìn)行Surface合成的客戶端,通過它發(fā)命令給SurfaceFlinger來進(jìn)行需要的操作。其初始化流程如下圖所示:

2.1.2 SurfaceComposerClient.cpp中的寶貝

        為了方便后面的理解,先看看SurfaceComposerClient中有些什么寶貝來完成這個(gè)任務(wù)。在其中定義了如下幾個(gè)類:

2.1.2.1 ComposerService(獲取SurfaceFlinger服務(wù))

        一看到名字為Service,應(yīng)該是用于從SurfaceFlinger中獲取Service以建立連接關(guān)系<它是一個(gè)單實(shí)例,一個(gè)進(jìn)程有且只有一個(gè)實(shí)例對(duì)象>,然后供后面進(jìn)行相關(guān)的操作。其構(gòu)造函數(shù)代碼如下:      

  1. class ComposerService : public Singleton<ComposerService>  
  2. {  
  3.     //實(shí)質(zhì)為BpSurfaceComposer,通過它與SurfaceFlinger進(jìn)行通信,   
  4.     //BnSurfaceComposer是SurfaceFlinger基類中的一個(gè)   
  5.     sp<ISurfaceComposer> mComposerService;  
  6.   
  7.     //實(shí)質(zhì)為BpMemoryHeap,它在SurfaceFlinger中對(duì)應(yīng)為管理一個(gè)4096字節(jié)的   
  8.     //一個(gè)MemoryHeapBase對(duì)象,在SurfaceFlinger::readyToRun中創(chuàng)建   
  9.     sp<IMemoryHeap> mServerCblkMemory;  
  10.       
  11.     //為MemoryHeapBase管理的內(nèi)存在用戶空間的基地址,通過mmap而來,   
  12.     //具體見MemoryHeapBase::mapfd   
  13.     surface_flinger_cblk_t volatile* mServerCblk;  
  14.     ComposerService();  
  15.     friend class Singleton<ComposerService>;  
  16. public:  
  17.     static sp<ISurfaceComposer> getComposerService();  
  18.     static surface_flinger_cblk_t const volatile * getControlBlock();  
  19. };  
  20.   
  21. ComposerService::ComposerService()  
  22. : Singleton<ComposerService>() {  
  23.     const String16 name("SurfaceFlinger");  
  24.     //獲取SurfaceFlinger服務(wù),即BpSurfaceComposer對(duì)象   
  25.     while (getService(name, &mComposerService) != NO_ERROR) {  
  26.         usleep(250000);  
  27.     }  
  28.     //獲取共享內(nèi)存塊   
  29.     mServerCblkMemory = mComposerService->getCblk();  
  30.     //獲取共享內(nèi)存塊基地址   
  31.     mServerCblk = static_cast<surface_flinger_cblk_t volatile *>(  
  32.             mServerCblkMemory->getBase());  
  33. }  

      由此可見,ComposerService主要是獲取SurfaceFlinger服務(wù)、獲取在SurfaceFlinger::readyToRun中創(chuàng)建的共享內(nèi)存塊及其基地址。在Client中,誰要想與SurfaceFlinger通信,需要通過接口getComposerService來獲取此BpSurfaceComposer。

     此ComposerService是在調(diào)用ComposerService::getInstance時(shí)進(jìn)行有且只有一個(gè)的實(shí)例化,因?yàn)榍懊嬷v過,它是一個(gè)單實(shí)例。

 

2.1.2.2 Composer

      它也是一個(gè)單實(shí)例,管理并發(fā)送每個(gè)layer的ComposerState。其定義如下:

  1. struct ComposerState {  
  2.     sp<ISurfaceComposerClient> client;  
  3.     layer_state_t state;  
  4.     status_t    write(Parcel& output) const;  
  5.     status_t    read(const Parcel& input);  
  6. };  
  7.   
  8. class Composer : public Singleton<Composer>  
  9. {  
  10.     friend class Singleton<Composer>;  
  11.   
  12.     mutable Mutex               mLock;  
  13.     //SurfaceComposerClient+SurfaceID與一個(gè)ComposerState一一對(duì)應(yīng)   
  14.     SortedVector<ComposerState> mStates;       
  15.     int                         mOrientation;//整個(gè)屏幕的方向   
  16.     Composer() : Singleton<Composer>(),  
  17.         mOrientation(ISurfaceComposer::eOrientationUnchanged) { }  
  18.     //通過BpSurfaceComposer把mStates發(fā)送給SurfaceFlinger處理   
  19.     void closeGlobalTransactionImpl();  
  20.   
  21.     //根據(jù)client和id從mStates中獲取對(duì)應(yīng)原ComposerState,從而獲取對(duì)應(yīng)的layer_state_t   
  22.     layer_state_t* getLayerStateLocked(  
  23.             const sp<SurfaceComposerClient>& client, SurfaceID id);  
  24.   
  25. public:  
  26.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的位置信息,并保存在mStates中   
  27.     status_t setPosition(const sp<SurfaceComposerClient>& client, SurfaceID id,  
  28.             float x, float y);  
  29.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的Size信息,并保存在mStates中   
  30.     status_t setSize(const sp<SurfaceComposerClient>& client, SurfaceID id,  
  31.             uint32_t w, uint32_t h);  
  32.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的z-order信息,并保存在mStates中   
  33.     status_t setLayer(const sp<SurfaceComposerClient>& client, SurfaceID id,  
  34.             int32_t z);  
  35.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的flags信息,并保存在mStates中   
  36.     status_t setFlags(const sp<SurfaceComposerClient>& client, SurfaceID id,  
  37.             uint32_t flags, uint32_t mask);  
  38.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的透明區(qū)域信息,并保存在mStates中   
  39.     status_t setTransparentRegionHint(  
  40.             const sp<SurfaceComposerClient>& client, SurfaceID id,  
  41.             const Region& transparentRegion);  
  42.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的alpha信息,并保存在mStates中   
  43.     status_t setAlpha(const sp<SurfaceComposerClient>& client, SurfaceID id,  
  44.             float alpha);  
  45.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的矩陣信息,并保存在mStates中   
  46.     status_t setMatrix(const sp<SurfaceComposerClient>& client, SurfaceID id,  
  47.             float dsdx, float dtdx, float dsdy, float dtdy);  
  48.     //設(shè)置與client和id對(duì)應(yīng)的layer_state_t中的位置信息,并保存在mStates中   
  49.     status_t setFreezeTint(  
  50.             const sp<SurfaceComposerClient>& client, SurfaceID id,  
  51.             uint32_t tint);  
  52.     //設(shè)置整個(gè)屏幕的方向   
  53.     status_t setOrientation(int orientation);  
  54.     //通過BpSurfaceComposer把mStates發(fā)送給SurfaceFlinger處理   
  55.     static void closeGlobalTransaction() {  
  56.         Composer::getInstance().closeGlobalTransactionImpl();  
  57.     }  
  58. }  

      把上面的comments看完就明白了,Composer管理每個(gè)SurfaceComposerClient中的每一個(gè)Surface的狀態(tài),并記錄在ComposerState的layer_state_t中,然后調(diào)用者可以調(diào)用其closeGlobalTransaction方法把這些mStates發(fā)送給SurfaceFlinger處理(處理函數(shù)為:SurfaceFlinger::setTransactionState)。

      誰來調(diào)用它的方法設(shè)置層的屬性及發(fā)送mStates呢? -----答案是由SurfaceComposerClient來調(diào)用。

linux

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多