ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
SurfaceFlinger และ WindowManager
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
SurfaceFlinger จะยอมรับบัฟเฟอร์ คอมโพสิทบัฟเฟอร์ และส่งบัฟเฟอร์ไปยังจอแสดงผล WindowManager
มอบบัฟเฟอร์และข้อมูลเมตาของหน้าต่างให้กับ SurfaceFlinger ซึ่ง SurfaceFlinger จะใช้เพื่อคอมโพสิทพื้นผิวไปยังจอแสดงผล
SurfaceFlinger
SurfaceFlinger ยอมรับบัฟเฟอร์ได้ 2 วิธี ได้แก่ ผ่าน BufferQueue และ SurfaceControl
หรือผ่าน ASurfaceControl
วิธีที่ SurfaceFlinger ยอมรับบัฟเฟอร์คือผ่าน BufferQueue และ SurfaceControl
เมื่อแอปปรากฏขึ้นที่เบื้องหน้า แอปจะขอบัฟเฟอร์จาก WindowManager
WindowManager
จากนั้นจะขอเลเยอร์จาก SurfaceFlinger เลเยอร์คือชุดค่าผสมของ surface ซึ่งมี BufferQueue และอินสแตนซ์ SurfaceControl
ซึ่งมีข้อมูลเมตาของเลเยอร์ เช่น เฟรมการแสดงผล
SurfaceFlinger จะสร้างเลเยอร์และส่งไปยัง WindowManager
WindowManager
จากนั้นส่ง Surface ไปยังแอป แต่เก็บอินสแตนซ์ SurfaceControl
ไว้เพื่อควบคุมลักษณะที่ปรากฏของแอปบนหน้าจอ
Android 10 เพิ่ม ASurfaceControl
ซึ่งเป็นอีกวิธีหนึ่งที่ SurfaceFlinger ยอมรับบัฟเฟอร์ ASurfaceControl
รวมอินสแตนซ์ SurfaceControl
และอินสแตนซ์ Surface เข้าเป็นแพ็กเกจธุรกรรมเดียวที่ส่งไปยัง SurfaceFlinger ASurfaceControl
เชื่อมโยงกับเลเยอร์ที่แอปอัปเดตผ่านอินสแตนซ์ ASurfaceTransaction
จากนั้นแอปจะได้รับข้อมูลเกี่ยวกับASurfaceTransaction
อินสแตนซ์ผ่านคอลแบ็กที่ส่ง ASurfaceTransactionStats
ที่มีข้อมูล เช่น เวลาแตะ เวลารับ และอื่นๆ
ตารางต่อไปนี้มีรายละเอียดเพิ่มเติมเกี่ยวกับ ASurfaceControl
และคอมโพเนนต์ที่เกี่ยวข้อง
ส่วนประกอบ |
คำอธิบาย |
ASurfaceControl |
ตัด SurfaceControl และช่วยให้แอปสร้างอินสแตนซ์ SurfaceControl ที่สอดคล้องกับเลเยอร์บนจอแสดงผลได้
สร้างเป็นอินสแตนซ์ย่อยของ ANativeWindow หรือเป็นอินสแตนซ์ย่อยของอินสแตนซ์ ASurfaceControl อื่นได้ |
ASurfaceTransaction |
ตัด Transaction เพื่อช่วยให้ไคลเอ็นต์แก้ไขพร็อพเพอร์ตี้ที่อธิบายของเลเยอร์ได้ เช่น เรขาคณิต และส่งบัฟเฟอร์ที่อัปเดตแล้วไปยัง SurfaceFlinger |
ASurfaceTransactionStats
| ส่งข้อมูลเกี่ยวกับธุรกรรมที่แสดง เช่น เวลาแตะ เวลารับ และรั้วการปล่อยก่อนหน้า ไปยังแอปผ่านการเรียกกลับที่ลงทะเบียนไว้ล่วงหน้า |
แม้ว่าแอปจะส่งบัฟเฟอร์ได้ทุกเมื่อ แต่ SurfaceFlinger จะตื่นขึ้นมาเพื่อรับบัฟเฟอร์ระหว่างการรีเฟรชจอแสดงผลเท่านั้น ซึ่งอาจแตกต่างกันไปตามอุปกรณ์ วิธีนี้ช่วยลดการใช้หน่วยความจำและหลีกเลี่ยงการฉีกขาดที่มองเห็นได้บนหน้าจอ ซึ่งอาจเกิดขึ้นเมื่ออัปเดตการแสดงผลระหว่างการรีเฟรช
เมื่อจอแสดงผลอยู่ระหว่างการรีเฟรช จอแสดงผลจะส่งสัญญาณ VSync ไปยัง SurfaceFlinger สัญญาณ VSync บ่งบอกว่าจอแสดงผลจะรีเฟรชได้โดยไม่ฉีกขาด เมื่อ SurfaceFlinger ได้รับสัญญาณ VSync ก็จะเรียกดูรายการเลเยอร์เพื่อหาบัฟเฟอร์ใหม่ หาก SurfaceFlinger พบบัฟเฟอร์ใหม่ ก็จะรับบัฟเฟอร์นั้น หากไม่พบ ก็จะใช้บัฟเฟอร์ที่รับไว้ก่อนหน้านี้ต่อไป SurfaceFlinger ต้องแสดงข้อมูลบางอย่างเสมอ จึงจะยึดบัฟเฟอร์ไว้ 1 รายการ หากไม่เคยส่งบัฟเฟอร์ในเลเยอร์ ระบบจะไม่สนใจเลเยอร์นั้น
หลังจาก SurfaceFlinger รวบรวมบัฟเฟอร์ทั้งหมดสําหรับเลเยอร์ที่มองเห็นแล้ว ก็จะถามเครื่องมือคอมโพสิตฮาร์ดแวร์ (HWC) ว่าควรใช้การคอมโพสอย่างไร หาก HWC ระบุว่าประเภทการคอมโพสิชันเลเยอร์เป็นการคอมโพสิชันไคลเอ็นต์ SurfaceFlinger จะคอมโพสิชันเลเยอร์เหล่านั้น จากนั้น SurfaceFlinger จะส่งบัฟเฟอร์เอาต์พุตไปยัง HWC
WindowManager
WindowManager
ควบคุมออบเจ็กต์ Window
ซึ่งเป็นคอนเทนเนอร์สำหรับออบเจ็กต์ View
ออบเจ็กต์ Window
จะสำรองข้อมูลโดยออบเจ็กต์ Surface
เสมอ
WindowManager
ดูแลวงจร อินพุต และโฟกัส เหตุการณ์ การวางแนวหน้าจอ ทรานซิชัน ภาพเคลื่อนไหว ตำแหน่ง การเปลี่ยนรูปแบบ ลําดับ z และอีกหลายแง่มุมของหน้าต่าง WindowManager
จะส่งข้อมูลเมตาของหน้าต่างทั้งหมดไปยัง SurfaceFlinger เพื่อให้ SurfaceFlinger ใช้ข้อมูลดังกล่าวในการคอมโพสิทพื้นผิวบนจอแสดงผลได้
ก่อนการหมุนเวียน
การวางซ้อนฮาร์ดแวร์จำนวนมากไม่รองรับการหมุน (และแม้ว่าจะรองรับ ก็ยังต้องใช้พลังงานในการประมวลผล) ทางออกคือการเปลี่ยนรูปแบบบัฟเฟอร์ก่อนที่จะไปถึง SurfaceFlinger Android รองรับคำแนะนำการค้นหา (NATIVE_WINDOW_TRANSFORM_HINT
) ใน ANativeWindow
เพื่อแสดงการเปลี่ยนรูปแบบที่มีแนวโน้มมากที่สุดที่จะใช้กับบัฟเฟอร์โดย SurfaceFlinger ไดรเวอร์ GL สามารถใช้คำแนะนำนี้เพื่อเปลี่ยนรูปแบบบัฟเฟอร์ล่วงหน้าก่อนที่จะส่งไปยัง SurfaceFlinger เพื่อให้บัฟเฟอร์ได้รับการเปลี่ยนรูปแบบอย่างถูกต้องเมื่อมาถึง
เช่น เมื่อได้รับคำแนะนำให้หมุน 90 องศา ให้สร้างและใช้เมทริกซ์กับบัฟเฟอร์เพื่อป้องกันไม่ให้แสดงที่ส่วนท้ายของหน้า โปรดดำเนินการนี้ก่อนการโรเตชันเพื่อประหยัดพลังงาน โปรดดูรายละเอียดที่อินเทอร์เฟซ ANativeWindow
ที่กําหนดไว้ใน system/core/include/system/window.h
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],null,["# SurfaceFlinger and WindowManager\n\nSurfaceFlinger accepts buffers, composes buffers, and sends buffers to the\ndisplay. `WindowManager` provides SurfaceFlinger with buffers and window\nmetadata, which SurfaceFlinger uses to composite surfaces to the display.\n\nSurfaceFlinger\n--------------\n\nSurfaceFlinger can accept buffers in two ways: through BufferQueue and\n`SurfaceControl`, or through `ASurfaceControl`.\n\nOne way SurfaceFlinger accepts buffers is through BufferQueue and\n`SurfaceControl`. When an app comes to the foreground, it requests buffers from\n[`WindowManager`](https://developer.android.com/reference/android/view/WindowManager.html). `WindowManager` then requests a layer from\nSurfaceFlinger. A layer is a combination of a [surface](/docs/core/graphics/arch-sh), which contains the BufferQueue, and\na [`SurfaceControl`](https://developer.android.com/reference/android/view/SurfaceControl) instance,\nwhich contains the layer metadata like the display frame.\nSurfaceFlinger creates the layer and sends it to `WindowManager`. `WindowManager`\nthen sends the surface to the app, but keeps the `SurfaceControl` instance to\nmanipulate the appearance of the app on the screen.\n\nAndroid 10 adds `ASurfaceControl`, which is another\nway that SurfaceFlinger can accept buffers. `ASurfaceControl` combines a surface\nand a `SurfaceControl` instance into one transaction package that is sent to\nSurfaceFlinger. `ASurfaceControl` is associated with a layer, which apps\nupdate through `ASurfaceTransaction` instances. Apps then get information about\n`ASurfaceTransaction` instances through callbacks that pass `ASurfaceTransactionStats`\ncontaining information, such as latch time, acquire times, and so on.\n\nThe following table includes more details about `ASurfaceControl` and its\nassociated components:\n\n| Component | Description |\n|----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ASurfaceControl` | Wraps `SurfaceControl` and enables an app to create `SurfaceControl` instances that correspond to layers on the display. Can be created as a child of `ANativeWindow` or as a child of another `ASurfaceControl` instance. |\n| `ASurfaceTransaction` | Wraps `Transaction` to enable the client to edit a layer's descriptive properties, such as geometry, and sends the updated buffers to SurfaceFlinger. |\n| `ASurfaceTransactionStats` | Sends information about transactions that have been presented, such as latch time, acquire times, and previous release fence, to an app through a preregistered callback. |\n\nThough apps can submit buffers at any time, SurfaceFlinger only wakes up to\naccept buffers between display refreshes, which can differ depending on the\ndevice. This minimizes memory usage and avoids visible tearing on the\nscreen, which can occur when updating the display mid-refresh.\n\nWhen the display is between refreshes, the display sends the VSync\nsignal to SurfaceFlinger. The VSync signal indicates that the display can be\nrefreshed without tearing. When SurfaceFlinger receives the VSync signal, SurfaceFlinger\nwalks through its list of layers looking for new buffers. If SurfaceFlinger finds a\nnew buffer, SurfaceFlinger acquires the buffer; if not, SurfaceFlinger continues\nto use the previously acquired buffer. SurfaceFlinger must always display something,\nso it hangs on to one buffer. If no buffers have ever been submitted on a\nlayer, the layer is ignored.\n\nAfter SurfaceFlinger has collected all buffers for visible layers, it asks\nthe Hardware Composer (HWC) how composition should be performed. If the HWC\nmarks layer composition type as client composition, SurfaceFlinger composites those\nlayers. Then, SurfaceFlinger passes the output buffer to the [HWC](/docs/core/graphics/implement-hwc).\n\nWindowManager\n-------------\n\n`WindowManager` controls [`Window`](https://developer.android.com/reference/android/view/Window) objects,\nwhich are containers for [`View`](https://developer.android.com/reference/android/view/View)\nobjects. `Window` objects are always backed by\n[`Surface`](https://developer.android.com/reference/android/view/Surface) objects.\n`WindowManager` oversees lifecycles, input and focus\nevents, screen orientation, transitions, animations, position, transforms,\nz-order, and many other aspects of a window. `WindowManager` sends all of the\nwindow metadata to SurfaceFlinger so SurfaceFlinger can use that data to\ncomposite surfaces on the display.\n\n### Pre-rotation\n\nMany hardware overlays don't support rotation (and even if they do, it costs\nprocessing power); the solution is to transform the buffer before it reaches\nSurfaceFlinger. Android supports a query hint\n(`NATIVE_WINDOW_TRANSFORM_HINT`) in `ANativeWindow` to\nrepresent the most likely transform to be applied to the buffer by\nSurfaceFlinger. GL drivers can use this hint to pre-transform the buffer\nbefore it reaches SurfaceFlinger so that when the buffer arrives, it's correctly\ntransformed.\n\nFor example, when receiving a hint to rotate 90 degrees, generate and apply a\nmatrix to the buffer to prevent it from running off the end of the page. To save\npower, do this pre-rotation. For details, see the `ANativeWindow`\ninterface defined in `system/core/include/system/window.h`."]]