Android 12(S) 图像显示系统 - 示例应用(二)

1 前言

为了更深刻的理解Android图形系统抽象的概念和BufferQueue的工作机制,这篇文章我们将从Native Level入手,基于Android图形系统API写作一个简单的图形处理小程序。透过这个小程序我们将学习如何使用Native API创建Surface,如何请求图形缓冲区,如何向图形缓冲区中写入数据等知识。Talk is cheap, show me the code。让我们马上开始吧!

注:本系列文章的分析及代码均基于Android 12(S) Source Code,可参考:http://aospxref.com/  或 http://aosp.opersys.com/

2 程序源码简介

  • 源码下载

地址:https://github.com/yrzroger/NativeSFDemo

注:可以git命令下载(比如git clone git@github.com:yrzroger/NativeSFDemo.git)或直接Download ZIP解压后使用

  • 源码编译

本demo程序是基于Android源码编译环境开发的,所以需要放到Android源码目录下编译。

将上一步中下载的的源码放到Android源码的合适目录下,然后执行mm进行编译,得到可执行档 NativeSFDemo

  • 源码运行

将可执行档NativeSFDemo放到目标测试平台/system/bin下(比如:adb push NativeSFDemo /system/bin/)

然后执行 adb shell NativeSFDemo

  • 效果展示

程序中去绘制单色背景: 红色->绿色->蓝色背景交替展示,如下图所示:

至此你已经收获一个可以供后续学习研究的demo小程序了 !!!


Tips:

Android源码是一个宝藏,即提供了丰富多彩的APIs供开发者使用,又可以在其中搜索到很多有价值的APIs使用实例。本文中提供的演示Demo亦是基于源码中的参考来完成的。

我把参考位置列于此:

参考1:/frameworks/av/media/libstagefright/SurfaceUtils.cpp 

参考2:/frameworks/native/libs/gui/tests/CpuConsumer_test.cpp


 

3 程序源码分析


  关注公众号,《图形图像》专栏 ,阅读系列文章!!


 

 

 


针对上述方法中会产生 bbq-wrapper#0” 和 “NativeSFDemo#0”两个Layer,我们提供另一种方法,去显式的创建BLASTBufferQueue,进而获取surface和producer来达成demo的功能

可以将代码的分支切换到 main_bbq 编译后来测试, 此时去dumpsys SurfaceFlinger, 如下,其中 NativeSFDemo#0BackgroundColorLayer 是我们设置的一个黑色背景

Display 4629995328241972480 HWC layers:
---------------------------------------------------------------------------------------------------------------------------------------------------------------
 Layer name
           Z |  Window Type |  Comp Type |  Transform |   Disp Frame (LTRB) |          Source Crop (LTRB) |     Frame Rate (Explicit) (Seamlessness) [Focused]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
 NativeSFDemo#0BackgroundColorLayer
  rel -2147483648 |            0 |     CLIENT |          0 |    0    0 1920 1080 |    0.0    0.0    0.0    0.0 |                                              [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
 NativeSFDemo#0
  2147483647 |            0 |     CLIENT |          0 |    0    0 1920 1080 |    0.0    0.0 1920.0 1080.0 |                                              [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------

两个BufferStateLayer:BufferStateLayer (NativeSFDemo#0)  和  BufferStateLayer (NativeSFDemo#0BackgroundColorLayer ),其中NativeSFDemo#0BackgroundColorLayer 的parent就是NativeSFDemo#0

+ EffectLayer (NativeSFDemo#0BackgroundColorLayer) uid=1000
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=1)
    [  0,   0, 1920, 1080]
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=-2147483648, pos=(0,0), size=(  -1,  -1), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=1, invalidate=0, dataspace=Default, defaultPixelFormat=Unknown/None, backgroundBlurRadius=0, color=(0.000,0.000,0.000,1.000), flags=0x00000000, tr=[0.00, 0.00][0.00, 0.00]
      parent=NativeSFDemo#0
      zOrderRelativeOf=none
      activeBuffer=[   0x   0:   0,Unknown/None], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0, mRefreshPending=0, metadata={}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 
+ BufferStateLayer (NativeSFDemo#0) uid=0
  Region TransparentRegion (this=0 count=0)
  Region VisibleRegion (this=0 count=1)
    [  0,   0, 1920, 1080]
  Region SurfaceDamageRegion (this=0 count=0)
      layerStack=   0, z=2147483647, pos=(0,0), size=(1920,1080), crop=[  0,   0,  -1,  -1], cornerRadius=0.000000, isProtected=0, isTrustedOverlay=0, isOpaque=0, invalidate=0, dataspace=Default, defaultPixelFormat=RGBA_8888, backgroundBlurRadius=0, color=(0.000,0.000,0.000,1.000), flags=0x00000100, tr=[0.00, 0.00][0.00, 0.00]
      parent=none
      zOrderRelativeOf=none
      activeBuffer=[1920x1080:1920,RGBA_8888], tr=[0.00, 0.00][0.00, 0.00] queued-frames=0, mRefreshPending=0, metadata={dequeueTime:5541992685473}, cornerRadiusCrop=[0.00, 0.00, 0.00, 0.00],  shadowRadius=0.000, 

 

4 小结

至此,我们已经建立起来了一个简单的图形图像处理的简单Demo,当让我们目前还是只从应用的较多介绍了基本图形APIs的使用逻辑,接下来的我们就基于此demo,深入底层逻辑探究其中的奥秘。

 

 



必读:

Android 12(S) 图像显示系统 - 开篇



 

 

posted on 2022-01-18 09:45  二的次方  阅读(12678)  评论(16编辑  收藏  举报