在上一篇文章中,我们大致的介绍了recovery的启动流程,那么,recovery升级或者做双清的时候,那些图形动画又是如何实现的呢?我们来看看代码。
以下这段代码位于recovery/screen_ui.cpp
1void ScreenRecoveryUI::Init() 2{ 3 gr_init(); 4 5 gr_font_size(&char_width, &char_height); 6 7 text_col = text_row = 0; 8 text_rows = gr_fb_height() / char_height; 9 if (text_rows > kMaxRows) text_rows = kMaxRows; 10 text_top = 1; 11 12 text_cols = gr_fb_width() / char_width; 13 if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; 14 15 backgroundIcon[NONE] = NULL; 16 LoadBitmapArray("icon_installing", &installing_frames, &installation); 17 backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL; 18 backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; 19 LoadBitmap("icon_error", &backgroundIcon[ERROR]); 20 backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; 21 22 LoadBitmap("progress_empty", &progressBarEmpty); 23 LoadBitmap("progress_fill", &progressBarFill); 24 LoadBitmap("stage_empty", &stageMarkerEmpty); 25 LoadBitmap("stage_fill", &stageMarkerFill); 26 27 LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]); 28 LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]); 29 LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); 30 LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); 31 32 pthread_create(&progress_t, NULL, progress_thread, NULL); 33 34 RecoveryUI::Init(); 35}
这段代码都做了哪些事情呢?这些recovery初始化图形显示最开始的部分:(1)调用了miniui中的gr_init初始化显示图形相关的步骤,因为recovery是基于framebuffer机制显示的。
(2)调用gr_font_size设置字体显示的大小,然后计算文本显示行列。
(3)接下来就是装载图片了,会调用到LoadBitmapArray和LoadBitmap这两个函数。其中,我们会看到这些函数里图片的名称:
将上面的字符串与下面的图片一一对应:
那么这些分别是怎么显示的?其中erasing_text是用来显示做清除的时候显示的文字,放大后如下:
这上面有许许多多的语言版本,我们可以根据需要来选择,这些主要要看接下来初始化文字的代码逻辑。
其余的图片中,后缀带text的,也和这些是类似的,有出现错误显示的字体error_text,更新系统显示的字体installing_text,没有命令的时候显示的字体no_command_text。
除了文字显示,我们最关心的就是icon_installing这张图片了,在做系统更新的时候,这个机器人会转动。这不是动画吗?怎么只有一张图片呢?我们找到Android官方网站看看是为什么?原因如下:
1# Copyright (C) 2014 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Script to take a set of frames (PNG files) for a recovery animation 15and turn it into a single output image which contains the input frames 16interlaced by row. Run with the names of all the input frames on the 17command line, in order, followed by the name of the output file.""" 18import sys 19try: 20 import Image 21 import PngImagePlugin 22except ImportError: 23 print "This script requires the Python Imaging Library to be installed." 24 sys.exit(1) 25frames = [Image.open(fn).convert("RGB") for fn in sys.argv[1:-1]] 26assert len(frames) > 0, "Must have at least one input frame." 27sizes = set() 28for fr in frames: 29 sizes.add(fr.size) 30assert len(sizes) == 1, "All input images must have the same size." 31w, h = sizes.pop() 32N = len(frames) 33out = Image.new("RGB", (w, h*N)) 34for j in range(h): 35 for i in range(w): 36 for fn, f in enumerate(frames): 37 out.putpixel((i, j*N+fn), f.getpixel((i, j))) 38# When loading this image, the graphics library expects to find a text 39# chunk that specifies how many frames this animation represents. If 40# you post-process the output of this script with some kind of 41# optimizer tool (eg pngcrush or zopflipng) make sure that your 42# optimizer preserves this text chunk. 43meta = PngImagePlugin.PngInfo() 44meta.add_text("Frames", str(N)) 45out.save(sys.argv[-1], pnginfo=meta)
这也就是为什么,调用这张图片需要用到LoadBitmapArray这个函数的原因。
1void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { 2 int result = res_create_multi_display_surface(filename, frames, surface); 3 if (result < 0) { 4 LOGE("missing bitmap %s\n(Code %d)\n", filename, result); 5 } 6}
调完这个函数后会调用
resources.cpp中的res_create_multi_display_surface函数用于显示,源码如下:
1int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) { 2 GRSurface** surface = NULL; 3 int result = 0; 4 png_structp png_ptr = NULL; 5 png_infop info_ptr = NULL; 6 png_uint_32 width, height; 7 png_byte channels; 8 int i; 9 png_textp text; 10 int num_text; 11 unsigned char* p_row; 12 unsigned int y; 13 14 *pSurface = NULL; 15 *frames = -1; 16 17 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); 18 if (result < 0) return result; 19 20 *frames = 1; 21 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { 22 for (i = 0; i < num_text; ++i) { 23 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { 24 *frames = atoi(text[i].text); 25 break; 26 } 27 } 28 printf(" found frames = %d\n", *frames); 29 } 30 31 if (height % *frames != 0) { 32 printf("bad height (%d) for frame count (%d)\n", height, *frames); 33 result = -9; 34 goto exit; 35 } 36 37 surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*))); 38 if (surface == NULL) { 39 result = -8; 40 goto exit; 41 } 42 for (i = 0; i < *frames; ++i) { 43 surface[i] = init_display_surface(width, height / *frames); 44 if (surface[i] == NULL) { 45 result = -8; 46 goto exit; 47 } 48 } 49 50#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) 51 png_set_bgr(png_ptr); 52#endif 53 54 p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); 55 for (y = 0; y < height; ++y) { 56 png_read_row(png_ptr, p_row, NULL); 57 int frame = y % *frames; 58 unsigned char* out_row = surface[frame]->data + 59 (y / *frames) * surface[frame]->row_bytes; 60 transform_rgb_to_draw(p_row, out_row, channels, width); 61 } 62 free(p_row); 63 64 *pSurface = reinterpret_cast<GRSurface**>(surface); 65 66exit: 67 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 68 69 if (result < 0) { 70 if (surface) { 71 for (i = 0; i < *frames; ++i) { 72 if (surface[i]) free(surface[i]); 73 } 74 free(surface); 75 } 76 } 77 return result; 78}
其余的和text无关图片,会用到LoadBitmap这个函数:
1void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { 2 int result = res_create_multi_display_surface(filename, frames, surface); 3 if (result < 0) { 4 LOGE("missing bitmap %s\n(Code %d)\n", filename, result); 5 } 6}
同样调用到以下函数:
1int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) { 2 GRSurface** surface = NULL; 3 int result = 0; 4 png_structp png_ptr = NULL; 5 png_infop info_ptr = NULL; 6 png_uint_32 width, height; 7 png_byte channels; 8 int i; 9 png_textp text; 10 int num_text; 11 unsigned char* p_row; 12 unsigned int y; 13 14 *pSurface = NULL; 15 *frames = -1; 16 17 result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); 18 if (result < 0) return result; 19 20 *frames = 1; 21 if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { 22 for (i = 0; i < num_text; ++i) { 23 if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { 24 *frames = atoi(text[i].text); 25 break; 26 } 27 } 28 printf(" found frames = %d\n", *frames); 29 } 30 31 if (height % *frames != 0) { 32 printf("bad height (%d) for frame count (%d)\n", height, *frames); 33 result = -9; 34 goto exit; 35 } 36 37 surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*))); 38 if (surface == NULL) { 39 result = -8; 40 goto exit; 41 } 42 for (i = 0; i < *frames; ++i) { 43 surface[i] = init_display_surface(width, height / *frames); 44 if (surface[i] == NULL) { 45 result = -8; 46 goto exit; 47 } 48 } 49 50#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) 51 png_set_bgr(png_ptr); 52#endif 53 54 p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); 55 for (y = 0; y < height; ++y) { 56 png_read_row(png_ptr, p_row, NULL); 57 int frame = y % *frames; 58 unsigned char* out_row = surface[frame]->data + 59 (y / *frames) * surface[frame]->row_bytes; 60 transform_rgb_to_draw(p_row, out_row, channels, width); 61 } 62 free(p_row); 63 64 *pSurface = reinterpret_cast<GRSurface**>(surface); 65 66exit: 67 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 68 69 if (result < 0) { 70 if (surface) { 71 for (i = 0; i < *frames; ++i) { 72 if (surface[i]) free(surface[i]); 73 } 74 free(surface); 75 } 76 } 77 return result; 78}
关于图片我们大概都知道怎么来显示的了,所以,现在我们可以替换Android原生态中的图片,换成我们自己的图片,当然,也不是什么图都可以的,在recovery中,所有的png图片必须是RGB且不带且不能带alhpa通道信息。关于这一点,我们可以看open_png这个函数:
1static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, 2 png_uint_32* width, png_uint_32* height, png_byte* channels) { 3 char resPath[256]; 4 unsigned char header[8]; 5 int result = 0; 6 int color_type, bit_depth; 7 size_t bytesRead; 8 9 snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); 10 resPath[sizeof(resPath)-1] = '\0'; 11 FILE* fp = fopen(resPath, "rb"); 12 if (fp == NULL) { 13 result = -1; 14 goto exit; 15 } 16 17 bytesRead = fread(header, 1, sizeof(header), fp); 18 if (bytesRead != sizeof(header)) { 19 result = -2; 20 goto exit; 21 } 22 23 if (png_sig_cmp(header, 0, sizeof(header))) { 24 result = -3; 25 goto exit; 26 } 27 28 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 29 if (!*png_ptr) { 30 result = -4; 31 goto exit; 32 } 33 34 *info_ptr = png_create_info_struct(*png_ptr); 35 if (!*info_ptr) { 36 result = -5; 37 goto exit; 38 } 39 40 if (setjmp(png_jmpbuf(*png_ptr))) { 41 result = -6; 42 goto exit; 43 } 44 45 png_init_io(*png_ptr, fp); 46 png_set_sig_bytes(*png_ptr, sizeof(header)); 47 png_read_info(*png_ptr, *info_ptr); 48 49 png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, 50 &color_type, NULL, NULL, NULL); 51 52 *channels = png_get_channels(*png_ptr, *info_ptr); 53 54 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { 55 // 8-bit RGB images: great, nothing to do. 56 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) { 57 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. 58 png_set_expand_gray_1_2_4_to_8(*png_ptr); 59 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { 60 // paletted images: expand to 8-bit RGB. Note that we DON'T 61 // currently expand the tRNS chunk (if any) to an alpha 62 // channel, because minui doesn't support alpha channels in 63 // general. 64 png_set_palette_to_rgb(*png_ptr); 65 *channels = 3; 66 } else { 67 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", 68 bit_depth, *channels, color_type); 69 result = -7; 70 goto exit; 71 } 72 73 return result; 74 75 exit: 76 if (result < 0) { 77 png_destroy_read_struct(png_ptr, info_ptr, NULL); 78 } 79 if (fp != NULL) { 80 fclose(fp); 81 } 82 83 return result; 84}
在代码中,我们可以看到如下:
1 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { 2 // 8-bit RGB images: great, nothing to do. 3 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) { 4 // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. 5 png_set_expand_gray_1_2_4_to_8(*png_ptr); 6 } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { 7 // paletted images: expand to 8-bit RGB. Note that we DON'T 8 // currently expand the tRNS chunk (if any) to an alpha 9 // channel, because minui doesn't support alpha channels in 10 // general. 11 png_set_palette_to_rgb(*png_ptr); 12 *channels = 3; 13 } else { 14 fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", 15 bit_depth, *channels, color_type); 16 result = -7; 17 goto exit; 18 }
以下参考一位网友给出的解决方案。
这个函数将图片文件的数据读取到内存,我在其中输出了一些调试信息,输出图片的 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 。图片终于正常显示啦。