Introduction
SurfaceFlinger is a system service in Android that manages the display and composition of all graphical output on the device. It is responsible for creating and managing windows, rendering graphics, and compositing them onto the display.
SurfaceFlinger is a key component of the Android graphics stack. It sits between the application layer and the hardware layer, and it provides a unified interface for applications to interact with the display.
SurfaceFlinger uses a layered rendering architecture, which allows it to efficiently composite graphics from multiple sources. The layers are arranged in a stack, and each layer can be rendered independently of the others. This allows SurfaceFlinger to optimize the rendering process for each layer, and it also allows applications to have fine-grained control over the rendering of their graphics.
SurfaceFlinger also supports a variety of display features, such as multiple windows, window resizing, and transparency. It also supports a variety of graphics APIs, such as OpenGL ES and Vulkan.
SurfaceFlinger is a complex system service, but it is essential for the smooth operation of the Android graphics stack. It provides a unified interface for applications to interact with the display, and it allows them to take advantage of a variety of display features.
Here are some of the key features of SurfaceFlinger:
- Manages the display and composition of all graphical output on the device.
- Creates and manages windows.
- Renders graphics.
- Composites them onto the display.
- Uses a layered rendering architecture.
- Supports a variety of display features.
- Supports a variety of graphics APIs.
SurfaceFlinger is a critical component of the Android graphics stack, and it plays an important role in ensuring that the user experience is smooth and consistent.
Class
Here are some of the important internal classes of SurfaceFlinger:
- SurfaceComposerClient: This class provides an interface for applications to interact with SurfaceFlinger. It allows applications to create and manage windows, and to render graphics.
- Surface: This class represents a graphical object that can be rendered onto the display. A Surface can be used to render a variety of graphics, including images, text, and video.
- Layer: This class represents a layer in the SurfaceFlinger stack. A Layer can contain a Surface, and it can be used to control the rendering of the Surface.
- Transaction: This class represents a change to the SurfaceFlinger state. A Transaction is used to update the state of SurfaceFlinger, such as the position or size of a window.
- Display: This class represents a physical display. A Display can contain multiple windows, and it is responsible for rendering the windows onto the display.
These are just a few of the important internal classes of SurfaceFlinger. There are many other classes that are used to implement the various features of SurfaceFlinger.
Interact
SurfaceFlinger interacts with apps through the SurfaceComposerClient class. The SurfaceComposerClient class provides an interface for apps to create and manage windows, and to render graphics.
To create a window, an app calls the createWindow
method of the SurfaceComposerClient class. The createWindow
method takes a number of parameters, including the size and position of the window, and the type of graphics that will be rendered in the window.
Once a window has been created, the app can render graphics into the window by calling the draw
method of the SurfaceComposerClient class. The draw
method takes a Surface object, which represents the graphical object that will be rendered into the window.
SurfaceFlinger then renders the windows onto the display. The order in which the windows are rendered is determined by the Z-order of the windows. The window with the highest Z-order will be rendered on top of the windows with lower Z-orders.
SurfaceFlinger also supports a variety of display features, such as multiple windows, window resizing, and transparency. These features are implemented by the SurfaceComposerClient class.
Here are some of the ways in which SurfaceFlinger interacts with apps:
- When an app creates a window, it calls the
createWindow
method of the SurfaceComposerClient class. - When an app renders graphics into a window, it calls the
draw
method of the SurfaceComposerClient class. - SurfaceFlinger renders the windows onto the display in the order specified by the Z-order of the windows.
- SurfaceFlinger supports a variety of display features, such as multiple windows, window resizing, and transparency.
Dump
There are a few ways to dump Android surface drawn content. One way is to use the dumpsys SurfaceFlinger
command. This command will dump a variety of information about SurfaceFlinger, including the drawn content of all the windows.
To use the dumpsys SurfaceFlinger
command, open a terminal window and type the following command:
dumpsys SurfaceFlinger
The output of the command will be a lot of text, so you may want to pipe it to a file or redirect it to a text editor.
Another way to dump Android surface drawn content is to use the adb shell screencap
command. This command will take a screenshot of the current display and save it to a file.
To use the adb shell screencap
command, open a terminal window and type the following command:
adb shell screencap /sdcard/screencap.png
This will create a file called screencap.png
in the /sdcard
directory.
Finally, you can also use a third-party app to dump Android surface drawn content. There are a number of apps available, such as Screenshot and Screen Recorder
and Dumpster
.
These apps allow you to take screenshots and record the screen, and they also allow you to dump the drawn content of the windows.
Code
Here are some of the important subdirectories of the SurfaceFlinger source code:
- include: This directory contains the header files for the SurfaceFlinger classes.
- services: This directory contains the implementation of the SurfaceFlinger classes.
- tests: This directory contains the unit tests for the SurfaceFlinger classes.
- binder: This directory contains the implementation of the Binder IPC mechanism, which is used by SurfaceFlinger to communicate with other system services.
- graphics: This directory contains the implementation of the graphics APIs, such as OpenGL ES and Vulkan.
- ui: This directory contains the implementation of the user interface, such as the window manager and the status bar.
- utils: This directory contains utility classes, such as the logging class and the thread pool class.
These are just a few of the important subdirectories of the SurfaceFlinger source code. There are many other subdirectories that are used to implement the various features of SurfaceFlinger.
Here is a brief description of each subdirectory:
- include: This directory contains the header files for the SurfaceFlinger classes. These header files define the interfaces for the SurfaceFlinger classes, and they are used by other system services to interact with SurfaceFlinger.
- services: This directory contains the implementation of the SurfaceFlinger classes. These classes are responsible for managing the display, rendering graphics, and compositing them onto the display.
- tests: This directory contains the unit tests for the SurfaceFlinger classes. These unit tests are used to verify the correctness of the SurfaceFlinger code.
- binder: This directory contains the implementation of the Binder IPC mechanism. The Binder IPC mechanism is used by SurfaceFlinger to communicate with other system services, such as the window manager and the audio system.
- graphics: This directory contains the implementation of the graphics APIs. The graphics APIs are used by SurfaceFlinger to render graphics onto the display.
- ui: This directory contains the implementation of the user interface. The user interface includes the window manager, the status bar, and other user interface elements.
- utils: This directory contains utility classes. These utility classes are used by other system services, such as the logging class and the thread pool class.