Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

理解 Android 3D Emulation 的運作架構

編輯:關於 Android 程式設計

Android Emulator 提供了 GPU on 選項,意思是利用 Host(運行 Emulator 的 PC)的 GPU,當然 PC 必須先裝好 OpenGL 驅動。

在實現上,就是把 libGLESv1_CM.so、libGLESv2.so 替換掉。當系統呼叫 GL 函式時,把呼叫打包成 stream,透過 pipe 發送到 Host 端處理,再轉化為對 Host OpenGL 的呼叫。光這樣還不夠,還得把 libegl、libgralloc 都替換掉,因為要呼叫 Host 端 OpenGL 必須有 rendercontext,所以也要把 Android 的 egl 轉化為 Host 的 xgl 呼叫(若你用的是 Ubuntu)。

創建

正常情況下,當一個 apk 進程需要創建窗體時,會呼叫 egl 函式創建 Surface,也要呼叫 gralloc 中的函式創建存放光柵的記憶體,而後把兩者關聯,這些都在本進程中完成。但在 Android 3D Emulation 架構中,窗體不能直接創建,必須發命令給 Host 端 render,讓它呼叫 xgl 命令來創建。此處 Surface 對應 3D Emulation 中的 WindowSurface,光柵記憶體對應 ColorBuffer。

圖中的 FrameBuffer 是 3D emulation 中的虛擬 FrameBuffer,它對 ColorBuffer、WindowSurface、RenderContext 進行集中管理。

更新

當 APK 呼叫 swapBuffers(把已畫好的內容上屏,這需要各 Surface 提供圖像記憶體,由 SurfaceFlinger 處理並顯示到螢幕)時,可以想像 OpenGL 的繪圖已經結束,接下來就是執行一系列記憶體 copy:ColorBuffer 把 OpenGL 的 image 拷貝到 egl 的 window surface,eglMakeCurrent 把 draw surface 拷貝到 read surface,最後由 SurfaceFlinger 把 image 合成後交給 FrameBuffer 顯示。

關鍵函式如 eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx ),以及把 colorbuffer 內容拷貝到 read surface 的邏輯,都是這套虛擬化流程的一環。

最後,你在 emulator 中跑賽車遊戲也沒有問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved