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

Android 性能優化入門:讀懂 Profile GPU Rendering 的三段柱狀圖

編輯:關於 Android 程式設計

開發者選項裡的 Profile GPU Rendering 是最直觀的渲染分析工具,它會在屏幕上以柱狀圖實時顯示每一幀的渲染耗時(毫秒)。

圖中綠色橫線代表 16ms——60Hz 屏幕下一幀的預算。所有柱子都壓在綠線下方,才算流暢。每根柱子由藍、紅、橙(部分版本顯示為黃)三段組成,分別對應渲染流程的三個階段,看懂它們就能定位瓶頸。

藍色:Draw,構建 display list

一個 View 在被真正渲染前,得先轉換成 GPU 能理解的格式。簡單的情況就是幾條 Draw 命令;複雜的情況可能要把 canvas 上的自定義路徑做三角化拆分。做完之後,系統會把結果緩存成 display list 對象。

藍色柱高度代表這一幀中所有需要更新的 View 完成上述過程的總耗時。它突然變高,通常意味著大量 View 被 invalidate,或者有幾個自定義 View 在 onDraw 裡寫了過於繁重的邏輯。

The blue section of that bar represents draw time, or rather how long it took to create and update your display lists in Java. Remember that before a view can actually be rendered, it has to first be transformed into a GPU-friendly format. On the simple side this could just be a few draw commands; on the complex end, we could be tessellating a custom path coming from your canvas object. Once done, the results are cached as a display list object by the system. When you see this bar shoot high, it could mean that a bunch of views suddenly became invalidated, or it may be a few custom views with extremely complex logic in their onDraw function.

紅色:Execute,執行 display list

Android 的 2D 渲染引擎需要通過 OpenGL ES API 把 display list 提交給 GPU,GPU 最終把像素寫到屏幕上。View 越複雜,OpenGL 需要執行的命令越多。紅色高,嫌疑人通常就是那些層級深、繪製內容繁重的自定義 View。

另外要注意紅色的尖峰:它往往來自重複提交同一批 View。這些 View 未必真的失效了,但只要發生諸如旋轉這類需要清理受影響區域的操作,下層 View 也會被順帶重畫一次。

The red section represents execute time — the time spent by Android's 2D renderer to execute display lists. In order to draw to the screen, Android interacts with the OpenGL ES API, which passes data along to the GPU. For more complex views, the more complex the commands needed for OpenGL to draw it. Large spikes in this bar can also come from re-submitting a load of views to be redrawn; these views haven't necessarily been invalidated, but if something happens, like a view rotates, we need to go back and clean up areas of the screen affected by that redraw.

橙色:Process,CPU 等待 GPU

這一段是 CPU 通知 GPU「這一幀我畫完了」的耗時。它是阻塞調用,CPU 會停下來等 GPU 確認收到命令。橙色偏高,說明 GPU 任務堆積太多,通常是複雜 View 產生了大量 OpenGL 命令所致。

The orange section represents process time — where the CPU tells the GPU that it's done rendering a frame. This is a blocking call, so the CPU sits around and waits for the GPU to acknowledge that it received the command. If this bar is getting large, you're doing a lot of work on the GPU, resulting from many complex views that require a lot of OpenGL rendering commands.

CPU 與 GPU 之間:圖形驅動與命令隊列

現代圖形 API 不允許 CPU 直接與 GPU 通信,中間隔著一層圖形驅動。

驅動內部維護一個隊列:CPU 把 display list 塞進隊列,GPU 從隊列另一頭取出並繪製。

這個模型解釋了很多現象:CPU 提交速度長期快過 GPU 消費速度,隊列就會堆積,最終表現為掉幀;而三段柱子的高度比例,能幫你判斷瓶頸究竟在 Java 層的繪製構建、在渲染命令執行,還是在 GPU 本身。

參考資料

Android Performance Patterns: Tool - Profile GPU Rendering
Perf Primer: CPU, GPU and your Android game

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