自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
影格放送速度
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android Frame Pacing 程式庫 (又稱為 Swappy) 是 Android Game SDK 的一部分。這個程式庫可讓 OpenGL 和 Vulkan 遊戲在 Android 裝置上流暢地顯示畫面,並修正影格同步情形。
影格同步是指遊戲的邏輯和算繪迴圈,與 OS 螢幕子系統和基礎顯示硬體的同步處理程序。Android 螢幕子系統經過特別設計,可避免某些視覺瑕疵 (例如撕裂) 情形。螢幕子系統會執行下列操作,避免撕裂現象:
- 在內部為已通過的影格預留緩衝區
- 偵測延遲提交的影格
- 偵測到影格延遲時,繼續顯示目前影格
影格顯示時間不一致,是因為遊戲的算繪迴圈以與原生顯示硬體支援的不同速率運作。如果遊戲的算繪迴圈執行速度過慢,導致顯示時間不一致,就會發生問題。舉例來說,如果以每秒 30 個影格數執行的遊戲試圖在原生支援每秒 60 個影格數的裝置上執行,遊戲的算繪迴圈就會導致重複影格在螢幕上保留額外 16 毫秒。這類不連結會導致影格時間出現明顯的不一致性,例如 33 毫秒、16 毫秒和 49 毫秒。過度複雜的場景會導致影格遭到遺漏,使得問題更加複雜。
Frame Pacing 程式庫會執行下列工作:
- 可補償因遊戲影格過短而導致的畫面延遲。
- 使用同步圍欄處理長影格,以免畫面卡頓或延遲。
- 如果裝置支援多種刷新率,則可選擇刷新率,以提供彈性和流暢的呈現效果。
- 使用影格統計資料提供用於偵錯和剖析的統計資料。
如要瞭解如何根據需求,將程式庫設為在不同模式中執行,請參閱「支援的作業模式」。
如要使用 OpenGL 或 Vulkan 轉譯器實作,請參閱:
詳情請參閱「Frame Pacing 程式庫」。
每秒影格數節流介入措施
影格率 (FPS) 節流介入措施可讓遊戲以適當的 FPS 執行,只需進行平台端變更,開發人員不必採取任何行動。
FPS 節流介入措施的實作方式會使用下列元件。
GameManagerService
GameManagerService 元件會維護遊戲模式和遊戲介入的所有使用者和遊戲資訊。FPS 資訊會與其他介入資訊 (例如解析度縮放係數) 一併儲存在 GameManagerService 中,並在每個使用者設定檔的 <PACKAGE_NAME, Interventions>
對應項目中。當遊戲模式變更或介入措施更新時,系統會存取 FPS 資訊。UID
是每個 PACKAGE_NAME
和使用者的專屬值,且可進一步轉譯為 <UID, Frame Rate>
組合,以便傳送至 SurfaceFlinger。
SurfaceFlinger
只要影格速率是螢幕重新整理頻率的除數,SurfaceFlinger 元件就已支援限制應用程式的 FPS。在 VSync 發生的情況下,SurfaceFlinger 會檢查受限應用程式的 VSync 有效性,方法是確認 VSync 時間戳記是否與應用程式的影格速率同步。如果影格速率與 VSync 不同步,SurfaceFlinger 會保留影格,直到影格速率和 VSync 同步為止。
下圖說明 GameManagerService 與 SurfaceFlinger 之間的互動:
圖 1. GameServiceManager 與 SurfaceFlinger 之間的互動。
SurfaceFinger 會維護 <UID, Frame Rate>
組對應,以便設定新的影格速率節流優先順序。UID
在使用者和遊戲之間是獨一無二的,因此單一裝置上的每位使用者都可以為同一款遊戲設定不同的影格速率。為了限制遊戲的影格速率,GameServiceManager 會呼叫 SurfaceFlinger,藉此覆寫 UID 的影格速率。透過這項機制,SurfaceFlinger 會在遊戲模式變更或介入措施更新時更新對應項目。SurfaceFlinger 會根據緩衝區鎖定處理 FPS 變更。
如要進一步瞭解 FPS 節流,請參閱「
FPS 節流」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Frame pacing\n\nThe Android Frame Pacing library, also known as Swappy, is part of the [Android Game SDK](https://android.googlesource.com/platform/frameworks/opt/gamesdk/). It helps [OpenGL](/docs/core/graphics/arch-egl-opengl) and [Vulkan](/docs/core/graphics/arch-vulkan) games achieve smooth rendering\nand correct frame pacing on Android.\n\nFrame pacing is the synchronization of a game's logic and rendering loop with\nan OS's display subsystem and underlying display hardware. The Android display\nsubsystem was designed to avoid certain visual artifacts, such as tearing. The\ndisplay subsystem avoids tearing by doing the following:\n\n- Buffering past frames internally\n- Detecting late frame submissions\n- Continuing to display the current frame when a late frame is detected\n\nInconsistent frame display times are caused by a game's render loop running at\na different rate than what the native display hardware supports. Problems arise\nwhen a game's render loop runs too slowly for the underlying display hardware,\nleading to inconsistent display times. For example, when a game running at\n30 fps attempts to render on a device that natively supports 60 fps,\nthe game's render loop causes a repeated frame to remain on the screen for an\nextra 16 ms. This type of disconnect creates substantial inconsistencies in frame times\nsuch as 33 ms, 16 ms, and 49 ms. Overly complex scenes further compound\nthis problem because they cause missed frames to occur.\n\nThe Frame Pacing library performs these tasks:\n\n- Compensates for stuttering due to short game frames.\n - Adds presentation timestamps so that frames are presented on time, not early.\n - Uses presentation timestamp extensions [`EGL_ANDROID_presentation_time`](https://www.khronos.org/registry/EGL/extensions/ANDROID/EGL_ANDROID_presentation_time.txt) and [`VK_GOOGLE_display_timing`](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VK_GOOGLE_display_timing.html).\n- Uses sync fences for long frames that lead to stuttering and latency.\n - Injects waits into the app. These allow the display pipeline to catch up, rather than allowing back pressure to build up.\n - Uses sync fences ([`EGL_KHR_fence_sync`](https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_fence_sync.txt) and [`VkFence`](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkFence.html)).\n- Chooses a refresh rate to provide flexibility and a smooth presentation, if your device supports multiple refresh rates.\n- Provides stats for debugging and profiling using [frame\n stats](https://developer.android.com/games/sdk/frame-pacing/#frame_stats).\n\nTo learn how to configure the library to operate in different modes according\nto what you need, see [Supported operating modes](https://developer.android.com/games/sdk/frame-pacing/#supported_operating_modes).\n\nTo implement using OpenGL renderer or Vulkan renderer, see:\n\n- [Integrate Android Frame Pacing into your OpenGL renderer](https://developer.android.com//games/sdk/frame-pacing/opengl/)\n- [Integrate Android Frame Pacing into your Vulkan renderer](https://developer.android.com/games/sdk/frame-pacing/vulkan/)\n\nTo read more, see [Frame Pacing library](https://developer.android.com/games/sdk/frame-pacing/).\n\nFrames per second throttling intervention\n-----------------------------------------\n\nFrames per second (FPS) throttling intervention enables games to pace at an appropriate FPS\nusing only platform side\nchanges and without requiring any action on the developers' part.\n\nThe implementation of the FPS throttling intervention uses the following components.\n\n#### GameManagerService\n\nThe [GameManagerService](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/app/GameManagerService.java) component maintains all per-user and\nper-game information of game mode and game intervention. The FPS information is stored in the\nGameManagerService with other intervention information, such as the resolution downscaling factor,\nin a `\u003cPACKAGE_NAME, Interventions\u003e` mapping for each user profile.\nThe FPS information is accessed when the game mode is changed or the intervention is updated. A\n`UID` is unique to each `PACKAGE_NAME` and user, and can further be translated\ninto a `\u003cUID, Frame Rate\u003e` pair to send to the SurfaceFlinger.\n\n#### SurfaceFlinger\n\n[SurfaceFlinger](/docs/core/graphics/surfaceflinger-windowmanager) component already supports\nthrottling the FPS of an application as long as the frame rate is a divisor of the display refresh rate.\nIn the event of a VSync, the SurfaceFlinger checks the validity of the VSync for the throttled\napplication by verifying whether the VSync timestamp is in phase with the frame rate of the\napplication. If the frame rate is not in phase with the VSync, then the SurfaceFlinger holds the\nframe until the frame rate and VSync are in phase.\n\nThe following figure describes the interaction between the GameManagerService and the SurfaceFlinger:\n\n**Figure 1.** Interaction between the GameServiceManager and SurfaceFlinger.\n\nThe SurfaceFinger maintains a `\u003cUID, Frame Rate\u003e` pair mapping to set a new\nframe rate throttling priority. The `UID` is unique between users and games, so that each\nuser on a single device can have different settings of frame rate on the same game. To throttle the\nframe rate of a game, the GameServiceManager calls the SurfaceFlinger to override the frame rate for\na UID. With this mechanism, the SurfaceFlinger updates the mapping whenever\nthe game mode is changed or the intervention is updated. The SurfaceFlinger handles the FPS change\nby latching buffers accordingly.\n\nTo understand more about FPS throttling, see\n[FPS throttling](https://developer.android.com/games/optimize/adpf/gamemode/fps-throttling)."]]