|
The Hardware Composer HAL (“HWC”) was first introduced in Android 3.0 (“Honeycomb”) and has evolved steadily over the years. Its primary purpose is to determine the most efficient way to composite buffers with the available hardware. As a HAL, its implementation is device-specific and usually implemented by the display hardware OEM. HWC是从android 3.0版本引入的,在过去的数年中逐渐变得稳定。它的作用是使用现有的硬件选择最有效的方式来合成缓冲区。做为一个HAL层接口,它的内容是由显示硬件设备厂商来具体实现的。 The value of this approach is easy to recognize when you consider “overlay planes.” The purpose of overlay planes is to composite multiple buffers together, but in the display hardware rather than the GPU. For example, suppose you have a typical Android phone in portrait orientation, with the status bar on top and navigation bar at the bottom, and app content everywhere else. The contents for each layer are in separate buffers. You could handle composition by rendering the app content into a scratch buffer, then rendering the status bar over it, then rendering the navigation bar on top of that, and finally passing the scratch buffer to the display hardware. Or, you could pass all three buffers to the display hardware, and tell it to read data from different buffers for different parts of the screen. The latter approach can be significantly more efficient. 如果设想一下”overlay planes.”的场景,那么这个方法的价值是显而易见的。”overlay planes”的作用是在display hardware而不是GPU中同时混合不同的buffer。打比方说,典型场景下,屏幕上方的status bar,屏幕下方的navigation bar,以及应用本身的UI。每个layer都有自己独立的buffer。你可以通过逐步绘制每个layer到缓冲区里的方式来合成,最后将缓冲区的数据传递给显示硬件设备;或者,你也可以将每个layer数据分别传给显示硬件设备,然后告知显示硬件设备从不同的缓冲区中读取数据。显然后一种方法更有效率。 *As you might expect, the capabilities of different display processors vary significantly. The number of overlays, whether layers can be rotated or blended, and restrictions on positioning and overlap can be difficult to express through an API. So, the HWC works like this: - SurfaceFlinger provides the HWC with a full list of layers, and asks, “how do you want to handle this?”
- The HWC responds by marking each layer as “overlay” or “GLES composition.”
- SurfaceFlinger takes care of any GLES composition, passing the output buffer to HWC, and lets HWC handle the rest.*
如你所料,不同显示处理器之间的性能有巨大的差距。很多Overlay, layer被旋转或者混合,因此一个api很难准确表达在位置和遮盖上的限制。因此,HWC模块是这样运作的:
1.SurfaceFlinger给HWC提供一份完整的layer列表,然后问,“你打算如何处理?”
2.HWC将每个layer标记为overlay或者GLES composition然后回复给SurfaceFlinger
3.SurfaceFlinger来处理被标记为GLES composition的layer,将处理之后的数据传输给HWC,并且让HWC模块来处理剩下的工作。 Since the decision-making code can be custom tailored by the hardware vendor, it’s possible to get the best performance out of every device. 因为硬件厂商可以自己定制decision-making的代码,所以每台机器达到性能最优成为了可能。 Overlay planes may be less efficient than GL composition when nothing on the screen is changing. This is particularly true when the overlay contents have transparent pixels, and overlapping layers are being blended together. In such cases, the HWC can choose to request GLES composition for some or all layers and retain the composited buffer. If SurfaceFlinger comes back again asking to composite the same set of buffers, the HWC can just continue to show the previously-composited scratch buffer. This can improve the battery life of an idle device. 当屏幕上没有任何东西变化时,Overlay planes的效率并不如GL composition的效率高。当overlay的内容中有很多透明的像素,或者重叠的layer在一起被混合时,这种差距尤其明显。在这种情况下,HWC会请求让GLES composition来处理部分或者全部的layer,并且保留混合后的buffer。如果Surfaceflinger又来请求混合相同的buffer时,HWC会直接显示之前保存的混合好的buffer。这么做将可以提升设备待机时间。 Devices shipping with Android 4.4 (“KitKat”) typically support four overlay planes. Attempting to composite more layers than there are overlays will cause the system to use GLES composition for some of them; so the number of layers used by an application can have a measurable impact on power consumption and performance. 搭载了KK的android设备一般支持四条overlay planes。如果我们尝试混合更多的layer时,系统会使用GLES composition来处理其中的部分;所以一个应用使用了多少layer会影响到系统的功耗和性能。 你可以通过adb shell dumpsys SurfaceFlinger这个命令来查看Surfaceflinger具体使用了什么。这个命令的输出十分的长,其中和我们上面探讨的问题关连最深的是HWC的一段总结,这段一般在输出内容的底部: type | source crop | frame name------------+-----------------------------------+-------------------------------- HWC | [ 0.0, 0.0, 320.0, 240.0] | [ 48, 411, 1032, 1149] SurfaceView HWC | [ 0.0, 75.0, 1080.0, 1776.0] | [ 0, 75, 1080, 1776] com.android.grafika/com.android.grafika.PlayMovieSurfaceActivity HWC | [ 0.0, 0.0, 1080.0, 75.0] | [ 0, 0, 1080, 75] StatusBar HWC | [ 0.0, 0.0, 1080.0, 144.0] | [ 0, 1776, 1080, 1920] NavigationBar FB TARGET | [ 0.0, 0.0, 1080.0, 1920.0] | [ 0, 0, 1080, 1920] HWC_FRAMEBUFFER_TARGETThis tells you what layers are on screen, whether they’re being handled with overlays (“HWC”) or OpenGL ES composition (“GLES”), and gives you a bunch of other facts you probably won’t care about (“handle” and “hints” and “flags” and other stuff that we’ve trimmed out of the snippet above). The “source crop” and “frame” values will be examined more closely later on. 从这里我们可以看到那些显示在屏幕上的layer,是被overlays (“HWC”)处理,还是被OpenGL ES composition (“GLES”)处理,另外还有一些我们目前不太关注的别的属性(”handle” and “hints” and “flags”还有别的一些属性,我们没有粘帖在上面的输出中)。我们会在后面详细解释”source crop” and “frame”这两个值的含义。 The FB_TARGET layer is where GLES composition output goes. Since all layers shown above are using overlays, FB_TARGET isn’t being used for this frame. The layer’s name is indicative of its original role: On a device with/dev/graphics/fb0 and no overlays, all composition would be done with GLES, and the output would be written to the framebuffer. On recent devices there generally is no simple framebuffer, so the FB_TARGET layer is a scratch buffer. (Note: This is why screen grabbers written for old versions of Android no longer work: They’re trying to read from The Framebuffer, but there is no such thing.) FB_TARGET这个layer就是由GLES composition输出组成。这个layer上面的其余layer都是由overlay渲染而成,所以在这一帧里面,FB_TARGET并没有被使用。这个layer的名字表明了初始的角色:一个在/dev/graphics/fb0 的设备,所有的合成工作由GLES来完成,然后输入将会被写入framebuffer中。在当前的设备上并没有一个单纯的framebuffer,所有这个FB_TARGET layer 实际上是一个scratch buffer(这就是为什么在android早期版本上写的一些屏幕截图工具现在不能正常工作的原因:程序试图从framebuffer中读取数据,但是现在已经没有了framebuffer). The overlay planes have another important role: they’re the only way to display DRM content. DRM-protected buffers cannot be accessed by SurfaceFlinger or the GLES driver, which means that your video will disappear if HWC switches to GLES composition. overlay planes有另外一个重要的作用:这是显示DRM内容的唯一方法。受保护的DRM视频的buffer是无法被Surfaceflinger或者GLES来读取的,这意味着如果你使用GLES而不是HWC的话,你的视频将无法播放。
|