Merge "Expose PngHandler via resources.h." am: 3c8fdf3274

am: c41aba80b7

Change-Id: Iabf5238f7f150ab721d402b7c4ab6efd77430a11
This commit is contained in:
Tao Bao
2018-04-14 16:23:39 -07:00
committed by android-build-merger
3 changed files with 154 additions and 155 deletions
+84
View File
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2018 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.
*/
#pragma once
#include <stdio.h>
#include <memory>
#include <string>
#include <png.h>
// This class handles the PNG file parsing. It also holds the ownership of the PNG pointer and the
// opened file pointer. Both will be destroyed / closed when this object goes out of scope.
class PngHandler {
public:
// Constructs an instance by loading the PNG file from '/res/images/<name>.png', or '<name>'.
PngHandler(const std::string& name);
~PngHandler();
png_uint_32 width() const {
return width_;
}
png_uint_32 height() const {
return height_;
}
png_byte channels() const {
return channels_;
}
int bit_depth() const {
return bit_depth_;
}
int color_type() const {
return color_type_;
}
png_structp png_ptr() const {
return png_ptr_;
}
png_infop info_ptr() const {
return info_ptr_;
}
int error_code() const {
return error_code_;
};
operator bool() const {
return error_code_ == 0;
}
private:
png_structp png_ptr_{ nullptr };
png_infop info_ptr_{ nullptr };
png_uint_32 width_;
png_uint_32 height_;
png_byte channels_;
int bit_depth_;
int color_type_;
// The |error_code_| is set to a negative value if an error occurs when opening the png file.
int error_code_{ 0 };
// After initialization, we'll keep the file pointer open before destruction of PngHandler.
std::unique_ptr<FILE, decltype(&fclose)> png_fp_{ nullptr, fclose };
};
+13 -58
View File
@@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
#include "private/resources.h"
#include <fcntl.h> #include <fcntl.h>
#include <linux/fb.h> #include <linux/fb.h>
#include <linux/kd.h> #include <linux/kd.h>
@@ -48,58 +50,13 @@ static GRSurface* malloc_surface(size_t data_size) {
return surface; return surface;
} }
// This class handles the png file parsing. It also holds the ownership of the png pointer and the PngHandler::PngHandler(const std::string& name) {
// opened file pointer. Both will be destroyed/closed when this object goes out of scope.
class PngHandler {
public:
PngHandler(const std::string& name);
~PngHandler();
png_uint_32 width() const {
return width_;
}
png_uint_32 height() const {
return height_;
}
png_byte channels() const {
return channels_;
}
png_structp png_ptr() const {
return png_ptr_;
}
png_infop info_ptr() const {
return info_ptr_;
}
int error_code() const {
return error_code_;
};
operator bool() const {
return error_code_ == 0;
}
private:
png_structp png_ptr_{ nullptr };
png_infop info_ptr_{ nullptr };
png_uint_32 width_;
png_uint_32 height_;
png_byte channels_;
// The |error_code_| is set to a negative value if an error occurs when opening the png file.
int error_code_;
// After initialization, we'll keep the file pointer open before destruction of PngHandler.
std::unique_ptr<FILE, decltype(&fclose)> png_fp_;
};
PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullptr, fclose) {
std::string res_path = android::base::StringPrintf("/res/images/%s.png", name.c_str()); std::string res_path = android::base::StringPrintf("/res/images/%s.png", name.c_str());
png_fp_.reset(fopen(res_path.c_str(), "rbe")); png_fp_.reset(fopen(res_path.c_str(), "rbe"));
// Try to read from |name| if the resource path does not work.
if (!png_fp_) {
png_fp_.reset(fopen(name.c_str(), "rbe"));
}
if (!png_fp_) { if (!png_fp_) {
error_code_ = -1; error_code_ = -1;
return; return;
@@ -138,19 +95,17 @@ PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullpt
png_set_sig_bytes(png_ptr_, sizeof(header)); png_set_sig_bytes(png_ptr_, sizeof(header));
png_read_info(png_ptr_, info_ptr_); png_read_info(png_ptr_, info_ptr_);
int color_type; png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr,
int bit_depth;
png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth, &color_type, nullptr, nullptr,
nullptr); nullptr);
channels_ = png_get_channels(png_ptr_, info_ptr_); channels_ = png_get_channels(png_ptr_, info_ptr_);
if (bit_depth == 8 && channels_ == 3 && color_type == PNG_COLOR_TYPE_RGB) { if (bit_depth_ == 8 && channels_ == 3 && color_type_ == PNG_COLOR_TYPE_RGB) {
// 8-bit RGB images: great, nothing to do. // 8-bit RGB images: great, nothing to do.
} else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_GRAY) { } 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. // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
png_set_expand_gray_1_2_4_to_8(png_ptr_); png_set_expand_gray_1_2_4_to_8(png_ptr_);
} else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { } 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 // paletted images: expand to 8-bit RGB. Note that we DON'T
// currently expand the tRNS chunk (if any) to an alpha // currently expand the tRNS chunk (if any) to an alpha
// channel, because minui doesn't support alpha channels in // channel, because minui doesn't support alpha channels in
@@ -158,8 +113,8 @@ PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullpt
png_set_palette_to_rgb(png_ptr_); png_set_palette_to_rgb(png_ptr_);
channels_ = 3; channels_ = 3;
} else { } else {
fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth, fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_,
channels_, color_type); channels_, color_type_);
error_code_ = -7; error_code_ = -7;
} }
} }
+57 -97
View File
@@ -15,10 +15,14 @@
*/ */
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -26,13 +30,13 @@
#include <android-base/strings.h> #include <android-base/strings.h>
#include <android/log.h> #include <android/log.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <png.h>
#include <private/android_logger.h> #include <private/android_logger.h>
#include "minui/minui.h" #include "minui/minui.h"
#include "private/resources.h"
static const std::string myFilename = "/data/misc/recovery/inject.txt"; static const std::string kInjectTxtFilename = "/data/misc/recovery/inject.txt";
static const std::string myContent = "Hello World\nWelcome to my recovery\n"; static const std::string kInjectTxtContent = "Hello World\nWelcome to my recovery\n";
static const std::string kLocale = "zu"; static const std::string kLocale = "zu";
// Failure is expected on systems that do not deliver either the // Failure is expected on systems that do not deliver either the
@@ -43,9 +47,9 @@ static ssize_t __pmsg_fn(log_id_t logId, char prio, const char *filename,
const char *buf, size_t len, void *arg) { const char *buf, size_t len, void *arg) {
EXPECT_EQ(LOG_ID_SYSTEM, logId); EXPECT_EQ(LOG_ID_SYSTEM, logId);
EXPECT_EQ(ANDROID_LOG_INFO, prio); EXPECT_EQ(ANDROID_LOG_INFO, prio);
EXPECT_NE(std::string::npos, myFilename.find(filename)); EXPECT_NE(std::string::npos, kInjectTxtFilename.find(filename));
EXPECT_EQ(myContent, buf); EXPECT_EQ(kInjectTxtContent, buf);
EXPECT_EQ(myContent.size(), len); EXPECT_EQ(kInjectTxtContent.size(), len);
EXPECT_EQ(nullptr, arg); EXPECT_EQ(nullptr, arg);
return len; return len;
} }
@@ -58,13 +62,14 @@ TEST(recovery, refresh) {
ssize_t ret = __android_log_pmsg_file_read( ssize_t ret = __android_log_pmsg_file_read(
LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
if (ret == -ENOENT) { if (ret == -ENOENT) {
EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, EXPECT_LT(0, __android_log_pmsg_file_write(
myFilename.c_str(), myContent.c_str(), myContent.size())); LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(),
kInjectTxtContent.c_str(), kInjectTxtContent.size()));
fprintf(stderr, "injected test data, requires two intervening reboots " fprintf(stderr,
"to check for replication\n"); "injected test data, requires two intervening reboots to check for replication\n");
} }
EXPECT_EQ(static_cast<ssize_t>(myContent.size()), ret); EXPECT_EQ(static_cast<ssize_t>(kInjectTxtContent.size()), ret);
} }
// recovery.persist - Requires recovery.inject, then a reboot, then // recovery.persist - Requires recovery.inject, then a reboot, then
@@ -75,30 +80,26 @@ TEST(recovery, persist) {
ssize_t ret = __android_log_pmsg_file_read( ssize_t ret = __android_log_pmsg_file_read(
LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr); LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", __pmsg_fn, nullptr);
if (ret == -ENOENT) { if (ret == -ENOENT) {
EXPECT_LT(0, __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, EXPECT_LT(0, __android_log_pmsg_file_write(
myFilename.c_str(), myContent.c_str(), myContent.size())); LOG_ID_SYSTEM, ANDROID_LOG_INFO, kInjectTxtFilename.c_str(),
kInjectTxtContent.c_str(), kInjectTxtContent.size()));
fprintf(stderr, "injected test data, requires intervening reboot " fprintf(stderr, "injected test data, requires intervening reboot to check for storage\n");
"to check for storage\n");
} }
std::string buf; std::string buf;
EXPECT_TRUE(android::base::ReadFileToString(myFilename, &buf)); EXPECT_TRUE(android::base::ReadFileToString(kInjectTxtFilename, &buf));
EXPECT_EQ(myContent, buf); EXPECT_EQ(kInjectTxtContent, buf);
if (access(myFilename.c_str(), F_OK) == 0) { if (access(kInjectTxtFilename.c_str(), F_OK) == 0) {
fprintf(stderr, "Removing persistent test data, " fprintf(stderr, "Removing persistent test data, check if reconstructed on reboot\n");
"check if reconstructed on reboot\n");
} }
EXPECT_EQ(0, unlink(myFilename.c_str())); EXPECT_EQ(0, unlink(kInjectTxtFilename.c_str()));
} }
std::vector<std::string> image_dir { static const std::vector<std::string> kResourceImagesDirs{ "res-mdpi/images/", "res-hdpi/images/",
"res-mdpi/images/", "res-xhdpi/images/",
"res-hdpi/images/", "res-xxhdpi/images/",
"res-xhdpi/images/", "res-xxxhdpi/images/" };
"res-xxhdpi/images/",
"res-xxxhdpi/images/"
};
static int png_filter(const dirent* de) { static int png_filter(const dirent* de) {
if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) { if (de->d_type != DT_REG || !android::base::EndsWith(de->d_name, "_text.png")) {
@@ -107,12 +108,12 @@ static int png_filter(const dirent* de) {
return 1; return 1;
} }
// Find out all the PNG files to test, which stay under the same dir with the executable. // Finds out all the PNG files to test, which stay under the same dir with the executable.
static std::vector<std::string> add_files() { static std::vector<std::string> add_files() {
std::string exec_dir = android::base::GetExecutableDirectory(); std::string exec_dir = android::base::GetExecutableDirectory();
std::vector<std::string> files; std::vector<std::string> files;
for (const std::string& image : image_dir) { for (const std::string& images_dir : kResourceImagesDirs) {
std::string dir_path = exec_dir + "/" + image; std::string dir_path = exec_dir + "/" + images_dir;
dirent** namelist; dirent** namelist;
int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort); int n = scandir(dir_path.c_str(), &namelist, png_filter, alphasort);
if (n == -1) { if (n == -1) {
@@ -133,71 +134,29 @@ static std::vector<std::string> add_files() {
return files; return files;
} }
class ResourceTest : public testing::TestWithParam<std::string> { class ResourcesTest : public testing::TestWithParam<std::string> {
public: public:
static std::vector<std::string> png_list; static std::vector<std::string> png_list;
// Parse a png file and test if it's qualified for the background text image
// under recovery.
void SetUp() override {
std::string file_path = GetParam();
fp = fopen(file_path.c_str(), "rbe");
ASSERT_NE(nullptr, fp);
unsigned char header[8];
size_t bytesRead = fread(header, 1, sizeof(header), fp);
ASSERT_EQ(sizeof(header), bytesRead);
ASSERT_EQ(0, png_sig_cmp(header, 0, sizeof(header)));
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
ASSERT_NE(nullptr, png_ptr);
info_ptr = png_create_info_struct(png_ptr);
ASSERT_NE(nullptr, info_ptr);
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sizeof(header));
png_read_info(png_ptr, info_ptr);
int color_type, bit_depth;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, nullptr, nullptr,
nullptr);
ASSERT_EQ(PNG_COLOR_TYPE_GRAY, color_type) << "Recovery expects grayscale PNG file.";
ASSERT_LT(static_cast<png_uint_32>(5), width);
ASSERT_LT(static_cast<png_uint_32>(0), height);
if (bit_depth <= 8) {
// 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
png_byte channels = png_get_channels(png_ptr, info_ptr);
ASSERT_EQ(1, channels) << "Recovery background text images expects 1-channel PNG file.";
}
void TearDown() override {
if (png_ptr != nullptr && info_ptr != nullptr) {
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
}
if (fp != nullptr) {
fclose(fp);
}
}
protected: protected:
png_structp png_ptr; void SetUp() override {
png_infop info_ptr; png_ = std::make_unique<PngHandler>(GetParam());
png_uint_32 width, height; ASSERT_TRUE(png_);
FILE* fp; ASSERT_EQ(PNG_COLOR_TYPE_GRAY, png_->color_type()) << "Recovery expects grayscale PNG file.";
ASSERT_LT(static_cast<png_uint_32>(5), png_->width());
ASSERT_LT(static_cast<png_uint_32>(0), png_->height());
ASSERT_EQ(1, png_->channels()) << "Recovery background text images expects 1-channel PNG file.";
}
std::unique_ptr<PngHandler> png_{ nullptr };
}; };
std::vector<std::string> ResourceTest::png_list = add_files(); // Parses a png file and tests if it's qualified for the background text image under recovery.
TEST_P(ResourcesTest, ValidateLocale) {
TEST_P(ResourceTest, ValidateLocale) { std::vector<unsigned char> row(png_->width());
std::vector<unsigned char> row(width); for (png_uint_32 y = 0; y < png_->height(); ++y) {
for (png_uint_32 y = 0; y < height; ++y) { png_read_row(png_->png_ptr(), row.data(), nullptr);
png_read_row(png_ptr, row.data(), nullptr);
int w = (row[1] << 8) | row[0]; int w = (row[1] << 8) | row[0];
int h = (row[3] << 8) | row[2]; int h = (row[3] << 8) | row[2];
int len = row[4]; int len = row[4];
@@ -206,19 +165,20 @@ TEST_P(ResourceTest, ValidateLocale) {
EXPECT_LT(0, len) << "Locale string should be non-empty."; EXPECT_LT(0, len) << "Locale string should be non-empty.";
EXPECT_NE(0, row[5]) << "Locale string is missing."; EXPECT_NE(0, row[5]) << "Locale string is missing.";
ASSERT_GT(height, y + 1 + h) << "Locale: " << kLocale << " is not found in the file."; ASSERT_GT(png_->height(), y + 1 + h) << "Locale: " << kLocale << " is not found in the file.";
char* loc = reinterpret_cast<char*>(&row[5]); char* loc = reinterpret_cast<char*>(&row[5]);
if (matches_locale(loc, kLocale.c_str())) { if (matches_locale(loc, kLocale.c_str())) {
EXPECT_TRUE(android::base::StartsWith(loc, kLocale)); EXPECT_TRUE(android::base::StartsWith(loc, kLocale));
break; break;
} else { }
for (int i = 0; i < h; ++i, ++y) { for (int i = 0; i < h; ++i, ++y) {
png_read_row(png_ptr, row.data(), nullptr); png_read_row(png_->png_ptr(), row.data(), nullptr);
}
} }
} }
} }
INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourceTest, std::vector<std::string> ResourcesTest::png_list = add_files();
::testing::ValuesIn(ResourceTest::png_list.cbegin(),
ResourceTest::png_list.cend())); INSTANTIATE_TEST_CASE_P(BackgroundTextValidation, ResourcesTest,
::testing::ValuesIn(ResourcesTest::png_list.cbegin(),
ResourcesTest::png_list.cend()));