Android5.0 Recovery源代码分析与定制---recovery UI相关(二)

       http://blog.csdn.net/morixinguan/article/details/72858346引用我的代码片

在上一篇文章中,我们大致的介绍了recovery的启动流程,那么,recovery升级或者做双清的时候,那些图形动画又是如何实现的呢?我们来看看代码:

     以下这段代码位于recovery/screen_ui.cpp

void ScreenRecoveryUI::Init()
{
    gr_init();

    gr_font_size(&char_width, &char_height);

    text_col = text_row = 0;
    text_rows = gr_fb_height() / char_height;
    if (text_rows > kMaxRows) text_rows = kMaxRows;
    text_top = 1;

    text_cols = gr_fb_width() / char_width;
    if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;

    backgroundIcon[NONE] = NULL;
    LoadBitmapArray("icon_installing", &installing_frames, &installation);
    backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
    backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
    LoadBitmap("icon_error", &backgroundIcon[ERROR]);
    backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];

    LoadBitmap("progress_empty", &progressBarEmpty);
    LoadBitmap("progress_fill", &progressBarFill);
    LoadBitmap("stage_empty", &stageMarkerEmpty);
    LoadBitmap("stage_fill", &stageMarkerFill);

    LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
    LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
    LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
    LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);

    pthread_create(&progress_t, NULL, progress_thread, NULL);

    RecoveryUI::Init();
}

这段代码都做了哪些事情呢?这些recovery初始化图形显示最开始的部分,

(1)调用了miniui中的gr_init初始化显示图形相关的步骤,因为recovery是基于framebuffer机制显示的。

(2)调用gr_font_size设置字体显示的大小,然后计算文本显示行列。

(3)接下来就是装载图片了,会调用到LoadBitmapArray和LoadBitmap这两个函数。其中,我们会看到这些函数里图片的名称:

"icon_installing"、"icon_error"、"progress_empty"、"progress_fill"、

        "stage_empty"、"stage_fill"、"installing_text"、"erasing_text"、

        "no_command_text"、"error_text"

将上面的字符串与下面的图片一一对应:

那么这些分别是怎么显示的?其中erasing_text是用来显示做清除的时候显示的文字,放大后如下:

这上面有许许多多的语言版本,我们可以根据需要来选择,这些主要要看接下来初始化文字的代码逻辑。

其余的图片中,后缀带text的,也和这些是类似的,有出现错误显示的字体error_text,更新系统显示的字体installing_text,没有命令的时候显示的字体no_command_text。

除了文字显示,我们最关心的就是icon_installing这张图片了,在做系统更新的时候,这个机器人会转动。这不是动画吗?怎么只有一张图片呢?我们找到Android官方网站看看是为什么?
原因如下:

Recovery UI images
Android 5.x
The recovery user interface consists images. Ideally, users never interact with the UI: During a normal update, 
the phone boots into recovery, fills the installation progress bar, and boots back into the new system without 
input from the user. In the event of a system update problem, the only user action that can be taken is to 
call customer care.An image-only interface obviates the need for localization. 
However, as of Android 5.x the update can display a string of text (e.g. "Installing system update...") 
along with the image. For details, see Localized recovery text.

efault images are available in different densities and are located inbootable/recovery/res$DENSITY/images 
(e.g., bootable/recovery/res-hdpi/images). To use a static image during installation, 
you need only provide the icon_installing.png image and set the number of frames in the animation to 0 
(the error icon is not animated; it is always a static image).
// 以上文档意思就是说,Android5.x以上的版本,机器人的动画是PNG图片和帧动画组成的,
//我们可以使用recovery目录下的interlace-frames.py这个python脚本来进行合成,
//具体的合成方法需要使用recovery代码中的一个python合成工具。
//我就可以把android原生态的动画给换了,因为这个机器人实在是丑。

源码如下:

# Copyright (C) 2014 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script to take a set of frames (PNG files) for a recovery animation
and turn it into a single output image which contains the input frames
interlaced by row.  Run with the names of all the input frames on the
command line, in order, followed by the name of the output file."""
import sys
try:
  import Image
  import PngImagePlugin
except ImportError:
  print "This script requires the Python Imaging Library to be installed."
  sys.exit(1)
frames = [Image.open(fn).convert("RGB") for fn in sys.argv[1:-1]]
assert len(frames) > 0, "Must have at least one input frame."
sizes = set()
for fr in frames:
  sizes.add(fr.size)
assert len(sizes) == 1, "All input images must have the same size."
w, h = sizes.pop()
N = len(frames)
out = Image.new("RGB", (w, h*N))
for j in range(h):
  for i in range(w):
    for fn, f in enumerate(frames):
      out.putpixel((i, j*N+fn), f.getpixel((i, j)))
# When loading this image, the graphics library expects to find a text
# chunk that specifies how many frames this animation represents.  If
# you post-process the output of this script with some kind of
# optimizer tool (eg pngcrush or zopflipng) make sure that your
# optimizer preserves this text chunk.
meta = PngImagePlugin.PngInfo()
meta.add_text("Frames", str(N))
out.save(sys.argv[-1], pnginfo=meta)

这也就是为什么,调用这张图片需要用到LoadBitmapArray这个函数的原因。

void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
    int result = res_create_multi_display_surface(filename, frames, surface);
    if (result < 0) {
        LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
    }
}

调完这个函数后会调用resources.cpp中的res_create_multi_display_surface函数用于显示,源码如下:

int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) {
    GRSurface** surface = NULL;
    int result = 0;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_uint_32 width, height;
    png_byte channels;
    int i;
    png_textp text;
    int num_text;
    unsigned char* p_row;
    unsigned int y;

    *pSurface = NULL;
    *frames = -1;

    result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
    if (result < 0) return result;

    *frames = 1;
    if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
        for (i = 0; i < num_text; ++i) {
            if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
                *frames = atoi(text[i].text);
                break;
            }
        }
        printf("  found frames = %d\n", *frames);
    }

    if (height % *frames != 0) {
        printf("bad height (%d) for frame count (%d)\n", height, *frames);
        result = -9;
        goto exit;
    }

    surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*)));
    if (surface == NULL) {
        result = -8;
        goto exit;
    }
    for (i = 0; i < *frames; ++i) {
        surface[i] = init_display_surface(width, height / *frames);
        if (surface[i] == NULL) {
            result = -8;
            goto exit;
        }
    }

#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
    png_set_bgr(png_ptr);
#endif

    p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
    for (y = 0; y < height; ++y) {
        png_read_row(png_ptr, p_row, NULL);
        int frame = y % *frames;
        unsigned char* out_row = surface[frame]->data +
            (y / *frames) * surface[frame]->row_bytes;
        transform_rgb_to_draw(p_row, out_row, channels, width);
    }
    free(p_row);

    *pSurface = reinterpret_cast<GRSurface**>(surface);

exit:
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

    if (result < 0) {
        if (surface) {
            for (i = 0; i < *frames; ++i) {
                if (surface[i]) free(surface[i]);
            }
            free(surface);
        }
    }
    return result;
}

其余的和text无关图片,会用到LoadBitmap这个函数:

void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
    int result = res_create_multi_display_surface(filename, frames, surface);
    if (result < 0) {
        LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
    }
}

同样调用到以下函数:

int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) {
    GRSurface** surface = NULL;
    int result = 0;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_uint_32 width, height;
    png_byte channels;
    int i;
    png_textp text;
    int num_text;
    unsigned char* p_row;
    unsigned int y;

    *pSurface = NULL;
    *frames = -1;

    result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
    if (result < 0) return result;

    *frames = 1;
    if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
        for (i = 0; i < num_text; ++i) {
            if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
                *frames = atoi(text[i].text);
                break;
            }
        }
        printf("  found frames = %d\n", *frames);
    }

    if (height % *frames != 0) {
        printf("bad height (%d) for frame count (%d)\n", height, *frames);
        result = -9;
        goto exit;
    }

    surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*)));
    if (surface == NULL) {
        result = -8;
        goto exit;
    }
    for (i = 0; i < *frames; ++i) {
        surface[i] = init_display_surface(width, height / *frames);
        if (surface[i] == NULL) {
            result = -8;
            goto exit;
        }
    }

#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
    png_set_bgr(png_ptr);
#endif

    p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
    for (y = 0; y < height; ++y) {
        png_read_row(png_ptr, p_row, NULL);
        int frame = y % *frames;
        unsigned char* out_row = surface[frame]->data +
            (y / *frames) * surface[frame]->row_bytes;
        transform_rgb_to_draw(p_row, out_row, channels, width);
    }
    free(p_row);

    *pSurface = reinterpret_cast<GRSurface**>(surface);

exit:
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

    if (result < 0) {
        if (surface) {
            for (i = 0; i < *frames; ++i) {
                if (surface[i]) free(surface[i]);
            }
            free(surface);
        }
    }
    return result;
}

关于图片我们大概都知道怎么来显示的了,所以,现在我们可以替换Android原生态中的图片,换成我们自己的图片,

当然,也不是什么图都可以的,在recovery中,所有的png图片必须是RGB且不带且不能带alhpa通道信息。
关于这一点,我们可以看open_png这个函数:

static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
                    png_uint_32* width, png_uint_32* height, png_byte* channels) {
    char resPath[256];
    unsigned char header[8];
    int result = 0;
    int color_type, bit_depth;
    size_t bytesRead;

    snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
    resPath[sizeof(resPath)-1] = '\0';
    FILE* fp = fopen(resPath, "rb");
    if (fp == NULL) {
        result = -1;
        goto exit;
    }

    bytesRead = fread(header, 1, sizeof(header), fp);
    if (bytesRead != sizeof(header)) {
        result = -2;
        goto exit;
    }

    if (png_sig_cmp(header, 0, sizeof(header))) {
        result = -3;
        goto exit;
    }

    *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!*png_ptr) {
        result = -4;
        goto exit;
    }

    *info_ptr = png_create_info_struct(*png_ptr);
    if (!*info_ptr) {
        result = -5;
        goto exit;
    }

    if (setjmp(png_jmpbuf(*png_ptr))) {
        result = -6;
        goto exit;
    }

    png_init_io(*png_ptr, fp);
    png_set_sig_bytes(*png_ptr, sizeof(header));
    png_read_info(*png_ptr, *info_ptr);

    png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
            &color_type, NULL, NULL, NULL);

    *channels = png_get_channels(*png_ptr, *info_ptr);

    if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
        // 8-bit RGB images: great, nothing to do.
    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
        // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
        png_set_expand_gray_1_2_4_to_8(*png_ptr);
    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
        // paletted images: expand to 8-bit RGB.  Note that we DON'T
        // currently expand the tRNS chunk (if any) to an alpha
        // channel, because minui doesn't support alpha channels in
        // general.
        png_set_palette_to_rgb(*png_ptr);
        *channels = 3;
    } else {
        fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
                bit_depth, *channels, color_type);
        result = -7;
        goto exit;
    }

    return result;

  exit:
    if (result < 0) {
        png_destroy_read_struct(png_ptr, info_ptr, NULL);
    }
    if (fp != NULL) {
        fclose(fp);
    }

    return result;
}

在代码中,我们可以看到如下:

 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
        // 8-bit RGB images: great, nothing to do.
    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
        // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
        png_set_expand_gray_1_2_4_to_8(*png_ptr);
    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
        // paletted images: expand to 8-bit RGB.  Note that we DON'T
        // currently expand the tRNS chunk (if any) to an alpha
        // channel, because minui doesn't support alpha channels in
        // general.
        png_set_palette_to_rgb(*png_ptr);
        *channels = 3;
    } else {
        fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
                bit_depth, *channels, color_type);
        result = -7;
        goto exit;
    }

以下参考一位网友给出的答案。
这个函数将图片文件的数据读取到内存,我在其中输出了一些调试信息,输出图片的 color_type, channels 等信息。
查看LOG发现,android原生的图片 channels == 3,channels 即色彩通道个数,等于 3 的话,意味着只有 R,G,B 三个通道的信息,
没有 ALPHA 通道信息!这段代码的逻辑是如果channels 不等于3, 则按channels = 1 来处理,即灰度图。
美工给的图片是带 alpha通道信息的,即channels = 4,被当成灰度图像来处理了,怪不得显示的效果是灰度图像。
我一直以为 png 图像就只有一种格式,都是带有 alpha通道的。。。
使用图像处理工具(photoshop 或者 gimp),将美工给的图片去掉 alpha 通道信息,再替换recovery 的图片,编译,替换recovery.img ,
reboot -r 。图片终于正常显示啦。

在下一节里,我们将继续分析UI显示....

时间: 2024-08-01 07:16:15

Android5.0 Recovery源代码分析与定制---recovery UI相关(二)的相关文章

Android5.0 Recovery源代码分析与定制(一)

在Tiny4412的Android5.0源代码中: bootable/recovery/recovery.cpp是recovery程序的主文件. 仔细一看,对比了其它平台的recovery源代码,除了MTK对Recovery做了相应的定制外,其它的平台几乎没有看到,关于MTK平台,后续再分析. 关于Android5.0的recovery,有什么功能,在recovery.cpp中开头就已经做了详细的说明,我们来看看: /* * The recovery tool communicates with

《LINUX3.0内核源代码分析》第二章:中断和异常 【转】

转自:http://blog.chinaunix.net/uid-25845340-id-2982887.html 摘要:第二章主要讲述linux如何处理ARM cortex A9多核处理器的中断.异常.介绍了中断向量表的入口.通用的中断处理代码.中断和软中断.延迟处理.中断异常的返回过程. 第二章内容较多,会分几个部分讲述.本部分主要讲进入.退出中断的过程,这部分代码涉及的都是汇编部分.   法律声明:<LINUX3.0内核源代码分析>系列文章由谢宝友(scxby@163.com)发表于ht

Android深度探索(卷2):系统应用源代码分析与ROM定制》——导读

目 录内容提要 前言 第1章 学习前的准备工作第2章 提取ROOT权限第3章 Root权限的安全屏障第4章 ROM定制第5章 Recovery深度分析与定制第6章 Android系统应用的开发与测试 6.1 什么是Android系统应用6.2 为什么要研究Android系统应用 6.3 如何编写Android系统应用 6.4 分析第一个Android系统应用:计算器6.5 小结第7章 安装与卸载应用程序(PackageInstaller)第8章 系统设置(一)第9章 系统设置(二)第10章 系统

《Android深度探索(卷2):系统应用源代码分析与ROM定制》——第6章,第6.4节分析第一个Android系统应用:计算器

6.4 分析第一个Android系统应用:计算器 Android深度探索(卷2):系统应用源代码分析与ROM定制 本节会分析一个比较简单的Android系统应用:计算器.这个系统应用几乎被包含在所有的ROM中,也是最常用的Android系统应用之一.计算器尽管从功能上看并不复杂,但实际的实现代码还是比较多的,不过大多数代码都用于控制各种效果.布局.历史处理等.这些部分的实现与普通的Android应用没什么不同,所以本节会直接深入计算器的核心:计算表达式.因为不管计算器的外观多绚丽,最终的目的都是

FreeBSD 5.0中强制访问控制机制的使用与源代码分析(2)

本文主要讲述FreeBSD 5.0操作系统中新增的重要安全机制,即强制访问控制机制(MAC)的使用与源代码分析,主要包括强制访问控制框架及多级安全(MLS)策略两部分内容.这一部分较系统地对MAC框架及MLS策略的源代码进行分析. 2 MAC框架与MLS策略源代码分析 与本文相关的源代码文件主要有两个,即 /usr/src/sys/kern/kern_mac.c 和 /usr/src/sys/security/mac_mls/mac_mls.c .另外还有一些头文件如mac.h.mac_poli

FreeBSD 5.0中强制访问控制机制的使用与源代码分析(1)

本文主要讲述FreeBSD 5.0操作系统中新增的重要安全机制,即强制访问控制机制(MAC)的使用与源代码分析,主要包括强制访问控制框架及多级安全(MLS)策略两部分内容.这一部分讲述要将MAC框架与MLS策略用起来,应该做的一些工作,以及如何有效使用它们的问题. 强制访问控制(英文缩写MAC)是实现操作系统安全的一个重要的方法,现在几乎所有的安全操作系统都采用强制访问控制作为其核心安全机制之一.强制访问控制是对操作系统的各种客体(如文件.socket.系统FIFO.SCD.IPC等)进行细粒度

Tiny4412 Android5.0 定制media codecs相关的格式(二)

http://blog.csdn.net/morixinguan/article/details/73149058 上一节说到4412的在Android 5.0源代码中支持了许多的格式,那么这些格式最终又是怎么确定的呢? 找到以下这个文件: android-5.0.2/frameworks/base/media/java\android/media/MediaFile.java /* * Copyright (C) 2007 The Android Open Source Project * *

第三方Rom团队CM展示谷歌眼镜定制Recovery

&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp;        今天著名的第三方Rom团队CyanogenMod成员在论坛展示了专为谷歌眼镜定制的ClockworkMod Recovery.CyanogenMod为我们带来的针对谷歌眼镜特制的ClockworkMod Recovery在界面上我们在 Android手机上看到的 差不多. 不过目前还处于非常初级的阶段.但 系统备份与还原.选取第三方系统文件刷入功能.清

ubuntu17.04编译Tiny4412 Android5.0源代码

跟着ubuntu12.04一样,我解开了4412的Android5.0源代码. 因为前面这篇文章已经安装了一些开发环境,所以这里不再哆嗦. 还是和这篇文章一样的: http://blog.csdn.net/morixinguan/article/details/70190518 遇到下面这个错误: 那么,这次就有经验了,直接用下面这个解决方法: clang编辑器的问题,在art/build/Android.common_build.mk 中将host 默认编辑器使用clang关掉,找到WITHO