diff options
Diffstat (limited to 'minui/resources.cpp')
-rw-r--r-- | minui/resources.cpp | 164 |
1 files changed, 66 insertions, 98 deletions
diff --git a/minui/resources.cpp b/minui/resources.cpp index 52ab60b1b..c01c1868e 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "private/resources.h" + #include <fcntl.h> #include <linux/fb.h> #include <linux/kd.h> @@ -30,76 +32,37 @@ #include <string> #include <vector> -#include <android-base/stringprintf.h> #include <android-base/strings.h> #include <png.h> #include "minui/minui.h" -#define SURFACE_DATA_ALIGNMENT 8 +static std::string g_resource_dir{ "/res/images" }; -static GRSurface* malloc_surface(size_t data_size) { - size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; - unsigned char* temp = static_cast<unsigned char*>(malloc(size)); - if (temp == NULL) return NULL; - GRSurface* surface = reinterpret_cast<GRSurface*>(temp); - surface->data = temp + sizeof(GRSurface) + - (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); - return surface; +std::unique_ptr<GRSurface> GRSurface::Create(size_t data_size) { + static constexpr size_t kSurfaceDataAlignment = 8; + std::unique_ptr<GRSurface> result = std::make_unique<GRSurface>(); + size_t aligned_size = + (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment; + result->data_ = static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, aligned_size)); + if (result->data_ == nullptr) return nullptr; + return result; } -// 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: - 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; +GRSurface::~GRSurface() { + if (data_ != nullptr) { + free(data_); + data_ = nullptr; } +} - 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()); +PngHandler::PngHandler(const std::string& name) { + std::string res_path = g_resource_dir + "/" + name + ".png"; 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_) { error_code_ = -1; return; @@ -138,19 +101,17 @@ PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullpt png_set_sig_bytes(png_ptr_, sizeof(header)); png_read_info(png_ptr_, info_ptr_); - int color_type; - int bit_depth; - png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth, &color_type, nullptr, nullptr, + png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth_, &color_type_, nullptr, nullptr, nullptr); 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. - } 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. 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 // currently expand the tRNS chunk (if any) to an alpha // channel, because minui doesn't support alpha channels in @@ -158,8 +119,8 @@ PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullpt 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); + fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth_, + channels_, color_type_); error_code_ = -7; } } @@ -177,18 +138,18 @@ PngHandler::~PngHandler() { // framebuffer pixel format; they need to be modified if the // framebuffer format changes (but nothing else should). -// Allocate and return a GRSurface* sufficient for storing an image of -// the indicated size in the framebuffer pixel format. -static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { - GRSurface* surface = malloc_surface(width * height * 4); - if (surface == NULL) return NULL; +// Allocates and returns a GRSurface* sufficient for storing an image of the indicated size in the +// framebuffer pixel format. +static std::unique_ptr<GRSurface> init_display_surface(png_uint_32 width, png_uint_32 height) { + std::unique_ptr<GRSurface> surface = GRSurface::Create(width * height * 4); + if (!surface) return nullptr; - surface->width = width; - surface->height = height; - surface->row_bytes = width * 4; - surface->pixel_bytes = 4; + surface->width = width; + surface->height = height; + surface->row_bytes = width * 4; + surface->pixel_bytes = 4; - return surface; + return surface; } // Copy 'input_row' to 'output_row', transforming it to the @@ -246,23 +207,24 @@ int res_create_display_surface(const char* name, GRSurface** pSurface) { png_uint_32 width = png_handler.width(); png_uint_32 height = png_handler.height(); - GRSurface* surface = init_display_surface(width, height); + std::unique_ptr<GRSurface> surface = init_display_surface(width, height); if (!surface) { return -8; } -#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) - png_set_bgr(png_ptr); -#endif + PixelFormat pixel_format = gr_pixel_format(); + if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) { + png_set_bgr(png_ptr); + } for (png_uint_32 y = 0; y < height; ++y) { std::vector<unsigned char> p_row(width * 4); png_read_row(png_ptr, p_row.data(), nullptr); - transform_rgb_to_draw(p_row.data(), surface->data + y * surface->row_bytes, + transform_rgb_to_draw(p_row.data(), surface->data() + y * surface->row_bytes, png_handler.channels(), width); } - *pSurface = surface; + *pSurface = surface.release(); return 0; } @@ -315,22 +277,23 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, goto exit; } for (int i = 0; i < *frames; ++i) { - surface[i] = init_display_surface(width, height / *frames); - if (!surface[i]) { + auto created_surface = init_display_surface(width, height / *frames); + if (!created_surface) { result = -8; goto exit; } + surface[i] = created_surface.release(); } -#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) - png_set_bgr(png_ptr); -#endif + if (gr_pixel_format() == PixelFormat::ABGR || gr_pixel_format() == PixelFormat::BGRA) { + png_set_bgr(png_ptr); + } for (png_uint_32 y = 0; y < height; ++y) { std::vector<unsigned char> p_row(width * 4); png_read_row(png_ptr, p_row.data(), nullptr); int frame = y % *frames; - unsigned char* out_row = surface[frame]->data + (y / *frames) * surface[frame]->row_bytes; + unsigned char* out_row = surface[frame]->data() + (y / *frames) * surface[frame]->row_bytes; transform_rgb_to_draw(p_row.data(), out_row, png_handler.channels(), width); } @@ -362,7 +325,7 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface) { png_uint_32 width = png_handler.width(); png_uint_32 height = png_handler.height(); - GRSurface* surface = malloc_surface(width * height); + std::unique_ptr<GRSurface> surface = GRSurface::Create(width * height); if (!surface) { return -8; } @@ -371,20 +334,25 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface) { surface->row_bytes = width; surface->pixel_bytes = 1; -#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) - png_set_bgr(png_ptr); -#endif + PixelFormat pixel_format = gr_pixel_format(); + if (pixel_format == PixelFormat::ABGR || pixel_format == PixelFormat::BGRA) { + png_set_bgr(png_ptr); + } for (png_uint_32 y = 0; y < height; ++y) { - unsigned char* p_row = surface->data + y * surface->row_bytes; + unsigned char* p_row = surface->data() + y * surface->row_bytes; png_read_row(png_ptr, p_row, nullptr); } - *pSurface = surface; + *pSurface = surface.release(); return 0; } +void res_set_resource_dir(const std::string& dirname) { + g_resource_dir = dirname; +} + // This function tests if a locale string stored in PNG (prefix) matches // the locale string provided by the system (locale). bool matches_locale(const std::string& prefix, const std::string& locale) { @@ -467,7 +435,7 @@ int res_create_localized_alpha_surface(const char* name, if (y + 1 + h >= height || matches_locale(loc, locale)) { printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); - GRSurface* surface = malloc_surface(w * h); + std::unique_ptr<GRSurface> surface = GRSurface::Create(w * h); if (!surface) { return -8; } @@ -478,10 +446,10 @@ int res_create_localized_alpha_surface(const char* name, for (int i = 0; i < h; ++i, ++y) { png_read_row(png_ptr, row.data(), nullptr); - memcpy(surface->data + i * w, row.data(), w); + memcpy(surface->data() + i * w, row.data(), w); } - *pSurface = surface; + *pSurface = surface.release(); break; } |