zoukankan      html  css  js  c++  java
  • Android开机动画

    Android开机动画

    一 前言

    标题3

    标题4

    Android的开机动画分为如下三个画面:

    • 内核启动过程出现的静态画面
    • Init进程启动过程出现的静态画面
    • 系统服务启动过程中出现的动态画面 
      下文我们就从源代码中一步一步的分析帧缓冲区(frame buffer)的硬件设备上进行渲染的过程。

    二 内核启动过程出现的静态画面

    Android开机的第一关画面其实是Linux内核的启动画面。在默认情况下,这个画面不会出现(我们可以使用make menuconfig查看)。 
    如果我们想要在启动的过程中看到这个画面,可以在编译内核的时候,启用以下两个编译选项:

    • CONFIG_FRAMEBUFFER_CONSOLE 
      Device Drivers —> Graphics support —> Support for frame buffer devices
    • CONFIG_LOGO 
      Device Drivers —> Graphics support —> Bootup logo

    初始化帧缓冲区

    帧缓冲区硬件设备在kernel中对于的驱动程序模块为fbmem,它的初始化代码如下: 
    kernel/shamu/drivers/video/fbmem.c

    /**
    * fbmem_init - init frame buffer subsystem
    *
    * Initialize the frame buffer subsystem.
    *
    * NOTE: This function is _only_ to be called by drivers/char/mem.c.
    *
    */

    static int __init
    fbmem_init(void)
    {
    /**
    * 调用函数proc_create在/proc目录下创建了一个fb文件
    */
    proc_create("fb", 0, NULL, &fb_proc_fops);

    /**
    * 注册fd字符设备
    */
    if (register_chrdev(FB_MAJOR,"fb",&fb_fops))
    printk("unable to get major %d for fb devs ", FB_MAJOR);

    /**
    * 在/sys/class目录下创建了一个graphics目录,主要是描述内核的图形系统
    */
    fb_class = class_create(THIS_MODULE, "graphics");
    if (IS_ERR(fb_class)) {
    printk(KERN_WARNING "Unable to create fb class; errno = %ld ", PTR_ERR(fb_class));
    fb_class = NULL;
    }
    return 0;
    }

    从fbmem_init函数中我们可以知道,在函数初始化工程中主要做了如下工作:

    • 调用函数proc_create(“fb”, 0, NULL, &fb_proc_fops);在/proc目录下创建fb文件
    • 接着又调用函数register_chrdev(FB_MAJOR,”fb”,&fb_fops);注册字符设备fb
    • 调用函数class_create(THIS_MODULE, “graphics”);在/sys/class目录下创建了一个graphics目录,主要是描述内核的图形系统

    驱动模块fbmem初始化工作完成后,就会导出一个函数register_framebuffer(此函数在内核的启动过程会被调用)。

    EXPORT_SYMBOL(register_framebuffer);

    注册帧缓冲区

    kernel/shamu/drivers/video/fbmem.c

    /**
    * register_framebuffer - registers a frame buffer device
    * @fb_info: frame buffer info structure
    *
    * Registers a frame buffer device @fb_info.
    *
    * Returns negative errno on error, or zero for success.
    *
    */
    int
    register_framebuffer(struct fb_info *fb_info)
    {
    int ret;

    mutex_lock(&registration_lock);
    ret = do_register_framebuffer(fb_info);/*每一个帧缓冲区硬件都是使用一个结构体fb_info来描述*/
    mutex_unlock(&registration_lock);

    return ret;
    }
    static int do_register_framebuffer(struct fb_info *fb_info)
    {
    int i;
    struct fb_event event;
    struct fb_videomode mode;

    if (fb_check_foreignness(fb_info))
    return -ENOSYS;

    do_remove_conflicting_framebuffers(fb_info->apertures, fb_info->fix.id,
    fb_is_primary_device(fb_info));

    if (num_registered_fb == FB_MAX)
    return -ENXIO;

    num_registered_fb++;
    for (i = 0 ; i < FB_MAX; i++)
    if (!registered_fb[i])
    break;
    fb_info->node = i;
    atomic_set(&fb_info->count, 1);
    mutex_init(&fb_info->lock);
    mutex_init(&fb_info->mm_lock);

    fb_info->dev = device_create(fb_class, fb_info->device,
    MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
    if (IS_ERR(fb_info->dev)) {
    /* Not fatal */
    printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld ", i, PTR_ERR(fb_info->dev));
    fb_info->dev = NULL;
    } else
    fb_init_device(fb_info);

    if (fb_info->pixmap.addr == NULL) {
    fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
    if (fb_info->pixmap.addr) {
    fb_info->pixmap.size = FBPIXMAPSIZE;
    fb_info->pixmap.buf_align = 1;
    fb_info->pixmap.scan_align = 1;
    fb_info->pixmap.access_align = 32;
    fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
    }
    }
    fb_info->pixmap.offset = 0;

    if (!fb_info->pixmap.blit_x)
    fb_info->pixmap.blit_x = ~(u32)0;

    if (!fb_info->pixmap.blit_y)
    fb_info->pixmap.blit_y = ~(u32)0;

    if (!fb_info->modelist.prev || !fb_info->modelist.next)
    INIT_LIST_HEAD(&fb_info->modelist);

    if (fb_info->skip_vt_switch)
    pm_vt_switch_required(fb_info->dev, false);
    else
    pm_vt_switch_required(fb_info->dev, true);

    fb_var_to_videomode(&mode, &fb_info->var);
    fb_add_videomode(&mode, &fb_info->modelist);
    registered_fb[i] = fb_info;/*数组registered_fb保存所有已经注册帧缓冲区的硬件设备*/

    event.info = fb_info;
    if (!lock_fb_info(fb_info))
    return -ENODEV;
    console_lock();
    fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);/*通知帧缓冲区控制台,有一个新的帧缓冲区设备被注册到内核中了*/
    console_unlock();
    unlock_fb_info(fb_info);
    return 0;
    }

    帧缓冲设备为标准的字符型设备,在Linux中主设备号29,定义在/include/linux/major.h中的FB_MAJOR(29),次设备号定义帧缓冲的个数,最大允许有32个FrameBuffer,定义在/include/linux/fb.h中的FB_MAX,对应于文件系统下/dev /fb%d设备文件。用户空间的应用程序通过这个设备文件就可以操作帧缓冲区硬件设备了,即将要显示的画面渲染到帧缓冲区硬件设备上去。 
    正因为在系统中可能会存在多个帧缓冲区硬件设备,所以fbmem模块使用一个数组registered_fb保存所有已经注册了的帧缓冲区硬件设备,其中,每一个帧缓冲区硬件都是使用一个结构体fb_info来描述的。

    帧缓冲区控制台

    帧缓冲区控制台在内核中的驱动程序模块为fbcon, 其初始化如下: 
    kernel/shamu/drivers/video/console/fbcon.c

    static int __init fb_console_init(void)
    {
    int i;

    console_lock();
    /**
    * 监听帧缓冲区硬件设备的注册事件
    */
    fb_register_client(&fbcon_event_notifier);
    /**
    * 创建类型为graphics的设备fbcon
    */
    fbcon_device = device_create(fb_class, NULL, MKDEV(0, 0), NULL,
    "fbcon");

    if (IS_ERR(fbcon_device)) {
    printk(KERN_WARNING "Unable to create device "
    "for fbcon; errno = %ld ",
    PTR_ERR(fbcon_device));
    fbcon_device = NULL;
    } else
    fbcon_init_device();

    for (i = 0; i < MAX_NR_CONSOLES; i++)
    con2fb_map[i] = -1;

    console_unlock();
    fbcon_start();
    return 0;
    }

    监听帧缓冲区硬件设备的注册事件, 函数fbcon_event_notify来实现:

    static struct notifier_block fbcon_event_notifier = {
    .notifier_call = fbcon_event_notify,
    };
    static int fbcon_event_notify(struct notifier_block *self,
    unsigned long action, void *data)
    {
    struct fb_event *event = data;
    struct fb_info *info = event->info;
    struct fb_videomode *mode;
    struct fb_con2fbmap *con2fb;
    struct fb_blit_caps *caps;
    int idx, ret = 0;

    /*
    * ignore all events except driver registration and deregistration
    * if fbcon is not active
    */
    if (fbcon_has_exited && !(action == FB_EVENT_FB_REGISTERED ||
    action == FB_EVENT_FB_UNREGISTERED))
    goto done;

    switch(action) {
    case FB_EVENT_SUSPEND:
    fbcon_suspended(info);
    break;
    case FB_EVENT_RESUME:
    fbcon_resumed(info);
    break;
    case FB_EVENT_MODE_CHANGE:
    fbcon_modechanged(info);
    break;
    case FB_EVENT_MODE_CHANGE_ALL:
    fbcon_set_all_vcs(info);
    break;
    case FB_EVENT_MODE_DELETE:
    mode = event->data;
    ret = fbcon_mode_deleted(info, mode);
    break;
    case FB_EVENT_FB_UNBIND:
    idx = info->node;
    ret = fbcon_fb_unbind(idx);
    break;
    case FB_EVENT_FB_REGISTERED:/*帧缓冲区硬件设备的注册事件最终是函数fbcon_fb_registere完成我*/
    ret = fbcon_fb_registered(info);
    break;
    case FB_EVENT_FB_UNREGISTERED:
    ret = fbcon_fb_unregistered(info);
    break;
    case FB_EVENT_SET_CONSOLE_MAP:
    /* called with console lock held */
    con2fb = event->data;
    ret = set_con2fb_map(con2fb->console - 1,
    con2fb->framebuffer, 1);
    break;
    case FB_EVENT_GET_CONSOLE_MAP:
    con2fb = event->data;
    con2fb->framebuffer = con2fb_map[con2fb->console - 1];
    break;
    case FB_EVENT_BLANK:
    fbcon_fb_blanked(info, *(int *)event->data);
    break;
    case FB_EVENT_NEW_MODELIST:
    fbcon_new_modelist(info);
    break;
    case FB_EVENT_GET_REQ:
    caps = event->data;
    fbcon_get_requirement(info, caps);
    break;
    case FB_EVENT_REMAP_ALL_CONSOLE:
    idx = info->node;
    fbcon_remap_all(idx);
    break;
    }
    done:
    return ret;
    }
    /* called with console_lock held */
    static int fbcon_fb_registered(struct fb_info *info)
    {
    int ret = 0, i, idx;

    idx = info->node;
    fbcon_select_primary(info);

    if (info_idx == -1) {
    for (i = first_fb_vc; i <= last_fb_vc; i++) {
    if (con2fb_map_boot[i] == idx) {
    info_idx = idx;
    break;
    }
    }

    if (info_idx != -1)
    ret = do_fbcon_takeover(1);
    } else {
    for (i = first_fb_vc; i <= last_fb_vc; i++) {
    if (con2fb_map_boot[i] == idx)
    set_con2fb_map(i, idx, 0);
    }
    }

    return ret;
    }

    在函数fbcon_fb_registered中主要完成了如下工作:

    • 调用函数fbcon_select_primary检查当前注册的帧缓冲区硬件设备是否是一个主帧缓冲区硬件设备 
      • 是—–>将信息保存
      • 否—–>
    • first_fb_vc和last_fb_vc(是数组con2fb_map_boot和con2fb_map的索引值): 用来指定系统当前可用的控制台编号范围,它们也是可以通过设置内核启动参数来初始化
    • info_idx: 表示系统当前所使用的帧缓冲区硬件的编号 
      • info_idx = -1—–>系统还没有设置好当前所使用的帧缓冲区硬件设备。 
        • 在数组con2fb_map_boot中检查是否存在一个控制台编号与当前所注册的帧缓冲区硬件设备的编号idx对应
        • 调用函数fbcon_takeover来初始化系统所使用的控制台(函数fbcon_takeove传进去的参数为1,表示要显示第一个开机画面。)
      • info_idx != -1 —–> 同样会在数组con2fb_map_boot中检查是否存在一个控制台编号与当前所注册的帧缓冲区硬件设备的编号idx对应。若存在,则调用函数set_con2fb_map来调整当前所注册的帧缓冲区硬件设备与控制台的映射关系,即调整数组con2fb_map_boot和con2fb_map的值。

    假设info_idx = -1, 也就是系统只有一个帧缓冲区硬件设备,所以在数组con2fb_map_boot中发现有一个控制台的编号与这个帧缓冲区硬件设备的编号idx对应,下来就会调用函数fbcon_takeover来设置系统所使用的控制台。

    static int fbcon_takeover(int show_logo)
    {
    int err, i;

    if (!num_registered_fb)
    return -ENODEV;

    if (!show_logo)
    logo_shown = FBCON_LOGO_DONTSHOW;

    /**
    * 将当前可用的控制台的编号都映射到当前正在注册的帧缓冲区硬件设备的编号info_idx中去,表示当前可用的控制台与缓冲区硬件设备的实际映射关系。
    */
    for (i = first_fb_vc; i <= last_fb_vc; i++)
    con2fb_map[i] = info_idx;

    err = take_over_console(&fb_con, first_fb_vc, last_fb_vc,
    fbcon_is_default);
    /**
    * 如果它的返回值不等于0,那么就表示初始化失败。
    * 所以将数组con2fb_map的各个元素的值设置为-1,表示系统当前可用的控制台还没有映射到实际的帧缓冲区硬件设备中。
    * 这时候全局变量info_idx的值也会被重新设置为-1。
    */
    if (err) {
    for (i = first_fb_vc; i <= last_fb_vc; i++) {
    con2fb_map[i] = -1;
    }
    info_idx = -1;
    } else {
    fbcon_has_console_bind = 1;
    }

    return err;
    }
    • logo_shown是一个全局变量,它的初始值为FBCON_LOGO_CANSHOW,表示可以显示第一个开机画面。
    • 如果传进来的show_logo的值为0,全局变量logo_shown的值会被重新设置为FBCON_LOGO_DONTSHOW,表示不可以显示第一个开机画面。
    • 把当前可以用的控制台编号映射到正在注册的帧缓冲区硬件设备的编号info_idx中
    • take_over_console: 初始化系统当前所使用的控制台
    • 函数take_over_console初始化系统当前所使用的控制台,实际上就是向系统注册一系列回调函数,以便系统可以通过这些回调函数来操作当前所使用的控制台。这些回调函数使用结构体consw来描述。这里所注册的结构体consw是由全局变量fb_con来指定的,它的定义如下所示
    /*
    * The console `switch' structure for the frame buffer based console
    */

    static const struct consw fb_con = {
    .owner = THIS_MODULE,
    .con_startup = fbcon_startup,
    .con_init = fbcon_init,
    .con_deinit = fbcon_deinit,
    .con_clear = fbcon_clear,
    .con_putc = fbcon_putc,
    .con_putcs = fbcon_putcs,
    .con_cursor = fbcon_cursor,
    .con_scroll = fbcon_scroll,
    .con_bmove = fbcon_bmove,
    .con_switch = fbcon_switch,
    .con_blank = fbcon_blank,
    .con_font_set = fbcon_set_font,
    .con_font_get = fbcon_get_font,
    .con_font_default = fbcon_set_def_font,
    .con_font_copy = fbcon_copy_font,
    .con_set_palette = fbcon_set_palette,
    .con_scrolldelta = fbcon_scrolldelta,
    .con_set_origin = fbcon_set_origin,
    .con_invert_region = fbcon_invert_region,
    .con_screen_pos = fbcon_screen_pos,
    .con_getxy = fbcon_getxy,
    .con_resize = fbcon_resize,
    .con_debug_enter = fbcon_debug_enter,
    .con_debug_leave = fbcon_debug_leave,
    };

    初始化控制台

    在这一过程中我们只需关注函数fbcon_init和fbcon_switch的实现,系统就是通过它来初始化和切换控制台的。在初始化的过程中,会决定是否需要准备第一个开机画面的内容,而在切换控制台的过程中,会决定是否需要显示第一个开机画面的内容。 
    fbcon_init初始化的过程中:

    static void fbcon_init(struct vc_data *vc, int init)
    {
    struct fb_info *info = registered_fb[con2fb_map[vc->vc_num]];
    struct fbcon_ops *ops;
    struct vc_data **default_mode = vc->vc_display_fg;
    struct vc_data *svc = *default_mode;
    struct display *t, *p = &fb_display[vc->vc_num];
    int logo = 1, new_rows, new_cols, rows, cols, charcnt = 256;
    int cap, ret;

    if (info_idx == -1 || info == NULL)
    return;

    cap = info->flags;

    if (vc != svc || logo_shown == FBCON_LOGO_DONTSHOW ||
    (info->fix.type == FB_TYPE_TEXT))
    logo = 0;

    if (var_to_display(p, &info->var, info))
    return;

    if (!info->fbcon_par)
    con2fb_acquire_newinfo(vc, info, vc->vc_num, -1);

    /* If we are not the first console on this
    fb, copy the font from that console */
    t = &fb_display[fg_console];
    if (!p->fontdata) {
    if (t->fontdata) {
    struct vc_data *fvc = vc_cons[fg_console].d;

    vc->vc_font.data = (void *)(p->fontdata =
    fvc->vc_font.data);
    vc->vc_font.width = fvc->vc_font.width;
    vc->vc_font.height = fvc->vc_font.height;
    p->userfont = t->userfont;

    if (p->userfont)
    REFCOUNT(p->fontdata)++;
    } else {
    const struct font_desc *font = NULL;

    if (!fontname[0] || !(font = find_font(fontname)))
    font = get_default_font(info->var.xres,
    info->var.yres,
    info->pixmap.blit_x,
    info->pixmap.blit_y);
    vc->vc_font.width = font->width;
    vc->vc_font.height = font->height;
    vc->vc_font.data = (void *)(p->fontdata = font->data);
    vc->vc_font.charcount = 256; /* FIXME Need to
    support more fonts */
    }
    }

    if (p->userfont)
    charcnt = FNTCHARCNT(p->fontdata);

    vc->vc_panic_force_write = !!(info->flags & FBINFO_CAN_FORCE_OUTPUT);
    vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
    vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
    if (charcnt == 256) {
    vc->vc_hi_font_mask = 0;
    } else {
    vc->vc_hi_font_mask = 0x100;
    if (vc->vc_can_do_color)
    vc->vc_complement_mask <<= 1;
    }

    if (!*svc->vc_uni_pagedir_loc)
    con_set_default_unimap(svc);
    if (!*vc->vc_uni_pagedir_loc)
    con_copy_unimap(vc, svc);

    ops = info->fbcon_par;
    p->con_rotate = initial_rotation;
    set_blitting_type(vc, info);

    cols = vc->vc_cols;
    rows = vc->vc_rows;
    new_cols = FBCON_SWAP(ops->rotate, info->var.xres, info->var.yres);
    new_rows = FBCON_SWAP(ops->rotate, info->var.yres, info->var.xres);
    new_cols /= vc->vc_font.width;
    new_rows /= vc->vc_font.height;

    /*
    * We must always set the mode. The mode of the previous console
    * driver could be in the same resolution but we are using different
    * hardware so we have to initialize the hardware.
    *
    * We need to do it in fbcon_init() to prevent screen corruption.
    */
    if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
    if (info->fbops->fb_set_par &&
    !(ops->flags & FBCON_FLAGS_INIT)) {
    ret = info->fbops->fb_set_par(info);

    if (ret)
    printk(KERN_ERR "fbcon_init: detected "
    "unhandled fb_set_par error, "
    "error code %d ", ret);
    }

    ops->flags |= FBCON_FLAGS_INIT;
    }

    ops->graphics = 0;

    if ((cap & FBINFO_HWACCEL_COPYAREA) &&
    !(cap & FBINFO_HWACCEL_DISABLED))
    p->scrollmode = SCROLL_MOVE;
    else /* default to something safe */
    p->scrollmode = SCROLL_REDRAW;

    /*
    * ++guenther: console.c:vc_allocate() relies on initializing
    * vc_{cols,rows}, but we must not set those if we are only
    * resizing the console.
    */
    if (init) {
    vc->vc_cols = new_cols;
    vc->vc_rows = new_rows;
    } else
    vc_resize(vc, new_cols, new_rows);

    if (logo)
    fbcon_prepare_logo(vc, info, cols, rows, new_cols, new_rows);

    if (vc == svc && softback_buf)
    fbcon_update_softback(vc);

    if (ops->rotate_font && ops->rotate_font(info, vc)) {
    ops->rotate = FB_ROTATE_UR;
    set_blitting_type(vc, info);
    }

    ops->p = &fb_display[fg_console];
    }
    • vc: 当前正在初始化的控制台 
      • vc_num: 当前正在初始化的控制台的编号,可以在数组con2fb_map中通过这个编号找到对应的帧缓冲区硬件设备编号。有帧缓冲区硬件设备编号后,就可以在另外一个数组registered_fb中找到一个fb_info结构体info,用来描述与当前正在初始化的控制台所对应的帧缓冲区硬件设备。
      • vc_display_fg: 系统当前可见的控制台,它是一个类型为vc_data**的指针。从这里就可以看出,最终得到的vc_data结构体svc就是用来描述系统当前可见的控制台的。
    • 变量logo开始的时候被设置为1,表示需要显示第一个开机画面。但是以下三种情况它的值会被设置为0,表示不需要显示开机画面: 
      • 参数vc和变量svc指向的不是同一个vc_data结构体,即当前正在初始化的控制台不是系统当前可见的控制台。
      • 全局变量logo_shown的值等于FBCON_LOGO_DONTSHOW,即系统不需要显示第一个开机画面。
      • 与当前正在初始化的控制台所对应的帧缓冲区硬件设备的显示方式被设置为文本方式,即info->fix.type的值等于FB_TYPE_TEXT。

    当最终得到的变量logo的值等于1的时候,接下来就会调用函数fbcon_prepare_logo来准备要显示的第一个开机画面的内容。

    #ifdef MODULE
    static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
    int cols, int rows, int new_cols, int new_rows)
    {
    logo_shown = FBCON_LOGO_DONTSHOW;
    }
    #else
    static void fbcon_prepare_logo(struct vc_data *vc, struct fb_info *info,
    int cols, int rows, int new_cols, int new_rows)
    {
    /* Need to make room for the logo */
    struct fbcon_ops *ops = info->fbcon_par;
    int cnt, erase = vc->vc_video_erase_char, step;
    unsigned short *save = NULL, *r, *q;
    int logo_height;

    if (info->flags & FBINFO_MODULE) {
    logo_shown = FBCON_LOGO_DONTSHOW;
    return;
    }

    /*
    * remove underline attribute from erase character
    * if black and white framebuffer.
    */
    if (fb_get_color_depth(&info->var, &info->fix) == 1)
    erase &= ~0x400;
    logo_height = fb_prepare_logo(info, ops->rotate);
    logo_lines = DIV_ROUND_UP(logo_height, vc->vc_font.height);
    q = (unsigned short *) (vc->vc_origin +
    vc->vc_size_row * rows);
    step = logo_lines * cols;
    for (r = q - logo_lines * cols; r < q; r++)
    if (scr_readw(r) != vc->vc_video_erase_char)
    break;
    if (r != q && new_rows >= rows + logo_lines) {
    save = kmalloc(logo_lines * new_cols * 2, GFP_KERNEL);
    if (save) {
    int i = cols < new_cols ? cols : new_cols;
    scr_memsetw(save, erase, logo_lines * new_cols * 2);
    r = q - step;
    for (cnt = 0; cnt < logo_lines; cnt++, r += i)
    scr_memcpyw(save + cnt * new_cols, r, 2 * i);
    r = q;
    }
    }
    if (r == q) {
    /* We can scroll screen down */
    r = q - step - cols;
    for (cnt = rows - logo_lines; cnt > 0; cnt--) {
    scr_memcpyw(r + step, r, vc->vc_size_row);
    r -= cols;
    }
    if (!save) {
    int lines;
    if (vc->vc_y + logo_lines >= rows)
    lines = rows - vc->vc_y - 1;
    else
    lines = logo_lines;
    vc->vc_y += lines;
    vc->vc_pos += lines * vc->vc_size_row;
    }
    }
    scr_memsetw((unsigned short *) vc->vc_origin,
    erase,
    vc->vc_size_row * logo_lines);

    if (CON_IS_VISIBLE(vc) && vc->vc_mode == KD_TEXT) {
    fbcon_clear_margins(vc, 0);
    update_screen(vc);
    }

    if (save) {
    q = (unsigned short *) (vc->vc_origin +
    vc->vc_size_row *
    rows);
    scr_memcpyw(q, save, logo_lines * new_cols * 2);
    vc->vc_y += logo_lines;
    vc->vc_pos += logo_lines * vc->vc_size_row;
    kfree(save);
    }

    if (logo_lines > vc->vc_bottom) {
    logo_shown = FBCON_LOGO_CANSHOW;
    printk(KERN_INFO
    "fbcon_init: disable boot-logo (boot-logo bigger than screen). ");
    } else if (logo_shown != FBCON_LOGO_DONTSHOW) {
    logo_shown = FBCON_LOGO_DRAW;
    vc->vc_top = logo_lines;
    }
    }
    #endif /* MODULE */

    在fbcon_prepare_logo函数中,第一个开机画面的内容是通过调用fb_prepare_logo函数来准备的。 
    从函数fb_prepare_logo返回来之后,如果要显示的第一个开机画面所占用的控制台行数小于等于参数vc所描述的控制台的最大行数,并且全局变量logo_show的值不等于FBCON_LOGO_DONTSHOW,那么就说明前面所提到的第一个开机画面可以显示在控制台中。这时候全局变量logo_show的值就会被设置为FBCON_LOGO_DRAW,表示第一个开机画面处于等待渲染的状态。

    kernel/shamu/drivers/video/fbmem.c

    int fb_prepare_logo(struct fb_info *info, int rotate)
    {
    int depth = fb_get_color_depth(&info->var, &info->fix);
    unsigned int yres;

    memset(&fb_logo, 0, sizeof(struct logo_data));

    if (info->flags & FBINFO_MISC_TILEBLITTING ||
    info->flags & FBINFO_MODULE)
    return 0;

    if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
    depth = info->var.blue.length;
    if (info->var.red.length < depth)
    depth = info->var.red.length;
    if (info->var.green.length < depth)
    depth = info->var.green.length;
    }

    if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
    /* assume console colormap */
    depth = 4;
    }

    /* Return if no suitable logo was found */
    fb_logo.logo = fb_find_logo(depth);

    if (!fb_logo.logo) {
    return 0;
    }

    if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
    yres = info->var.yres;
    else
    yres = info->var.xres;

    if (fb_logo.logo->height > yres) {
    fb_logo.logo = NULL;
    return 0;
    }

    /* What depth we asked for might be different from what we get */
    if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
    fb_logo.depth = 8;
    else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
    fb_logo.depth = 4;
    else
    fb_logo.depth = 1;


    if (fb_logo.depth > 4 && depth > 4) {
    switch (info->fix.visual) {
    case FB_VISUAL_TRUECOLOR:
    fb_logo.needs_truepalette = 1;
    break;
    case FB_VISUAL_DIRECTCOLOR:
    fb_logo.needs_directpalette = 1;
    fb_logo.needs_cmapreset = 1;
    break;
    case FB_VISUAL_PSEUDOCOLOR:
    fb_logo.needs_cmapreset = 1;
    break;
    }
    }

    return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
    }
    • 得到参数info所描述的帧缓冲区硬件设备的颜色深度depth
    • 调用函数fb_find_logo来获得要显示的第一个开机画面的内容,并且保存在全局变量fb_logo的变量logo中

    kernel/shamu/drivers/video/logo/logo.c

    #include <linux/linux_logo.h>
    #include <linux/stddef.h>
    #include <linux/module.h>

    #ifdef CONFIG_M68K
    #include <asm/setup.h>
    #endif

    #ifdef CONFIG_MIPS
    #include <asm/bootinfo.h>
    #endif

    static bool nologo;
    module_param(nologo, bool, 0);
    MODULE_PARM_DESC(nologo, "Disables startup logo");

    /* logo's are marked __initdata. Use __init_refok to tell
    * modpost that it is intended that this function uses data
    * marked __initdata.
    */
    const struct linux_logo * __init_refok fb_find_logo(int depth)
    {
    const struct linux_logo *logo = NULL;

    if (nologo)
    return NULL;

    if (depth >= 1) {
    #ifdef CONFIG_LOGO_LINUX_MONO
    /* Generic Linux logo */
    logo = &logo_linux_mono;
    #endif
    #ifdef CONFIG_LOGO_SUPERH_MONO
    /* SuperH Linux logo */
    logo = &logo_superh_mono;
    #endif
    }

    if (depth >= 4) {
    #ifdef CONFIG_LOGO_LINUX_VGA16
    /* Generic Linux logo */
    logo = &logo_linux_vga16;
    #endif
    #ifdef CONFIG_LOGO_BLACKFIN_VGA16
    /* Blackfin processor logo */
    logo = &logo_blackfin_vga16;
    #endif
    #ifdef CONFIG_LOGO_SUPERH_VGA16
    /* SuperH Linux logo */
    logo = &logo_superh_vga16;
    #endif
    }

    if (depth >= 8) {
    #ifdef CONFIG_LOGO_LINUX_CLUT224
    /* Generic Linux logo */
    logo = &logo_linux_clut224;
    #endif
    #ifdef CONFIG_LOGO_BLACKFIN_CLUT224
    /* Blackfin Linux logo */
    logo = &logo_blackfin_clut224;
    #endif
    #ifdef CONFIG_LOGO_DEC_CLUT224
    /* DEC Linux logo on MIPS/MIPS64 or ALPHA */
    logo = &logo_dec_clut224;
    #endif
    #ifdef CONFIG_LOGO_MAC_CLUT224
    /* Macintosh Linux logo on m68k */
    if (MACH_IS_MAC)
    logo = &logo_mac_clut224;
    #endif
    #ifdef CONFIG_LOGO_PARISC_CLUT224
    /* PA-RISC Linux logo */
    logo = &logo_parisc_clut224;
    #endif
    #ifdef CONFIG_LOGO_SGI_CLUT224
    /* SGI Linux logo on MIPS/MIPS64 and VISWS */
    logo = &logo_sgi_clut224;
    #endif
    #ifdef CONFIG_LOGO_SUN_CLUT224
    /* Sun Linux logo */
    logo = &logo_sun_clut224;
    #endif
    #ifdef CONFIG_LOGO_SUPERH_CLUT224
    /* SuperH Linux logo */
    logo = &logo_superh_clut224;
    #endif
    #ifdef CONFIG_LOGO_M32R_CLUT224
    /* M32R Linux logo */
    logo = &logo_m32r_clut224;
    #endif
    }
    return logo;
    }
    EXPORT_SYMBOL_GPL(fb_find_logo);
    • 声明一系列linux_logo结构体变量,用来保存kernel/shamu/drivers/video/logo目录下的一系列ppm或者pbm文件的内容。这些ppm或者pbm文件都是用来描述第一个开机画面的。
    • 全局变量nologo是一个类型为布尔变量的模块参数,它的默认值等于0,表示要显示第一个开机画面。在这种情况下,函数fb_find_logo就会根据参数depth的值以及不同的编译选项来选择第一个开机画面的内容,并且保存在变量logo中返回给调用者。

    完成这个过程之后,第一个开机画面的内容就保存在模块fbmem的全局变量fb_logo的变量logo中。这时候控制台的初始化过程也就结束了,接下来系统就会执行切换控制台的操作。前面提到,当系统执行切换控制台的操作的时候,模块fbcon中的函数fbcon_switch就会被调用。在调用的过程中,就会执行显示第一个开机画面的操作。

    显示第一个开机界面

    kernel/shamu/drivers/video/console/fbcon.c 
    显示第一个开机画面的过程如下:

    ``` cppstatic int fbcon_switch(struct vc_data *vc) 

    struct fb_info *info, *old_info = NULL; 
    struct fbcon_ops *ops; 
    struct display *p = &fb_display[vc->vc_num]; 
    struct fb_var_screeninfo var; 
    int i, ret, prev_console, charcnt = 256;

    info = registered_fb[con2fb_map[vc->vc_num]];
    ops = info->fbcon_par;

    if (softback_top) {
    if (softback_lines)
    fbcon_set_origin(vc);
    softback_top = softback_curr = softback_in = softback_buf;
    softback_lines = 0;
    fbcon_update_softback(vc);
    }

    if (logo_shown >= 0) {
    struct vc_data *conp2 = vc_cons[logo_shown].d;

    if (conp2->vc_top == logo_lines
    && conp2->vc_bottom == conp2->vc_rows)
    conp2->vc_top = 0;
    logo_shown = FBCON_LOGO_CANSHOW;
    }

    prev_console = ops->currcon;
    if (prev_console != -1)
    old_info = registered_fb[con2fb_map[prev_console]];
    /*
    * FIXME: If we have multiple fbdev's loaded, we need to
    * update all info->currcon. Perhaps, we can place this
    * in a centralized structure, but this might break some
    * drivers.
    *
    * info->currcon = vc->vc_num;
    */
    for (i = 0; i < FB_MAX; i++) {
    if (registered_fb[i] != NULL && registered_fb[i]->fbcon_par) {
    struct fbcon_ops *o = registered_fb[i]->fbcon_par;

    o->currcon = vc->vc_num;
    }
    }
    memset(&var, 0, sizeof(struct fb_var_screeninfo));
    display_to_var(&var, p);
    var.activate = FB_ACTIVATE_NOW;

    /*
    * make sure we don't unnecessarily trip the memcmp()
    * in fb_set_var()
    */
    info->var.activate = var.activate;
    var.vmode |= info->var.vmode & ~FB_VMODE_MASK;
    fb_set_var(info, &var);
    ops->var = info->var;

    if (old_info != NULL && (old_info != info ||
    info->flags & FBINFO_MISC_ALWAYS_SETPAR)) {
    if (info->fbops->fb_set_par) {
    ret = info->fbops->fb_set_par(info);

    if (ret)
    printk(KERN_ERR "fbcon_switch: detected "
    "unhandled fb_set_par error, "
    "error code %d ", ret);
    }

    if (old_info != info)
    fbcon_del_cursor_timer(old_info);
    }

    if (fbcon_is_inactive(vc, info) ||
    ops->blank_state != FB_BLANK_UNBLANK)
    fbcon_del_cursor_timer(info);
    else
    fbcon_add_cursor_timer(info);

    set_blitting_type(vc, info);
    ops->cursor_reset = 1;

    if (ops->rotate_font && ops->rotate_font(info, vc)) {
    ops->rotate = FB_ROTATE_UR;
    set_blitting_type(vc, info);
    }

    vc->vc_can_do_color = (fb_get_color_depth(&info->var, &info->fix)!=1);
    vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;

    if (p->userfont)
    charcnt = FNTCHARCNT(vc->vc_font.data);

    if (charcnt > 256)
    vc->vc_complement_mask <<= 1;

    updatescrollmode(p, info, vc);

    switch (p->scrollmode) {
    case SCROLL_WRAP_MOVE:
    scrollback_phys_max = p->vrows - vc->vc_rows;
    break;
    case SCROLL_PAN_MOVE:
    case SCROLL_PAN_REDRAW:
    scrollback_phys_max = p->vrows - 2 * vc->vc_rows;
    if (scrollback_phys_max < 0)
    scrollback_phys_max = 0;
    break;
    default:
    scrollback_phys_max = 0;
    break;
    }

    scrollback_max = 0;
    scrollback_current = 0;

    if (!fbcon_is_inactive(vc, info)) {
    ops->var.xoffset = ops->var.yoffset = p->yscroll = 0;
    ops->update_start(info);
    }

    fbcon_set_palette(vc, color_table);
    fbcon_clear_margins(vc, 0);

    if (logo_shown == FBCON_LOGO_DRAW) {

    logo_shown = fg_console;
    /* This is protected above by initmem_freed */
    fb_show_logo(info, ops->rotate);
    update_region(vc,
    vc->vc_origin + vc->vc_size_row * vc->vc_top,
    vc->vc_size_row * (vc->vc_bottom -
    vc->vc_top) / 2);
    return 0;
    }
    return 1;

    }

    因为前文我们假设logo_show==FBCON_LOGO_DRAW, 所以程序会走到fb_show_logo函数来显示第一个开机画面。在显示之前,这个函数会将全局变量logo_shown的值设置为fg_console,后者表示系统当前可见的控制台的编号。

    <font color=#0099ff >kernel/shamu/drivers/video/fbmem.c</font>




    <div class="se-preview-section-delimiter"></div>

    ``` cpp
    int fb_show_logo(struct fb_info *info, int rotate)
    {
    int y;

    y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
    num_online_cpus());
    y = fb_show_extra_logos(info, y, rotate);

    return y;
    }

    调用fb_show_logo_line函数来进一步执行渲染第一个开机画面的操作。

    static int fb_show_logo_line(struct fb_info *info, int rotate,
    const struct linux_logo *logo, int y,
    unsigned int n)
    {
    u32 *palette = NULL, *saved_pseudo_palette = NULL;
    unsigned char *logo_new = NULL, *logo_rotate = NULL;
    struct fb_image image;

    /* Return if the frame buffer is not mapped or suspended */
    if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
    info->flags & FBINFO_MODULE)
    return 0;

    image.depth = 8;
    image.data = logo->data;

    if (fb_logo.needs_cmapreset)
    fb_set_logocmap(info, logo);

    if (fb_logo.needs_truepalette ||
    fb_logo.needs_directpalette) {
    palette = kmalloc(256 * 4, GFP_KERNEL);
    if (palette == NULL)
    return 0;

    if (fb_logo.needs_truepalette)
    fb_set_logo_truepalette(info, logo, palette);
    else
    fb_set_logo_directpalette(info, logo, palette);

    saved_pseudo_palette = info->pseudo_palette;
    info->pseudo_palette = palette;
    }

    if (fb_logo.depth <= 4) {
    logo_new = kmalloc(logo->width * logo->height, GFP_KERNEL);
    if (logo_new == NULL) {
    kfree(palette);
    if (saved_pseudo_palette)
    info->pseudo_palette = saved_pseudo_palette;
    return 0;
    }
    image.data = logo_new;
    fb_set_logo(info, logo, logo_new, fb_logo.depth);
    }

    image.dx = 0;
    image.dy = y;
    image.width = logo->width;
    image.height = logo->height;

    if (rotate) {
    logo_rotate = kmalloc(logo->width *
    logo->height, GFP_KERNEL);
    if (logo_rotate)
    fb_rotate_logo(info, logo_rotate, &image, rotate);
    }

    fb_do_show_logo(info, &image, rotate, n);

    kfree(palette);
    if (saved_pseudo_palette != NULL)
    info->pseudo_palette = saved_pseudo_palette;
    kfree(logo_new);
    kfree(logo_rotate);
    return logo->height;
    }

    参数logo指向了前面所准备的第一个开机画面的内容。这个函数首先根据参数logo的内容来构造一个fb_image结构体image,用来描述最终要显示的第一个开机画面。最后就调用函数fb_do_show_logo来真正执行渲染第一个开机画面的操作。

    static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
    int rotate, unsigned int num)
    {
    unsigned int x;

    if (rotate == FB_ROTATE_UR) {
    for (x = 0;
    x < num && image->dx + image->width <= info->var.xres;
    x++) {
    info->fbops->fb_imageblit(info, image);
    image->dx += image->width + 8;
    }
    } else if (rotate == FB_ROTATE_UD) {
    for (x = 0; x < num && image->dx >= 0; x++) {
    info->fbops->fb_imageblit(info, image);
    image->dx -= image->width + 8;
    }
    } else if (rotate == FB_ROTATE_CW) {
    for (x = 0;
    x < num && image->dy + image->height <= info->var.yres;
    x++) {
    info->fbops->fb_imageblit(info, image);
    image->dy += image->height + 8;
    }
    } else if (rotate == FB_ROTATE_CCW) {
    for (x = 0; x < num && image->dy >= 0; x++) {
    info->fbops->fb_imageblit(info, image);
    image->dy -= image->height + 8;
    }
    }
    }
    • rotate:描述屏幕的当前旋转方向。屏幕旋转方向不同,第一个开机画面的渲染方式也有所不同。例如,当屏幕上下颠倒时(FB_ROTATE_UD),第一个开机画面的左右顺序就刚好调换过来,这时候就需要从右到左来渲染。其它三个方向FB_ROTATE_UR、FB_ROTATE_CW和FB_ROTATE_CCW分别表示没有旋转、顺时针旋转90度和逆时针旋转90度。
    • info: 描述要渲染的帧缓冲区硬件设备,它的变量fbops指向了一系列回调函数,用来操作帧缓冲区硬件设备,其中,回调函数fb_imageblit就是用来在指定的帧缓冲区硬件设备渲染指定的图像的。

    三 Init进程启动过程出现的静态画面

    init进程的入口函数main如下所示: 
    system/core/init/init.cpp

    int main(int argc, char** argv) {
    if (!strcmp(basename(argv[0]), "ueventd")) {
    return ueventd_main(argc, argv);
    }

    if (!strcmp(basename(argv[0]), "watchdogd")) {
    return watchdogd_main(argc, argv);
    }

    // Clear the umask.
    umask(0);

    add_environment("PATH", _PATH_DEFPATH);

    bool is_first_stage = (argc == 1) || (strcmp(argv[1], "--second-stage") != 0);

    // Get the basic filesystem setup we need put together in the initramdisk
    // on / and then we'll let the rc file figure out the rest.
    if (is_first_stage) {
    mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
    mkdir("/dev/pts", 0755);
    mkdir("/dev/socket", 0755);
    mount("devpts", "/dev/pts", "devpts", 0, NULL);
    mount("proc", "/proc", "proc", 0, NULL);
    mount("sysfs", "/sys", "sysfs", 0, NULL);
    }

    // We must have some place other than / to create the device nodes for
    // kmsg and null, otherwise we won't be able to remount / read-only
    // later on. Now that tmpfs is mounted on /dev, we can actually talk
    // to the outside world.
    open_devnull_stdio();
    klog_init();
    klog_set_level(KLOG_NOTICE_LEVEL);

    NOTICE("init%s started! ", is_first_stage ? "" : " second stage");

    if (!is_first_stage) {
    // Indicate that booting is in progress to background fw loaders, etc.
    close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));

    property_init();

    // If arguments are passed both on the command line and in DT,
    // properties set in DT always have priority over the command-line ones.
    process_kernel_dt();
    process_kernel_cmdline();

    // Propogate the kernel variables to internal variables
    // used by init as well as the current required properties.
    export_kernel_boot_props();
    }

    // Set up SELinux, including loading the SELinux policy if we're in the kernel domain.
    selinux_initialize(is_first_stage);

    // If we're in the kernel domain, re-exec init to transition to the init domain now
    // that the SELinux policy has been loaded.
    if (is_first_stage) {
    if (restorecon("/init") == -1) {
    ERROR("restorecon failed: %s ", strerror(errno));
    security_failure();
    }
    char* path = argv[0];
    char* args[] = { path, const_cast<char*>("--second-stage"), nullptr };
    if (execv(path, args) == -1) {
    ERROR("execv("%s") failed: %s ", path, strerror(errno));
    security_failure();
    }
    }

    // These directories were necessarily created before initial policy load
    // and therefore need their security context restored to the proper value.
    // This must happen before /dev is populated by ueventd.
    INFO("Running restorecon... ");
    restorecon("/dev");
    restorecon("/dev/socket");
    restorecon("/dev/__properties__");
    restorecon_recursive("/sys");

    epoll_fd = epoll_create1(EPOLL_CLOEXEC);
    if (epoll_fd == -1) {
    ERROR("epoll_create1 failed: %s ", strerror(errno));
    exit(1);
    }

    signal_handler_init();

    property_load_boot_defaults();
    start_property_service();

    init_parse_config_file("/init.rc");

    action_for_each_trigger("early-init", action_add_queue_tail);

    // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
    queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
    // ... so that we can start queuing up actions that require stuff from /dev.
    queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
    queue_builtin_action(keychord_init_action, "keychord_init");
    queue_builtin_action(console_init_action, "console_init");

    // Trigger all the boot actions to get us started.
    action_for_each_trigger("init", action_add_queue_tail);

    // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
    // wasn't ready immediately after wait_for_coldboot_done
    queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");

    // Don't mount filesystems or start core system services in charger mode.
    char bootmode[PROP_VALUE_MAX];
    if (property_get("ro.bootmode", bootmode) > 0 && strcmp(bootmode, "charger") == 0) {
    action_for_each_trigger("charger", action_add_queue_tail);
    } else {
    action_for_each_trigger("late-init", action_add_queue_tail);
    }

    // Run all property triggers based on current state of the properties.
    queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");

    while (true) {
    if (!waiting_for_exec) {
    execute_one_command();
    restart_processes();
    }

    int timeout = -1;
    if (process_needs_restart) {
    timeout = (process_needs_restart - gettime()) * 1000;
    if (timeout < 0)
    timeout = 0;
    }

    if (!action_queue_empty() || cur_action) {
    timeout = 0;
    }

    bootchart_sample(&timeout);

    epoll_event ev;
    int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout));
    if (nr == -1) {
    ERROR("epoll_wait failed: %s ", strerror(errno));
    } else if (nr == 1) {
    ((void (*)()) ev.data.ptr)();
    }
    }

    return 0;
    }
    • 判断参数argv[0]的值是否等于“ueventd”,即当前正在启动的进程名称是否等于“ueventd”
    • 调用函数queue_builtin_action来向init进程中的一个待执行action队列增加了一个名称等于“console_init”的action。这个action对应的执行函数为console_init_action,它就是用来显示第二个开机画面的。queue_builtin_action(console_init_action, “console_init”);

    在while (true)死循环中,完成如下工作:

    • 调用execute_one_command函数,检查action_queue列表是否为空 
      • 不为空—–> init进程就会将保存在列表头中的action移除,并且执行这个被移除的action。由于前面我们将一个名称为“console_init”的action添加到了action_queue列表中,因此,在这个无限循环中,这个action就会被执行,即函数console_init_action会被调用。
    • 调用restart_processes函数,检查系统中是否有进程需要重启。在启动脚本/init.rc中,我们可以指定一个进程在退出之后会自动重新启动。在这种情况下,restart_processes函数就会检查是否存在需要重新启动的进程,如果存在的话,那么就会将它重新启动起来。
    • 处理系统属性变化事件。当我们调用函数property_set来改变一个系统属性值时,系统就会通过一个socket(通过调用函数get_property_set_fd可以获得它的文件描述符)来向init进程发送一个属性值改变事件通知。init进程接收到这个属性值改变事件之后,就会调用函数handle_property_set_fd来进行相应的处理。后面在分析第三个开机画面的显示过程时,我们就会看到,SurfaceFlinger服务就是通过修改“ctl.start”和“ctl.stop”属性值来启动和停止第三个开机画面的。
    • 处理一种称为“chorded keyboard”的键盘输入事件。这种类型为chorded keyboard”的键盘设备通过不同的铵键组合来描述不同的命令或者操作,它对应的设备文件为/dev/keychord。我们可以通过调用函数get_keychord_fd来获得这个设备的文件描述符,以便可以监控它的输入事件,并且调用函数handle_keychord来对这些输入事件进行处理。
    • 回收僵尸进程。我们知道,在Linux内核中,如果父进程不等待子进程结束就退出,那么当子进程结束的时候,就会变成一个僵尸进程,从而占用系统的资源。为了回收这些僵尸进程,init进程会安装一个SIGCHLD信号接收器。当那些父进程已经退出了的子进程退出的时候,内核就会发出一个SIGCHLD信号给init进程。init进程可以通过一个socket(通过调用函数get_signal_fd可以获得它的文件描述符)来将接收到的SIGCHLD信号读取回来,并且调用函数handle_signal来对接收到的SIGCHLD信号进行处理,即回收那些已经变成了僵尸的子进程。

    注意,由于后面三个事件都是可以通过文件描述符来描述的,因此,init进程的入口函数main使用poll机制来同时轮询它们,以便可以提高效率。

    system/core/init/init_parser.cpp

    void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
    {
    action* act = (action*) calloc(1, sizeof(*act));
    trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
    cur_trigger->name = name;
    list_init(&act->triggers);
    list_add_tail(&act->triggers, &cur_trigger->nlist);
    list_init(&act->commands);
    list_init(&act->qlist);

    command* cmd = (command*) calloc(1, sizeof(*cmd));
    cmd->func = func;
    cmd->args[0] = const_cast<char*>(name);
    cmd->nargs = 1;
    list_add_tail(&act->commands, &cmd->clist);

    list_add_tail(&action_list, &act->alist);
    action_add_queue_tail(act);
    }

    action_list列表用来保存从启动脚本/init.rc解析得到的一系列action,以及一系列内建的action。当这些action需要执行的时候,它们就会被添加到action_queue列表中去,以便init进程可以执行它们

    console_init_action

    static int console_init_action(int nargs, char **args)
    {
    char console[PROP_VALUE_MAX];
    if (property_get("ro.boot.console", console) > 0) {
    snprintf(console_name, sizeof(console_name), "/dev/%s", console);
    }

    int fd = open(console_name, O_RDWR | O_CLOEXEC);
    if (fd >= 0)
    have_console = 1;
    close(fd);

    fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
    if (fd >= 0) {
    const char *msg;
    msg = " "
    " "
    " "
    " "
    " "
    " "
    " " // console is 40 cols x 30 lines
    " "
    " "
    " "
    " "
    " "
    " "
    " "
    " A N D R O I D ";
    write(fd, msg, strlen(msg));
    close(fd);
    }

    return 0;
    }
    • 初始化控制台。init进程在启动的时候,会解析内核的启动参数(保存在文件/proc/cmdline中)。如果发现内核的启动参数中包含有了一个名称为“androidboot.console”的属性,那么就会将这个属性的值保存在字符数组console中。这样我们就可以通过设备文件/dev/来访问系统的控制台。如果内核的启动参数没有包含名称为“androidboot.console”的属性,那么默认就通过设备文件/dev/console来访问系统的控制台。如果能够成功地打开设备文件/dev/或者/dev/console,那么就说明系统支持访问控制台,因此,全局变量have_console的就会被设置为1。
    • 显示第二个开机画面。显示第二个开机画面是通过调用函数load_565rle_image来实现的。在调用函数load_565rle_image的时候,指定的开机画面文件为INIT_IMAGE_FILE。

    四 系统服务启动过程中出现的动态画面

    第三个开机画面是由应用程序bootanimation来负责显示的。应用程序bootanimation在启动脚本init.rc中被配置成了一个服务,如下所示: 
    system/core/rootdir/init.rc

    service bootanim /system/bin/bootanimation
    class core
    user graphics
    group graphics audio
    disabled
    oneshot

    应用程序bootanimation的用户和用户组名称分别被设置为graphics。用来启动应用程序bootanimation的服务是disable的,即init进程在启动的时候,不会主动将应用程序bootanimation启动起来。当SurfaceFlinger服务启动的时候,它会通过修改系统属性ctl.start的值来通知init进程启动应用程序bootanimation,以便可以显示第三个开机画面,而当System进程将系统中的关键服务都启动起来之后,ActivityManagerService服务就会通知SurfaceFlinger服务来修改系统属性ctl.stop的值,以便可以通知init进程停止执行应用程序bootanimation,即停止显示第三个开机画面。下文我们就分别分析第三个开机画面的显示过程和停止过程。

    Zygote —–> SystemServer —–> SurfaceFlinger 
    frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

    void SurfaceFlinger::init() {
    ALOGI( "SurfaceFlinger's main thread ready to run. "
    "Initializing graphics H/W...");

    Mutex::Autolock _l(mStateLock);

    // initialize EGL for the default display
    mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    eglInitialize(mEGLDisplay, NULL, NULL);

    // start the EventThread
    sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
    vsyncPhaseOffsetNs, true, "app");
    mEventThread = new EventThread(vsyncSrc);
    sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
    sfVsyncPhaseOffsetNs, true, "sf");
    mSFEventThread = new EventThread(sfVsyncSrc);
    mEventQueue.setEventThread(mSFEventThread);

    // Initialize the H/W composer object. There may or may not be an
    // actual hardware composer underneath.
    mHwc = new HWComposer(this,
    *static_cast<HWComposer::EventHandler *>(this));

    // get a RenderEngine for the given display / config (can't fail)
    mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());

    // retrieve the EGL context that was selected/created
    mEGLContext = mRenderEngine->getEGLContext();

    LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
    "couldn't create EGLContext");

    // initialize our non-virtual displays
    for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
    DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
    // set-up the displays that are already connected
    if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
    // All non-virtual displays are currently considered secure.
    bool isSecure = true;
    createBuiltinDisplayLocked(type);
    wp<IBinder> token = mBuiltinDisplays[i];

    sp<IGraphicBufferProducer> producer;
    sp<IGraphicBufferConsumer> consumer;
    BufferQueue::createBufferQueue(&producer, &consumer,
    new GraphicBufferAlloc());

    sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
    consumer);
    int32_t hwcId = allocateHwcDisplayId(type);
    sp<DisplayDevice> hw = new DisplayDevice(this,
    type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
    fbs, producer,
    mRenderEngine->getEGLConfig());
    if (i > DisplayDevice::DISPLAY_PRIMARY) {
    // FIXME: currently we don't get blank/unblank requests
    // for displays other than the main display, so we always
    // assume a connected display is unblanked.
    ALOGD("marking display %zu as acquired/unblanked", i);
    hw->setPowerMode(HWC_POWER_MODE_NORMAL);
    }
    mDisplays.add(token, hw);
    }
    }

    // make the GLContext current so that we can create textures when creating Layers
    // (which may happens before we render something)
    getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);

    mEventControlThread = new EventControlThread(this);
    mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);

    // set a fake vsync period if there is no HWComposer
    if (mHwc->initCheck() != NO_ERROR) {
    mPrimaryDispSync.setPeriod(16666667);
    }

    // initialize our drawing state
    mDrawingState = mCurrentState;

    // set initial conditions (e.g. unblank default device)
    initializeDisplays();

    // start boot animation
    startBootAnim();
    }

    启动SurfaceFlinger的主线程,对设备主屏幕以及OpenGL库进行初始化。初始化完成之后,调用startBootAnim()

    void SurfaceFlinger::startBootAnim() {
    // start boot animation
    property_set("service.bootanim.exit", "0");
    property_set("ctl.start", "bootanim");
    }

    调用函数property_set来将系统属性“ctl.start”的值设置为“bootanim” 
    当系统属性发生改变时,init进程就会接收到一个系统属性变化通知,这个通知最终是由在init进程中的函数handle_property_set_fd来处理的。

    system/core/init/property_service.cpp

    static void handle_property_set_fd()
    {
    prop_msg msg;
    int s;
    int r;
    struct ucred cr;
    struct sockaddr_un addr;
    socklen_t addr_size = sizeof(addr);
    socklen_t cr_size = sizeof(cr);
    char * source_ctx = NULL;
    struct pollfd ufds[1];
    const int timeout_ms = 2 * 1000; /* Default 2 sec timeout for caller to send property. */
    int nr;

    if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
    return;
    }

    /* Check socket options here */
    if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
    close(s);
    ERROR("Unable to receive socket options ");
    return;
    }

    ufds[0].fd = s;
    ufds[0].events = POLLIN;
    ufds[0].revents = 0;
    nr = TEMP_FAILURE_RETRY(poll(ufds, 1, timeout_ms));
    if (nr == 0) {
    ERROR("sys_prop: timeout waiting for uid=%d to send property message. ", cr.uid);
    close(s);
    return;
    } else if (nr < 0) {
    ERROR("sys_prop: error waiting for uid=%d to send property message: %s ", cr.uid, strerror(errno));
    close(s);
    return;
    }

    r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), MSG_DONTWAIT));
    if(r != sizeof(prop_msg)) {
    ERROR("sys_prop: mis-match msg size received: %d expected: %zu: %s ",
    r, sizeof(prop_msg), strerror(errno));
    close(s);
    return;
    }

    switch(msg.cmd) {
    case PROP_MSG_SETPROP:
    msg.name[PROP_NAME_MAX-1] = 0;
    msg.value[PROP_VALUE_MAX-1] = 0;

    if (!is_legal_property_name(msg.name, strlen(msg.name))) {
    ERROR("sys_prop: illegal property name. Got: "%s" ", msg.name);
    close(s);
    return;
    }

    getpeercon(s, &source_ctx);

    if(memcmp(msg.name,"ctl.",4) == 0) {
    // Keep the old close-socket-early behavior when handling
    // ctl.* properties.
    close(s);
    if (check_control_mac_perms(msg.value, source_ctx)) {
    handle_control_message((char*) msg.name + 4, (char*) msg.value);
    } else {
    ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d ",
    msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid);
    }
    } else {
    if (check_perms(msg.name, source_ctx)) {
    property_set((char*) msg.name, (char*) msg.value);
    } else {
    ERROR("sys_prop: permission denied uid:%d name:%s ",
    cr.uid, msg.name);
    }

    // Note: bionic's property client code assumes that the
    // property server will not close the socket until *AFTER*
    // the property is written to memory.
    close(s);
    }
    freecon(source_ctx);
    break;

    default:
    close(s);
    break;
    }
    }
    • init进程是通过一个socket来接收系统属性变化事件的。每一个系统属性变化事件的内容都是通过一个prop_msg对象来描述的。
    • 在prop_msg对象中: 
      • 变量name用来描述发生变化的系统属性的名称
      • 变量value用来描述发生变化的系统属性的值。
    • 系统属性分为两种类型: 
      • 控制类型的系统属性(属性名称以“ctl.”开头)。 
        • 控制类型的系统属性在发生变化时,会触发init进程执行一个命令
      • 普通类型的系统属性 
        • 而普通类型的系统属性就不具有这个特性。 

          注意,改变系统属性是需要权限,因此,函数handle_property_set_fd在处理一个系统属性变化事件之前,首先会检查修改系统属性的进程是否具有相应的权限,这是通过调用函数check_control_perms或者check_perms来实现的。


    从前面的调用过程可以知道,当前发生变化的系统属性的名称为“ctl.start”,它的值被设置为“bootanim”。由于这是一个控制类型的系统属性,因此,在通过了权限检查之后,另外一个函数handle_control_message就会被调用,以便可以执行一个名称为“bootanim”的命令。 handle_control_message函数实现如下:system/core/init/init.cpp
    void handle_control_message(const char *msg, const char *arg)
    {
    if (!strcmp(msg,"start")) {
    msg_start(arg);
    } else if (!strcmp(msg,"stop")) {
    msg_stop(arg);
    } else if (!strcmp(msg,"restart")) {
    msg_restart(arg);
    } else {
    ERROR("unknown control msg '%s' ", msg);
    }
    }
    控制类型的系统属性的名称是以”ctl.”开头,并且是以“start”或者“stop”结尾的。
    • start:启动某一个服务,通过msg_start函数实现
    • stop:停止某一个服务,通过msg_stop来函数实现
    • restart:重启某一个服务,通过msg_restart函数实现 
      由于当前发生变化的系统属性是以“start”来结尾的,因此,接下来就会调用函数msg_start来启动一个名称为“bootanim”的服务。
    static void msg_start(const char *name)
    {
    struct service *svc = NULL;
    char *tmp = NULL;
    char *args = NULL;

    if (!strchr(name, ':'))
    svc = service_find_by_name(name);
    else {
    tmp = strdup(name);
    if (tmp) {
    args = strchr(tmp, ':');
    *args = '';
    args++;

    svc = service_find_by_name(tmp);
    }
    }

    if (svc) {
    service_start(svc, args);
    } else {
    ERROR("no such service '%s' ", name);
    }
    if (tmp)
    free(tmp);
    }
    • 参数name的值等于“bootanim”,它用来描述一个服务名称
    • 调用函数service_find_by_name来找到名称等于“bootanim”的服务的信息,这些信息保存在一个service结构体svc中
    • 调用函数service_start来将对应的应用程序启动起来。

    从前面的内容可以知道,名称等于“bootanim”的服务所对应的应用程序为/system/bin/bootanimation 
    这个应用程序实现在frameworks/base/cmds/bootanimation目录中,其中,应用程序入口函数main在bootanimation_main.cpp中 
    sframeworks/base/cmds/bootanimation/bootanimation_main.cpp

    int main()
    {
    setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_DISPLAY);

    char value[PROPERTY_VALUE_MAX];
    property_get("debug.sf.nobootanimation", value, "0");
    int noBootAnimation = atoi(value);
    ALOGI_IF(noBootAnimation, "boot animation disabled");
    if (!noBootAnimation) {

    sp<ProcessState> proc(ProcessState::self());
    ProcessState::self()->startThreadPool();

    // create the boot animation object
    sp<BootAnimation> boot = new BootAnimation();

    IPCThreadState::self()->joinThreadPool();

    }
    return 0;
    }
    • 检查系统属性“debug.sf.nobootnimaition”的值是否等于0。 
      • 如果不等于的话,那么接下来就会启动一个Binder线程池,并且创建一个BootAnimation对象(因为BootAnimation对象在显示第三个开机画面的过程中,需要与SurfaceFlinger服务通信,因此,应用程序bootanimation就需要启动一个Binder线程池。)
    • BootAnimation类间接地继承了RefBase类,并且重写了RefBase类的成员函数onFirstRef,因此,当一个BootAnimation对象第一次被智能指针引用的时,这个BootAnimation对象的成员函数onFirstRef就会被调用。

    BootAnimation类的函数onFirstRef实现如下: 
    sframeworks/base/cmds/bootanimation/BootAnimation.cpp

    void BootAnimation::onFirstRef() {
    status_t err = mSession->linkToComposerDeath(this);
    ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
    if (err == NO_ERROR) {
    run("BootAnimation", PRIORITY_DISPLAY);
    }
    }
    • mSession是BootAnimation类的一个变量,它的类型为SurfaceComposerClient,是用来和SurfaceFlinger执行Binder进程间通信的
    • 由于BootAnimation类引用了SurfaceFlinger服务,因此,当SurfaceFlinger服务意外死亡时,BootAnimation类就需要得到通知,这是通过调用成员变量mSession的成员函数linkToComposerDeath来注册SurfaceFlinger服务的死亡接收通知来实现的。
    • BootAnimation类继承了Thread类,因此,当BootAnimation类的成员函数onFirstRef调用了父类Thread的成员函数run之后,系统就会创建一个线程,这个线程在第一次运行之前,会调用BootAnimation类的成员函数readyToRun来执行一些初始化工作,后面再调用BootAnimation类的成员函数htreadLoop来显示第三个开机画面。

    mSession在BootAnimation类的构造函数中创建的,如下所示:

    BootAnimation::BootAnimation() : Thread(false), mZip(NULL)
    {
    mSession = new SurfaceComposerClient();
    }

    SurfaceComposerClient类内部有一个实现了ISurfaceComposerClient接口的Binder代理对象mClient,这个Binder代理对象引用了SurfaceFlinger服务,SurfaceComposerClient类就是通过它来和SurfaceFlinger服务通信的。

    readyToRun执行一些初始化工作

    #define OEM_BOOTANIMATION_FILE "/oem/media/bootanimation.zip"
    #define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"

    status_t BootAnimation::readyToRun() {
    mAssets.addDefaultAssets();

    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
    ISurfaceComposer::eDisplayIdMain));
    DisplayInfo dinfo;
    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
    if (status)
    return -1;

    /**
    * 获得一个SurfaceControl对象control
    */
    // create the native surface
    sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
    dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);

    SurfaceComposerClient::openGlobalTransaction();
    control->setLayer(0x40000000);
    SurfaceComposerClient::closeGlobalTransaction();

    sp<Surface> s = control->getSurface();

    // initialize opengl and egl
    const EGLint attribs[] = {
    EGL_RED_SIZE, 8,
    EGL_GREEN_SIZE, 8,
    EGL_BLUE_SIZE, 8,
    EGL_DEPTH_SIZE, 0,
    EGL_NONE
    };
    EGLint w, h;
    EGLint numConfigs;
    EGLConfig config;
    EGLSurface surface;
    EGLContext context;

    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

    eglInitialize(display, 0, 0);
    eglChooseConfig(display, attribs, &config, 1, &numConfigs);
    surface = eglCreateWindowSurface(display, config, s.get(), NULL);
    context = eglCreateContext(display, config, NULL, NULL);
    eglQuerySurface(display, surface, EGL_WIDTH, &w);
    eglQuerySurface(display, surface, EGL_HEIGHT, &h);

    if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
    return NO_INIT;

    mDisplay = display;
    mContext = context;
    mSurface = surface;
    mWidth = w;
    mHeight = h;
    mFlingerSurfaceControl = control;
    mFlingerSurface = s;

    // If the device has encryption turned on or is in process
    // of being encrypted we show the encrypted boot animation.
    char decrypt[PROPERTY_VALUE_MAX];
    property_get("vold.decrypt", decrypt, "");

    bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);

    ZipFileRO* zipFile = NULL;
    if ((encryptedAnimation &&
    (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
    ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||

    ((access(OEM_BOOTANIMATION_FILE, R_OK) == 0) &&
    ((zipFile = ZipFileRO::open(OEM_BOOTANIMATION_FILE)) != NULL)) ||

    ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
    ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
    mZip = zipFile;
    }

    return NO_ERROR;
    }
    bool BootAnimation::threadLoop()
    {
    bool r;
    // We have no bootanimation file, so we use the stock android logo
    // animation.
    if (mZip == NULL) {
    r = android();
    } else {
    r = movie();
    }

    eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglDestroyContext(mDisplay, mContext);
    eglDestroySurface(mDisplay, mSurface);
    mFlingerSurface.clear();
    mFlingerSurfaceControl.clear();
    eglTerminate(mDisplay);
    IPCThreadState::self()->stopProcess();
    return r;
    }

    如果BootAnimation类的成员变量mAndroidAnimation的值等于true,那么接下来就会调用BootAnimation类的成员函数android来显示系统默认的开机动画,否则的话,就会调用BootAnimation类的成员函数movie来显示用户自定义的开机动画。显示完成之后,就会销毁前面所创建的EGLContext对象mContext、EGLSurface对象mSurface,以及EGLDisplay对象mDisplay等。

    接下来,我们就分别分析BootAnimation类的成员函数android和movie的实现。 
    BootAnimation类的函数android的实现如下所示:

    bool BootAnimation::android()
    {
    initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
    initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");

    // clear screen
    glShadeModel(GL_FLAT);
    glDisable(GL_DITHER);
    glDisable(GL_SCISSOR_TEST);
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    eglSwapBuffers(mDisplay, mSurface);

    glEnable(GL_TEXTURE_2D);
    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    const GLint xc = (mWidth - mAndroid[0].w) / 2;
    const GLint yc = (mHeight - mAndroid[0].h) / 2;
    const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);

    glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
    updateRect.height());

    // Blend state
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    const nsecs_t startTime = systemTime();
    do {
    nsecs_t now = systemTime();
    double time = now - startTime;
    float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
    GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
    GLint x = xc - offset;

    glDisable(GL_SCISSOR_TEST);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_SCISSOR_TEST);
    glDisable(GL_BLEND);
    glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
    glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
    glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);

    glEnable(GL_BLEND);
    glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
    glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);

    EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
    if (res == EGL_FALSE)
    break;

    // 12fps: don't animate too fast to preserve CPU
    const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
    if (sleepTime > 0)
    usleep(sleepTime);

    checkExit();
    } while (!exitPending());

    glDeleteTextures(1, &mAndroid[0].name);
    glDeleteTextures(1, &mAndroid[1].name);
    return false;
    }
    • Android系统默认的开机动画是由两张图片android-logo-mask.png和android-logo-shine.png中。这两张图片保存在frameworks/base/core/res/assets/images目录中,它们最终会被编译在framework-res模块(frameworks/base/core/res)中,即编译在framework-res.apk文件中。编译在framework-res模块中的资源文件可以通过AssetManager类来访问。
    • BootAnimation类的成员函数android首先调用另外一个成员函数initTexture来将根据图片android-logo-mask.png和android-logo-shine.png的内容来分别创建两个纹理对象,这两个纹理对象就分别保存在BootAnimation类的成员变量mAndroid所描述的一个数组中。通过混合渲染这两个纹理对象,我们就可以得到一个开机动画,这是通过中间的while循环语句来实现的。
    • 图片android-logo-mask.png用作动画前景,它是一个镂空的“ANDROID”图像。图片android-logo-shine.png用作动画背景,它的中间包含有一个高亮的呈45度角的条纹。在每一次循环中,图片android-logo-shine.png被划分成左右两部分内容来显示。左右两个部分的图像宽度随着时间的推移而此消彼长,这样就可以使得图片android-logo-shine.png中间高亮的条纹好像在移动一样。另一方面,在每一次循环中,图片android-logo-shine.png都作为一个整体来渲染,而且它的位置是恒定不变的。由于它是一个镂空的“ANDROID”图像,因此,我们就可以通过它的镂空来看到它背后的图片android-logo-shine.png的条纹一闪一闪地划过。

    BootAnimation类的函数movie的实现如下所示:

    bool BootAnimation::movie()
    {
    String8 desString;

    if (!readFile("desc.txt", desString)) {
    return false;
    }
    char const* s = desString.string();

    // Create and initialize an AudioPlayer if we have an audio_conf.txt file
    String8 audioConf;
    if (readFile("audio_conf.txt", audioConf)) {
    mAudioPlayer = new AudioPlayer;
    if (!mAudioPlayer->init(audioConf.string())) {
    ALOGE("mAudioPlayer.init failed");
    mAudioPlayer = NULL;
    }
    }

    Animation animation;

    /**
    * 从readyToRun函数实现可以知道,如果目标设备上存在压缩文件/data/local/bootanimation.zip,
    * 那么变量mZip就会指向它,否则的话,就会指向目标设备上的压缩文件/system/media/bootanimation.zip。
    * 无论变量mZip指向的是哪一个压缩文件,这个压缩文件都必须包含有一个名称为“desc.txt”的文件,
    * 用来描述用户自定义的开机动画是如何显示的。
    * 就得到了开机动画的显示大小、速度以及片断信息。这些信息都保存在Animation对象animation中,
    * 其中,每一个动画片断都使用一个Animation::Part对象来描述,
    * 并且保存在Animation对象animation的成员变量parts所描述的一个片断列表中。
    */
    // Parse the description file
    for (;;) {
    const char* endl = strstr(s, " ");
    if (endl == NULL) break;
    String8 line(s, endl - s);
    const char* l = line.string();
    int fps, width, height, count, pause;
    char path[ANIM_ENTRY_NAME_MAX];
    char color[7] = "000000"; // default to black if unspecified

    char pathType;
    if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
    // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
    animation.width = width;
    animation.height = height;
    animation.fps = fps;
    }
    else if (sscanf(l, " %c %d %d %s #%6s", &pathType, &count, &pause, path, color) >= 4) {
    // ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s", pathType, count, pause, path, color);
    Animation::Part part;
    part.playUntilComplete = pathType == 'c';
    part.count = count;
    part.pause = pause;
    part.path = path;
    part.audioFile = NULL;
    if (!parseColor(color, part.backgroundColor)) {
    ALOGE("> invalid color '#%s'", color);
    part.backgroundColor[0] = 0.0f;
    part.backgroundColor[1] = 0.0f;
    part.backgroundColor[2] = 0.0f;
    }
    animation.parts.add(part);
    }

    s = ++endl;
    }

    // read all the data structures
    const size_t pcount = animation.parts.size();
    void *cookie = NULL;
    if (!mZip->startIteration(&cookie)) {
    return false;
    }

    ZipEntryRO entry;
    char name[ANIM_ENTRY_NAME_MAX];
    while ((entry = mZip->nextEntry(cookie)) != NULL) {
    const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
    if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
    ALOGE("Error fetching entry file name");
    continue;
    }
    /**
    * 接下来,BootAnimation类的成员函数movie再断续将每一个片断所对应的png图片读取出来。
    * 每一个png图片都表示一个动画帧,使用一个Animation::Frame对象来描述,
    * 并且保存在对应的Animation::Part对象的成员变量frames所描述的一个帧列表中
    */
    const String8 entryName(name);
    const String8 path(entryName.getPathDir());
    const String8 leaf(entryName.getPathLeaf());
    if (leaf.size() > 0) {
    for (size_t j=0 ; j<pcount ; j++) {
    if (path == animation.parts[j].path) {
    uint16_t method;
    // supports only stored png files
    if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
    if (method == ZipFileRO::kCompressStored) {
    FileMap* map = mZip->createEntryFileMap(entry);
    if (map) {
    Animation::Part& part(animation.parts.editItemAt(j));
    if (leaf == "audio.wav") {
    // a part may have at most one audio file
    part.audioFile = map;
    } else {
    Animation::Frame frame;
    frame.name = leaf;
    frame.map = map;
    part.frames.add(frame);
    }
    }
    }
    }
    }
    }
    }
    }

    mZip->endIteration(cookie);
    /**
    * 一系列gl函数首先用来清理屏幕,接下来的一系列gl函数用来设置OpenGL的纹理显示方式。
    */
    glShadeModel(GL_FLAT);
    glDisable(GL_DITHER);
    glDisable(GL_SCISSOR_TEST);
    glDisable(GL_BLEND);

    glBindTexture(GL_TEXTURE_2D, 0);
    glEnable(GL_TEXTURE_2D);
    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    /**
    * 变量xc和yc的值用来描述开机动画的显示位置,即需要在屏幕中间显示开机动画,
    * 另外一个变量frameDuration的值用来描述每一帧的显示时间,它是以纳秒为单位的。
    * Region对象clearReg用来描述屏幕中除了开机动画之外的其它区域,
    * 它是用整个屏幕区域减去开机动画所点据的区域来得到的。
    */
    const int xc = (mWidth - animation.width) / 2;
    const int yc = ((mHeight - animation.height) / 2);
    nsecs_t frameDuration = s2ns(1) / animation.fps;

    Region clearReg(Rect(mWidth, mHeight));
    clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));

    /**
    * 准备好开机动画的显示参数之后,最后就可以执行显示开机动画的操作了。
    */
    for (size_t i=0 ; i<pcount ; i++) {
    const Animation::Part& part(animation.parts[i]);
    const size_t fcount = part.frames.size();
    glBindTexture(GL_TEXTURE_2D, 0);

    for (int r=0 ; !part.count || r<part.count ; r++) {
    // Exit any non playuntil complete parts immediately
    if(exitPending() && !part.playUntilComplete)
    break;

    // only play audio file the first time we animate the part
    if (r == 0 && mAudioPlayer != NULL && part.audioFile) {
    mAudioPlayer->playFile(part.audioFile);
    }

    glClearColor(
    part.backgroundColor[0],
    part.backgroundColor[1],
    part.backgroundColor[2],
    1.0f);

    for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
    const Animation::Frame& frame(part.frames[j]);
    nsecs_t lastFrame = systemTime();

    if (r > 0) {
    glBindTexture(GL_TEXTURE_2D, frame.tid);
    } else {
    if (part.count != 1) {
    glGenTextures(1, &frame.tid);
    glBindTexture(GL_TEXTURE_2D, frame.tid);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }
    initTexture(frame);
    }

    if (!clearReg.isEmpty()) {
    Region::const_iterator head(clearReg.begin());
    Region::const_iterator tail(clearReg.end());
    glEnable(GL_SCISSOR_TEST);
    while (head != tail) {
    const Rect& r2(*head++);
    glScissor(r2.left, mHeight - r2.bottom,
    r2.width(), r2.height());
    glClear(GL_COLOR_BUFFER_BIT);
    }
    glDisable(GL_SCISSOR_TEST);
    }
    // specify the y center as ceiling((mHeight - animation.height) / 2)
    // which is equivalent to mHeight - (yc + animation.height)
    glDrawTexiOES(xc, mHeight - (yc + animation.height),
    0, animation.width, animation.height);
    eglSwapBuffers(mDisplay, mSurface);

    nsecs_t now = systemTime();
    nsecs_t delay = frameDuration - (now - lastFrame);
    //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
    lastFrame = now;

    if (delay > 0) {
    struct timespec spec;
    spec.tv_sec = (now + delay) / 1000000000;
    spec.tv_nsec = (now + delay) % 1000000000;
    int err;
    do {
    err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
    } while (err<0 && errno == EINTR);
    }

    checkExit();
    }

    usleep(part.pause * ns2us(frameDuration));

    // For infinite parts, we've now played them at least once, so perhaps exit
    if(exitPending() && !part.count)
    break;
    }

    // free the textures for this part
    if (part.count != 1) {
    for (size_t j=0 ; j<fcount ; j++) {
    const Animation::Frame& frame(part.frames[j]);
    glDeleteTextures(1, &frame.tid);
    }
    }
    }

    return false;
    }
    • 显示每一个动画片断
    • 循环显示每一个动画片断
    • 显示每一个动画片断所对应的png图片。这些png图片以纹理的方式来显示在屏幕中。
    • 如果Region对象clearReg所包含的区域不为空,那么在调用函数glDrawTexiOES和eglSwapBuffers来显示每一个png图片之前,首先要将它所包含的区域裁剪掉,避免开机动画可以显示在指定的位置以及大小中。
    • 每当显示完成一个png图片之后,都要将变量frameDuration的值从纳秒转换为毫秒。如果转换后的值大小于,那么就需要调用函数usleep函数来让线程睡眠一下,以保证每一个png图片,即每一帧动画都按照预先指定好的速度来显示。注意,函数usleep指定的睡眠时间只能精确到毫秒,因此,如果预先指定的帧显示时间小于1毫秒,那么BootAnimation类的成员函数movie是无法精确地控制地每一帧的显示时间的。
    • 判断一个动画片断是否是循环显示的,即循环次数不等于1。如果是的话,那么就说明前面为它所对应的每一个png图片都创建过一个纹理对象。现在既然这个片断的显示过程已经结束了,因此,就需要释放前面为它所创建的纹理对象。
    • 注意,如果一个动画片断的循环显示次数不等于1,那么就说明这个动画片断中的png图片需要重复地显示在屏幕中。由于每一个png图片都需要转换为一个纹理对象之后才能显示在屏幕中,因此,为了避免重复地为同一个png图片创建纹理对象,第三层的for循环在第一次显示一个png图片的时候,会调用函数glGenTextures来为这个png图片创建一个纹理对象,并且将这个纹理对象的名称保存在对应的Animation::Frame对象的成员变量tid中,这样,下次再显示相同的图片时,就可以使用前面已经创建好了的纹理对象,即调用函数glBindTexture来指定当前要操作的纹理对象。
    • 还有另外一个地方需要注意的是,每当循环显示完成一个片断时,需要调用usleep函数来使得线程睡眠part.pause * ns2us(frameDuration)毫秒,以便可以按照预先设定的节奏来显示开机动画。
  • 相关阅读:
    Java学习
    机器学习
    机器学习
    Java 学习
    哈希表复习
    [转] 数据库设计步骤
    Java
    c++的函数重载-笔记
    进程与线程-笔记
    内存知识-笔记
  • 原文地址:https://www.cnblogs.com/solo-heart/p/5463250.html
Copyright © 2011-2022 走看看