Android研究_Gralloc_3fb设备Open过程
来源:互联网 发布:linux php环境 编辑:程序博客网 时间:2024/05/28 15:41
4.4 fb 设备open过程分析
我们回过头来看看open函数。在4.1小节中,我们提到,初始化Gralloc的时候,会打开两个设备:gralloc设备(modulename:GRALLOC_HARDWARE_GPU0)和一个fb0设备(modulename:GRALLOC_HARDWARE_FB0)。实际上,具体实现在/hardware/qcom/display/msm8974/libgralloc/gralloc.cpp中。
// Open Gralloc device
int gralloc_device_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
int status = -EINVAL;
if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
const private_module_t* m = reinterpret_cast<const private_module_t*>(
module);
gpu_context_t *dev;
IAllocController* alloc_ctrl = IAllocController::getInstance();
dev = new gpu_context_t(m, alloc_ctrl);
*device = &dev->common;
status = 0;
} else {
// 很显然,第一次加载,是open GRALLOC_HARDWARE_FB0,走的是这个分支。
status = fb_device_open(module, name, device);
}
return status;
}
Fb_device_open函数定义在/hardware/qcom/display/msm8974/libgralloc/framebuffer.cpp中。
int fb_device_open(hw_module_t const* module, const char* name,
hw_device_t** device)
{
int status = -EINVAL;
if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
alloc_device_t* gralloc_device;
// 打开gralloc_device设备。GRALLOC_HARDWARE_GPU0。
status = gralloc_open(module, &gralloc_device);
if (status < 0)
return status;
/* initialize our state here */
fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
memset(dev, 0, sizeof(*dev));
// 完成数据结构framebuffer_device_t的初始化
/* initialize the procs */
dev->device.common.tag = HARDWARE_DEVICE_TAG;
dev->device.common.version = 0;
dev->device.common.module = const_cast<hw_module_t*>(module);
dev->device.common.close = fb_close;
dev->device.setSwapInterval = fb_setSwapInterval;
dev->device.post = fb_post;
dev->device.setUpdateRect = 0;
dev->device.compositionComplete = fb_compositionComplete;
private_module_t* m = (private_module_t*)module;
status = mapFrameBuffer(m);
if (status >= 0) {
int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
const_cast<uint32_t&>(dev->device.flags) = 0;
const_cast<uint32_t&>(dev->device.width) = m->info.xres;
const_cast<uint32_t&>(dev->device.height) = m->info.yres;
const_cast<int&>(dev->device.stride) = stride;
const_cast<int&>(dev->device.format) = m->fbFormat;
const_cast<float&>(dev->device.xdpi) = m->xdpi;
const_cast<float&>(dev->device.ydpi) = m->ydpi;
const_cast<float&>(dev->device.fps) = m->fps;
const_cast<int&>(dev->device.minSwapInterval) =
PRIV_MIN_SWAP_INTERVAL;
const_cast<int&>(dev->device.maxSwapInterval) =
PRIV_MAX_SWAP_INTERVAL;
const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
dev->device.setUpdateRect = 0;
*device = &dev->device.common;
}
// Close the gralloc module
gralloc_close(gralloc_device);
}
return status;
}
函数int mapFrameBufferLocked(structprivate_module_t* module)
int mapFrameBufferLocked(struct private_module_t* module)
{
// already initialized...
if (module->framebuffer) {
return 0;
}
char const * const device_template[] = {
"/dev/graphics/fb%u",
"/dev/fb%u",
0 };
int fd = -1;
int i=0;
char name[64];
char property[PROPERTY_VALUE_MAX];
// 首先在系统中检查是否存在设备文件/dev/graphics/fb0或者/dev/fb0。如果存在的话,那么就调用函数open来打开它,并且将得到的文件描述符保存在变量fd中。这样,接下来函数mapFrameBufferLocked就可以通过文件描述符fd来与内核中的帧缓冲区驱动程序交互。
while ((fd==-1) && device_template[i]) {
snprintf(name, 64, device_template[i], 0);
fd = open(name, O_RDWR, 0);
i++;
}
if (fd < 0)
return -errno;
memset(&module->commit, 0, sizeof(struct mdp_display_commit));
// 分别通过IO控制命令FBIOGET_FSCREENINFO和FBIOGET_VSCREENINFO来获得系统帧缓冲区的信息,分别保存在fb_fix_screeninfo结构体finfo和fb_var_screeninfo结构体info中。
// fb_var_screeninfo记录着用户可修改的显示控制器参数,包括屏幕分辨率和每个像素的比特数,fb_var_screeninfo中的xres定义屏幕一行有对少个点,yres定义屏幕和每个像素点的比特数,bits_per_pixeld定义每个点用多少个字节表示。
// fb_fix_screeninfo中记录用户不能修改的显示控制器的参数,如屏幕缓冲区的物理地址,长度,当对缓冲区设备进行映射操作的时候,就是从fb_fix_screeninfo中取得缓冲区物理地址的。这些数据成员都需要在驱动程序中初始化中设置。
struct fb_fix_screeninfo finfo;
if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
return -errno;
struct fb_var_screeninfo info;
if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
return -errno;
// 完成fb_var_screeninfo结构体的填充
info.reserved[0] = 0;
info.reserved[1] = 0;
info.reserved[2] = 0;
info.xoffset = 0;
info.yoffset = 0;
info.activate = FB_ACTIVATE_NOW;
/* Interpretation of offset for color fields: All offsets are from the
* right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
* (means: you can use the offset as right argument to <<). A pixel
* afterwards is a bit stream and is written to video memory as that
* unmodified. This implies big-endian byte order if bits_per_pixel is
* greater than 8.
*/
if(info.bits_per_pixel == 32) {
/*
* Explicitly request RGBA_8888
*/
info.bits_per_pixel = 32;
info.red.offset = 24;
info.red.length = 8;
info.green.offset = 16;
info.green.length = 8;
info.blue.offset = 8;
info.blue.length = 8;
info.transp.offset = 0;
info.transp.length = 8;
/* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
* do not use the MDP for composition (i.e. hw composition == 0), ask
* for RGBA instead of RGBX. */
if (property_get("debug.sf.hw", property, NULL) > 0 &&
atoi(property) == 0)
module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
else if(property_get("debug.composition.type", property, NULL) > 0 &&
(strncmp(property, "mdp", 3) == 0))
module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
else
module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
} else {
/*
* Explicitly request 5/6/5
*/
info.bits_per_pixel = 16;
info.red.offset = 11;
info.red.length = 5;
info.green.offset = 5;
info.green.length = 6;
info.blue.offset = 0;
info.blue.length = 5;
info.transp.offset = 0;
info.transp.length = 0;
module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
}
//adreno needs 4k aligned offsets. Max hole size is 4096-1 4K对齐
// 计算实际的缓冲个数
int size = roundUpToPageSize(info.yres * info.xres * (info.bits_per_pixel/8));
/*
* Request NUM_BUFFERS screens (at least 2 for page flipping)
*/
int numberOfBuffers = (int)(finfo.smem_len/size);
ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
int num = atoi(property);
if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
numberOfBuffers = num;
}
}
if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
ALOGV("We support %d buffers", numberOfBuffers);
//consider the included hole by 4k alignment
uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
info.yres_virtual = (size * numberOfBuffers) / line_length;
uint32_t flags = PAGE_FLIP;
if (info.yres_virtual < ((size * 2) / line_length) ) {
// we need at least 2 for page-flipping
info.yres_virtual = size / line_length;
flags &= ~PAGE_FLIP;
ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
info.yres_virtual, info.yres*2);
}
// ??? 为什么这边需要再次获取b_var_screeninfo信息???
if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
return -errno;
// 首先计算显示屏的密度,即每英寸有多少个像素点,分别宽度和高度两个维度,分别保存在变量xdpi和ydpi中。注意,fb_var_screeninfo结构体info的成员变量width和height用来描述显示屏的宽度和高度,它们是以毫米(mm)为单位的。
这段代码接着再将前面计算得到的显示屏刷新频率的单位由10E-3 HZ转换为HZ,即帧每秒,并且保存在变量fps中。
if (int(info.width) <= 0 || int(info.height) <= 0) {
// the driver doesn't return that information
// default to 160 dpi
info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
}
float xdpi = (info.xres * 25.4f) / info.width;
float ydpi = (info.yres * 25.4f) / info.height;
#ifdef MSMFB_METADATA_GET
struct msmfb_metadata metadata;
memset(&metadata, 0 , sizeof(metadata));
metadata.op = metadata_op_frame_rate;
if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
ALOGE("Error retrieving panel frame rate");
return -errno;
}
float fps = metadata.data.panel_frame_rate;
#else
//XXX: Remove reserved field usage on all baselines
//The reserved[3] field is used to store FPS by the driver.
float fps = info.reserved[3] & 0xFF;
#endif
ALOGI("using (fd=%d)\n"
"id = %s\n"
"xres = %d px\n"
"yres = %d px\n"
"xres_virtual = %d px\n"
"yres_virtual = %d px\n"
"bpp = %d\n"
"r = %2u:%u\n"
"g = %2u:%u\n"
"b = %2u:%u\n",
fd,
finfo.id,
info.xres,
info.yres,
info.xres_virtual,
info.yres_virtual,
info.bits_per_pixel,
info.red.offset, info.red.length,
info.green.offset, info.green.length,
info.blue.offset, info.blue.length
);
ALOGI("width = %d mm (%f dpi)\n"
"height = %d mm (%f dpi)\n"
"refresh rate = %.2f Hz\n",
info.width, xdpi,
info.height, ydpi,
fps
);
// 再次通过IO控制命令FBIOGET_FSCREENINFO来获得系统帧缓冲区的固定信息,并且保存在fb_fix_screeninfo结构体finfo中,接下来再使用fb_fix_screeninfo结构体finfo以及前面得到的系统帧缓冲区的其它信息来初始化参数module所描述的一个private_module_t结构体。
if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
return -errno;
if (finfo.smem_len <= 0)
return -errno;
module->flags = flags;
module->info = info;
module->finfo = finfo;
module->xdpi = xdpi;
module->ydpi = ydpi;
module->fps = fps;
module->swapInterval = 1;
CALC_INIT();
/*
* map the framebuffer
*/
int err;
// 表达式finfo.yres_virtual / info.yres计算的是整个系统帧缓冲区可以划分为多少个图形缓冲区来使用,这个数值保存在参数module所描述的一个private_module_t结构体的成员变量nmBuffers中。
module->numBuffers = info.yres_virtual / info.yres;
// bufferMask的值接着被设置为0,表示系统帧缓冲区中的所有图形缓冲区都是处于空闲状态,即它们可以分配出去给应用程序使用。
module->bufferMask = 0;
// 表达式finfo.line_length * info.yres_virtual计算的是整个系统帧缓冲区的大小,它的值等于显示屏行数(虚拟分辨率的高度值,info.yres_virtual)乘以每一行所占用的字节数(finfo.line_length)。
// 函数roundUpToPageSize用来将整个系统帧缓冲区的大小对齐到页面边界。对齐后的大小保存在变量fbSize中。
//adreno needs page aligned offsets. Align the fbsize to pagesize.
size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
module->numBuffers;
// 最后根据上面的信息,完成private_handle_t即framebuffer描述结构体的填充。
module->framebuffer = new private_handle_t(fd, fbSize,
private_handle_t::PRIV_FLAGS_USES_ION,
BUFFER_TYPE_UI,
module->fbFormat, info.xres, info.yres);
// 调用函数mmap来映射到当前进程的地址空间来的。然后把起始地址赋给private_handle_t中的base变量。到目前位置,标识framebuffer的private_hanle_t的所有域都得到填充,包括该buffer的起始地址,大小,持有者,pixelformat,可见区域的长宽像素等。
void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (vaddr == MAP_FAILED) {
ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
return -errno;
}
module->framebuffer->base = intptr_t(vaddr);
memset(vaddr, 0, fbSize);
module->currentOffset = 0;
//Enable vsync 开启VSYNC
int enable = 1;
ioctl(module->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL,
&enable);
return 0;
}
Gralloc Open过程总结:整个open过程需要初始化两个设备:alloc_device& fb_device,本质上就是完成alloc_device& fb_device两个设备描述结构体private_module_t& framebuffer_device_t的填充,以及framebuffer描述结构体private_handle_t的填充。而填充这些需要的参数大部分通过打开fb0,然后调用ioctl获取到fb_var_screeninfo &fb_fix_screeninfo的信息来计算得到的。最后,通过mmp分配到所需的内存,将起始地址填充到private_handle_t中的base域中。
- Android研究_Gralloc_3fb设备Open过程
- Android研究_Gralloc_4alloc设备open过程分析
- open cv研究笔记之:find-Obj程序过程
- Android蓝牙扫描设备过程
- android 7.1 服务进程 open串口设备失败
- 对Android启动过程的进一步研究
- Android启动过程的深入研究
- Android启动过程的深入研究
- open vswitch研究:datapath
- open vswitch研究:flow
- open vswitch研究: action
- open vswitch研究:vswitchd
- open vswitch研究:utility
- open vswitch研究: ovsdb
- open vswitch研究:datapath
- open vswitch研究:vswitchd
- android输入设备配置文件加载过程分析
- 字符设备OPEN
- 如何搭建高可用架构(二)--Nginx双机主备(Keepalived实现)
- Bootstrap(三十五)
- Application.mk的用處
- 如何搭建高可用架构(三)--Nginx双主热备(Keepalived实现)
- Pro Power BI Desktop, 2nd Edition.pdf 2017 英文原版 免费下载
- Android研究_Gralloc_3fb设备Open过程
- spring-mvc-jpa环境搭建
- ZOJ 1008 Gnome Tetravex (使用状态进行DFS)
- 欢迎使用CSDN-markdown编辑器
- SparkStreaming之foreachRDD
- 如何搭建高可用架构(四)--负载均衡(LVS实现)
- Centos7.4安装Nginx及优化(一)
- maven 运行后提示数组越界问题解决。
- Bootstrap(三十六)