萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android 開機畫面顯示流程源碼級解析:從 init 到 initlogo.rle

Android 開機畫面分兩階段:核心層面的 啟動畫面(內核驅動顯示,通常為廠商 Logo)與用戶空間 init 進程顯示的第二畫面(/initlogo.rle)。本文基於 AOSP 原始碼 (system/core/init/),詳細追蹤 console_init_action、load_565rle_image、vt_set_mode、fb_open 等關鍵函數,揭示開機畫面如何繪製到幀緩衝區。

開機畫面兩階段對比

階段觸發主體顯示內容可定製性
第一階段Kernel 驅動 / Bootloader廠商 Logo、解鎖圖示高 (修改內核/boot.img)
第二階段init 進程 (用戶空間第一進程)/initlogo.rle (RLE 壓縮 565 格式)中 (替換 rootfs 中檔案)

init 進程啟動與 console_init_action

init 解析 /init.rc,遇到 on init 觸發 console_init_action:

// system/core/init/init.c
static int console_init_action(int nargs, char **args) {
  // 1. 初始化控制台設備 (/dev/tty0 或 kernel cmdline androidboot.console 指定)
  int fd = open(console_name, O_RDWR);
  if (fd >= 0) have_console = 1;
  close(fd);

  // 2. 嘗試載入並顯示 /initlogo.rle
  if (load_565rle_image(INIT_IMAGE_FILE)) { // 失敗返回 -1
    // 降級:文字模式輸出 "ANDROID"
    fd = open("/dev/tty0", O_WRONLY);
    if (fd >= 0) {
      write(fd, "\n\n\n\n\n\n\n\n\n\n\n          A N D R O I D", strlen(...));
      close(fd);
    }
  }
  return 0;
}

load_565rle_image 核心流程

// system/core/init/logo.c
int load_565rle_image(char *fn) {
  // 1. 切換控制台至圖形模式 (KD_GRAPHICS)
  if (vt_set_mode(1)) return -1; // ioctl(fd, KDSETMODE, KD_GRAPHICS)

  // 2. 開啟圖片檔案
  int fd = open(fn, O_RDONLY); // fn = "/initlogo.rle"
  if (fd < 0) goto fail;

  // 3. 取得檔案大小並 mmap 映射到進程位址空間
  struct stat s; fstat(fd, &s);
  unsigned short *data = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (data == MAP_FAILED) goto fail;

  // 4. 開啟幀緩衝區設備 /dev/graphics/fb0
  struct FB fb; if (fb_open(&fb)) goto fail_unmap;

  // 5. RLE 解碼直接寫入 fb->bits (幀緩衝區記憶體)
  // 格式: [count:2bytes][pixel:2bytes] 重複
  unsigned short *ptr = data, *bits = fb.bits;
  unsigned count, max = fb_width(&fb) * fb_height(&fb);
  while (count > 3) {
    unsigned n = ptr[0];          // run length
    if (n > max) break;
    android_memset16(bits, ptr[1], n << 1); // 填入像素值
    bits += n; max -= n;
    ptr += 2; count -= 4;
  }

  // 6. 刷新顯示、清理
  fb_update(&fb); fb_close(&fb);
  munmap(data, s.st_size); close(fd);
  unlink(fn); // 顯示後刪除,避免二次顯示
  return 0;

fail_unmap: munmap(data, s.st_size);
fail: close(fd); vt_set_mode(0); return -1; // 恢復文字模式
}

關鍵輔助函數

vt_set_mode - 控制台模式切換

static int vt_set_mode(int graphics) {
  int fd = open("/dev/tty0", O_RDWR | O_SYNC);
  if (fd < 0) return -1;
  int r = ioctl(fd, KDSETMODE, (void*)(graphics ? KD_GRAPHICS : KD_TEXT));
  close(fd);
  return r;
}

fb_open / fb_update - 幀緩衝區操作

  • fb_open:開啟 /dev/graphics/fb0,mmap 映射硬體顯存至用戶空間 fb.bits,取得 fb_width/fb_height/fb_stride。
  • fb_update:觸發 FBIOPAN_DISPLAY 或 FBIOPUT_VSCREENINFO 通知硬體顯示新幀。

initlogo.rle 格式與製作

  • 格式:565 RLE (RGB565 + Run-Length Encoding)。每筆 4 位元組:uint16_t run_length + uint16_t pixel_value。
  • 製作工具:AOSP build/tools/rgb2565 或自行編寫 Python 腳本將 PNG 轉換。
  • 尺寸:必須與目標裝置螢幕解析度完全一致 (如 1080x1920)。
  • 放置:放入 device///initlogo.rle,經 PRODUCT_COPY_FILES 複製至 rootfs 根目錄。

常見問題與除錯

  • 畫面不顯示/花屏:解析度不匹配、RLE 格式錯誤、stride 未對齊。
  • 顯示後殘留:unlink(fn) 失敗導致二次顯示,檢查 rootfs 可寫權限。
  • 文字模式殘留:vt_set_mode(0) 未正確恢復,後續 log 顯示異常。
  • 動態 Logo:需修改 init 進程或引入 bootanimation 服務 (Android 5.0+ 獨立服務播放 /system/media/bootanimation.zip)。

總結

Android 第二階段開機畫面由 init 進程通過 load_565rle_image 直接操作幀緩衝區繪製,流程極精簡、依賴最小。理解此流程有助於客製化開機體驗、除錯顯示異常、以及理解後續 SurfaceFlinger 接管顯示的銜接點。現代 Android 版本更多採用 bootanimation 服務播放動畫,但 initlogo.rle 仍作為極早期靜態畫面保留。

copyright © 萬盛學電腦網 all rights reserved