diff options
Diffstat (limited to 'minui')
-rw-r--r-- | minui/Android.mk | 27 | ||||
-rw-r--r-- | minui/events.c | 211 | ||||
-rw-r--r-- | minui/events.cpp | 232 | ||||
-rw-r--r-- | minui/graphics.cpp (renamed from minui/graphics.c) | 88 | ||||
-rw-r--r-- | minui/graphics.h | 25 | ||||
-rw-r--r-- | minui/graphics_adf.cpp (renamed from minui/graphics_adf.c) | 52 | ||||
-rw-r--r-- | minui/graphics_drm.cpp | 476 | ||||
-rw-r--r-- | minui/graphics_fbdev.cpp (renamed from minui/graphics_fbdev.c) | 38 | ||||
-rw-r--r-- | minui/minui.h | 85 | ||||
-rw-r--r-- | minui/resources.cpp (renamed from minui/resources.c) | 76 |
10 files changed, 904 insertions, 406 deletions
diff --git a/minui/Android.mk b/minui/Android.mk index df4aac169..97724fbf0 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -1,21 +1,29 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES := graphics.c graphics_adf.c graphics_fbdev.c events.c \ - resources.c - -LOCAL_C_INCLUDES +=\ - external/libpng\ - external/zlib +LOCAL_SRC_FILES := \ + events.cpp \ + graphics.cpp \ + graphics_adf.cpp \ + graphics_drm.cpp \ + graphics_fbdev.cpp \ + resources.cpp \ LOCAL_WHOLE_STATIC_LIBRARIES += libadf +LOCAL_WHOLE_STATIC_LIBRARIES += libdrm +LOCAL_STATIC_LIBRARIES += libpng LOCAL_MODULE := libminui +LOCAL_CLANG := true + # This used to compare against values in double-quotes (which are just # ordinary characters in this context). Strip double-quotes from the # value so that either will work. +ifeq ($(subst ",,$(TARGET_RECOVERY_PIXEL_FORMAT)),ABGR_8888) + LOCAL_CFLAGS += -DRECOVERY_ABGR +endif ifeq ($(subst ",,$(TARGET_RECOVERY_PIXEL_FORMAT)),RGBX_8888) LOCAL_CFLAGS += -DRECOVERY_RGBX endif @@ -30,3 +38,10 @@ else endif include $(BUILD_STATIC_LIBRARY) + +# Used by OEMs for factory test images. +include $(CLEAR_VARS) +LOCAL_MODULE := libminui +LOCAL_WHOLE_STATIC_LIBRARIES += libminui +LOCAL_SHARED_LIBRARIES := libpng +include $(BUILD_SHARED_LIBRARY) diff --git a/minui/events.c b/minui/events.c deleted file mode 100644 index df7dad448..000000000 --- a/minui/events.c +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright (C) 2007 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. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <fcntl.h> -#include <dirent.h> -#include <sys/epoll.h> - -#include <linux/input.h> - -#include "minui.h" - -#define MAX_DEVICES 16 -#define MAX_MISC_FDS 16 - -#define BITS_PER_LONG (sizeof(unsigned long) * 8) -#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) - -#define test_bit(bit, array) \ - ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG))) - -struct fd_info { - int fd; - ev_callback cb; - void *data; -}; - -static int epollfd; -static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; -static int npolledevents; - -static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; - -static unsigned ev_count = 0; -static unsigned ev_dev_count = 0; -static unsigned ev_misc_count = 0; - -int ev_init(ev_callback input_cb, void *data) -{ - DIR *dir; - struct dirent *de; - int fd; - struct epoll_event ev; - bool epollctlfail = false; - - epollfd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); - if (epollfd == -1) - return -1; - - dir = opendir("/dev/input"); - if(dir != 0) { - while((de = readdir(dir))) { - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; - -// fprintf(stderr,"/dev/input/%s\n", de->d_name); - if(strncmp(de->d_name,"event",5)) continue; - fd = openat(dirfd(dir), de->d_name, O_RDONLY); - if(fd < 0) continue; - - /* read the evbits of the input device */ - if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) { - close(fd); - continue; - } - - /* TODO: add ability to specify event masks. For now, just assume - * that only EV_KEY and EV_REL event types are ever needed. */ - if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) { - close(fd); - continue; - } - - ev.events = EPOLLIN | EPOLLWAKEUP; - ev.data.ptr = (void *)&ev_fdinfo[ev_count]; - if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) { - close(fd); - epollctlfail = true; - continue; - } - - ev_fdinfo[ev_count].fd = fd; - ev_fdinfo[ev_count].cb = input_cb; - ev_fdinfo[ev_count].data = data; - ev_count++; - ev_dev_count++; - if(ev_dev_count == MAX_DEVICES) break; - } - } - - if (epollctlfail && !ev_count) { - close(epollfd); - epollfd = -1; - return -1; - } - - return 0; -} - -int ev_add_fd(int fd, ev_callback cb, void *data) -{ - struct epoll_event ev; - int ret; - - if (ev_misc_count == MAX_MISC_FDS || cb == NULL) - return -1; - - ev.events = EPOLLIN | EPOLLWAKEUP; - ev.data.ptr = (void *)&ev_fdinfo[ev_count]; - ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); - if (!ret) { - ev_fdinfo[ev_count].fd = fd; - ev_fdinfo[ev_count].cb = cb; - ev_fdinfo[ev_count].data = data; - ev_count++; - ev_misc_count++; - } - - return ret; -} - -int ev_get_epollfd(void) -{ - return epollfd; -} - -void ev_exit(void) -{ - while (ev_count > 0) { - close(ev_fdinfo[--ev_count].fd); - } - ev_misc_count = 0; - ev_dev_count = 0; - close(epollfd); -} - -int ev_wait(int timeout) -{ - npolledevents = epoll_wait(epollfd, polledevents, ev_count, timeout); - if (npolledevents <= 0) - return -1; - return 0; -} - -void ev_dispatch(void) -{ - int n; - int ret; - - for (n = 0; n < npolledevents; n++) { - struct fd_info *fdi = polledevents[n].data.ptr; - ev_callback cb = fdi->cb; - if (cb) - cb(fdi->fd, polledevents[n].events, fdi->data); - } -} - -int ev_get_input(int fd, uint32_t epevents, struct input_event *ev) -{ - int r; - - if (epevents & EPOLLIN) { - r = read(fd, ev, sizeof(*ev)); - if (r == sizeof(*ev)) - return 0; - } - return -1; -} - -int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data) -{ - unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; - unsigned i; - int ret; - - for (i = 0; i < ev_dev_count; i++) { - int code; - - memset(key_bits, 0, sizeof(key_bits)); - memset(ev_bits, 0, sizeof(ev_bits)); - - ret = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits); - if (ret < 0 || !test_bit(EV_KEY, ev_bits)) - continue; - - ret = ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits); - if (ret < 0) - continue; - - for (code = 0; code <= KEY_MAX; code++) { - if (test_bit(code, key_bits)) - set_key_cb(code, 1, data); - } - } - - return 0; -} diff --git a/minui/events.cpp b/minui/events.cpp new file mode 100644 index 000000000..3b2262a4b --- /dev/null +++ b/minui/events.cpp @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2007 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. + */ + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/epoll.h> +#include <unistd.h> + +#include <linux/input.h> + +#include "minui.h" + +#define MAX_DEVICES 16 +#define MAX_MISC_FDS 16 + +#define BITS_PER_LONG (sizeof(unsigned long) * 8) +#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) + +struct fd_info { + int fd; + ev_callback cb; + void* data; +}; + +static int g_epoll_fd; +static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; +static int npolledevents; + +static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; + +static unsigned ev_count = 0; +static unsigned ev_dev_count = 0; +static unsigned ev_misc_count = 0; + +static bool test_bit(size_t bit, unsigned long* array) { + return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; +} + +int ev_init(ev_callback input_cb, void* data) { + bool epollctlfail = false; + + g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); + if (g_epoll_fd == -1) { + return -1; + } + + DIR* dir = opendir("/dev/input"); + if (dir != NULL) { + dirent* de; + while ((de = readdir(dir))) { + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; + +// fprintf(stderr,"/dev/input/%s\n", de->d_name); + if (strncmp(de->d_name, "event", 5)) continue; + int fd = openat(dirfd(dir), de->d_name, O_RDONLY); + if (fd == -1) continue; + + // Read the evbits of the input device. + if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { + close(fd); + continue; + } + + // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. + if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { + close(fd); + continue; + } + + epoll_event ev; + ev.events = EPOLLIN | EPOLLWAKEUP; + ev.data.ptr = &ev_fdinfo[ev_count]; + if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { + close(fd); + epollctlfail = true; + continue; + } + + ev_fdinfo[ev_count].fd = fd; + ev_fdinfo[ev_count].cb = input_cb; + ev_fdinfo[ev_count].data = data; + ev_count++; + ev_dev_count++; + if (ev_dev_count == MAX_DEVICES) break; + } + + closedir(dir); + } + + if (epollctlfail && !ev_count) { + close(g_epoll_fd); + g_epoll_fd = -1; + return -1; + } + + return 0; +} + +int ev_get_epollfd(void) { + return g_epoll_fd; +} + +int ev_add_fd(int fd, ev_callback cb, void* data) { + if (ev_misc_count == MAX_MISC_FDS || cb == NULL) { + return -1; + } + + epoll_event ev; + ev.events = EPOLLIN | EPOLLWAKEUP; + ev.data.ptr = (void *)&ev_fdinfo[ev_count]; + int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); + if (!ret) { + ev_fdinfo[ev_count].fd = fd; + ev_fdinfo[ev_count].cb = cb; + ev_fdinfo[ev_count].data = data; + ev_count++; + ev_misc_count++; + } + + return ret; +} + +void ev_exit(void) { + while (ev_count > 0) { + close(ev_fdinfo[--ev_count].fd); + } + ev_misc_count = 0; + ev_dev_count = 0; + close(g_epoll_fd); +} + +int ev_wait(int timeout) { + npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout); + if (npolledevents <= 0) { + return -1; + } + return 0; +} + +void ev_dispatch(void) { + for (int n = 0; n < npolledevents; n++) { + fd_info* fdi = reinterpret_cast<fd_info*>(polledevents[n].data.ptr); + ev_callback cb = fdi->cb; + if (cb) { + cb(fdi->fd, polledevents[n].events, fdi->data); + } + } +} + +int ev_get_input(int fd, uint32_t epevents, input_event* ev) { + if (epevents & EPOLLIN) { + ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev))); + if (r == sizeof(*ev)) { + return 0; + } + } + return -1; +} + +int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; + unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; + + for (size_t i = 0; i < ev_dev_count; ++i) { + memset(ev_bits, 0, sizeof(ev_bits)); + memset(key_bits, 0, sizeof(key_bits)); + + if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { + continue; + } + if (!test_bit(EV_KEY, ev_bits)) { + continue; + } + if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) { + continue; + } + + for (int code = 0; code <= KEY_MAX; code++) { + if (test_bit(code, key_bits)) { + set_key_cb(code, 1, data); + } + } + } + + return 0; +} + +void ev_iterate_available_keys(std::function<void(int)> f) { + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; + unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; + + for (size_t i = 0; i < ev_dev_count; ++i) { + memset(ev_bits, 0, sizeof(ev_bits)); + memset(key_bits, 0, sizeof(key_bits)); + + // Does this device even have keys? + if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { + continue; + } + if (!test_bit(EV_KEY, ev_bits)) { + continue; + } + + int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits); + if (rc == -1) { + continue; + } + + for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { + if (test_bit(key_code, key_bits)) { + f(key_code); + } + } + } +} diff --git a/minui/graphics.c b/minui/graphics.cpp index 6049d85ca..c0eea9e38 100644 --- a/minui/graphics.c +++ b/minui/graphics.cpp @@ -16,6 +16,7 @@ #include <stdbool.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include <fcntl.h> @@ -34,11 +35,11 @@ #include "minui.h" #include "graphics.h" -typedef struct { +struct GRFont { GRSurface* texture; int cwidth; int cheight; -} GRFont; +}; static GRFont* gr_font = NULL; static minui_backend* gr_backend = NULL; @@ -47,8 +48,6 @@ static int overscan_percent = OVERSCAN_PERCENT; static int overscan_offset_x = 0; static int overscan_offset_y = 0; -static int gr_vt_fd = -1; - static unsigned char gr_current_r = 255; static unsigned char gr_current_g = 255; static unsigned char gr_current_b = 255; @@ -76,11 +75,10 @@ static void text_blend(unsigned char* src_p, int src_row_bytes, unsigned char* dst_p, int dst_row_bytes, int width, int height) { - int i, j; - for (j = 0; j < height; ++j) { + for (int j = 0; j < height; ++j) { unsigned char* sx = src_p; unsigned char* px = dst_p; - for (i = 0; i < width; ++i) { + for (int i = 0; i < width; ++i) { unsigned char a = *sx++; if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255; if (a == 255) { @@ -105,34 +103,33 @@ static void text_blend(unsigned char* src_p, int src_row_bytes, } } - -void gr_text(int x, int y, const char *s, int bold) +void gr_text(int x, int y, const char *s, bool bold) { - GRFont *font = gr_font; - unsigned off; + GRFont* font = gr_font; - if (!font->texture) return; - if (gr_current_a == 0) return; + if (!font->texture || gr_current_a == 0) return; bold = bold && (font->texture->height != font->cheight); x += overscan_offset_x; y += overscan_offset_y; - while((off = *s++)) { - off -= 32; + unsigned char ch; + while ((ch = *s++)) { if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; - if (off < 96) { - unsigned char* src_p = font->texture->data + (off * font->cwidth) + - (bold ? font->cheight * font->texture->row_bytes : 0); - unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; + if (ch < ' ' || ch > '~') { + ch = '?'; + } - text_blend(src_p, font->texture->row_bytes, - dst_p, gr_draw->row_bytes, - font->cwidth, font->cheight); + unsigned char* src_p = font->texture->data + ((ch - ' ') * font->cwidth) + + (bold ? font->cheight * font->texture->row_bytes : 0); + unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; + + text_blend(src_p, font->texture->row_bytes, + dst_p, gr_draw->row_bytes, + font->cwidth, font->cheight); - } x += font->cwidth; } } @@ -160,22 +157,27 @@ void gr_texticon(int x, int y, GRSurface* icon) { void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + gr_current_r = b; + gr_current_g = g; + gr_current_b = r; + gr_current_a = a; +#else gr_current_r = r; gr_current_g = g; gr_current_b = b; gr_current_a = a; +#endif } void gr_clear() { - if (gr_current_r == gr_current_g && - gr_current_r == gr_current_b) { + if (gr_current_r == gr_current_g && gr_current_r == gr_current_b) { memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes); } else { - int x, y; unsigned char* px = gr_draw->data; - for (y = 0; y < gr_draw->height; ++y) { - for (x = 0; x < gr_draw->width; ++x) { + for (int y = 0; y < gr_draw->height; ++y) { + for (int x = 0; x < gr_draw->width; ++x) { *px++ = gr_current_r; *px++ = gr_current_g; *px++ = gr_current_b; @@ -267,7 +269,7 @@ unsigned int gr_get_height(GRSurface* surface) { static void gr_init_font(void) { - gr_font = calloc(sizeof(*gr_font), 1); + gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1)); int res = res_create_alpha_surface("font", &(gr_font->texture)); if (res == 0) { @@ -280,14 +282,14 @@ static void gr_init_font(void) printf("failed to read font: res=%d\n", res); // fall back to the compiled-in font. - gr_font->texture = malloc(sizeof(*gr_font->texture)); + gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture))); gr_font->texture->width = font.width; gr_font->texture->height = font.height; gr_font->texture->row_bytes = font.width; gr_font->texture->pixel_bytes = 1; - unsigned char* bits = malloc(font.width * font.height); - gr_font->texture->data = (void*) bits; + unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height)); + gr_font->texture->data = reinterpret_cast<unsigned char*>(bits); unsigned char data; unsigned char* in = font.rundata; @@ -324,7 +326,7 @@ static void gr_test() { gr_clear(); gr_color(255, 0, 0, 255); - gr_surface frame = images[x%frames]; + GRSurface* frame = images[x%frames]; gr_blit(frame, 0, 0, frame->width, frame->height, x, 0); gr_color(255, 0, 0, 128); @@ -358,17 +360,6 @@ int gr_init(void) { gr_init_font(); - gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); - if (gr_vt_fd < 0) { - // This is non-fatal; post-Cupcake kernels don't have tty0. - perror("can't open /dev/tty0"); - } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { - // However, if we do open tty0, we expect the ioctl to work. - perror("failed KDSETMODE to KD_GRAPHICS on tty0"); - gr_exit(); - return -1; - } - gr_backend = open_adf(); if (gr_backend) { gr_draw = gr_backend->init(gr_backend); @@ -378,6 +369,11 @@ int gr_init(void) } if (!gr_draw) { + gr_backend = open_drm(); + gr_draw = gr_backend->init(gr_backend); + } + + if (!gr_draw) { gr_backend = open_fbdev(); gr_draw = gr_backend->init(gr_backend); if (gr_draw == NULL) { @@ -397,10 +393,6 @@ int gr_init(void) void gr_exit(void) { gr_backend->exit(gr_backend); - - ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); - close(gr_vt_fd); - gr_vt_fd = -1; } int gr_fb_width(void) diff --git a/minui/graphics.h b/minui/graphics.h index 993e986ee..52968eb10 100644 --- a/minui/graphics.h +++ b/minui/graphics.h @@ -17,34 +17,27 @@ #ifndef _GRAPHICS_H_ #define _GRAPHICS_H_ -#ifdef __cplusplus -extern "C" { -#endif - -#include <stdbool.h> #include "minui.h" -typedef struct minui_backend { - // Initializes the backend and returns a gr_surface to draw into. - gr_surface (*init)(struct minui_backend*); +// TODO: lose the function pointers. +struct minui_backend { + // Initializes the backend and returns a GRSurface* to draw into. + GRSurface* (*init)(minui_backend*); // Causes the current drawing surface (returned by the most recent // call to flip() or init()) to be displayed, and returns a new // drawing surface. - gr_surface (*flip)(struct minui_backend*); + GRSurface* (*flip)(minui_backend*); // Blank (or unblank) the screen. - void (*blank)(struct minui_backend*, bool); + void (*blank)(minui_backend*, bool); // Device cleanup when drawing is done. - void (*exit)(struct minui_backend*); -} minui_backend; + void (*exit)(minui_backend*); +}; minui_backend* open_fbdev(); minui_backend* open_adf(); - -#ifdef __cplusplus -} -#endif +minui_backend* open_drm(); #endif diff --git a/minui/graphics_adf.c b/minui/graphics_adf.cpp index ac6d64e9e..5d0867f58 100644 --- a/minui/graphics_adf.c +++ b/minui/graphics_adf.cpp @@ -19,6 +19,7 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include <sys/cdefs.h> @@ -43,15 +44,13 @@ struct adf_pdata { unsigned int current_surface; unsigned int n_surfaces; - struct adf_surface_pdata surfaces[2]; + adf_surface_pdata surfaces[2]; }; -static gr_surface adf_flip(struct minui_backend *backend); -static void adf_blank(struct minui_backend *backend, bool blank); +static GRSurface* adf_flip(minui_backend *backend); +static void adf_blank(minui_backend *backend, bool blank); -static int adf_surface_init(struct adf_pdata *pdata, - struct drm_mode_modeinfo *mode, struct adf_surface_pdata *surf) -{ +static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) { memset(surf, 0, sizeof(*surf)); surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, @@ -64,8 +63,9 @@ static int adf_surface_init(struct adf_pdata *pdata, surf->base.row_bytes = surf->pitch; surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; - surf->base.data = mmap(NULL, surf->pitch * surf->base.height, PROT_WRITE, - MAP_SHARED, surf->fd, surf->offset); + surf->base.data = reinterpret_cast<uint8_t*>(mmap(NULL, + surf->pitch * surf->base.height, PROT_WRITE, + MAP_SHARED, surf->fd, surf->offset)); if (surf->base.data == MAP_FAILED) { close(surf->fd); return -errno; @@ -74,9 +74,9 @@ static int adf_surface_init(struct adf_pdata *pdata, return 0; } -static int adf_interface_init(struct adf_pdata *pdata) +static int adf_interface_init(adf_pdata *pdata) { - struct adf_interface_data intf_data; + adf_interface_data intf_data; int ret = 0; int err; @@ -106,7 +106,7 @@ done: return ret; } -static int adf_device_init(struct adf_pdata *pdata, struct adf_device *dev) +static int adf_device_init(adf_pdata *pdata, adf_device *dev) { adf_id_t intf_id; int intf_fd; @@ -134,14 +134,16 @@ static int adf_device_init(struct adf_pdata *pdata, struct adf_device *dev) return err; } -static gr_surface adf_init(minui_backend *backend) +static GRSurface* adf_init(minui_backend *backend) { - struct adf_pdata *pdata = (struct adf_pdata *)backend; + adf_pdata *pdata = (adf_pdata *)backend; adf_id_t *dev_ids = NULL; ssize_t n_dev_ids, i; - gr_surface ret; + GRSurface* ret; -#if defined(RECOVERY_BGRA) +#if defined(RECOVERY_ABGR) + pdata->format = DRM_FORMAT_ABGR8888; +#elif defined(RECOVERY_BGRA) pdata->format = DRM_FORMAT_BGRA8888; #elif defined(RECOVERY_RGBX) pdata->format = DRM_FORMAT_RGBX8888; @@ -161,7 +163,7 @@ static gr_surface adf_init(minui_backend *backend) pdata->intf_fd = -1; for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { - struct adf_device dev; + adf_device dev; int err = adf_device_open(dev_ids[i], O_RDWR, &dev); if (err < 0) { @@ -191,10 +193,10 @@ static gr_surface adf_init(minui_backend *backend) return ret; } -static gr_surface adf_flip(struct minui_backend *backend) +static GRSurface* adf_flip(minui_backend *backend) { - struct adf_pdata *pdata = (struct adf_pdata *)backend; - struct adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface]; + adf_pdata *pdata = (adf_pdata *)backend; + adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface]; int fence_fd = adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, surf->base.width, surf->base.height, pdata->format, surf->fd, @@ -206,22 +208,22 @@ static gr_surface adf_flip(struct minui_backend *backend) return &pdata->surfaces[pdata->current_surface].base; } -static void adf_blank(struct minui_backend *backend, bool blank) +static void adf_blank(minui_backend *backend, bool blank) { - struct adf_pdata *pdata = (struct adf_pdata *)backend; + adf_pdata *pdata = (adf_pdata *)backend; adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); } -static void adf_surface_destroy(struct adf_surface_pdata *surf) +static void adf_surface_destroy(adf_surface_pdata *surf) { munmap(surf->base.data, surf->pitch * surf->base.height); close(surf->fd); } -static void adf_exit(struct minui_backend *backend) +static void adf_exit(minui_backend *backend) { - struct adf_pdata *pdata = (struct adf_pdata *)backend; + adf_pdata *pdata = (adf_pdata *)backend; unsigned int i; for (i = 0; i < pdata->n_surfaces; i++) @@ -233,7 +235,7 @@ static void adf_exit(struct minui_backend *backend) minui_backend *open_adf() { - struct adf_pdata *pdata = calloc(1, sizeof(*pdata)); + adf_pdata* pdata = reinterpret_cast<adf_pdata*>(calloc(1, sizeof(*pdata))); if (!pdata) { perror("allocating adf backend failed"); return NULL; diff --git a/minui/graphics_drm.cpp b/minui/graphics_drm.cpp new file mode 100644 index 000000000..03e33b775 --- /dev/null +++ b/minui/graphics_drm.cpp @@ -0,0 +1,476 @@ +/* + * Copyright (C) 2015 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. + */ + +#include <drm_fourcc.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/cdefs.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <unistd.h> +#include <xf86drm.h> +#include <xf86drmMode.h> + +#include "minui.h" +#include "graphics.h" + +#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) + +struct drm_surface { + GRSurface base; + uint32_t fb_id; + uint32_t handle; +}; + +static drm_surface *drm_surfaces[2]; +static int current_buffer; + +static drmModeCrtc *main_monitor_crtc; +static drmModeConnector *main_monitor_connector; + +static int drm_fd = -1; + +static void drm_disable_crtc(int drm_fd, drmModeCrtc *crtc) { + if (crtc) { + drmModeSetCrtc(drm_fd, crtc->crtc_id, + 0, // fb_id + 0, 0, // x,y + NULL, // connectors + 0, // connector_count + NULL); // mode + } +} + +static void drm_enable_crtc(int drm_fd, drmModeCrtc *crtc, + struct drm_surface *surface) { + int32_t ret; + + ret = drmModeSetCrtc(drm_fd, crtc->crtc_id, + surface->fb_id, + 0, 0, // x,y + &main_monitor_connector->connector_id, + 1, // connector_count + &main_monitor_crtc->mode); + + if (ret) + printf("drmModeSetCrtc failed ret=%d\n", ret); +} + +static void drm_blank(minui_backend* backend __unused, bool blank) { + if (blank) + drm_disable_crtc(drm_fd, main_monitor_crtc); + else + drm_enable_crtc(drm_fd, main_monitor_crtc, + drm_surfaces[current_buffer]); +} + +static void drm_destroy_surface(struct drm_surface *surface) { + struct drm_gem_close gem_close; + int ret; + + if(!surface) + return; + + if (surface->base.data) + munmap(surface->base.data, + surface->base.row_bytes * surface->base.height); + + if (surface->fb_id) { + ret = drmModeRmFB(drm_fd, surface->fb_id); + if (ret) + printf("drmModeRmFB failed ret=%d\n", ret); + } + + if (surface->handle) { + memset(&gem_close, 0, sizeof(gem_close)); + gem_close.handle = surface->handle; + + ret = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close); + if (ret) + printf("DRM_IOCTL_GEM_CLOSE failed ret=%d\n", ret); + } + + free(surface); +} + +static int drm_format_to_bpp(uint32_t format) { + switch(format) { + case DRM_FORMAT_ABGR8888: + case DRM_FORMAT_BGRA8888: + case DRM_FORMAT_RGBX8888: + case DRM_FORMAT_BGRX8888: + case DRM_FORMAT_XBGR8888: + case DRM_FORMAT_XRGB8888: + return 32; + case DRM_FORMAT_RGB565: + return 16; + default: + printf("Unknown format %d\n", format); + return 32; + } +} + +static drm_surface *drm_create_surface(int width, int height) { + struct drm_surface *surface; + struct drm_mode_create_dumb create_dumb; + uint32_t format; + int ret; + + surface = (struct drm_surface*)calloc(1, sizeof(*surface)); + if (!surface) { + printf("Can't allocate memory\n"); + return NULL; + } + +#if defined(RECOVERY_ABGR) + format = DRM_FORMAT_RGBA8888; +#elif defined(RECOVERY_BGRA) + format = DRM_FORMAT_ARGB8888; +#elif defined(RECOVERY_RGBX) + format = DRM_FORMAT_XBGR8888; +#else + format = DRM_FORMAT_RGB565; +#endif + + memset(&create_dumb, 0, sizeof(create_dumb)); + create_dumb.height = height; + create_dumb.width = width; + create_dumb.bpp = drm_format_to_bpp(format); + create_dumb.flags = 0; + + ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb); + if (ret) { + printf("DRM_IOCTL_MODE_CREATE_DUMB failed ret=%d\n",ret); + drm_destroy_surface(surface); + return NULL; + } + surface->handle = create_dumb.handle; + + uint32_t handles[4], pitches[4], offsets[4]; + + handles[0] = surface->handle; + pitches[0] = create_dumb.pitch; + offsets[0] = 0; + + ret = drmModeAddFB2(drm_fd, width, height, + format, handles, pitches, offsets, + &(surface->fb_id), 0); + if (ret) { + printf("drmModeAddFB2 failed ret=%d\n", ret); + drm_destroy_surface(surface); + return NULL; + } + + struct drm_mode_map_dumb map_dumb; + memset(&map_dumb, 0, sizeof(map_dumb)); + map_dumb.handle = create_dumb.handle; + ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb); + if (ret) { + printf("DRM_IOCTL_MODE_MAP_DUMB failed ret=%d\n",ret); + drm_destroy_surface(surface); + return NULL;; + } + + surface->base.height = height; + surface->base.width = width; + surface->base.row_bytes = create_dumb.pitch; + surface->base.pixel_bytes = create_dumb.bpp / 8; + surface->base.data = (unsigned char*) + mmap(NULL, + surface->base.height * surface->base.row_bytes, + PROT_READ | PROT_WRITE, MAP_SHARED, + drm_fd, map_dumb.offset); + if (surface->base.data == MAP_FAILED) { + perror("mmap() failed"); + drm_destroy_surface(surface); + return NULL; + } + + return surface; +} + +static drmModeCrtc *find_crtc_for_connector(int fd, + drmModeRes *resources, + drmModeConnector *connector) { + int i, j; + drmModeEncoder *encoder; + int32_t crtc; + + /* + * Find the encoder. If we already have one, just use it. + */ + if (connector->encoder_id) + encoder = drmModeGetEncoder(fd, connector->encoder_id); + else + encoder = NULL; + + if (encoder && encoder->crtc_id) { + crtc = encoder->crtc_id; + drmModeFreeEncoder(encoder); + return drmModeGetCrtc(fd, crtc); + } + + /* + * Didn't find anything, try to find a crtc and encoder combo. + */ + crtc = -1; + for (i = 0; i < connector->count_encoders; i++) { + encoder = drmModeGetEncoder(fd, connector->encoders[i]); + + if (encoder) { + for (j = 0; j < resources->count_crtcs; j++) { + if (!(encoder->possible_crtcs & (1 << j))) + continue; + crtc = resources->crtcs[j]; + break; + } + if (crtc >= 0) { + drmModeFreeEncoder(encoder); + return drmModeGetCrtc(fd, crtc); + } + } + } + + return NULL; +} + +static drmModeConnector *find_used_connector_by_type(int fd, + drmModeRes *resources, + unsigned type) { + int i; + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector *connector; + + connector = drmModeGetConnector(fd, resources->connectors[i]); + if (connector) { + if ((connector->connector_type == type) && + (connector->connection == DRM_MODE_CONNECTED) && + (connector->count_modes > 0)) + return connector; + + drmModeFreeConnector(connector); + } + } + return NULL; +} + +static drmModeConnector *find_first_connected_connector(int fd, + drmModeRes *resources) { + int i; + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector *connector; + + connector = drmModeGetConnector(fd, resources->connectors[i]); + if (connector) { + if ((connector->count_modes > 0) && + (connector->connection == DRM_MODE_CONNECTED)) + return connector; + + drmModeFreeConnector(connector); + } + } + return NULL; +} + +static drmModeConnector *find_main_monitor(int fd, drmModeRes *resources, + uint32_t *mode_index) { + unsigned i = 0; + int modes; + /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */ + unsigned kConnectorPriority[] = { + DRM_MODE_CONNECTOR_LVDS, + DRM_MODE_CONNECTOR_eDP, + DRM_MODE_CONNECTOR_DSI, + }; + + drmModeConnector *main_monitor_connector = NULL; + do { + main_monitor_connector = find_used_connector_by_type(fd, + resources, + kConnectorPriority[i]); + i++; + } while (!main_monitor_connector && i < ARRAY_SIZE(kConnectorPriority)); + + /* If we didn't find a connector, grab the first one that is connected. */ + if (!main_monitor_connector) + main_monitor_connector = + find_first_connected_connector(fd, resources); + + /* If we still didn't find a connector, give up and return. */ + if (!main_monitor_connector) + return NULL; + + *mode_index = 0; + for (modes = 0; modes < main_monitor_connector->count_modes; modes++) { + if (main_monitor_connector->modes[modes].type & + DRM_MODE_TYPE_PREFERRED) { + *mode_index = modes; + break; + } + } + + return main_monitor_connector; +} + +static void disable_non_main_crtcs(int fd, + drmModeRes *resources, + drmModeCrtc* main_crtc) { + int i; + drmModeCrtc* crtc; + + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector *connector; + + connector = drmModeGetConnector(fd, resources->connectors[i]); + crtc = find_crtc_for_connector(fd, resources, connector); + if (crtc->crtc_id != main_crtc->crtc_id) + drm_disable_crtc(fd, crtc); + drmModeFreeCrtc(crtc); + } +} + +static GRSurface* drm_init(minui_backend* backend __unused) { + drmModeRes *res = NULL; + uint32_t selected_mode; + char *dev_name; + int width, height; + int ret, i; + + /* Consider DRM devices in order. */ + for (i = 0; i < DRM_MAX_MINOR; i++) { + uint64_t cap = 0; + + ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i); + if (ret < 0) + continue; + + drm_fd = open(dev_name, O_RDWR, 0); + free(dev_name); + if (drm_fd < 0) + continue; + + /* We need dumb buffers. */ + ret = drmGetCap(drm_fd, DRM_CAP_DUMB_BUFFER, &cap); + if (ret || cap == 0) { + close(drm_fd); + continue; + } + + res = drmModeGetResources(drm_fd); + if (!res) { + close(drm_fd); + continue; + } + + /* Use this device if it has at least one connected monitor. */ + if (res->count_crtcs > 0 && res->count_connectors > 0) + if (find_first_connected_connector(drm_fd, res)) + break; + + drmModeFreeResources(res); + close(drm_fd); + res = NULL; + } + + if (drm_fd < 0 || res == NULL) { + perror("cannot find/open a drm device"); + return NULL; + } + + main_monitor_connector = find_main_monitor(drm_fd, + res, &selected_mode); + + if (!main_monitor_connector) { + printf("main_monitor_connector not found\n"); + drmModeFreeResources(res); + close(drm_fd); + return NULL; + } + + main_monitor_crtc = find_crtc_for_connector(drm_fd, res, + main_monitor_connector); + + if (!main_monitor_crtc) { + printf("main_monitor_crtc not found\n"); + drmModeFreeResources(res); + close(drm_fd); + return NULL; + } + + disable_non_main_crtcs(drm_fd, + res, main_monitor_crtc); + + main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode]; + + width = main_monitor_crtc->mode.hdisplay; + height = main_monitor_crtc->mode.vdisplay; + + drmModeFreeResources(res); + + drm_surfaces[0] = drm_create_surface(width, height); + drm_surfaces[1] = drm_create_surface(width, height); + if (!drm_surfaces[0] || !drm_surfaces[1]) { + drm_destroy_surface(drm_surfaces[0]); + drm_destroy_surface(drm_surfaces[1]); + drmModeFreeResources(res); + close(drm_fd); + return NULL; + } + + current_buffer = 0; + + drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[1]); + + return &(drm_surfaces[0]->base); +} + +static GRSurface* drm_flip(minui_backend* backend __unused) { + int ret; + + ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, + drm_surfaces[current_buffer]->fb_id, 0, NULL); + if (ret < 0) { + printf("drmModePageFlip failed ret=%d\n", ret); + return NULL; + } + current_buffer = 1 - current_buffer; + return &(drm_surfaces[current_buffer]->base); +} + +static void drm_exit(minui_backend* backend __unused) { + drm_disable_crtc(drm_fd, main_monitor_crtc); + drm_destroy_surface(drm_surfaces[0]); + drm_destroy_surface(drm_surfaces[1]); + drmModeFreeCrtc(main_monitor_crtc); + drmModeFreeConnector(main_monitor_connector); + close(drm_fd); + drm_fd = -1; +} + +static minui_backend drm_backend = { + .init = drm_init, + .flip = drm_flip, + .blank = drm_blank, + .exit = drm_exit, +}; + +minui_backend* open_drm() { + return &drm_backend; +} diff --git a/minui/graphics_fbdev.c b/minui/graphics_fbdev.cpp index 6df2726f5..997e9cac2 100644 --- a/minui/graphics_fbdev.c +++ b/minui/graphics_fbdev.cpp @@ -16,6 +16,7 @@ #include <stdbool.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include <fcntl.h> @@ -32,8 +33,8 @@ #include "minui.h" #include "graphics.h" -static gr_surface fbdev_init(minui_backend*); -static gr_surface fbdev_flip(minui_backend*); +static GRSurface* fbdev_init(minui_backend*); +static GRSurface* fbdev_flip(minui_backend*); static void fbdev_blank(minui_backend*, bool); static void fbdev_exit(minui_backend*); @@ -42,7 +43,7 @@ static bool double_buffered; static GRSurface* gr_draw = NULL; static int displayed_buffer; -static struct fb_var_screeninfo vi; +static fb_var_screeninfo vi; static int fb_fd = -1; static minui_backend my_backend = { @@ -78,18 +79,14 @@ static void set_displayed_framebuffer(unsigned n) displayed_buffer = n; } -static gr_surface fbdev_init(minui_backend* backend) { - int fd; - void *bits; - - struct fb_fix_screeninfo fi; - - fd = open("/dev/graphics/fb0", O_RDWR); - if (fd < 0) { +static GRSurface* fbdev_init(minui_backend* backend) { + int fd = open("/dev/graphics/fb0", O_RDWR); + if (fd == -1) { perror("cannot open fb0"); return NULL; } + fb_fix_screeninfo fi; if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { perror("failed to get fb0 info"); close(fd); @@ -123,7 +120,7 @@ static gr_surface fbdev_init(minui_backend* backend) { vi.green.offset, vi.green.length, vi.blue.offset, vi.blue.length); - bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (bits == MAP_FAILED) { perror("failed to mmap framebuffer"); close(fd); @@ -136,7 +133,7 @@ static gr_surface fbdev_init(minui_backend* backend) { gr_framebuffer[0].height = vi.yres; gr_framebuffer[0].row_bytes = fi.line_length; gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8; - gr_framebuffer[0].data = bits; + gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits); memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); /* check if we can use double buffering */ @@ -177,7 +174,7 @@ static gr_surface fbdev_init(minui_backend* backend) { return gr_draw; } -static gr_surface fbdev_flip(minui_backend* backend __unused) { +static GRSurface* fbdev_flip(minui_backend* backend __unused) { if (double_buffered) { #if defined(RECOVERY_BGRA) // In case of BGRA, do some byte swapping @@ -198,21 +195,8 @@ static gr_surface fbdev_flip(minui_backend* backend __unused) { set_displayed_framebuffer(1-displayed_buffer); } else { // Copy from the in-memory surface to the framebuffer. - -#if defined(RECOVERY_BGRA) - unsigned int idx; - unsigned char* ucfb_vaddr = (unsigned char*)gr_framebuffer[0].data; - unsigned char* ucbuffer_vaddr = (unsigned char*)gr_draw->data; - for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); idx += 4) { - ucfb_vaddr[idx ] = ucbuffer_vaddr[idx + 2]; - ucfb_vaddr[idx + 1] = ucbuffer_vaddr[idx + 1]; - ucfb_vaddr[idx + 2] = ucbuffer_vaddr[idx ]; - ucfb_vaddr[idx + 3] = ucbuffer_vaddr[idx + 3]; - } -#else memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes); -#endif } return gr_draw; } diff --git a/minui/minui.h b/minui/minui.h index 733b675f3..bdde083f3 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -19,67 +19,70 @@ #include <sys/types.h> -#include <stdbool.h> +#include <functional> -#ifdef __cplusplus -extern "C" { -#endif +// +// Graphics. +// -typedef struct { +struct GRSurface { int width; int height; int row_bytes; int pixel_bytes; unsigned char* data; -} GRSurface; +}; -typedef GRSurface* gr_surface; +int gr_init(); +void gr_exit(); -int gr_init(void); -void gr_exit(void); +int gr_fb_width(); +int gr_fb_height(); -int gr_fb_width(void); -int gr_fb_height(void); - -void gr_flip(void); +void gr_flip(); void gr_fb_blank(bool blank); void gr_clear(); // clear entire surface to current color void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); void gr_fill(int x1, int y1, int x2, int y2); -void gr_text(int x, int y, const char *s, int bold); -void gr_texticon(int x, int y, gr_surface icon); +void gr_text(int x, int y, const char *s, bool bold); +void gr_texticon(int x, int y, GRSurface* icon); int gr_measure(const char *s); void gr_font_size(int *x, int *y); -void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy); -unsigned int gr_get_width(gr_surface surface); -unsigned int gr_get_height(gr_surface surface); +void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); +unsigned int gr_get_width(GRSurface* surface); +unsigned int gr_get_height(GRSurface* surface); + +// +// Input events. +// -// input event structure, include <linux/input.h> for the definition. -// see http://www.mjmwired.net/kernel/Documentation/input/ for info. struct input_event; -typedef int (*ev_callback)(int fd, uint32_t epevents, void *data); -typedef int (*ev_set_key_callback)(int code, int value, void *data); +// TODO: move these over to std::function. +typedef int (*ev_callback)(int fd, uint32_t epevents, void* data); +typedef int (*ev_set_key_callback)(int code, int value, void* data); -int ev_init(ev_callback input_cb, void *data); -void ev_exit(void); -int ev_add_fd(int fd, ev_callback cb, void *data); -int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data); +int ev_init(ev_callback input_cb, void* data); +void ev_exit(); +int ev_add_fd(int fd, ev_callback cb, void* data); +void ev_iterate_available_keys(std::function<void(int)> f); +int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); -/* timeout has the same semantics as for poll - * 0 : don't block - * < 0 : block forever - * > 0 : block for 'timeout' milliseconds - */ +// 'timeout' has the same semantics as poll(2). +// 0 : don't block +// < 0 : block forever +// > 0 : block for 'timeout' milliseconds int ev_wait(int timeout); -int ev_get_input(int fd, uint32_t epevents, struct input_event *ev); -void ev_dispatch(void); -int ev_get_epollfd(void); +int ev_get_input(int fd, uint32_t epevents, input_event* ev); +void ev_dispatch(); +int ev_get_epollfd(); +// // Resources +// // res_create_*_surface() functions return 0 if no error, else // negative. @@ -92,17 +95,17 @@ int ev_get_epollfd(void); // All these functions load PNG images from "/res/images/${name}.png". // Load a single display surface from a PNG image. -int res_create_display_surface(const char* name, gr_surface* pSurface); +int res_create_display_surface(const char* name, GRSurface** pSurface); // Load an array of display surfaces from a single PNG image. The PNG // should have a 'Frames' text chunk whose value is the number of // frames this image represents. The pixel data itself is interlaced // by row. int res_create_multi_display_surface(const char* name, - int* frames, gr_surface** pSurface); + int* frames, GRSurface*** pSurface); // Load a single alpha surface from a grayscale PNG image. -int res_create_alpha_surface(const char* name, gr_surface* pSurface); +int res_create_alpha_surface(const char* name, GRSurface** pSurface); // Load part of a grayscale PNG image that is the first match for the // given locale. The image is expected to be a composite of multiple @@ -111,14 +114,10 @@ int res_create_alpha_surface(const char* name, gr_surface* pSurface); // development/tools/recovery_l10n for an app that will generate these // specialized images from Android resources. int res_create_localized_alpha_surface(const char* name, const char* locale, - gr_surface* pSurface); + GRSurface** pSurface); // Free a surface allocated by any of the res_create_*_surface() // functions. -void res_free_surface(gr_surface surface); - -#ifdef __cplusplus -} -#endif +void res_free_surface(GRSurface* surface); #endif diff --git a/minui/resources.c b/minui/resources.cpp index 2bae4ded0..5e4789277 100644 --- a/minui/resources.c +++ b/minui/resources.cpp @@ -15,6 +15,7 @@ */ #include <stdlib.h> +#include <string.h> #include <unistd.h> #include <fcntl.h> @@ -35,10 +36,11 @@ extern char* locale; #define SURFACE_DATA_ALIGNMENT 8 -static gr_surface malloc_surface(size_t data_size) { - unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT); +static GRSurface* malloc_surface(size_t data_size) { + size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; + unsigned char* temp = reinterpret_cast<unsigned char*>(malloc(size)); if (temp == NULL) return NULL; - gr_surface surface = (gr_surface) temp; + GRSurface* surface = reinterpret_cast<GRSurface*>(temp); surface->data = temp + sizeof(GRSurface) + (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); return surface; @@ -49,6 +51,8 @@ static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, 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'; @@ -58,7 +62,7 @@ static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, goto exit; } - size_t bytesRead = fread(header, 1, sizeof(header), fp); + bytesRead = fread(header, 1, sizeof(header), fp); if (bytesRead != sizeof(header)) { result = -2; goto exit; @@ -90,7 +94,6 @@ static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, 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, NULL, NULL, NULL); @@ -135,12 +138,10 @@ static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, // framebuffer pixel format; they need to be modified if the // framebuffer format changes (but nothing else should). -// Allocate and return a gr_surface sufficient for storing an image of +// Allocate and return a GRSurface* sufficient for storing an image of // the indicated size in the framebuffer pixel format. -static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height) { - gr_surface surface; - - surface = malloc_surface(width * height * 4); +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; surface->width = width; @@ -196,13 +197,15 @@ static void transform_rgb_to_draw(unsigned char* input_row, } } -int res_create_display_surface(const char* name, gr_surface* pSurface) { - gr_surface surface = NULL; +int res_create_display_surface(const char* name, 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; + unsigned char* p_row; + unsigned int y; *pSurface = NULL; @@ -215,8 +218,11 @@ int res_create_display_surface(const char* name, gr_surface* pSurface) { goto exit; } - unsigned char* p_row = malloc(width * 4); - unsigned int y; +#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); transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width); @@ -231,14 +237,18 @@ int res_create_display_surface(const char* name, gr_surface* pSurface) { return result; } -int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) { - gr_surface* surface = NULL; +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; @@ -247,8 +257,6 @@ int res_create_multi_display_surface(const char* name, int* frames, gr_surface** if (result < 0) return result; *frames = 1; - png_textp text; - int num_text; 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) { @@ -265,7 +273,7 @@ int res_create_multi_display_surface(const char* name, int* frames, gr_surface** goto exit; } - surface = malloc(*frames * sizeof(gr_surface)); + surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*))); if (surface == NULL) { result = -8; goto exit; @@ -278,8 +286,11 @@ int res_create_multi_display_surface(const char* name, int* frames, gr_surface** } } - unsigned char* p_row = malloc(width * 4); - unsigned int y; +#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; @@ -289,7 +300,7 @@ int res_create_multi_display_surface(const char* name, int* frames, gr_surface** } free(p_row); - *pSurface = (gr_surface*) surface; + *pSurface = reinterpret_cast<GRSurface**>(surface); exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); @@ -305,8 +316,8 @@ exit: return result; } -int res_create_alpha_surface(const char* name, gr_surface* pSurface) { - gr_surface surface = NULL; +int res_create_alpha_surface(const char* name, GRSurface** pSurface) { + GRSurface* surface = NULL; int result = 0; png_structp png_ptr = NULL; png_infop info_ptr = NULL; @@ -333,6 +344,10 @@ int res_create_alpha_surface(const char* name, gr_surface* pSurface) { surface->row_bytes = width; surface->pixel_bytes = 1; +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + png_set_bgr(png_ptr); +#endif + unsigned char* p_row; unsigned int y; for (y = 0; y < height; ++y) { @@ -367,13 +382,15 @@ static int matches_locale(const char* loc, const char* locale) { int res_create_localized_alpha_surface(const char* name, const char* locale, - gr_surface* pSurface) { - gr_surface surface = NULL; + 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; + unsigned char* row; + png_uint_32 y; *pSurface = NULL; @@ -394,8 +411,7 @@ int res_create_localized_alpha_surface(const char* name, goto exit; } - unsigned char* row = malloc(width); - png_uint_32 y; + row = reinterpret_cast<unsigned char*>(malloc(width)); for (y = 0; y < height; ++y) { png_read_row(png_ptr, row, NULL); int w = (row[1] << 8) | row[0]; @@ -422,7 +438,7 @@ int res_create_localized_alpha_surface(const char* name, memcpy(surface->data + i*w, row, w); } - *pSurface = (gr_surface) surface; + *pSurface = reinterpret_cast<GRSurface*>(surface); break; } else { int i; @@ -438,6 +454,6 @@ exit: return result; } -void res_free_surface(gr_surface surface) { +void res_free_surface(GRSurface* surface) { free(surface); } |