Android GUI系统学习1:Gralloc

移动开发
Gralloc模块是从Android Eclair(android 2.1)开始加入的一个HAL模块,Gralloc的含义为是Graphics Alloc(图形分配)。他对上为libui提供服务,为其分配显存,刷新显示等。对下对framebuffer进行管理。

Gralloc模块是从Android Eclair(android 2.1)开始加入的一个HAL模块,Gralloc的含义为是Graphics Alloc(图形分配)。他对上为libui提供服务,为其分配显存,刷新显示等。对下对framebuffer进行管理。

gralloc代码通常位于hardware/libhardware/modules/gralloc目录下。包含以下几个文件:

Android.mk  framebuffer.cpp  gralloc.cpp  gralloc_priv.h  gr.h  mapper.cpp

另外,与其相关的头文件位于hardware/libhardware/include/hardware,涉及fb.h和gralloc.h。

下面从gralloc的调用开始学习gralloc的代码。代码基于android4.4。

gralloc的调用是从FramebufferNativeWindow.cpp的构造函数开始的。FramebufferNativeWindow实现FrameBuffer的管理,它主要被SurfaceFlinger使用,也可以被OpenGL Native程序使用。在本质上,它在Framebuffer之上实现了一个ANativeWindow,目前它只管理两个buffers:front and back buffer。

如下所示(FramebufferNativeWindow.cpp):

  1. FramebufferNativeWindow::FramebufferNativeWindow() 
  2.  : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false
  3.  { 
  4.  hw_module_t const* module; 
  5.  if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) { 
  6.  int stride; 
  7.  int err; 
  8.  int i; 
  9.  err = framebuffer_open(module, &fbDev); 
  10.  ALOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err)); 
  11.   
  12. err = gralloc_open(module, &grDev); 
  13.  ALOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err)); 
  14.   
  15. // bail out if we can't initialize the modules 
  16.  if (!fbDev || !grDev) 
  17.  return
  18.   
  19. mUpdateOnDemand = (fbDev->setUpdateRect != 0); 
  20.   
  21. // initialize the buffer FIFO 
  22.  if(fbDev->numFramebuffers >= MIN_NUM_FRAME_BUFFERS && 
  23.  fbDev->numFramebuffers <= MAX_NUM_FRAME_BUFFERS){ 
  24. mNumBuffers = fbDev->numFramebuffers; 
  25.  } else { 
  26.  mNumBuffers = MIN_NUM_FRAME_BUFFERS; 
  27.  } 
  28.  mNumFreeBuffers = mNumBuffers; 
  29.  mBufferHead = mNumBuffers-1
  30.   
  31. /* 
  32.  * This does not actually change the framebuffer format. It merely 
  33.  * fakes this format to surfaceflinger so that when it creates 
  34.  * framebuffer surfaces it will use this format. It's really a giant 
  35.  * HACK to allow interworking with buggy gralloc+GPU driver 
  36.  * implementations. You should *NEVER* need to set this for shipping 
  37.  * devices. 
  38.  */ 
  39.  #ifdef FRAMEBUFFER_FORCE_FORMAT 
  40.  *((uint32_t *)&fbDev->format) = FRAMEBUFFER_FORCE_FORMAT; 
  41.  #endif 
  42.   
  43. for (i = 0; i < mNumBuffers; i++) { buffers[i] = new NativeBuffer
  44. ( fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB); 
  45.  } 
  46.   
  47. for (i = 0; i < mNumBuffers; i++) { err = grDev->alloc(grDev, 
  48.  fbDev->width, fbDev->height, fbDev->format, 
  49.  GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride); 
  50.   
  51. ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s"
  52.  i, fbDev->width, fbDev->height, strerror(-err)); 
  53.   
  54. if (err) 
  55.  { 
  56.  mNumBuffers = i; 
  57.  mNumFreeBuffers = i; 
  58.  mBufferHead = mNumBuffers-1
  59.  break
  60.  } 
  61.  } 
  62.   
  63. const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags; 
  64.  const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi; 
  65.  const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi; 
  66.  const_cast<int&>(ANativeWindow::minSwapInterval) = 
  67.  fbDev->minSwapInterval; 
  68.  const_cast<int&>(ANativeWindow::maxSwapInterval) = 
  69.  fbDev->maxSwapInterval; 
  70.  } else { 
  71.  ALOGE("Couldn't get gralloc module"); 
  72.  } 
  73.   
  74. ANativeWindow::setSwapInterval = setSwapInterval; 
  75.  ANativeWindow::dequeueBuffer = dequeueBuffer; 
  76.  ANativeWindow::queueBuffer = queueBuffer; 
  77.  ANativeWindow::query = query; 
  78.  ANativeWindow::perform = perform; 
  79.   
  80. ANativeWindow::dequeueBuffer_DEPRECATED = dequeueBuffer_DEPRECATED; 
  81.  ANativeWindow::lockBuffer_DEPRECATED = lockBuffer_DEPRECATED; 
  82.  ANativeWindow::queueBuffer_DEPRECATED = queueBuffer_DEPRECATED; 
  83.  } 

这里会先根据gralloc的module ID来得到hw_module_t结构。hw_get_module->hw_get_module_by_class。在hw_get_module_by_class里面,首先根据平台配置找到gralloc动态库的位置,默认使用gralloc.default.so。
参见以下代码(hardware.c):

  1. for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) { 
  2.         if (i < HAL_VARIANT_KEYS_COUNT) { 
  3.             if (property_get(variant_keys[i], prop, NULL) == 0) { 
  4.                 continue
  5.             } 
  6.             snprintf(path, sizeof(path), "%s/%s.%s.so"
  7.                      HAL_LIBRARY_PATH2, name, prop); 
  8.             if (access(path, R_OK) == 0break
  9.   
  10.             snprintf(path, sizeof(path), "%s/%s.%s.so"
  11.                      HAL_LIBRARY_PATH1, name, prop); 
  12.             if (access(path, R_OK) == 0break
  13.         } else { 
  14.             snprintf(path, sizeof(path), "%s/%s.default.so"
  15.                      HAL_LIBRARY_PATH2, name); 
  16.             if (access(path, R_OK) == 0break
  17.   
  18.             snprintf(path, sizeof(path), "%s/%s.default.so"
  19.                      HAL_LIBRARY_PATH1, name); 
  20.             if (access(path, R_OK) == 0break
  21.         } 
  22.     } 
  23.  status = -ENOENT; 
  24.  if (i < HAL_VARIANT_KEYS_COUNT+1) { 
  25.  /* load the module, if this fails, we're doomed, and we should not try 
  26.  * to load a different variant. */ 
  27.  status = load(class_id, path, module); 
  28.  } 

找到gralloc库的路径后,会调用load函数,在load函数中使用dlopen打开找到的库,并根据HAL_MODULE_INFO_SYM_AS_STR(其值为HMI)获取到hw_module_t(即HAL_MODULE_INFO_SYM)结构体指针,以及把dlopen返回的handle保存在hw_module_t中。而hw_module_t HMI
结构是一个全局结构,在gralloc.cpp中已经得到初始化了。这也是为什么每一个HAL模块都要定义并初始化一个名字为HAL_MODULE_INFO_SYM的hw_module_t结构

  1. struct private_module_t HAL_MODULE_INFO_SYM = { 
  2.     base: { 
  3.         common: { 
  4.             tag: HARDWARE_MODULE_TAG, 
  5.             version_major: 1
  6.             version_minor: 0
  7.             id: GRALLOC_HARDWARE_MODULE_ID, 
  8.             name: "Graphics Memory Allocator Module"
  9.             author: "The Android Open Source Project"
  10.             methods: &gralloc_module_methods 
  11.         }, 
  12.         registerBuffer: gralloc_register_buffer, 
  13.         unregisterBuffer: gralloc_unregister_buffer, 
  14.         lock: gralloc_lock, 
  15.         unlock: gralloc_unlock, 
  16.     }, 
  17.     framebuffer: 0
  18.     flags: 0
  19.     numBuffers: 0
  20.     bufferMask: 0
  21.     lock: PTHREAD_MUTEX_INITIALIZER, 
  22.     currentBuffer: 0
  23. }; 

#p#

回过头,回到FramebufferNativeWindow的构造函数处,接下来调用了err = framebuffer_open(module, &fbDev);framebuffer_open定义在fb.h中,是一个inline函数,其实最终调用了就是上面结构体中初始化的open函数,open函数指向gralloc_device_open,其实现为(gralloc.cpp):

  1. int gralloc_device_open(const hw_module_t* module, const char* name, hw_device_t** device) 
  2.     int status = -EINVAL; 
  3.     if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { 
  4.         gralloc_context_t *dev; 
  5.         dev = (gralloc_context_t*)malloc(sizeof(*dev)); 
  6.   
  7.         /* initialize our state here */ 
  8.         memset(dev, 0, sizeof(*dev)); 
  9.   
  10.         /* initialize the procs */ 
  11.         dev->device.common.tag = HARDWARE_DEVICE_TAG; 
  12.         dev->device.common.version = 0
  13.         dev->device.common.module = const_cast<hw_module_t*>(module); 
  14.         dev->device.common.close = gralloc_close; 
  15.   
  16.         dev->device.alloc   = gralloc_alloc; 
  17.         dev->device.free    = gralloc_free; 
  18.   
  19.         *device = &dev->device.common; 
  20.         status = 0
  21.     } else { 
  22.         status = fb_device_open(module, name, device); 
  23.     } 
  24.     return status; 

fb_device_open的定义如下所示(framebuffer.cpp):

  1. int fb_device_open(hw_module_t const* module, const char* name, 
  2.         hw_device_t** device) 
  3.     int status = -EINVAL; 
  4.     if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { 
  5.         /* initialize our state here */ 
  6.         fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); 
  7.         memset(dev, 0, sizeof(*dev)); 
  8.   
  9.         /* initialize the procs */ 
  10.         dev->device.common.tag = HARDWARE_DEVICE_TAG; 
  11.         dev->device.common.version = 0
  12.         dev->device.common.module = const_cast<hw_module_t*>(module); 
  13.         dev->device.common.close = fb_close; 
  14.         dev->device.setSwapInterval = fb_setSwapInterval; 
  15.         dev->device.post            = fb_post; 
  16.         dev->device.setUpdateRect = 0
  17.   
  18.         private_module_t* m = (private_module_t*)module; 
  19.         status = mapFrameBuffer(m); 
  20.         if (status >= 0) { 
  21.             int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); 
  22.             int format = (m->info.bits_per_pixel == 32
  23.                          ? HAL_PIXEL_FORMAT_RGBX_8888 
  24.                          : HAL_PIXEL_FORMAT_RGB_565; 
  25.             const_cast<uint32_t&>(dev->device.flags) = 0
  26.             const_cast<uint32_t&>(dev->device.width) = m->info.xres; 
  27.             const_cast<uint32_t&>(dev->device.height) = m->info.yres; 
  28.             const_cast<int&>(dev->device.stride) = stride; 
  29.             const_cast<int&>(dev->device.format) = format; 
  30.             const_cast<float&>(dev->device.xdpi) = m->xdpi; 
  31.             const_cast<float&>(dev->device.ydpi) = m->ydpi; 
  32.             const_cast<float&>(dev->device.fps) = m->fps; 
  33.             const_cast<int&>(dev->device.minSwapInterval) = 1
  34.             const_cast<int&>(dev->device.maxSwapInterval) = 1
  35.             *device = &dev->device.common; 
  36.         } 
  37.     } 
  38.     return status; 

接下来的gralloc_open也是调用了gralloc_device_open,只不过name参数一个是GRALLOC_HARDWARE_GPU0,而另外一个是GRALLOC_HARDWARE_FB0,这两个函数分别得到alloc_device_t 和 framebuffer_device_t结构。到现在为止,gralloc模块的三个主要结构体,gralloc_module_t,alloc_device_t,framebuffer_device_t都已经获取到了。其中在fb_device_open函数中会获取实际的framebuffer设备(通常是/dev/graphics/fb0)的一些重要参数以及能力,比如分辨率信息以及支持多少个缓冲等,另外会把framebuffer映射到内测的地址保存到alloc_module_t中。android一般使用的都是双缓冲机制。具体代码如下(framebuffer.cpp),其中涉及到对private_module_t中一些成员的完善,涉及到gralloc_module_t以及private_handle_t等,其定义在gralloc_priv.h中,这两个结构中都保存了framebuffer的一些私有信息。

  1. int mapFrameBufferLocked(struct private_module_t* module) 
  2. // already initialized... 
  3. if (module->framebuffer) { 
  4. return 0
  5.   
  6. char const * const device_template[] = { 
  7. "/dev/graphics/fb%u"
  8. "/dev/fb%u"
  9. 0 }; 
  10.   
  11. int fd = -1
  12. int i=0
  13. char name[64]; 
  14.   
  15. while ((fd==-1) && device_template[i]) { 
  16. snprintf(name, 64, device_template[i], 0); 
  17. fd = open(name, O_RDWR, 0); 
  18. i++; 
  19. if (fd < 0
  20. return -errno; 
  21.   
  22. struct fb_fix_screeninfo finfo; 
  23. if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1
  24. return -errno; 
  25.   
  26. struct fb_var_screeninfo info; 
  27. if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1
  28. return -errno; 
  29.   
  30. info.reserved[0] = 0
  31. info.reserved[1] = 0
  32. info.reserved[2] = 0
  33. info.xoffset = 0
  34. info.yoffset = 0
  35. info.activate = FB_ACTIVATE_NOW; 
  36.   
  37. /* 
  38. * Request NUM_BUFFERS screens (at lest 2 for page flipping) 
  39. */ 
  40. info.yres_virtual = info.yres * NUM_BUFFERS; 
  41. uint32_t flags = PAGE_FLIP; 
  42. if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) { 
  43. info.yres_virtual = info.yres; 
  44. flags &= ~PAGE_FLIP; 
  45. ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported"); 
  46.   
  47. if (info.yres_virtual < info.yres * 2) { 
  48. // we need at least 2 for page-flipping 
  49. info.yres_virtual = info.yres; 
  50. flags &= ~PAGE_FLIP; 
  51. ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)"
  52. info.yres_virtual, info.yres*2); 
  53.   
  54. if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1
  55. return -errno; 
  56.   
  57. uint64_t refreshQuotient = 
  58. uint64_t( info.upper_margin + info.lower_margin + info.yres ) 
  59. * ( info.left_margin + info.right_margin + info.xres ) 
  60. * info.pixclock 
  61. ); 
  62.   
  63. /* Beware, info.pixclock might be 0 under emulation, so avoid a 
  64. * division-by-0 here (SIGFPE on ARM) */ 
  65. int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0
  66.   
  67. if (refreshRate == 0) { 
  68. // bleagh, bad info from the driver 
  69. refreshRate = 60*1000// 60 Hz 
  70.   
  71. if (int(info.width) <= 0 || int(info.height) <= 0) { 
  72. // the driver doesn't return that information 
  73. // default to 160 dpi 
  74. info.width = ((info.xres * 25.4f)/160.0f + 0.5f); 
  75. info.height = ((info.yres * 25.4f)/160.0f + 0.5f); 
  76.   
  77. float xdpi = (info.xres * 25.4f) / info.width; 
  78. float ydpi = (info.yres * 25.4f) / info.height; 
  79. float fps = refreshRate / 1000.0f; 
  80.   
  81. ALOGI( "using (fd=%d)\n" 
  82. "id = %s\n" 
  83. "xres = %d px\n" 
  84. "yres = %d px\n" 
  85. "xres_virtual = %d px\n" 
  86. "yres_virtual = %d px\n" 
  87. "bpp = %d\n" 
  88. "r = %2u:%u\n" 
  89. "g = %2u:%u\n" 
  90. "b = %2u:%u\n"
  91. fd, 
  92. finfo.id, 
  93. info.xres, 
  94. info.yres, 
  95. info.xres_virtual, 
  96. info.yres_virtual, 
  97. info.bits_per_pixel, 
  98. info.red.offset, info.red.length, 
  99. info.green.offset, info.green.length, 
  100. info.blue.offset, info.blue.length 
  101. ); 
  102.   
  103. ALOGI( "width = %d mm (%f dpi)\n" 
  104. "height = %d mm (%f dpi)\n" 
  105. "refresh rate = %.2f Hz\n"
  106. info.width, xdpi, 
  107. info.height, ydpi, 
  108. fps 
  109. ); 
  110. if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1
  111. return -errno; 
  112.   
  113. if (finfo.smem_len <= 0
  114. return -errno; 
  115. module->flags = flags; 
  116. module->info = info; 
  117. module->finfo = finfo; 
  118. module->xdpi = xdpi; 
  119. module->ydpi = ydpi; 
  120. module->fps = fps; 
  121.   
  122. /* 
  123. * map the framebuffer 
  124. */ 
  125.   
  126. int err; 
  127. size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual); 
  128. module->framebuffer = new private_handle_t(dup(fd), fbSize, 0); 
  129.   
  130. module->numBuffers = info.yres_virtual / info.yres; 
  131. module->bufferMask = 0
  132.   
  133. void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 
  134. if (vaddr == MAP_FAILED) { 
  135. ALOGE("Error mapping the framebuffer (%s)", strerror(errno)); 
  136. return -errno; 
  137. module->framebuffer->base = intptr_t(vaddr); 
  138. memset(vaddr, 0, fbSize); 
  139. return 0

由上面函数看出,mapFrameBufferLocked主要做了下面几件事情:

1. 打开framebuffer设备

2. 获取 fb_fix_screeninfo and fb_var_screeninfo

3. refill fb_var_screeninfo

4. 判断是否支持PAGE_FLIP

5. 计算刷新率

6. 打印gralloc信息

7. 填充private_module_t

8. mmap the framebuffer

#p#

看之前的HAL模块比如Camera模块,有一个hw_module_t结构和一个hw_device_t结构,而这里的gralloc模块却包含了两个hw_device_t结构,一个alloc_device_t和一个framebuffer_device_t结构。先看framebuffer_device_t定义:

framebuffer_device_t(fb.h):

  1. typedef struct framebuffer_device_t { 
  2. struct hw_device_t common; 
  3.   
  4. /* flags describing some attributes of the framebuffer */ 
  5. const uint32_t flags; 
  6.   
  7. /* dimensions of the framebuffer in pixels */ 
  8. const uint32_t width; 
  9. const uint32_t height; 
  10.   
  11. /* frambuffer stride in pixels */ 
  12. const int stride; 
  13.   
  14. /* framebuffer pixel format */ 
  15. const int format; 
  16.   
  17. /* resolution of the framebuffer's display panel in pixel per inch*/ 
  18. const float xdpi; 
  19. const float ydpi; 
  20.   
  21. /* framebuffer's display panel refresh rate in frames per second */ 
  22. const float fps; 
  23.   
  24. /* min swap interval supported by this framebuffer */ 
  25. const int minSwapInterval; 
  26.   
  27. /* max swap interval supported by this framebuffer */ 
  28. const int maxSwapInterval; 
  29.   
  30. /* Number of framebuffers supported*/ 
  31. const int numFramebuffers; 
  32.   
  33. int reserved[7]; 
  34.   
  35. /* 
  36. * requests a specific swap-interval (same definition than EGL) 
  37. * 
  38. * Returns 0 on success or -errno on error. 
  39. */ 
  40. int (*setSwapInterval)(struct framebuffer_device_t* window, 
  41. int interval); 
  42.   
  43. /* 
  44. * This hook is OPTIONAL. 
  45. * 
  46. * It is non NULL If the framebuffer driver supports "update-on-demand" 
  47. * and the given rectangle is the area of the screen that gets 
  48. * updated during (*post)(). 
  49. * 
  50. * This is useful on devices that are able to DMA only a portion of 
  51. * the screen to the display panel, upon demand -- as opposed to 
  52. * constantly refreshing the panel 60 times per second, for instance. 
  53. * 
  54. * Only the area defined by this rectangle is guaranteed to be valid, that 
  55. * is, the driver is not allowed to post anything outside of this 
  56. * rectangle. 
  57. * 
  58. * The rectangle evaluated during (*post)() and specifies which area 
  59. * of the buffer passed in (*post)() shall to be posted. 
  60. * 
  61. * return -EINVAL if width or height <=0, or if left or top < 0 
  62. */ 
  63. int (*setUpdateRect)(struct framebuffer_device_t* window, 
  64. int left, int top, int width, int height); 
  65.   
  66. /* 
  67. * Post <buffer> to the display (display it on the screen) 
  68. * The buffer must have been allocated with the 
  69. * GRALLOC_USAGE_HW_FB usage flag. 
  70. * buffer must be the same width and height as the display and must NOT 
  71. * be locked. 
  72. * 
  73. * The buffer is shown during the next VSYNC. 
  74. * 
  75. * If the same buffer is posted again (possibly after some other buffer), 
  76. * post() will block until the the first post is completed. 
  77. * 
  78. * Internally, post() is expected to lock the buffer so that a 
  79. * subsequent call to gralloc_module_t::(*lock)() with USAGE_RENDER or 
  80. * USAGE_*_WRITE will block until it is safe; that is typically once this 
  81. * buffer is shown and another buffer has been posted. 
  82. * 
  83. * Returns 0 on success or -errno on error. 
  84. */ 
  85. int (*post)(struct framebuffer_device_t* dev, buffer_handle_t buffer); 
  86. /* 
  87. * The (*compositionComplete)() method must be called after the 
  88. * compositor has finished issuing GL commands for client buffers. 
  89. */ 
  90.   
  91. int (*compositionComplete)(struct framebuffer_device_t* dev); 
  92.   
  93. /* 
  94. * This hook is OPTIONAL. 
  95. * 
  96. * If non NULL it will be caused by SurfaceFlinger on dumpsys 
  97. */ 
  98. void (*dump)(struct framebuffer_device_t* dev, char *buff, int buff_len); 
  99.   
  100. /* 
  101. * (*enableScreen)() is used to either blank (enable=0) or 
  102. * unblank (enable=1) the screen this framebuffer is attached to. 
  103. * 
  104. * Returns 0 on success or -errno on error. 
  105. */ 
  106. int (*enableScreen)(struct framebuffer_device_t* dev, int enable); 
  107.   
  108. void* reserved_proc[6]; 
  109.   
  110. } framebuffer_device_t; 

从这个结构看以看出,framebuffer_device_t里面主要保存了framebuffer相关的一些信息,例如分辨率,刷新率,framebuffer的数量等,另外,里面定义了一些操作framebuffer的函数,一下简单介绍其中几个函数。

1. static int fb_setSwapInterval(struct framebuffer_device_t* dev, int interval)

这个函数基本没有用,因为maxSwapInterval=minSwapInterval= 1;

2. int (*setUpdateRect)(struct framebuffer_device_t* window, int left, int top, int width, int height);

这个函数是局部刷新用的,默认没有启用。和平台有关。

3. int (*post)(struct framebuffer_device_t* dev, buffer_handle_t buffer);

这个是最关键的函数。用来将图形缓冲区buffer的内容渲染到帧缓冲区中去,即显示在设备的显示屏中去。函数实现如下:

  1. static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) 
  2. if (private_handle_t::validate(buffer) < 0
  3. return -EINVAL; 
  4.   
  5. fb_context_t* ctx = (fb_context_t*)dev; 
  6.   
  7. private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer); 
  8. private_module_t* m = reinterpret_cast<private_module_t*>( 
  9. dev->common.module); 
  10.   
  11. if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { 
  12. const size_t offset = hnd->base - m->framebuffer->base; 
  13. m->info.activate = FB_ACTIVATE_VBL; 
  14. m->info.yoffset = offset / m->finfo.line_length; 
  15. if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { 
  16. ALOGE("FBIOPUT_VSCREENINFO failed"); 
  17. m->base.unlock(&m->base, buffer); 
  18. return -errno; 
  19. m->currentBuffer = buffer; 
  20.   
  21. else { 
  22. // If we can't do the page_flip, just copy the buffer to the front 
  23. // FIXME: use copybit HAL instead of memcpy 
  24.   
  25. void* fb_vaddr; 
  26. void* buffer_vaddr; 
  27.   
  28. m->base.lock(&m->base, m->framebuffer, 
  29. GRALLOC_USAGE_SW_WRITE_RARELY, 
  30. 00, m->info.xres, m->info.yres, 
  31. &fb_vaddr); 
  32.   
  33. m->base.lock(&m->base, buffer, 
  34. GRALLOC_USAGE_SW_READ_RARELY, 
  35. 00, m->info.xres, m->info.yres, 
  36. &buffer_vaddr); 
  37.   
  38. memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres); 
  39.   
  40. m->base.unlock(&m->base, buffer); 
  41. m->base.unlock(&m->base, m->framebuffer); 
  42.   
  43. return 0

从fb_post的函数定义可以看出,其实现方式有两种方式,***种方式是把Framebuffer的后buffer切为前buffer,然后通过IOCTRL机制告诉FB驱动切换DMA源地地址。

具体原理是这样的:当private_handle_t结构体hnd所描述的图形缓冲区是在系统帧缓冲区中分配的时候,即这个图形缓冲区的标志值flags的PRIV_FLAGS_FRAMEBUFFER位等于1的时候,我们是不需要将图形缓冲区的内容拷贝到系统帧缓冲区去的,因为我们将内容写入到图形缓冲区的时候,已经相当于是将内容写入到了系统帧缓冲区中去了。虽然在这种情况下,我们不需要将图形缓冲区的内容拷贝到系统帧缓冲区去,但是我们需要告诉系统帧缓冲区设备将要渲染的图形缓冲区作为系统当前的输出图形缓冲区,这样才可以将要渲染的图形缓冲区的内容绘制到设备显示屏来。例如,假设系统帧缓冲区有2个图形缓冲区,当前是以第1个图形缓冲区作为输出图形缓冲区的,这时候如果我们需要渲染第2个图形缓冲区,那么就必须告诉系统帧绘冲区设备,将第2个图形缓冲区作为输出图形缓冲区。这个实现方式的前提是Linux内核必须分配至少两个缓冲区大小的物理内存和实现切换的ioctrol,这个比较快速。

设置系统帧缓冲区的当前输出图形缓冲区是通过IO控制命令FBIOPUT_VSCREENINFO来进行的。IO控制命令FBIOPUT_VSCREENINFO需要一个fb_var_screeninfo结构体作为参数。从前面第3部分的内容可以知道,private_module_t结构体m的成员变量info正好保存在我们所需要的这个fb_var_screeninfo结构体。有了个m->info这个fb_var_screeninfo结构体之后,我们只需要设置好它的成员变量yoffset的值(不用设置成员变量xoffset的值是因为所有的图形缓冲区的宽度是相等的),就可以将要渲染的图形缓冲区设置为系统帧缓冲区的当前输出图形缓冲区。fb_var_screeninfo结构体的成员变量yoffset保存的是当前输出图形缓冲区在整个系统帧缓冲区的纵向偏移量,即Y偏移量。我们只需要将要渲染的图形缓冲区的开始地址hnd->base的值减去系统帧缓冲区的基地址m->framebuffer->base的值,再除以图形缓冲区一行所占据的字节数m->finfo.line_length,就可以得到所需要的Y偏移量。

在执行IO控制命令FBIOPUT_VSCREENINFO之前,还会将作为参数的fb_var_screeninfo结构体的成员变量activate的值设置FB_ACTIVATE_VBL,表示要等到下一个垂直同步事件出现时,再将当前要渲染的图形缓冲区的内容绘制出来。这样做的目的是避免出现屏幕闪烁,即避免前后两个图形缓冲区的内容各有一部分同时出现屏幕中。

第二种方式是利用copy的方式来实现,比较耗时。当private_handle_t结构体hnd所描述的图形缓冲区是在内存中分配的时候,即这个图形缓冲区的标志值flags的PRIV_FLAGS_FRAMEBUFFER位等于0的时候,我们就需要将它的内容拷贝到系统帧缓冲区中去了。这个拷贝的工作是通过调用函数memcpy来完成的。在拷贝之前,我们需要三个参数。***个参数是要渲染的图形缓冲区的起址地址,这个地址保存在参数buffer所指向的一个private_handle_t结构体中。第二个参数是要系统帧缓冲区的基地址,这个地址保存在private_module_t结构体m的成员变量framebuffer所指向的一个private_handle_t结构体中。第三个参数是要拷贝的内容的大小,这个大小就刚好是一个屏幕像素所占据的内存的大小。屏幕高度由m->info.yres来描述,而一行屏幕像素所占用的字节数由m->finfo.line_length来描述,将这两者相乘,就可以得到一个屏幕像素所占据的内存的大小。

在将一块内存缓冲区的内容拷贝到系统帧缓冲区中去之前,需要对这两块缓冲区进行锁定,以保证在拷贝的过程中,这两块缓冲区的内容不会被修改。这个锁定的工作是由Gralloc模块中的函数gralloc_lock来实现的。从前面第1部分的内容可以知道,Gralloc模块中的函数gralloc_lock的地址正好就保存在private_module_t结构体m的成员变量base所描述的一个gralloc_module_t结构体的成员函数lock中。

在调用函数gralloc_lock来锁定一块缓冲区之后,还可以通过***一个输出参数来获得被锁定的缓冲区的开始地址,因此,通过调用函数gralloc_lock来锁定要渲染的图形缓冲区以及系统帧缓冲区,就可以得到前面所需要的***个和第二个参数。

将要渲染的图形缓冲区的内容拷贝到系统帧缓冲区之后,就可以解除前面对它们的锁定了,这个解锁的工作是由Gralloc模块中的函数gralloc_unlock来实现的。从前面第1部分的内容可以知道,Gralloc模块中的函数gralloc_unlock的地址正好就保存在private_module_t结构体m的成员变量base所描述的一个gralloc_module_t结构体的成员函数unlock中。

以上是framebuffer_device_t结构相关的一些内容,其主要作用是渲染图形缓冲区来显示内容。下面在看看alloc_device_t的内容。

#p#

alloc_device_t结构的内容如下:

  1. typedef struct alloc_device_t { 
  2. struct hw_device_t common; 
  3.   
  4. /* 
  5. * (*alloc)() Allocates a buffer in graphic memory with the requested 
  6. * parameters and returns a buffer_handle_t and the stride in pixels to 
  7. * allow the implementation to satisfy hardware constraints on the width 
  8. * of a pixmap (eg: it may have to be multiple of 8 pixels). 
  9. * The CALLER TAKES OWNERSHIP of the buffer_handle_t. 
  10. * 
  11. * If format is HAL_PIXEL_FORMAT_YCbCr_420_888, the returned stride must be 
  12. * 0, since the actual strides are available from the android_ycbcr 
  13. * structure. 
  14. * 
  15. * Returns 0 on success or -errno on error. 
  16. */ 
  17.   
  18. int (*alloc)(struct alloc_device_t* dev, 
  19. int w, int h, int format, int usage, 
  20. buffer_handle_t* handle, int* stride); 
  21.   
  22. /* 
  23. * (*free)() Frees a previously allocated buffer. 
  24. * Behavior is undefined if the buffer is still mapped in any process, 
  25. * but shall not result in termination of the program or security breaches 
  26. * (allowing a process to get access to another process' buffers). 
  27. * THIS FUNCTION TAKES OWNERSHIP of the buffer_handle_t which becomes 
  28. * invalid after the call. 
  29. * 
  30. * Returns 0 on success or -errno on error. 
  31. */ 
  32. int (*free)(struct alloc_device_t* dev, 
  33. buffer_handle_t handle); 
  34.   
  35. /* This hook is OPTIONAL. 
  36. * 
  37. * If non NULL it will be caused by SurfaceFlinger on dumpsys 
  38. */ 
  39. void (*dump)(struct alloc_device_t *dev, char *buff, int buff_len); 
  40.   
  41. void* reserved_proc[7]; 
  42. } alloc_device_t; 

从其结构体成员可以看出,其主要作用是为请求者分配图形缓冲区。先看alloc函数的实现(gralloc.cpp):

  1. static int gralloc_alloc(alloc_device_t* dev, 
  2.  int w, int h, int format, int usage, 
  3.  buffer_handle_t* pHandle, int* pStride) 
  4.  if (!pHandle || !pStride) 
  5.  return -EINVAL; 
  6.   
  7. size_t size, stride; 
  8.   
  9. int align = 4
  10.  int bpp = 0
  11.  switch (format) { 
  12.  case HAL_PIXEL_FORMAT_RGBA_8888: 
  13.  case HAL_PIXEL_FORMAT_RGBX_8888: 
  14.  case HAL_PIXEL_FORMAT_BGRA_8888: 
  15.  bpp = 4
  16.  break
  17.  case HAL_PIXEL_FORMAT_RGB_888: 
  18.  bpp = 3
  19.  break
  20.  case HAL_PIXEL_FORMAT_RGB_565: 
  21.  case HAL_PIXEL_FORMAT_RAW_SENSOR: 
  22.  bpp = 2
  23.  break
  24.  default
  25.  return -EINVAL; 
  26.  } 
  27.  size_t bpr = (w*bpp + (align-1)) & ~(align-1); 
  28.  size = bpr * h; 
  29.  stride = bpr / bpp; 
  30.   
  31. int err; 
  32.  if (usage & GRALLOC_USAGE_HW_FB) { 
  33.  err = gralloc_alloc_framebuffer(dev, size, usage, pHandle); 
  34.  } else { 
  35.  err = gralloc_alloc_buffer(dev, size, usage, pHandle); 
  36.  } 
  37.   
  38. if (err < 0) { 
  39.  return err; 
  40.  } 
  41.   
  42. *pStride = stride; 
  43.  return 0

参数format用来描述要分配的图形缓冲区的颜色格式,描述一个像素需要几个字节来表示。参数w表示要分配的图形缓冲区所保存的图像的宽度,将它乘以bpp,就可以得到保存一行像素所需要使用的字节数。我们需要将这个字节数对齐到4个字节边界,***得到一行像素所需要的字节数就保存在变量bpr中。

参数h表示要分配的图形缓冲区所保存的图像的高度,将它乘以bpr,就可以得到保存整个图像所需要使用的字节数。将变量bpr的值除以变量bpp的值,就得到要分配的图形缓冲区一行包含有多少个像素点,这个结果需要保存在输出参数pStride中,以便可以返回给调用者。

参数usage用来描述要分配的图形缓冲区的用途。如果是用来在系统帧缓冲区中渲染的,即参数usage的GRALLOC_USAGE_HW_FB位等于1,那么就必须要系统帧缓冲区中分配,否则的话,就在内存中分配。注意,在内存中分配的图形缓冲区,最终是需要拷贝到系统帧缓冲区去的,以便可以将它所描述的图形渲染出来。函数gralloc_alloc_framebuffer用来在系统帧缓冲区中分配图形缓冲区,而函数gralloc_alloc_buffer用来在内存在分配图形缓冲区,接下来我们就来看看这两个函数的实现。

gralloc_alloc_framebuffer最终调用了gralloc_alloc_framebuffer_locked(gralloc.cpp):

  1. static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev, 
  2.  size_t size, int usage, buffer_handle_t* pHandle) 
  3.  private_module_t* m = reinterpret_cast<private_module_t*>( 
  4.  dev->common.module); 
  5.   
  6. // allocate the framebuffer 
  7.  if (m->framebuffer == NULL) { 
  8.  // initialize the framebuffer, the framebuffer is mapped once 
  9.  // and forever. 
  10.  int err = mapFrameBufferLocked(m); 
  11.  if (err < 0) { 
  12.  return err; 
  13.  } 
  14.  } 
  15.   
  16. const uint32_t bufferMask = m->bufferMask; 
  17.  const uint32_t numBuffers = m->numBuffers; 
  18.  const size_t bufferSize = m->finfo.line_length * m->info.yres; 
  19.  if (numBuffers == 1) { 
  20.  // If we have only one buffer, we never use page-flipping. Instead, 
  21.  // we return a regular buffer which will be memcpy'ed to the main 
  22.  // screen when post is called. 
  23.  int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; 
  24.  return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle); 
  25.  } 
  26.   
  27. if (bufferMask >= ((1LU<<numBuffers)-1)) { 
  28.  // We ran out of buffers. 
  29.  return -ENOMEM; 
  30.  } 
  31.   
  32. // create a "fake" handles for it 
  33.  intptr_t vaddr = intptr_t(m->framebuffer->base); 
  34.  private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), size, 
  35.  private_handle_t::PRIV_FLAGS_FRAMEBUFFER); 
  36.   
  37. // find a free slot 
  38.  for (uint32_t i=0 ; i<numBuffers ; i++) { 
  39.  if ((bufferMask & (1LU<<i)) == 0) { 
  40.  m->bufferMask |= (1LU<<i); 
  41.  break
  42.  } 
  43.  vaddr += bufferSize; 
  44.  } 
  45.   
  46.  hnd->base = vaddr; 
  47.  hnd->offset = vaddr - intptr_t(m->framebuffer->base); 
  48.  *pHandle = hnd; 
  49.   
  50. return 0

变量bufferMask用来描述系统帧缓冲区的使用情况,而变量numBuffers用来描述系统帧缓冲区可以划分为多少个图形缓冲区来使用,另外一个变量bufferSize用来描述设备显示屏一屏内容所占用的内存的大小。如果系统帧缓冲区只有一个图形缓冲区大小,即变量numBuffers的值等于1,那么这个图形缓冲区就始终用作系统主图形缓冲区来使用。在这种情况下,我们就不能够在系统帧缓冲区中分配图形缓冲区来给用户空间的应用程序使用,因此,这时候就会转向内存中来分配图形缓冲区,即调用函数gralloc_alloc_buffer来分配图形缓冲区。注意,这时候分配的图形缓冲区的大小为一屏内容的大小,即bufferSize。

如果bufferMask的值大于等于((1LU<<numBuffers)-1)的值,那么就说明系统帧缓冲区中的图形缓冲区全部都分配出去了,这时候分配图形缓冲区就失败了。例如,假设图形缓冲区的个数为2,那么((1LU<<numBuffers)-1)的值就等于3,即二制制0x11。如果这时候bufferMask的值也等于0x11,那么就表示***个和第二个图形缓冲区都已经分配出去了。因此,这时候就不能再在系统帧缓冲区中分配图形缓冲区。

假设此时系统帧缓冲区中尚有空闲的图形缓冲区的,接下来函数就会创建一个private_handle_t结构体hnd来描述这个即将要分配出去的图形缓冲区。注意,这个图形缓冲区的标志值等于PRIV_FLAGS_FRAMEBUFFER,即表示这是一块在系统帧缓冲区中分配的图形缓冲区。

接下来的for循环从低位到高位检查变量bufferMask的值,并且找到***个值等于0的位,这样就可以知道在系统帧缓冲区中,第几个图形缓冲区的是空闲的。注意,变量vadrr的值开始的时候指向系统帧缓冲区的基地址,在下面的for循环中,每循环一次它的值都会增加bufferSize。从这里就可以看出,每次从系统帧缓冲区中分配出去的图形缓冲区的大小都是刚好等于显示屏一屏内容大小的。***分配出去的图形缓冲区的开始地址就保存在前面所创建的private_handle_t结构体hnd的成员变量base中,这样,用户空间的应用程序就可以直接将要渲染的图形内容拷贝到这个地址上去,这就相当于是直接将图形渲染到系统帧缓冲区中去。

在将private_handle_t结构体hnd返回给调用者之前,还需要设置它的成员变量offset,以便可以知道它所描述的图形缓冲区的起始地址相对于系统帧缓冲区的基地址的偏移量。

gralloc_alloc_buffer(gralloc.cpp)的实现如下:

  1. static int gralloc_alloc_buffer(alloc_device_t* dev, 
  2. size_t size, int usage, buffer_handle_t* pHandle) 
  3. int err = 0
  4. int fd = -1
  5.   
  6. size = roundUpToPageSize(size); 
  7.   
  8. fd = ashmem_create_region("gralloc-buffer", size); 
  9. if (fd < 0) { 
  10. ALOGE("couldn't create ashmem (%s)", strerror(-errno)); 
  11. err = -errno; 
  12.   
  13. if (err == 0) { 
  14. private_handle_t* hnd = new private_handle_t(fd, size, 0); 
  15. gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>( 
  16. dev->common.module); 
  17. err = mapBuffer(module, hnd); 
  18. if (err == 0) { 
  19. *pHandle = hnd; 
  20.   
  21. ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); 
  22.   
  23. return err; 

它首先调用函数ashmem_create_region来创建一块匿名共享内存,接着再在这块匿名共享内存上分配一个图形缓冲区。注意,这个图形缓冲区也是使用一个private_handle_t结构体来描述的,不过这个图形缓冲区的标志值等于0,以区别于在系统帧缓冲区中分配的图形缓冲区。其中mapBuffer又把hnd所描述的一个图形缓冲区映射到当前进程的地址空间来。

以上内容就是alloc_device_t的相关内容。在private_module_t中有一个registerBuffer的函数指针,此函数是干什么的呢?在Android系统中,所有的图形缓冲区都是由SurfaceFlinger服务分配的,而当一个图形缓冲区被分配的时候,它会同时被映射到请求分配的进程的地址空间去,即分配的过程同时也包含了注册的过程。但是对用户空间的其它的应用程序来说,它们所需要的图形缓冲区是在由SurfaceFlinger服务分配的,因此,当它们得到SurfaceFlinger服务分配的图形缓冲区之后,还需要将这块图形缓冲区映射到自己的地址空间来,以便可以使用这块图形缓冲区。这个映射的过程即为我们接下来要分析的图形缓冲区注册过程。

由于在系统帧缓冲区中分配的图形缓冲区只在SurfaceFlinger服务中使用,而SurfaceFlinger服务在初始化系统帧缓冲区的时候,已经将系统帧缓冲区映射到自己所在的进程中来了,因此,函数gralloc_map如果发现要注册的图形缓冲区是在系统帧缓冲区分配的时候,那么就不需要再执行映射图形缓冲区的操作了。

如果要注册的图形缓冲区是在内存中分配的,即它的标志值flags的PRIV_FLAGS_FRAMEBUFFER位等于1,那么接下来就需要将它映射到当前进程的地址空间来了。由于要注册的图形缓冲区是在文件描述符hnd->fd所描述的一块匿名共享内存中分配的,因此,我们只需要将文件描述符hnd->fd所描述的一块匿名共享内存映射到当前进程的地址空间来,就可以将参数hnd所描述的一个图形缓冲区映射到当前进程的地址空间来。

由于映射文件描述符hnd->fd得到的是一整块匿名共享内存在当前进程地址空间的基地址,而要注册的图形缓冲区可能只占据这块匿名共享内存的某一小部分,因此,我们还需要将要注册的图形缓冲区的在被映射的匿名共享内存中的偏移量hnd->offset加上被映射的匿名共享内存的基地址hnd->base,才可以得到要注册的图形缓冲区在当前进程中的访问地址,这个地址最终又被写入到hnd->base中去。

参考文档:http://blog.csdn.net/luoshengyang/article/details/7747932

 

责任编辑:倪明 来源: HAOMCU‘s blog
相关推荐

2009-12-16 18:02:48

Linux UNIX系

2016-09-09 12:28:12

大数据

2010-03-25 10:36:58

CentOS Samb

2020-09-21 09:15:12

系统

2017-05-24 10:58:28

linux系统技巧

2023-06-03 20:45:34

2022-02-22 13:50:01

TypeScrip前端框架

2014-07-24 16:29:07

linux学习网站

2020-12-28 08:29:47

Vuecss框架

2022-02-23 08:50:37

MySQL

2020-12-14 15:00:09

大数据IT技术

2018-05-04 12:11:17

Linux操作系统误区

2010-02-06 14:23:49

Android系统手机

2010-03-02 15:10:27

Android系统

2011-07-05 15:59:18

Qt 嵌入式 linux

2014-07-22 13:09:21

android

2009-12-24 11:17:14

Fedora 10

2010-02-05 14:48:04

Android手机操作

2011-04-20 09:15:37

iOS苹果Android

2009-04-12 08:46:43

Symbian诺基亚移动OS
点赞
收藏

51CTO技术栈公众号