歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 4.0.3 顯示系統深入理解

Android 4.0.3 顯示系統深入理解

日期:2017/3/1 10:27:26   编辑:Linux編程

1. 簡介

網上已經有很多兄弟對Android的顯示系統做了深入解剖,很是佩服。可最近小弟在研究Android4.0時發現出入比較大,也許是Android4.0的修改比較多吧!因為小弟沒有看Android4.0以前的代碼。

面對這麼復雜一個Android顯示系統,如何入手呢? 根據以前的經驗,不管它有多麼復雜,其功能不就是以下三步曲嗎?

  1)顯示系統的創建及初始化

2)畫圖

3)銷毀

哪我的分析就從顯示系統的創建及初始化開始吧!由於小弟對Java沒有什麼研究興趣,所有重點就分析Native部分。當然Native的入口就在android_view_Surface.cpp中,此文件主要包含以下兩部分給Java層調用:

1)gSurfaceSessionMethods: 操作SurfaceSession的方法

2)gSurfaceMethods:操作Surface的方法

2. android_view_Surface.cpp

2.1 SurfaceSession操作方法

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

2.1.1 SurfaceSession_init

其功能如下:

1)創建SurfaceComposerClient對象

2)調用SurfaceComposerClient::onFirstRef方法

現在已經進入到SurfaceComposerClient的地盤,根據其名字含義,它應該是一個進行Surface合成的客戶端,通過它發命令給SurfaceFlinger來進行需要的操作。其初始化流程如下圖所示:

2.1.2 SurfaceComposerClient.cpp中的寶貝

為了方便後面的理解,先看看SurfaceComposerClient中有些什麼寶貝來完成這個任務。在其中定義了如下幾個類:

2.1.2.1 ComposerService(獲取SurfaceFlinger服務)

一看到名字為Service,應該是用於從SurfaceFlinger中獲取Service以建立連接關系<它是一個單實例,一個進程有且只有一個實例對象>,然後供後面進行相關的操作。其構造函數代碼如下:

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

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

此ComposerService是在調用ComposerService::getInstance時進行有且只有一個的實例化,因為前面講過,它是一個單實例。

2.1.2.2 Composer

它也是一個單實例,管理並發送每個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. class Composer : public Singleton<Composer>
  8. {
  9. friend class Singleton<Composer>;
  10. mutable Mutex mLock;
  11. //SurfaceComposerClient+SurfaceID與一個ComposerState一一對應
  12. SortedVector<ComposerState> mStates;
  13. int mOrientation;//整個屏幕的方向
  14. Composer() : Singleton<Composer>(),
  15. mOrientation(ISurfaceComposer::eOrientationUnchanged) { }
  16. //通過BpSurfaceComposer把mStates發送給SurfaceFlinger處理
  17. void closeGlobalTransactionImpl();
  18. //根據client和id從mStates中獲取對應原ComposerState,從而獲取對應的layer_state_t
  19. layer_state_t* getLayerStateLocked(
  20. const sp<SurfaceComposerClient>& client, SurfaceID id);
  21. public:
  22. //設置與client和id對應的layer_state_t中的位置信息,並保存在mStates中
  23. status_t setPosition(const sp<SurfaceComposerClient>& client, SurfaceID id,
  24. float x, float y);
  25. //設置與client和id對應的layer_state_t中的Size信息,並保存在mStates中
  26. status_t setSize(const sp<SurfaceComposerClient>& client, SurfaceID id,
  27. uint32_t w, uint32_t h);
  28. //設置與client和id對應的layer_state_t中的z-order信息,並保存在mStates中
  29. status_t setLayer(const sp<SurfaceComposerClient>& client, SurfaceID id,
  30. int32_t z);
  31. //設置與client和id對應的layer_state_t中的flags信息,並保存在mStates中
  32. status_t setFlags(const sp<SurfaceComposerClient>& client, SurfaceID id,
  33. uint32_t flags, uint32_t mask);
  34. //設置與client和id對應的layer_state_t中的透明區域信息,並保存在mStates中
  35. status_t setTransparentRegionHint(
  36. const sp<SurfaceComposerClient>& client, SurfaceID id,
  37. const Region& transparentRegion);
  38. //設置與client和id對應的layer_state_t中的alpha信息,並保存在mStates中
  39. status_t setAlpha(const sp<SurfaceComposerClient>& client, SurfaceID id,
  40. float alpha);
  41. //設置與client和id對應的layer_state_t中的矩陣信息,並保存在mStates中
  42. status_t setMatrix(const sp<SurfaceComposerClient>& client, SurfaceID id,
  43. float dsdx, float dtdx, float dsdy, float dtdy);
  44. //設置與client和id對應的layer_state_t中的位置信息,並保存在mStates中
  45. status_t setFreezeTint(
  46. const sp<SurfaceComposerClient>& client, SurfaceID id,
  47. uint32_t tint);
  48. //設置整個屏幕的方向
  49. status_t setOrientation(int orientation);
  50. //通過BpSurfaceComposer把mStates發送給SurfaceFlinger處理
  51. static void closeGlobalTransaction() {
  52. Composer::getInstance().closeGlobalTransactionImpl();
  53. }
  54. }

把上面的comments看完就明白了,Composer管理每個SurfaceComposerClient中的每一個Surface的狀態,並記錄在ComposerState的layer_state_t中,然後調用者可以調用其closeGlobalTransaction方法把這些mStates發送給SurfaceFlinger處理(處理函數為:SurfaceFlinger::setTransactionState)。

誰來調用它的方法設置層的屬性及發送mStates呢? -----答案是由SurfaceComposerClient來調用。

Copyright © Linux教程網 All Rights Reserved