From 4425c1d960234ae5db904b199ccf39c4ec64b37f Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Wed, 10 Feb 2016 13:47:32 -0800 Subject: Fix some memory leaks. Bug: 26906328 Change-Id: Iebaf03db0cb3054f91715f8c849be6087d01b27b --- minui/resources.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'minui') diff --git a/minui/resources.cpp b/minui/resources.cpp index 63a0dff28..fdbe554fe 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include "minui.h" @@ -398,18 +399,13 @@ int res_create_localized_alpha_surface(const char* name, png_infop info_ptr = NULL; png_uint_32 width, height; png_byte channels; - unsigned char* row; png_uint_32 y; + std::vector row; *pSurface = NULL; if (locale == NULL) { - surface = malloc_surface(0); - surface->width = 0; - surface->height = 0; - surface->row_bytes = 0; - surface->pixel_bytes = 1; - goto exit; + return result; } result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); @@ -420,13 +416,13 @@ int res_create_localized_alpha_surface(const char* name, goto exit; } - row = reinterpret_cast(malloc(width)); + row.resize(width); for (y = 0; y < height; ++y) { - png_read_row(png_ptr, row, NULL); + png_read_row(png_ptr, row.data(), NULL); int w = (row[1] << 8) | row[0]; int h = (row[3] << 8) | row[2]; int len = row[4]; - char* loc = (char*)row+5; + char* loc = reinterpret_cast(&row[5]); if (y+1+h >= height || matches_locale(loc, locale)) { printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); @@ -443,8 +439,8 @@ int res_create_localized_alpha_surface(const char* name, int i; for (i = 0; i < h; ++i, ++y) { - png_read_row(png_ptr, row, NULL); - memcpy(surface->data + i*w, row, w); + png_read_row(png_ptr, row.data(), NULL); + memcpy(surface->data + i*w, row.data(), w); } *pSurface = reinterpret_cast(surface); @@ -452,7 +448,7 @@ int res_create_localized_alpha_surface(const char* name, } else { int i; for (i = 0; i < h; ++i, ++y) { - png_read_row(png_ptr, row, NULL); + png_read_row(png_ptr, row.data(), NULL); } } } -- cgit v1.2.3 From 7d626df079196780635068ae13de2fdadccdfa2c Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 19 Feb 2016 10:33:01 -0800 Subject: Fix minui cleanup path on error. bootable/recovery/minui/resources.cpp:321:21: warning: Branch condition evaluates to a garbage value if (surface[i]) free(surface[i]); ^~~~~~~~~~ bootable/recovery/minui/resources.cpp:424:13: warning: Value stored to 'len' during its initialization is never read int len = row[4]; ^~~ ~~~~~~ Bug: http://b/27264652 Change-Id: Icc1a914c59d6a89dab1b752b2cd2b40549566481 --- minui/resources.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'minui') diff --git a/minui/resources.cpp b/minui/resources.cpp index fdbe554fe..8489d60ef 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -283,7 +283,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, goto exit; } - surface = reinterpret_cast(malloc(*frames * sizeof(GRSurface*))); + surface = reinterpret_cast(calloc(*frames, sizeof(GRSurface*))); if (surface == NULL) { result = -8; goto exit; @@ -318,7 +318,7 @@ exit: if (result < 0) { if (surface) { for (int i = 0; i < *frames; ++i) { - if (surface[i]) free(surface[i]); + free(surface[i]); } free(surface); } @@ -421,7 +421,7 @@ int res_create_localized_alpha_surface(const char* name, png_read_row(png_ptr, row.data(), NULL); int w = (row[1] << 8) | row[0]; int h = (row[3] << 8) | row[2]; - int len = row[4]; + __unused int len = row[4]; char* loc = reinterpret_cast(&row[5]); if (y+1+h >= height || matches_locale(loc, locale)) { -- cgit v1.2.3 From 54a2747ef305c10d07d8db393125dbcbb461c428 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Mon, 18 Apr 2016 11:30:55 -0700 Subject: Fix google-runtime-int warnings. Bug: 28220065 Change-Id: Ida199c66692a1638be6990d583d2ed42583fb592 --- minui/events.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'minui') diff --git a/minui/events.cpp b/minui/events.cpp index 3b2262a4b..a6b9671ed 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -49,7 +49,7 @@ 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) { +static bool test_bit(size_t bit, unsigned long* array) { // NOLINT return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; } @@ -65,7 +65,8 @@ int ev_init(ev_callback input_cb, void* data) { if (dir != NULL) { dirent* de; while ((de = readdir(dir))) { - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; + // Use unsigned long to match ioctl's parameter type. + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT // fprintf(stderr,"/dev/input/%s\n", de->d_name); if (strncmp(de->d_name, "event", 5)) continue; @@ -175,8 +176,9 @@ int ev_get_input(int fd, uint32_t epevents, input_event* ev) { } 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)]; + // Use unsigned long to match ioctl's parameter type. + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT + unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT for (size_t i = 0; i < ev_dev_count; ++i) { memset(ev_bits, 0, sizeof(ev_bits)); @@ -203,8 +205,9 @@ int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { } void ev_iterate_available_keys(std::function f) { - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; - unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; + // Use unsigned long to match ioctl's parameter type. + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT + unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT for (size_t i = 0; i < ev_dev_count; ++i) { memset(ev_bits, 0, sizeof(ev_bits)); -- cgit v1.2.3 From bab6e492ef06f8ed3c83578b2ac5b4b8e07197ae Mon Sep 17 00:00:00 2001 From: Jonathan Hamilton Date: Thu, 5 May 2016 15:30:57 -0700 Subject: Keep ADF device alive for the lifetime of the minui backend Some ADF drivers do some amount of state cleanup when the ADF device node is closed, making and attempts to draw using it fail. This changes the minui ADF backend to keep the adf_device open until it is exited, fixing issues on such devices. --- minui/graphics_adf.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'minui') diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index 5d0867f58..a72e40b58 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -42,6 +42,8 @@ struct adf_pdata { adf_id_t eng_id; __u32 format; + adf_device dev; + unsigned int current_surface; unsigned int n_surfaces; adf_surface_pdata surfaces[2]; @@ -163,21 +165,20 @@ static GRSurface* adf_init(minui_backend *backend) pdata->intf_fd = -1; for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { - adf_device dev; - int err = adf_device_open(dev_ids[i], O_RDWR, &dev); + int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev); if (err < 0) { fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err)); continue; } - err = adf_device_init(pdata, &dev); - if (err < 0) + err = adf_device_init(pdata, &pdata->dev); + if (err < 0) { fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err)); - - adf_device_close(&dev); + adf_device_close(&pdata->dev); + } } free(dev_ids); @@ -226,6 +227,7 @@ static void adf_exit(minui_backend *backend) adf_pdata *pdata = (adf_pdata *)backend; unsigned int i; + adf_device_close(&pdata->dev); for (i = 0; i < pdata->n_surfaces; i++) adf_surface_destroy(&pdata->surfaces[i]); if (pdata->intf_fd >= 0) -- cgit v1.2.3 From 23abfd37a55f281186f9275ad35b7182efd2e8b6 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Wed, 27 Jul 2016 10:19:47 -0700 Subject: Fix clang-tidy performance warnings. * Use const reference parameter type to avoid unnecessary copy. * Use more efficient overloaded string methods. Bug: 30407689 Bug: 30411878 Change-Id: Iefab05c077367f272abf545036b853e8a295c8cd Test: build with WITH_TIDY=1 --- minui/events.cpp | 2 +- minui/minui.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'minui') diff --git a/minui/events.cpp b/minui/events.cpp index a6b9671ed..e6e7bd28c 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -204,7 +204,7 @@ int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { return 0; } -void ev_iterate_available_keys(std::function f) { +void ev_iterate_available_keys(const std::function& f) { // Use unsigned long to match ioctl's parameter type. unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT diff --git a/minui/minui.h b/minui/minui.h index e3bc00548..b2648fb33 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -67,7 +67,7 @@ typedef int (*ev_set_key_callback)(int code, int value, 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 f); +void ev_iterate_available_keys(const std::function& f); int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); // 'timeout' has the same semantics as poll(2). -- cgit v1.2.3 From 9a259772ccf1e9708baecc559323ac5b6229e954 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Thu, 28 Jul 2016 14:15:00 -0700 Subject: Add docs on regeneration background text image Also add a missing string in the recovery_l10n APP. Bug: 30415666 Change-Id: Ice2a9f7cad4ebe332b427bc0c7a9adccb6cf3af3 --- minui/minui.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'minui') diff --git a/minui/minui.h b/minui/minui.h index b2648fb33..d572205f3 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -111,8 +111,8 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface); // given locale. The image is expected to be a composite of multiple // translations of the same text, with special added rows that encode // the subimages' size and intended locale in the pixel data. See -// development/tools/recovery_l10n for an app that will generate these -// specialized images from Android resources. +// bootable/recovery/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, GRSurface** pSurface); -- cgit v1.2.3 From c4fa2c2c401a96d2a69855096c9ecfc28df8e575 Mon Sep 17 00:00:00 2001 From: "xinglong.zhu" Date: Fri, 5 Aug 2016 14:52:22 +0800 Subject: Recovery mode UI flicker [root cause ] miniui has no mechanism to protect the buffer which is displaying [changes ] recovery [side effects] ui show [self test ] sc9850-2 general operation has not display abnormal [reviewers ] xinglong.zhu Signed-off-by: xinglong.zhu [change_type ] AOB --> google_original [tag_product ] common Change-Id: I989a0b2943ff6070a0e98718cfbe95144510d3a2 --- minui/Android.mk | 1 + minui/graphics_adf.cpp | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'minui') diff --git a/minui/Android.mk b/minui/Android.mk index 3057f452c..380ec2bfd 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -11,6 +11,7 @@ LOCAL_SRC_FILES := \ LOCAL_WHOLE_STATIC_LIBRARIES += libadf LOCAL_WHOLE_STATIC_LIBRARIES += libdrm +LOCAL_WHOLE_STATIC_LIBRARIES += libsync_recovery LOCAL_STATIC_LIBRARIES += libpng LOCAL_MODULE := libminui diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index a72e40b58..3c3541094 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -26,11 +26,13 @@ #include #include +#include #include "graphics.h" struct adf_surface_pdata { GRSurface base; + int fence_fd; int fd; __u32 offset; __u32 pitch; @@ -55,6 +57,7 @@ static void adf_blank(minui_backend *backend, bool blank); static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) { memset(surf, 0, sizeof(*surf)); + surf->fence_fd = -1; surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, mode->vdisplay, pdata->format, &surf->offset, &surf->pitch); if (surf->fd < 0) @@ -194,6 +197,23 @@ static GRSurface* adf_init(minui_backend *backend) return ret; } +static void adf_sync(adf_surface_pdata *surf) +{ + unsigned int warningTimeout = 3000; + + if (surf == NULL) + return; + + if (surf->fence_fd >= 0){ + int err = sync_wait(surf->fence_fd, warningTimeout); + if (err < 0) + perror("adf sync fence wait error\n"); + + close(surf->fence_fd); + surf->fence_fd = -1; + } +} + static GRSurface* adf_flip(minui_backend *backend) { adf_pdata *pdata = (adf_pdata *)backend; @@ -203,9 +223,10 @@ static GRSurface* adf_flip(minui_backend *backend) surf->base.width, surf->base.height, pdata->format, surf->fd, surf->offset, surf->pitch, -1); if (fence_fd >= 0) - close(fence_fd); + surf->fence_fd = fence_fd; pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; + adf_sync(&pdata->surfaces[pdata->current_surface]); return &pdata->surfaces[pdata->current_surface].base; } @@ -219,6 +240,7 @@ static void adf_blank(minui_backend *backend, bool blank) static void adf_surface_destroy(adf_surface_pdata *surf) { munmap(surf->base.data, surf->pitch * surf->base.height); + close(surf->fence_fd); close(surf->fd); } -- cgit v1.2.3 From 7aa88748f6ec4e53333d1a15747bc44826ccc410 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Wed, 28 Sep 2016 11:42:17 -0700 Subject: Turn on -Werror for recovery Also remove the 0xff comparison when validating the bootloader message fields. As the fields won't be erased to 0xff after we remove the MTD support. Bug: 28202046 Test: The recovery folder compiles for aosp_x86-eng Change-Id: Ibb30ea1b2b28676fb08c7e92a1e5f7b6ef3247ab --- minui/Android.mk | 2 ++ 1 file changed, 2 insertions(+) (limited to 'minui') diff --git a/minui/Android.mk b/minui/Android.mk index 380ec2bfd..67b81fc6d 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -13,6 +13,7 @@ LOCAL_WHOLE_STATIC_LIBRARIES += libadf LOCAL_WHOLE_STATIC_LIBRARIES += libdrm LOCAL_WHOLE_STATIC_LIBRARIES += libsync_recovery LOCAL_STATIC_LIBRARIES += libpng +LOCAL_CFLAGS := -Werror LOCAL_MODULE := libminui @@ -46,4 +47,5 @@ LOCAL_CLANG := true LOCAL_MODULE := libminui LOCAL_WHOLE_STATIC_LIBRARIES += libminui LOCAL_SHARED_LIBRARIES := libpng +LOCAL_CFLAGS := -Werror include $(BUILD_SHARED_LIBRARY) -- cgit v1.2.3 From 17e316cce0047dcb892fb6dae9924cf6e0b41516 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Wed, 28 Sep 2016 11:42:17 -0700 Subject: Turn on -Werror for recovery Also remove the 0xff comparison when validating the bootloader message fields. As the fields won't be erased to 0xff after we remove the MTD support. Bug: 28202046 Test: The recovery folder compiles for aosp_x86-eng Change-Id: Ibb30ea1b2b28676fb08c7e92a1e5f7b6ef3247ab (cherry picked from commit 7aa88748f6ec4e53333d1a15747bc44826ccc410) --- minui/Android.mk | 2 ++ 1 file changed, 2 insertions(+) (limited to 'minui') diff --git a/minui/Android.mk b/minui/Android.mk index 380ec2bfd..67b81fc6d 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -13,6 +13,7 @@ LOCAL_WHOLE_STATIC_LIBRARIES += libadf LOCAL_WHOLE_STATIC_LIBRARIES += libdrm LOCAL_WHOLE_STATIC_LIBRARIES += libsync_recovery LOCAL_STATIC_LIBRARIES += libpng +LOCAL_CFLAGS := -Werror LOCAL_MODULE := libminui @@ -46,4 +47,5 @@ LOCAL_CLANG := true LOCAL_MODULE := libminui LOCAL_WHOLE_STATIC_LIBRARIES += libminui LOCAL_SHARED_LIBRARIES := libpng +LOCAL_CFLAGS := -Werror include $(BUILD_SHARED_LIBRARY) -- cgit v1.2.3 From 4f7faac8d705317271e1d6aa773f00f05ca70848 Mon Sep 17 00:00:00 2001 From: Rahul Chaudhry Date: Tue, 8 Nov 2016 16:06:13 -0800 Subject: Use static_cast to cast pointers returned by malloc/calloc/realloc/mmap. static_cast is preferable to reinterpret_cast when casting from void* pointers returned by malloc/calloc/realloc/mmap calls. Discovered while looking at compiler warnings (b/26936282). Test: WITH_TIDY=1 WITH_STATIC_ANALYZER=1 mma Change-Id: I151642d5a60c94f312d0611576ad0143c249ba3d --- minui/graphics.cpp | 8 ++++---- minui/graphics_adf.cpp | 8 ++++---- minui/graphics_fbdev.cpp | 2 +- minui/resources.cpp | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) (limited to 'minui') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index ab56a6fd6..d5d8d526e 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -265,7 +265,7 @@ unsigned int gr_get_height(GRSurface* surface) { } int gr_init_font(const char* name, GRFont** dest) { - GRFont* font = reinterpret_cast(calloc(1, sizeof(*gr_font))); + GRFont* font = static_cast(calloc(1, sizeof(*gr_font))); if (font == nullptr) { return -1; } @@ -298,14 +298,14 @@ static void gr_init_font(void) // fall back to the compiled-in font. - gr_font = reinterpret_cast(calloc(1, sizeof(*gr_font))); - gr_font->texture = reinterpret_cast(malloc(sizeof(*gr_font->texture))); + gr_font = static_cast(calloc(1, sizeof(*gr_font))); + gr_font->texture = static_cast(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 = reinterpret_cast(malloc(font.width * font.height)); + unsigned char* bits = static_cast(malloc(font.width * font.height)); gr_font->texture->data = reinterpret_cast(bits); unsigned char data; diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index 3c3541094..9e262b044 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -68,9 +68,9 @@ static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surfa surf->base.row_bytes = surf->pitch; surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; - surf->base.data = reinterpret_cast(mmap(NULL, - surf->pitch * surf->base.height, PROT_WRITE, - MAP_SHARED, surf->fd, surf->offset)); + surf->base.data = static_cast(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; @@ -259,7 +259,7 @@ static void adf_exit(minui_backend *backend) minui_backend *open_adf() { - adf_pdata* pdata = reinterpret_cast(calloc(1, sizeof(*pdata))); + adf_pdata* pdata = static_cast(calloc(1, sizeof(*pdata))); if (!pdata) { perror("allocating adf backend failed"); return NULL; diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 0788f7552..631ef4e13 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -133,7 +133,7 @@ static GRSurface* 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 = reinterpret_cast(bits); + gr_framebuffer[0].data = static_cast(bits); memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); /* check if we can use double buffering */ diff --git a/minui/resources.cpp b/minui/resources.cpp index 730b05f13..c723476cc 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -37,7 +37,7 @@ static GRSurface* malloc_surface(size_t data_size) { size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; - unsigned char* temp = reinterpret_cast(malloc(size)); + unsigned char* temp = static_cast(malloc(size)); if (temp == NULL) return NULL; GRSurface* surface = reinterpret_cast(temp); surface->data = temp + sizeof(GRSurface) + @@ -221,7 +221,7 @@ int res_create_display_surface(const char* name, GRSurface** pSurface) { png_set_bgr(png_ptr); #endif - p_row = reinterpret_cast(malloc(width * 4)); + p_row = static_cast(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); @@ -281,7 +281,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, goto exit; } - surface = reinterpret_cast(calloc(*frames, sizeof(GRSurface*))); + surface = static_cast(calloc(*frames, sizeof(GRSurface*))); if (surface == NULL) { result = -8; goto exit; @@ -298,7 +298,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, png_set_bgr(png_ptr); #endif - p_row = reinterpret_cast(malloc(width * 4)); + p_row = static_cast(malloc(width * 4)); for (y = 0; y < height; ++y) { png_read_row(png_ptr, p_row, NULL); int frame = y % *frames; -- cgit v1.2.3 From 126cf8cda8334328058985a7a59a66b9c47e66a2 Mon Sep 17 00:00:00 2001 From: MinSeong Kim Date: Fri, 11 Nov 2016 05:26:06 +0000 Subject: Fix "ordered comparison between pointer and zero". From Clang 4.x releases, DR583 and DR1512 will be addressed. This patch, in advance, fixes the error(s). Test: `mmma bootable/recovery` Change-Id: I29dc85ae681307c322ab3a698c3f3bbad1c784ee Signed-off-by: MinSeong Kim --- minui/resources.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'minui') diff --git a/minui/resources.cpp b/minui/resources.cpp index 730b05f13..455ef2784 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -269,7 +269,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, printf(" found fps = %d\n", *fps); } - if (frames <= 0 || fps <= 0) { + if (*frames <= 0 || *fps <= 0) { printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps); result = -10; goto exit; -- cgit v1.2.3 From d5d34d70a5d9c24e214855bb7e1e5402a96ec7cc Mon Sep 17 00:00:00 2001 From: Damien Bargiacchi Date: Thu, 11 Aug 2016 15:57:03 -0700 Subject: Support use of custom fonts in miniui Bug: 29547343 Change-Id: I398160c85daac90ffab2fa9bb2e96795b9e9885a (cherry picked from commit 35fff61b1c0d736d090a1cd1bb4e99141cc88ad8) --- minui/font_10x18.h | 8 ++--- minui/graphics.cpp | 102 +++++++++++++++++++++++++++++------------------------ minui/minui.h | 16 +++++++-- 3 files changed, 72 insertions(+), 54 deletions(-) (limited to 'minui') diff --git a/minui/font_10x18.h b/minui/font_10x18.h index 29d705344..30dfb9c56 100644 --- a/minui/font_10x18.h +++ b/minui/font_10x18.h @@ -1,14 +1,14 @@ struct { unsigned width; unsigned height; - unsigned cwidth; - unsigned cheight; + unsigned char_width; + unsigned char_height; unsigned char rundata[2973]; } font = { .width = 960, .height = 18, - .cwidth = 10, - .cheight = 18, + .char_width = 10, + .char_height = 18, .rundata = { 0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0x55,0x82,0x06,0x82,0x02,0x82,0x10,0x82, 0x11,0x83,0x08,0x82,0x0a,0x82,0x04,0x82,0x46,0x82,0x08,0x82,0x07,0x84,0x06, diff --git a/minui/graphics.cpp b/minui/graphics.cpp index c0eea9e38..43ec83f08 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -35,12 +35,6 @@ #include "minui.h" #include "graphics.h" -struct GRFont { - GRSurface* texture; - int cwidth; - int cheight; -}; - static GRFont* gr_font = NULL; static minui_backend* gr_backend = NULL; @@ -60,15 +54,20 @@ static bool outside(int x, int y) return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; } -int gr_measure(const char *s) +const GRFont* gr_sys_font() { - return gr_font->cwidth * strlen(s); + return gr_font; } -void gr_font_size(int *x, int *y) +int gr_measure(const GRFont* font, const char *s) { - *x = gr_font->cwidth; - *y = gr_font->cheight; + return font->char_width * strlen(s); +} + +void gr_font_size(const GRFont* font, int *x, int *y) +{ + *x = font->char_width; + *y = font->char_height; } static void text_blend(unsigned char* src_p, int src_row_bytes, @@ -103,34 +102,32 @@ static void text_blend(unsigned char* src_p, int src_row_bytes, } } -void gr_text(int x, int y, const char *s, bool bold) +void gr_text(const GRFont* font, int x, int y, const char *s, bool bold) { - GRFont* font = gr_font; - if (!font->texture || gr_current_a == 0) return; - bold = bold && (font->texture->height != font->cheight); + bold = bold && (font->texture->height != font->char_height); x += overscan_offset_x; y += overscan_offset_y; unsigned char ch; while ((ch = *s++)) { - if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; + if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break; if (ch < ' ' || ch > '~') { ch = '?'; } - unsigned char* src_p = font->texture->data + ((ch - ' ') * font->cwidth) + - (bold ? font->cheight * font->texture->row_bytes : 0); + unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) + + (bold ? font->char_height * 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); + font->char_width, font->char_height); - x += font->cwidth; + x += font->char_width; } } @@ -267,40 +264,51 @@ unsigned int gr_get_height(GRSurface* surface) { return surface->height; } +int gr_init_font(const char* name, GRFont* dest) { + int res = res_create_alpha_surface(name, &(dest->texture)); + if (res < 0) { + return res; + } + + // The font image should be a 96x2 array of character images. The + // columns are the printable ASCII characters 0x20 - 0x7f. The + // top row is regular text; the bottom row is bold. + dest->char_width = dest->texture->width / 96; + dest->char_height = dest->texture->height / 2; + + return 0; +} + static void gr_init_font(void) { gr_font = reinterpret_cast(calloc(sizeof(*gr_font), 1)); - int res = res_create_alpha_surface("font", &(gr_font->texture)); + int res = gr_init_font("font", gr_font); if (res == 0) { - // The font image should be a 96x2 array of character images. The - // columns are the printable ASCII characters 0x20 - 0x7f. The - // top row is regular text; the bottom row is bold. - gr_font->cwidth = gr_font->texture->width / 96; - gr_font->cheight = gr_font->texture->height / 2; - } else { - printf("failed to read font: res=%d\n", res); - - // fall back to the compiled-in font. - gr_font->texture = reinterpret_cast(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 = reinterpret_cast(malloc(font.width * font.height)); - gr_font->texture->data = reinterpret_cast(bits); - - unsigned char data; - unsigned char* in = font.rundata; - while((data = *in++)) { - memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); - bits += (data & 0x7f); - } + return; + } - gr_font->cwidth = font.cwidth; - gr_font->cheight = font.cheight; + printf("failed to read font: res=%d\n", res); + + // fall back to the compiled-in font. + gr_font->texture = reinterpret_cast(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 = reinterpret_cast(malloc(font.width * font.height)); + gr_font->texture->data = reinterpret_cast(bits); + + unsigned char data; + unsigned char* in = font.rundata; + while((data = *in++)) { + memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); + bits += (data & 0x7f); } + + gr_font->char_width = font.char_width; + gr_font->char_height = font.char_height; } #if 0 diff --git a/minui/minui.h b/minui/minui.h index 5362d3fe3..f6b42c628 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -33,6 +33,12 @@ struct GRSurface { unsigned char* data; }; +struct GRFont { + GRSurface* texture; + int char_width; + int char_height; +}; + int gr_init(); void gr_exit(); @@ -45,10 +51,14 @@ 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, 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); + +const GRFont* gr_sys_font(); +int gr_init_font(const char* name, GRFont* dest); +void gr_text(const GRFont* font, int x, int y, const char *s, bool bold); +int gr_measure(const GRFont* font, const char *s); +void gr_font_size(const GRFont* font, int *x, int *y); void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); unsigned int gr_get_width(GRSurface* surface); -- cgit v1.2.3 From 97eda9db705f0435dd049e1fa93bd878defa747c Mon Sep 17 00:00:00 2001 From: Damien Bargiacchi Date: Fri, 9 Sep 2016 07:14:08 -0700 Subject: Have gr_init_font alloc memory for the font Change-Id: I8ccf369d52011bc5d07d8e041fe558ce734a78fc (cherry picked from commit d00f5eb63a8e4690f9bef1e943d539d052444d9b) --- minui/graphics.cpp | 22 +++++++++++++++------- minui/minui.h | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'minui') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index 43ec83f08..ab56a6fd6 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -264,33 +264,41 @@ unsigned int gr_get_height(GRSurface* surface) { return surface->height; } -int gr_init_font(const char* name, GRFont* dest) { - int res = res_create_alpha_surface(name, &(dest->texture)); +int gr_init_font(const char* name, GRFont** dest) { + GRFont* font = reinterpret_cast(calloc(1, sizeof(*gr_font))); + if (font == nullptr) { + return -1; + } + + int res = res_create_alpha_surface(name, &(font->texture)); if (res < 0) { + free(font); return res; } // The font image should be a 96x2 array of character images. The // columns are the printable ASCII characters 0x20 - 0x7f. The // top row is regular text; the bottom row is bold. - dest->char_width = dest->texture->width / 96; - dest->char_height = dest->texture->height / 2; + font->char_width = font->texture->width / 96; + font->char_height = font->texture->height / 2; + + *dest = font; return 0; } static void gr_init_font(void) { - gr_font = reinterpret_cast(calloc(sizeof(*gr_font), 1)); - - int res = gr_init_font("font", gr_font); + int res = gr_init_font("font", &gr_font); if (res == 0) { return; } printf("failed to read font: res=%d\n", res); + // fall back to the compiled-in font. + gr_font = reinterpret_cast(calloc(1, sizeof(*gr_font))); gr_font->texture = reinterpret_cast(malloc(sizeof(*gr_font->texture))); gr_font->texture->width = font.width; gr_font->texture->height = font.height; diff --git a/minui/minui.h b/minui/minui.h index f6b42c628..78890b84b 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -55,7 +55,7 @@ void gr_fill(int x1, int y1, int x2, int y2); void gr_texticon(int x, int y, GRSurface* icon); const GRFont* gr_sys_font(); -int gr_init_font(const char* name, GRFont* dest); +int gr_init_font(const char* name, GRFont** dest); void gr_text(const GRFont* font, int x, int y, const char *s, bool bold); int gr_measure(const GRFont* font, const char *s); void gr_font_size(const GRFont* font, int *x, int *y); -- cgit v1.2.3 From b29f23f7e7c791e7d8786de93d630a12e4250c71 Mon Sep 17 00:00:00 2001 From: Rahul Chaudhry Date: Wed, 9 Nov 2016 13:17:01 -0800 Subject: Use static_cast to cast pointers returned by malloc/calloc/realloc/mmap. static_cast is preferable to reinterpret_cast when casting from void* pointers returned by malloc/calloc/realloc/mmap calls. Discovered while looking at compiler warnings (b/26936282). Test: WITH_TIDY=1 WITH_STATIC_ANALYZER=1 mma Change-Id: Iaffd537784aa857108f6981fdfd82d0496eb5592 Merged-In: I151642d5a60c94f312d0611576ad0143c249ba3d --- minui/graphics.cpp | 6 +++--- minui/graphics_adf.cpp | 8 ++++---- minui/graphics_fbdev.cpp | 2 +- minui/resources.cpp | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) (limited to 'minui') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index ab56a6fd6..0680c32bb 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -298,14 +298,14 @@ static void gr_init_font(void) // fall back to the compiled-in font. - gr_font = reinterpret_cast(calloc(1, sizeof(*gr_font))); - gr_font->texture = reinterpret_cast(malloc(sizeof(*gr_font->texture))); + gr_font = static_cast(calloc(sizeof(*gr_font), 1)); + gr_font->texture = static_cast(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 = reinterpret_cast(malloc(font.width * font.height)); + unsigned char* bits = static_cast(malloc(font.width * font.height)); gr_font->texture->data = reinterpret_cast(bits); unsigned char data; diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index 3c3541094..9e262b044 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -68,9 +68,9 @@ static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surfa surf->base.row_bytes = surf->pitch; surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; - surf->base.data = reinterpret_cast(mmap(NULL, - surf->pitch * surf->base.height, PROT_WRITE, - MAP_SHARED, surf->fd, surf->offset)); + surf->base.data = static_cast(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; @@ -259,7 +259,7 @@ static void adf_exit(minui_backend *backend) minui_backend *open_adf() { - adf_pdata* pdata = reinterpret_cast(calloc(1, sizeof(*pdata))); + adf_pdata* pdata = static_cast(calloc(1, sizeof(*pdata))); if (!pdata) { perror("allocating adf backend failed"); return NULL; diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 0788f7552..631ef4e13 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -133,7 +133,7 @@ static GRSurface* 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 = reinterpret_cast(bits); + gr_framebuffer[0].data = static_cast(bits); memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); /* check if we can use double buffering */ diff --git a/minui/resources.cpp b/minui/resources.cpp index 455ef2784..34e7b8be3 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -37,7 +37,7 @@ static GRSurface* malloc_surface(size_t data_size) { size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; - unsigned char* temp = reinterpret_cast(malloc(size)); + unsigned char* temp = static_cast(malloc(size)); if (temp == NULL) return NULL; GRSurface* surface = reinterpret_cast(temp); surface->data = temp + sizeof(GRSurface) + @@ -221,7 +221,7 @@ int res_create_display_surface(const char* name, GRSurface** pSurface) { png_set_bgr(png_ptr); #endif - p_row = reinterpret_cast(malloc(width * 4)); + p_row = static_cast(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); @@ -281,7 +281,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, goto exit; } - surface = reinterpret_cast(calloc(*frames, sizeof(GRSurface*))); + surface = static_cast(calloc(*frames, sizeof(GRSurface*))); if (surface == NULL) { result = -8; goto exit; @@ -298,7 +298,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, png_set_bgr(png_ptr); #endif - p_row = reinterpret_cast(malloc(width * 4)); + p_row = static_cast(malloc(width * 4)); for (y = 0; y < height; ++y) { png_read_row(png_ptr, p_row, NULL); int frame = y % *frames; -- cgit v1.2.3 From 1cf93f5f74680e22044c6e0051a975770a4b819b Mon Sep 17 00:00:00 2001 From: Rahul Chaudhry Date: Tue, 8 Nov 2016 16:35:22 -0800 Subject: Remove unnecessary uses of reinterpret_cast. Discovered while looking at compiler warnings (b/26936282). Test: WITH_TIDY=1 WITH_STATIC_ANALYZER=1 mma Change-Id: I66f8f6026ed732a504504ade93ff196dc8b727ca --- minui/graphics.cpp | 2 +- minui/resources.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'minui') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index 0680c32bb..dcca3ec41 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -306,7 +306,7 @@ static void gr_init_font(void) gr_font->texture->pixel_bytes = 1; unsigned char* bits = static_cast(malloc(font.width * font.height)); - gr_font->texture->data = reinterpret_cast(bits); + gr_font->texture->data = bits; unsigned char data; unsigned char* in = font.rundata; diff --git a/minui/resources.cpp b/minui/resources.cpp index 34e7b8be3..9ccbf4b1b 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -308,7 +308,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, } free(p_row); - *pSurface = reinterpret_cast(surface); + *pSurface = surface; exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); @@ -436,7 +436,7 @@ int res_create_localized_alpha_surface(const char* name, memcpy(surface->data + i*w, row.data(), w); } - *pSurface = reinterpret_cast(surface); + *pSurface = surface; break; } else { int i; -- cgit v1.2.3 From 0ecbd76b220ba6f3d02fa028b9c6481f7fb5c089 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 16 Jan 2017 21:16:58 -0800 Subject: minui: Export minui/minui.h. For libminui static and shared libraries. Test: build Change-Id: Ib30dc5e2ef4a3c8b3ca3a0cec68cb65e229a0c16 --- minui/Android.mk | 31 +++++++--- minui/events.cpp | 2 +- minui/graphics.cpp | 6 +- minui/graphics.h | 2 +- minui/graphics_drm.cpp | 3 +- minui/graphics_fbdev.cpp | 3 +- minui/include/minui/minui.h | 135 ++++++++++++++++++++++++++++++++++++++++++++ minui/minui.h | 135 -------------------------------------------- minui/resources.cpp | 2 +- 9 files changed, 167 insertions(+), 152 deletions(-) create mode 100644 minui/include/minui/minui.h delete mode 100644 minui/minui.h (limited to 'minui') diff --git a/minui/Android.mk b/minui/Android.mk index 67b81fc6d..281f64912 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -1,3 +1,17 @@ +# 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. + LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) @@ -9,16 +23,18 @@ LOCAL_SRC_FILES := \ graphics_fbdev.cpp \ resources.cpp \ -LOCAL_WHOLE_STATIC_LIBRARIES += libadf -LOCAL_WHOLE_STATIC_LIBRARIES += libdrm -LOCAL_WHOLE_STATIC_LIBRARIES += libsync_recovery -LOCAL_STATIC_LIBRARIES += libpng +LOCAL_WHOLE_STATIC_LIBRARIES := \ + libadf \ + libdrm \ + libsync_recovery + +LOCAL_STATIC_LIBRARIES := libpng LOCAL_CFLAGS := -Werror +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include 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. @@ -43,9 +59,10 @@ include $(BUILD_STATIC_LIBRARY) # Used by OEMs for factory test images. include $(CLEAR_VARS) -LOCAL_CLANG := true LOCAL_MODULE := libminui LOCAL_WHOLE_STATIC_LIBRARIES += libminui LOCAL_SHARED_LIBRARIES := libpng LOCAL_CFLAGS := -Werror +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include +LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include include $(BUILD_SHARED_LIBRARY) diff --git a/minui/events.cpp b/minui/events.cpp index e6e7bd28c..237af1ca5 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -25,7 +25,7 @@ #include -#include "minui.h" +#include "minui/minui.h" #define MAX_DEVICES 16 #define MAX_MISC_FDS 16 diff --git a/minui/graphics.cpp b/minui/graphics.cpp index dcca3ec41..34ea81c7c 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -14,7 +14,8 @@ * limitations under the License. */ -#include +#include "graphics.h" + #include #include #include @@ -32,8 +33,7 @@ #include #include "font_10x18.h" -#include "minui.h" -#include "graphics.h" +#include "minui/minui.h" static GRFont* gr_font = NULL; static minui_backend* gr_backend = NULL; diff --git a/minui/graphics.h b/minui/graphics.h index 52968eb10..1eaafc75a 100644 --- a/minui/graphics.h +++ b/minui/graphics.h @@ -17,7 +17,7 @@ #ifndef _GRAPHICS_H_ #define _GRAPHICS_H_ -#include "minui.h" +#include "minui/minui.h" // TODO: lose the function pointers. struct minui_backend { diff --git a/minui/graphics_drm.cpp b/minui/graphics_drm.cpp index 03e33b775..199f4d83c 100644 --- a/minui/graphics_drm.cpp +++ b/minui/graphics_drm.cpp @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -28,7 +27,7 @@ #include #include -#include "minui.h" +#include "minui/minui.h" #include "graphics.h" #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 631ef4e13..2d70249ed 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include #include #include #include @@ -30,7 +29,7 @@ #include #include -#include "minui.h" +#include "minui/minui.h" #include "graphics.h" static GRSurface* fbdev_init(minui_backend*); diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h new file mode 100644 index 000000000..78890b84b --- /dev/null +++ b/minui/include/minui/minui.h @@ -0,0 +1,135 @@ +/* + * 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. + */ + +#ifndef _MINUI_H_ +#define _MINUI_H_ + +#include + +#include + +// +// Graphics. +// + +struct GRSurface { + int width; + int height; + int row_bytes; + int pixel_bytes; + unsigned char* data; +}; + +struct GRFont { + GRSurface* texture; + int char_width; + int char_height; +}; + +int gr_init(); +void gr_exit(); + +int gr_fb_width(); +int gr_fb_height(); + +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_texticon(int x, int y, GRSurface* icon); + +const GRFont* gr_sys_font(); +int gr_init_font(const char* name, GRFont** dest); +void gr_text(const GRFont* font, int x, int y, const char *s, bool bold); +int gr_measure(const GRFont* font, const char *s); +void gr_font_size(const GRFont* font, int *x, int *y); + +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. +// + +struct input_event; + +// 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(); +int ev_add_fd(int fd, ev_callback cb, void* data); +void ev_iterate_available_keys(const std::function& f); +int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); + +// '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, input_event* ev); +void ev_dispatch(); +int ev_get_epollfd(); + +// +// Resources +// + +bool matches_locale(const char* prefix, const char* locale); + +// res_create_*_surface() functions return 0 if no error, else +// negative. +// +// A "display" surface is one that is intended to be drawn to the +// screen with gr_blit(). An "alpha" surface is a grayscale image +// interpreted as an alpha mask used to render text in the current +// color (with gr_text() or gr_texticon()). +// +// 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, 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, + int* fps, GRSurface*** pSurface); + +// Load a single alpha surface from a grayscale PNG image. +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 +// translations of the same text, with special added rows that encode +// the subimages' size and intended locale in the pixel data. See +// bootable/recovery/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, + GRSurface** pSurface); + +// Free a surface allocated by any of the res_create_*_surface() +// functions. +void res_free_surface(GRSurface* surface); + +#endif diff --git a/minui/minui.h b/minui/minui.h deleted file mode 100644 index 78890b84b..000000000 --- a/minui/minui.h +++ /dev/null @@ -1,135 +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. - */ - -#ifndef _MINUI_H_ -#define _MINUI_H_ - -#include - -#include - -// -// Graphics. -// - -struct GRSurface { - int width; - int height; - int row_bytes; - int pixel_bytes; - unsigned char* data; -}; - -struct GRFont { - GRSurface* texture; - int char_width; - int char_height; -}; - -int gr_init(); -void gr_exit(); - -int gr_fb_width(); -int gr_fb_height(); - -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_texticon(int x, int y, GRSurface* icon); - -const GRFont* gr_sys_font(); -int gr_init_font(const char* name, GRFont** dest); -void gr_text(const GRFont* font, int x, int y, const char *s, bool bold); -int gr_measure(const GRFont* font, const char *s); -void gr_font_size(const GRFont* font, int *x, int *y); - -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. -// - -struct input_event; - -// 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(); -int ev_add_fd(int fd, ev_callback cb, void* data); -void ev_iterate_available_keys(const std::function& f); -int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); - -// '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, input_event* ev); -void ev_dispatch(); -int ev_get_epollfd(); - -// -// Resources -// - -bool matches_locale(const char* prefix, const char* locale); - -// res_create_*_surface() functions return 0 if no error, else -// negative. -// -// A "display" surface is one that is intended to be drawn to the -// screen with gr_blit(). An "alpha" surface is a grayscale image -// interpreted as an alpha mask used to render text in the current -// color (with gr_text() or gr_texticon()). -// -// 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, 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, - int* fps, GRSurface*** pSurface); - -// Load a single alpha surface from a grayscale PNG image. -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 -// translations of the same text, with special added rows that encode -// the subimages' size and intended locale in the pixel data. See -// bootable/recovery/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, - GRSurface** pSurface); - -// Free a surface allocated by any of the res_create_*_surface() -// functions. -void res_free_surface(GRSurface* surface); - -#endif diff --git a/minui/resources.cpp b/minui/resources.cpp index 9ccbf4b1b..e6909f269 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -31,7 +31,7 @@ #include #include -#include "minui.h" +#include "minui/minui.h" #define SURFACE_DATA_ALIGNMENT 8 -- cgit v1.2.3 From d17a6885253da909e376ba5ca5084f5281f3557c Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Mon, 9 Jan 2017 11:18:29 -0800 Subject: Add checkers and tests for empty locale in PNG file match_locale() will return false for empty locale string in the PNG file. Also add a manual test to validate if a PNG file is qualified to use under recovery. Bug: 34054052 Test: recovery_manual_test catches invalid PNG files successfully & Locale_test passed Change-Id: Id7e2136e1d8abf20da15825aa7901effbced8b03 --- minui/resources.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'minui') diff --git a/minui/resources.cpp b/minui/resources.cpp index e6909f269..726c627ed 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -374,7 +374,9 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface) { // This function tests if a locale string stored in PNG (prefix) matches // the locale string provided by the system (locale). bool matches_locale(const char* prefix, const char* locale) { - if (locale == NULL) return false; + if (locale == nullptr) { + return false; + } // Return true if the whole string of prefix matches the top part of // locale. For instance, prefix == "en" matches locale == "en_US"; -- cgit v1.2.3 From 0b1118d6b9f5de948048b5ed86e639571c744a7f Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Sat, 14 Jan 2017 07:46:10 -0800 Subject: minui: Move callback functions to std::function. Also make minor clean up to the header includes. Test: mmma bootable/recovery system/core/healthd system/extra/slideshow Change-Id: I3bfcf2c0e203c26a98ee08f1f8036c68356a69fd --- minui/events.cpp | 209 +++++++++++++++++++++----------------------- minui/include/minui/minui.h | 11 ++- minui/resources.cpp | 14 ++- 3 files changed, 113 insertions(+), 121 deletions(-) (limited to 'minui') diff --git a/minui/events.cpp b/minui/events.cpp index 237af1ca5..6dd60fe68 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -15,16 +15,14 @@ */ #include -#include -#include +#include #include #include #include #include +#include #include -#include - #include "minui/minui.h" #define MAX_DEVICES 16 @@ -34,9 +32,8 @@ #define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) struct fd_info { - int fd; - ev_callback cb; - void* data; + int fd; + ev_callback cb; }; static int g_epoll_fd; @@ -53,89 +50,87 @@ static bool test_bit(size_t bit, unsigned long* array) { // NOLINT return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; } -int ev_init(ev_callback input_cb, void* data) { - bool epollctlfail = false; +int ev_init(ev_callback input_cb) { + bool epollctlfail = false; - g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); - if (g_epoll_fd == -1) { - return -1; + 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))) { + // Use unsigned long to match ioctl's parameter type. + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT + + // 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 = std::move(input_cb); + ev_count++; + ev_dev_count++; + if (ev_dev_count == MAX_DEVICES) break; } - DIR* dir = opendir("/dev/input"); - if (dir != NULL) { - dirent* de; - while ((de = readdir(dir))) { - // Use unsigned long to match ioctl's parameter type. - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT - -// 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); + } - closedir(dir); - } - - if (epollctlfail && !ev_count) { - close(g_epoll_fd); - g_epoll_fd = -1; - return -1; - } + if (epollctlfail && !ev_count) { + close(g_epoll_fd); + g_epoll_fd = -1; + return -1; + } - return 0; + 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; +int ev_add_fd(int fd, ev_callback cb) { + if (ev_misc_count == MAX_MISC_FDS || cb == NULL) { + return -1; + } + + epoll_event ev; + ev.events = EPOLLIN | EPOLLWAKEUP; + ev.data.ptr = static_cast(&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 = std::move(cb); + ev_count++; + ev_misc_count++; + } + + return ret; } void ev_exit(void) { @@ -156,13 +151,13 @@ int ev_wait(int timeout) { } void ev_dispatch(void) { - for (int n = 0; n < npolledevents; n++) { - fd_info* fdi = reinterpret_cast(polledevents[n].data.ptr); - ev_callback cb = fdi->cb; - if (cb) { - cb(fdi->fd, polledevents[n].events, fdi->data); - } + for (int n = 0; n < npolledevents; n++) { + fd_info* fdi = static_cast(polledevents[n].data.ptr); + const ev_callback& cb = fdi->cb; + if (cb) { + cb(fdi->fd, polledevents[n].events); } + } } int ev_get_input(int fd, uint32_t epevents, input_event* ev) { @@ -175,33 +170,33 @@ int ev_get_input(int fd, uint32_t epevents, input_event* ev) { return -1; } -int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { - // Use unsigned long to match ioctl's parameter type. - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT - unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT +int ev_sync_key_state(const ev_set_key_callback& set_key_cb) { + // Use unsigned long to match ioctl's parameter type. + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT + unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT - for (size_t i = 0; i < ev_dev_count; ++i) { - memset(ev_bits, 0, sizeof(ev_bits)); - memset(key_bits, 0, sizeof(key_bits)); + 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; - } + 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); - } - } + for (int code = 0; code <= KEY_MAX; code++) { + if (test_bit(code, key_bits)) { + set_key_cb(code, 1); + } } + } - return 0; + return 0; } void ev_iterate_available_keys(const std::function& f) { diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h index 78890b84b..a1749dfe6 100644 --- a/minui/include/minui/minui.h +++ b/minui/include/minui/minui.h @@ -70,15 +70,14 @@ unsigned int gr_get_height(GRSurface* surface); struct input_event; -// 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); +using ev_callback = std::function; +using ev_set_key_callback = std::function; -int ev_init(ev_callback input_cb, void* data); +int ev_init(ev_callback input_cb); void ev_exit(); -int ev_add_fd(int fd, ev_callback cb, void* data); +int ev_add_fd(int fd, ev_callback cb); void ev_iterate_available_keys(const std::function& f); -int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); +int ev_sync_key_state(const ev_set_key_callback& set_key_cb); // 'timeout' has the same semantics as poll(2). // 0 : don't block diff --git a/minui/resources.cpp b/minui/resources.cpp index e6909f269..9c69030da 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -14,21 +14,19 @@ * limitations under the License. */ -#include -#include -#include - #include +#include +#include #include - +#include +#include #include #include #include - -#include -#include +#include #include + #include #include "minui/minui.h" -- cgit v1.2.3 From e8020f4fa3a32e3d17c6daf0ebc40e73d0ba4748 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 3 Feb 2017 09:30:07 -0800 Subject: minui: Minor clean up to graphics.cpp. Remove unneeded header includes. Remove the dead code in gr_test() (already commented out). Similar tests have been covered by the "Run graphics test" from recovery menu. Test: mmma -j32 bootable/recovery Change-Id: If977c1b780602f5c5054469a3dae4fd85f34ab1a --- minui/graphics.cpp | 63 +----------------------------------------------------- 1 file changed, 1 insertion(+), 62 deletions(-) (limited to 'minui') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index 34ea81c7c..c0c67f948 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -16,21 +16,9 @@ #include "graphics.h" +#include #include #include -#include - -#include -#include - -#include -#include -#include - -#include -#include - -#include #include "font_10x18.h" #include "minui/minui.h" @@ -319,55 +307,6 @@ static void gr_init_font(void) gr_font->char_height = font.char_height; } -#if 0 -// Exercises many of the gr_*() functions; useful for testing. -static void gr_test() { - GRSurface** images; - int frames; - int result = res_create_multi_surface("icon_installing", &frames, &images); - if (result < 0) { - printf("create surface %d\n", result); - gr_exit(); - return; - } - - time_t start = time(NULL); - int x; - for (x = 0; x <= 1200; ++x) { - if (x < 400) { - gr_color(0, 0, 0, 255); - } else { - gr_color(0, (x-400)%128, 0, 255); - } - gr_clear(); - - gr_color(255, 0, 0, 255); - GRSurface* frame = images[x%frames]; - gr_blit(frame, 0, 0, frame->width, frame->height, x, 0); - - gr_color(255, 0, 0, 128); - gr_fill(400, 150, 600, 350); - - gr_color(255, 255, 255, 255); - gr_text(500, 225, "hello, world!", 0); - gr_color(255, 255, 0, 128); - gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1); - - gr_color(0, 0, 255, 128); - gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500); - - gr_draw = gr_backend->flip(gr_backend); - } - printf("getting end time\n"); - time_t end = time(NULL); - printf("got end time\n"); - printf("start %ld end %ld\n", (long)start, (long)end); - if (end > start) { - printf("%.2f fps\n", ((double)x) / (end-start)); - } -} -#endif - void gr_flip() { gr_draw = gr_backend->flip(gr_backend); } -- cgit v1.2.3 From 8f0e21b2710ecbac64f84352ae104246e7bc1647 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 7 Feb 2017 13:00:18 -0800 Subject: minui: Clean up graphics_adf.cpp. Remove unneeded header includes. Switch a few memset() to '= {}' style. Otherwise mostly cosmetic changes like reformatting. Test: 'Run graphics test' on N9 (which is an ADF device). Change-Id: If008af18ddae9521f53216b581d882d5eed76d41 --- minui/graphics_adf.cpp | 347 ++++++++++++++++++++++--------------------------- 1 file changed, 156 insertions(+), 191 deletions(-) (limited to 'minui') diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index 9e262b044..17f30d1d4 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -16,14 +16,10 @@ #include #include -#include #include #include -#include -#include - -#include #include +#include #include #include @@ -31,243 +27,212 @@ #include "graphics.h" struct adf_surface_pdata { - GRSurface base; - int fence_fd; - int fd; - __u32 offset; - __u32 pitch; + GRSurface base; + int fence_fd; + int fd; + __u32 offset; + __u32 pitch; }; struct adf_pdata { - minui_backend base; - int intf_fd; - adf_id_t eng_id; - __u32 format; + minui_backend base; + int intf_fd; + adf_id_t eng_id; + __u32 format; - adf_device dev; + adf_device dev; - unsigned int current_surface; - unsigned int n_surfaces; - adf_surface_pdata surfaces[2]; + unsigned int current_surface; + unsigned int n_surfaces; + adf_surface_pdata surfaces[2]; }; -static GRSurface* adf_flip(minui_backend *backend); -static void adf_blank(minui_backend *backend, bool blank); - -static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) { - memset(surf, 0, sizeof(*surf)); - - surf->fence_fd = -1; - surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, - mode->vdisplay, pdata->format, &surf->offset, &surf->pitch); - if (surf->fd < 0) - return surf->fd; - - surf->base.width = mode->hdisplay; - surf->base.height = mode->vdisplay; - surf->base.row_bytes = surf->pitch; - surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; - - surf->base.data = static_cast(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; - } +static GRSurface* adf_flip(minui_backend* backend); +static void adf_blank(minui_backend* backend, bool blank); + +static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surface_pdata* surf) { + *surf = {}; + surf->fence_fd = -1; + surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, mode->vdisplay, + pdata->format, &surf->offset, &surf->pitch); + if (surf->fd < 0) { + return surf->fd; + } + + surf->base.width = mode->hdisplay; + surf->base.height = mode->vdisplay; + surf->base.row_bytes = surf->pitch; + surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; + + surf->base.data = static_cast(mmap(nullptr, surf->pitch * surf->base.height, PROT_WRITE, + MAP_SHARED, surf->fd, surf->offset)); + if (surf->base.data == MAP_FAILED) { + close(surf->fd); + return -errno; + } - return 0; + return 0; } -static int adf_interface_init(adf_pdata *pdata) -{ - adf_interface_data intf_data; - int ret = 0; - int err; - - err = adf_get_interface_data(pdata->intf_fd, &intf_data); - if (err < 0) - return err; - - err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); - if (err < 0) { - fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); - ret = err; - goto done; - } - - err = adf_surface_init(pdata, &intf_data.current_mode, - &pdata->surfaces[1]); - if (err < 0) { - fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); - memset(&pdata->surfaces[1], 0, sizeof(pdata->surfaces[1])); - pdata->n_surfaces = 1; - } else { - pdata->n_surfaces = 2; - } +static int adf_interface_init(adf_pdata* pdata) { + adf_interface_data intf_data; + int err = adf_get_interface_data(pdata->intf_fd, &intf_data); + if (err < 0) return err; + + int ret = 0; + err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); + if (err < 0) { + fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); + ret = err; + goto done; + } + + err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[1]); + if (err < 0) { + fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); + pdata->surfaces[1] = {}; + pdata->n_surfaces = 1; + } else { + pdata->n_surfaces = 2; + } done: - adf_free_interface_data(&intf_data); - return ret; + adf_free_interface_data(&intf_data); + return ret; } -static int adf_device_init(adf_pdata *pdata, adf_device *dev) -{ - adf_id_t intf_id; - int intf_fd; - int err; +static int adf_device_init(adf_pdata* pdata, adf_device* dev) { + adf_id_t intf_id; + int err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, &pdata->eng_id); + if (err < 0) return err; - err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, - &pdata->eng_id); - if (err < 0) - return err; + err = adf_device_attach(dev, pdata->eng_id, intf_id); + if (err < 0 && err != -EALREADY) return err; - err = adf_device_attach(dev, pdata->eng_id, intf_id); - if (err < 0 && err != -EALREADY) - return err; + pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); + if (pdata->intf_fd < 0) return pdata->intf_fd; - pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); - if (pdata->intf_fd < 0) - return pdata->intf_fd; - - err = adf_interface_init(pdata); - if (err < 0) { - close(pdata->intf_fd); - pdata->intf_fd = -1; - } + err = adf_interface_init(pdata); + if (err < 0) { + close(pdata->intf_fd); + pdata->intf_fd = -1; + } - return err; + return err; } -static GRSurface* adf_init(minui_backend *backend) -{ - adf_pdata *pdata = (adf_pdata *)backend; - adf_id_t *dev_ids = NULL; - ssize_t n_dev_ids, i; - GRSurface* ret; +static GRSurface* adf_init(minui_backend* backend) { + adf_pdata* pdata = reinterpret_cast(backend); #if defined(RECOVERY_ABGR) - pdata->format = DRM_FORMAT_ABGR8888; + pdata->format = DRM_FORMAT_ABGR8888; #elif defined(RECOVERY_BGRA) - pdata->format = DRM_FORMAT_BGRA8888; + pdata->format = DRM_FORMAT_BGRA8888; #elif defined(RECOVERY_RGBX) - pdata->format = DRM_FORMAT_RGBX8888; + pdata->format = DRM_FORMAT_RGBX8888; #else - pdata->format = DRM_FORMAT_RGB565; + pdata->format = DRM_FORMAT_RGB565; #endif - n_dev_ids = adf_devices(&dev_ids); - if (n_dev_ids == 0) { - return NULL; - } else if (n_dev_ids < 0) { - fprintf(stderr, "enumerating adf devices failed: %s\n", - strerror(-n_dev_ids)); - return NULL; - } + adf_id_t* dev_ids = nullptr; + ssize_t n_dev_ids = adf_devices(&dev_ids); + if (n_dev_ids == 0) { + return nullptr; + } else if (n_dev_ids < 0) { + fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids)); + return nullptr; + } - pdata->intf_fd = -1; + pdata->intf_fd = -1; - for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { - - int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev); - if (err < 0) { - fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], - strerror(-err)); - continue; - } - - err = adf_device_init(pdata, &pdata->dev); - if (err < 0) { - fprintf(stderr, "initializing adf device %u failed: %s\n", - dev_ids[i], strerror(-err)); - adf_device_close(&pdata->dev); - } + for (ssize_t i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { + int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev); + if (err < 0) { + fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err)); + continue; } - free(dev_ids); + err = adf_device_init(pdata, &pdata->dev); + if (err < 0) { + fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err)); + adf_device_close(&pdata->dev); + } + } - if (pdata->intf_fd < 0) - return NULL; + free(dev_ids); - ret = adf_flip(backend); + if (pdata->intf_fd < 0) return nullptr; - adf_blank(backend, true); - adf_blank(backend, false); + GRSurface* ret = adf_flip(backend); - return ret; -} + adf_blank(backend, true); + adf_blank(backend, false); -static void adf_sync(adf_surface_pdata *surf) -{ - unsigned int warningTimeout = 3000; + return ret; +} - if (surf == NULL) - return; +static void adf_sync(adf_surface_pdata* surf) { + static constexpr unsigned int warningTimeout = 3000; - if (surf->fence_fd >= 0){ - int err = sync_wait(surf->fence_fd, warningTimeout); - if (err < 0) - perror("adf sync fence wait error\n"); + if (surf == nullptr) return; - close(surf->fence_fd); - surf->fence_fd = -1; + if (surf->fence_fd >= 0) { + int err = sync_wait(surf->fence_fd, warningTimeout); + if (err < 0) { + perror("adf sync fence wait error\n"); } + + close(surf->fence_fd); + surf->fence_fd = -1; + } } -static GRSurface* adf_flip(minui_backend *backend) -{ - adf_pdata *pdata = (adf_pdata *)backend; - adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface]; +static GRSurface* adf_flip(minui_backend* backend) { + adf_pdata* pdata = reinterpret_cast(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, - surf->offset, surf->pitch, -1); - if (fence_fd >= 0) - surf->fence_fd = fence_fd; + int fence_fd = + adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, surf->base.width, surf->base.height, + pdata->format, surf->fd, surf->offset, surf->pitch, -1); + if (fence_fd >= 0) surf->fence_fd = fence_fd; - pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; - adf_sync(&pdata->surfaces[pdata->current_surface]); - return &pdata->surfaces[pdata->current_surface].base; + pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; + adf_sync(&pdata->surfaces[pdata->current_surface]); + return &pdata->surfaces[pdata->current_surface].base; } -static void adf_blank(minui_backend *backend, bool blank) -{ - adf_pdata *pdata = (adf_pdata *)backend; - adf_interface_blank(pdata->intf_fd, - blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); +static void adf_blank(minui_backend* backend, bool blank) { + adf_pdata* pdata = reinterpret_cast(backend); + adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); } -static void adf_surface_destroy(adf_surface_pdata *surf) -{ - munmap(surf->base.data, surf->pitch * surf->base.height); - close(surf->fence_fd); - close(surf->fd); +static void adf_surface_destroy(adf_surface_pdata* surf) { + munmap(surf->base.data, surf->pitch * surf->base.height); + close(surf->fence_fd); + close(surf->fd); } -static void adf_exit(minui_backend *backend) -{ - adf_pdata *pdata = (adf_pdata *)backend; - unsigned int i; - - adf_device_close(&pdata->dev); - for (i = 0; i < pdata->n_surfaces; i++) - adf_surface_destroy(&pdata->surfaces[i]); - if (pdata->intf_fd >= 0) - close(pdata->intf_fd); - free(pdata); +static void adf_exit(minui_backend* backend) { + adf_pdata* pdata = reinterpret_cast(backend); + adf_device_close(&pdata->dev); + for (unsigned int i = 0; i < pdata->n_surfaces; i++) { + adf_surface_destroy(&pdata->surfaces[i]); + } + if (pdata->intf_fd >= 0) close(pdata->intf_fd); + free(pdata); } -minui_backend *open_adf() -{ - adf_pdata* pdata = static_cast(calloc(1, sizeof(*pdata))); - if (!pdata) { - perror("allocating adf backend failed"); - return NULL; - } +minui_backend* open_adf() { + adf_pdata* pdata = static_cast(calloc(1, sizeof(*pdata))); + if (!pdata) { + perror("allocating adf backend failed"); + return nullptr; + } + + pdata->base.init = adf_init; + pdata->base.flip = adf_flip; + pdata->base.blank = adf_blank; + pdata->base.exit = adf_exit; - pdata->base.init = adf_init; - pdata->base.flip = adf_flip; - pdata->base.blank = adf_blank; - pdata->base.exit = adf_exit; - return &pdata->base; + return &pdata->base; } -- cgit v1.2.3 From acf4dd157addc66c7c39efdc5d5bafc4eeecaa6d Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 7 Feb 2017 14:32:05 -0800 Subject: minui: Clean up graphics_fbdev.cpp. Remove unneeded header includes. Otherwise mostly cosmetic changes like reformatting. Test: 'Run graphics test' on bullhead (which uses fbdev). Change-Id: I9b92c96128fa332ac940f73764f9c5fc93ff6c8c --- minui/graphics_fbdev.cpp | 268 +++++++++++++++++++++++------------------------ 1 file changed, 129 insertions(+), 139 deletions(-) (limited to 'minui') diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 2d70249ed..dd4c9d465 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -14,20 +14,15 @@ * limitations under the License. */ -#include -#include -#include - #include +#include #include - -#include +#include +#include #include #include #include - -#include -#include +#include #include "minui/minui.h" #include "graphics.h" @@ -39,162 +34,157 @@ static void fbdev_exit(minui_backend*); static GRSurface gr_framebuffer[2]; static bool double_buffered; -static GRSurface* gr_draw = NULL; +static GRSurface* gr_draw = nullptr; static int displayed_buffer; static fb_var_screeninfo vi; static int fb_fd = -1; static minui_backend my_backend = { - .init = fbdev_init, - .flip = fbdev_flip, - .blank = fbdev_blank, - .exit = fbdev_exit, + .init = fbdev_init, + .flip = fbdev_flip, + .blank = fbdev_blank, + .exit = fbdev_exit, }; minui_backend* open_fbdev() { - return &my_backend; + return &my_backend; } -static void fbdev_blank(minui_backend* backend __unused, bool blank) -{ - int ret; - - ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); - if (ret < 0) - perror("ioctl(): blank"); +static void fbdev_blank(minui_backend* backend __unused, bool blank) { + int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); + if (ret < 0) { + perror("ioctl(): blank"); + } } -static void set_displayed_framebuffer(unsigned n) -{ - if (n > 1 || !double_buffered) return; +static void set_displayed_framebuffer(unsigned n) { + if (n > 1 || !double_buffered) return; - vi.yres_virtual = gr_framebuffer[0].height * 2; - vi.yoffset = n * gr_framebuffer[0].height; - vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8; - if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { - perror("active fb swap failed"); - } - displayed_buffer = n; + vi.yres_virtual = gr_framebuffer[0].height * 2; + vi.yoffset = n * gr_framebuffer[0].height; + vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8; + if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { + perror("active fb swap failed"); + } + displayed_buffer = n; } static GRSurface* fbdev_init(minui_backend* backend) { - int fd = open("/dev/graphics/fb0", O_RDWR); - if (fd == -1) { - perror("cannot open fb0"); - return NULL; + int fd = open("/dev/graphics/fb0", O_RDWR); + if (fd == -1) { + perror("cannot open fb0"); + return nullptr; + } + + fb_fix_screeninfo fi; + if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { + perror("failed to get fb0 info"); + close(fd); + return nullptr; + } + + if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { + perror("failed to get fb0 info"); + close(fd); + return nullptr; + } + + // We print this out for informational purposes only, but + // throughout we assume that the framebuffer device uses an RGBX + // pixel format. This is the case for every development device I + // have access to. For some of those devices (eg, hammerhead aka + // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a + // different format (XBGR) but actually produces the correct + // results on the display when you write RGBX. + // + // If you have a device that actually *needs* another pixel format + // (ie, BGRX, or 565), patches welcome... + + printf( + "fb0 reports (possibly inaccurate):\n" + " vi.bits_per_pixel = %d\n" + " vi.red.offset = %3d .length = %3d\n" + " vi.green.offset = %3d .length = %3d\n" + " vi.blue.offset = %3d .length = %3d\n", + vi.bits_per_pixel, vi.red.offset, vi.red.length, vi.green.offset, vi.green.length, + vi.blue.offset, vi.blue.length); + + 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); + return nullptr; + } + + memset(bits, 0, fi.smem_len); + + gr_framebuffer[0].width = vi.xres; + 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 = static_cast(bits); + memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); + + /* check if we can use double buffering */ + if (vi.yres * fi.line_length * 2 <= fi.smem_len) { + double_buffered = true; + + memcpy(gr_framebuffer + 1, gr_framebuffer, sizeof(GRSurface)); + gr_framebuffer[1].data = + gr_framebuffer[0].data + gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; + + gr_draw = gr_framebuffer + 1; + + } else { + double_buffered = false; + + // Without double-buffering, we allocate RAM for a buffer to + // draw in, and then "flipping" the buffer consists of a + // memcpy from the buffer we allocated to the framebuffer. + + gr_draw = static_cast(malloc(sizeof(GRSurface))); + memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); + gr_draw->data = static_cast(malloc(gr_draw->height * gr_draw->row_bytes)); + if (!gr_draw->data) { + perror("failed to allocate in-memory surface"); + return nullptr; } + } - fb_fix_screeninfo fi; - if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { - perror("failed to get fb0 info"); - close(fd); - return NULL; - } + memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes); + fb_fd = fd; + set_displayed_framebuffer(0); - if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { - perror("failed to get fb0 info"); - close(fd); - return NULL; - } + printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); - // We print this out for informational purposes only, but - // throughout we assume that the framebuffer device uses an RGBX - // pixel format. This is the case for every development device I - // have access to. For some of those devices (eg, hammerhead aka - // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a - // different format (XBGR) but actually produces the correct - // results on the display when you write RGBX. - // - // If you have a device that actually *needs* another pixel format - // (ie, BGRX, or 565), patches welcome... - - printf("fb0 reports (possibly inaccurate):\n" - " vi.bits_per_pixel = %d\n" - " vi.red.offset = %3d .length = %3d\n" - " vi.green.offset = %3d .length = %3d\n" - " vi.blue.offset = %3d .length = %3d\n", - vi.bits_per_pixel, - vi.red.offset, vi.red.length, - vi.green.offset, vi.green.length, - vi.blue.offset, vi.blue.length); - - 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); - return NULL; - } + fbdev_blank(backend, true); + fbdev_blank(backend, false); - memset(bits, 0, fi.smem_len); - - gr_framebuffer[0].width = vi.xres; - 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 = static_cast(bits); - memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); - - /* check if we can use double buffering */ - if (vi.yres * fi.line_length * 2 <= fi.smem_len) { - double_buffered = true; - - memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface)); - gr_framebuffer[1].data = gr_framebuffer[0].data + - gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; - - gr_draw = gr_framebuffer+1; - - } else { - double_buffered = false; - - // Without double-buffering, we allocate RAM for a buffer to - // draw in, and then "flipping" the buffer consists of a - // memcpy from the buffer we allocated to the framebuffer. - - gr_draw = (GRSurface*) malloc(sizeof(GRSurface)); - memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); - gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes); - if (!gr_draw->data) { - perror("failed to allocate in-memory surface"); - return NULL; - } - } - - memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes); - fb_fd = fd; - set_displayed_framebuffer(0); - - printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); - - fbdev_blank(backend, true); - fbdev_blank(backend, false); - - return gr_draw; + return gr_draw; } static GRSurface* fbdev_flip(minui_backend* backend __unused) { - if (double_buffered) { - // Change gr_draw to point to the buffer currently displayed, - // then flip the driver so we're displaying the other buffer - // instead. - gr_draw = gr_framebuffer + displayed_buffer; - set_displayed_framebuffer(1-displayed_buffer); - } else { - // Copy from the in-memory surface to the framebuffer. - memcpy(gr_framebuffer[0].data, gr_draw->data, - gr_draw->height * gr_draw->row_bytes); - } - return gr_draw; + if (double_buffered) { + // Change gr_draw to point to the buffer currently displayed, + // then flip the driver so we're displaying the other buffer + // instead. + gr_draw = gr_framebuffer + displayed_buffer; + set_displayed_framebuffer(1 - displayed_buffer); + } else { + // Copy from the in-memory surface to the framebuffer. + memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes); + } + return gr_draw; } static void fbdev_exit(minui_backend* backend __unused) { - close(fb_fd); - fb_fd = -1; - - if (!double_buffered && gr_draw) { - free(gr_draw->data); - free(gr_draw); - } - gr_draw = NULL; + close(fb_fd); + fb_fd = -1; + + if (!double_buffered && gr_draw) { + free(gr_draw->data); + free(gr_draw); + } + gr_draw = nullptr; } -- cgit v1.2.3 From 76be34cb347d6b58b5bf3649c8033a1cb85559b4 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 7 Feb 2017 13:30:19 -0800 Subject: minui: Clean up graphics_drm.cpp. Remove unneeded header includes. Switch a few memset() to '= {}' style. Otherwise mostly cosmetic changes like reformatting. Test: 'Run graphics test' on ryu (which is a DRM device). Change-Id: I4b0ab2dc0da69a690f09e4f0674b8377de662962 --- minui/graphics_drm.cpp | 648 +++++++++++++++++++++++-------------------------- 1 file changed, 297 insertions(+), 351 deletions(-) (limited to 'minui') diff --git a/minui/graphics_drm.cpp b/minui/graphics_drm.cpp index 199f4d83c..2eeff5800 100644 --- a/minui/graphics_drm.cpp +++ b/minui/graphics_drm.cpp @@ -14,16 +14,14 @@ * limitations under the License. */ -#include #include #include #include -#include -#include -#include #include #include #include + +#include #include #include @@ -33,9 +31,9 @@ #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) struct drm_surface { - GRSurface base; - uint32_t fb_id; - uint32_t handle; + GRSurface base; + uint32_t fb_id; + uint32_t handle; }; static drm_surface *drm_surfaces[2]; @@ -47,429 +45,377 @@ 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 - } + if (crtc) { + drmModeSetCrtc(drm_fd, crtc->crtc_id, + 0, // fb_id + 0, 0, // x,y + nullptr, // connectors + 0, // connector_count + nullptr); // mode + } } -static void drm_enable_crtc(int drm_fd, drmModeCrtc *crtc, - struct drm_surface *surface) { - int32_t ret; +static void drm_enable_crtc(int drm_fd, drmModeCrtc* crtc, struct drm_surface* surface) { + int32_t 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); - 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); + 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]); + 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) return; - if (surface->base.data) - munmap(surface->base.data, - surface->base.row_bytes * surface->base.height); + 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->fb_id) { + int 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; + if (surface->handle) { + drm_gem_close 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); + int ret = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close); + if (ret) { + printf("DRM_IOCTL_GEM_CLOSE failed ret=%d\n", ret); } + } - free(surface); + 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; - } + 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; - } + drm_surface* surface = static_cast(calloc(1, sizeof(*surface))); + if (!surface) { + printf("Can't allocate memory\n"); + return nullptr; + } + uint32_t format; #if defined(RECOVERY_ABGR) - format = DRM_FORMAT_RGBA8888; + format = DRM_FORMAT_RGBA8888; #elif defined(RECOVERY_BGRA) - format = DRM_FORMAT_ARGB8888; + format = DRM_FORMAT_ARGB8888; #elif defined(RECOVERY_RGBX) - format = DRM_FORMAT_XBGR8888; + format = DRM_FORMAT_XBGR8888; #else - format = DRM_FORMAT_RGB565; + 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; + drm_mode_create_dumb create_dumb = {}; + create_dumb.height = height; + create_dumb.width = width; + create_dumb.bpp = drm_format_to_bpp(format); + create_dumb.flags = 0; + + int 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 nullptr; + } + 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 nullptr; + } + + drm_mode_map_dumb 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 nullptr; + } + + 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 = + static_cast(mmap(nullptr, 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 nullptr; + } + + 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; +static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources, + drmModeConnector* connector) { + // Find the encoder. If we already have one, just use it. + drmModeEncoder* encoder; + if (connector->encoder_id) { + encoder = drmModeGetEncoder(fd, connector->encoder_id); + } else { + encoder = nullptr; + } + + int32_t crtc; + 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 (int i = 0; i < connector->count_encoders; i++) { + encoder = drmModeGetEncoder(fd, connector->encoders[i]); + + if (encoder) { + for (int 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); + } } + } - /* - * 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; + return nullptr; } -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); - } +static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) { + for (int i = 0; i < resources->count_connectors; i++) { + drmModeConnector* 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; + } + return nullptr; } -static drmModeConnector *find_first_connected_connector(int fd, - drmModeRes *resources) { - int i; - for (i = 0; i < resources->count_connectors; i++) { - drmModeConnector *connector; +static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) { + for (int 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; + connector = drmModeGetConnector(fd, resources->connectors[i]); + if (connector) { + if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED)) + return connector; - drmModeFreeConnector(connector); - } + drmModeFreeConnector(connector); } - return NULL; + } + return nullptr; } -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; - } +static drmModeConnector* find_main_monitor(int fd, drmModeRes* resources, uint32_t* mode_index) { + /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */ + static constexpr unsigned kConnectorPriority[] = { + DRM_MODE_CONNECTOR_LVDS, + DRM_MODE_CONNECTOR_eDP, + DRM_MODE_CONNECTOR_DSI, + }; + + drmModeConnector* main_monitor_connector = nullptr; + unsigned i = 0; + 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 nullptr; + + *mode_index = 0; + for (int 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; + 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 void disable_non_main_crtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) { + for (int i = 0; i < resources->count_connectors; i++) { + drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]); + drmModeCrtc* 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; + drmModeRes* res = nullptr; + + /* Consider DRM devices in order. */ + for (int i = 0; i < DRM_MAX_MINOR; i++) { + char* dev_name; + int 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; + + uint64_t cap = 0; + /* We need dumb buffers. */ + ret = drmGetCap(drm_fd, DRM_CAP_DUMB_BUFFER, &cap); + if (ret || cap == 0) { + close(drm_fd); + continue; } - if (drm_fd < 0 || res == NULL) { - perror("cannot find/open a drm device"); - return NULL; + res = drmModeGetResources(drm_fd); + if (!res) { + close(drm_fd); + continue; } - 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; + /* 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; } - main_monitor_crtc = find_crtc_for_connector(drm_fd, res, - main_monitor_connector); + drmModeFreeResources(res); + close(drm_fd); + res = nullptr; + } - if (!main_monitor_crtc) { - printf("main_monitor_crtc not found\n"); - drmModeFreeResources(res); - close(drm_fd); - return NULL; - } + if (drm_fd < 0 || res == nullptr) { + perror("cannot find/open a drm device"); + return nullptr; + } - disable_non_main_crtcs(drm_fd, - res, main_monitor_crtc); + uint32_t selected_mode; + main_monitor_connector = find_main_monitor(drm_fd, res, &selected_mode); - main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode]; + if (!main_monitor_connector) { + printf("main_monitor_connector not found\n"); + drmModeFreeResources(res); + close(drm_fd); + return nullptr; + } - width = main_monitor_crtc->mode.hdisplay; - height = main_monitor_crtc->mode.vdisplay; + 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 nullptr; + } - 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; - } + disable_non_main_crtcs(drm_fd, res, main_monitor_crtc); + + main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode]; - current_buffer = 0; + int width = main_monitor_crtc->mode.hdisplay; + int height = main_monitor_crtc->mode.vdisplay; - drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[1]); + drmModeFreeResources(res); - return &(drm_surfaces[0]->base); + 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 nullptr; + } + + 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); + int ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, drm_surfaces[current_buffer]->fb_id, + 0, nullptr); + if (ret < 0) { + printf("drmModePageFlip failed ret=%d\n", ret); + return nullptr; + } + 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; + 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, + .init = drm_init, + .flip = drm_flip, + .blank = drm_blank, + .exit = drm_exit, }; minui_backend* open_drm() { - return &drm_backend; + return &drm_backend; } -- cgit v1.2.3 From f04592ba230139d5132ebd8bb8610f6a98836882 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 9 Feb 2017 12:59:19 -0800 Subject: minui: Save errno before calling close(). Otherwise errno would be overwritten when calling close(2). Test: mmma bootable/recovery Change-Id: I661e46b1b040f550639a728aa2683e91621b4307 --- minui/graphics_adf.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'minui') diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index 17f30d1d4..9ab0b06bf 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -67,8 +67,9 @@ static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surfa surf->base.data = static_cast(mmap(nullptr, surf->pitch * surf->base.height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset)); if (surf->base.data == MAP_FAILED) { + int saved_errno = errno; close(surf->fd); - return -errno; + return -saved_errno; } return 0; -- cgit v1.2.3 From 557fa1f45e413777a31b1bb2db4eee826c3ee486 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 7 Feb 2017 12:51:00 -0800 Subject: minui: Move graphics_{adf,drm,fbdev} into classes. This CL defines minui_backend as an interface, and expresses the three backends (adf, drm and fbdev) as subclasses to the interface. Test: 'Run graphics test' on N9, Pixel C and N5X. Change-Id: I0e23951c7b2e2ff918957a8d9fc8b0085b6e5952 --- minui/graphics.cpp | 78 +++++++++++------------ minui/graphics.h | 29 ++++----- minui/graphics_adf.cpp | 162 ++++++++++++++++++----------------------------- minui/graphics_adf.h | 58 +++++++++++++++++ minui/graphics_drm.cpp | 124 ++++++++++++++---------------------- minui/graphics_drm.h | 58 +++++++++++++++++ minui/graphics_fbdev.cpp | 51 ++++----------- minui/graphics_fbdev.h | 44 +++++++++++++ 8 files changed, 333 insertions(+), 271 deletions(-) create mode 100644 minui/graphics_adf.h create mode 100644 minui/graphics_drm.h create mode 100644 minui/graphics_fbdev.h (limited to 'minui') diff --git a/minui/graphics.cpp b/minui/graphics.cpp index c0c67f948..3bdc33fd1 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -20,11 +20,16 @@ #include #include +#include + #include "font_10x18.h" +#include "graphics_adf.h" +#include "graphics_drm.h" +#include "graphics_fbdev.h" #include "minui/minui.h" static GRFont* gr_font = NULL; -static minui_backend* gr_backend = NULL; +static MinuiBackend* gr_backend = nullptr; static int overscan_percent = OVERSCAN_PERCENT; static int overscan_offset_x = 0; @@ -308,59 +313,52 @@ static void gr_init_font(void) } void gr_flip() { - gr_draw = gr_backend->flip(gr_backend); + gr_draw = gr_backend->Flip(); } -int gr_init(void) -{ - gr_init_font(); +int gr_init() { + gr_init_font(); - gr_backend = open_adf(); - if (gr_backend) { - gr_draw = gr_backend->init(gr_backend); - if (!gr_draw) { - gr_backend->exit(gr_backend); - } - } + auto backend = std::unique_ptr{ std::make_unique() }; + gr_draw = backend->Init(); - if (!gr_draw) { - gr_backend = open_drm(); - gr_draw = gr_backend->init(gr_backend); - } + if (!gr_draw) { + backend = std::make_unique(); + gr_draw = backend->Init(); + } - if (!gr_draw) { - gr_backend = open_fbdev(); - gr_draw = gr_backend->init(gr_backend); - if (gr_draw == NULL) { - return -1; - } - } + if (!gr_draw) { + backend = std::make_unique(); + gr_draw = backend->Init(); + } - overscan_offset_x = gr_draw->width * overscan_percent / 100; - overscan_offset_y = gr_draw->height * overscan_percent / 100; + if (!gr_draw) { + return -1; + } - gr_flip(); - gr_flip(); + gr_backend = backend.release(); - return 0; + overscan_offset_x = gr_draw->width * overscan_percent / 100; + overscan_offset_y = gr_draw->height * overscan_percent / 100; + + gr_flip(); + gr_flip(); + + return 0; } -void gr_exit(void) -{ - gr_backend->exit(gr_backend); +void gr_exit() { + delete gr_backend; } -int gr_fb_width(void) -{ - return gr_draw->width - 2*overscan_offset_x; +int gr_fb_width() { + return gr_draw->width - 2 * overscan_offset_x; } -int gr_fb_height(void) -{ - return gr_draw->height - 2*overscan_offset_y; +int gr_fb_height() { + return gr_draw->height - 2 * overscan_offset_y; } -void gr_fb_blank(bool blank) -{ - gr_backend->blank(gr_backend, blank); +void gr_fb_blank(bool blank) { + gr_backend->Blank(blank); } diff --git a/minui/graphics.h b/minui/graphics.h index 1eaafc75a..3c45a406b 100644 --- a/minui/graphics.h +++ b/minui/graphics.h @@ -19,25 +19,20 @@ #include "minui/minui.h" -// TODO: lose the function pointers. -struct minui_backend { - // Initializes the backend and returns a GRSurface* to draw into. - GRSurface* (*init)(minui_backend*); +class MinuiBackend { + public: + // Initializes the backend and returns a GRSurface* to draw into. + virtual GRSurface* Init() = 0; - // Causes the current drawing surface (returned by the most recent - // call to flip() or init()) to be displayed, and returns a new - // drawing surface. - GRSurface* (*flip)(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. + virtual GRSurface* Flip() = 0; - // Blank (or unblank) the screen. - void (*blank)(minui_backend*, bool); + // Blank (or unblank) the screen. + virtual void Blank(bool) = 0; - // Device cleanup when drawing is done. - void (*exit)(minui_backend*); + // Device cleanup when drawing is done. + virtual ~MinuiBackend() {}; }; -minui_backend* open_fbdev(); -minui_backend* open_adf(); -minui_backend* open_drm(); - -#endif +#endif // _GRAPHICS_H_ diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index 9ab0b06bf..1b15a04fb 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "graphics_adf.h" + #include #include #include @@ -24,49 +26,27 @@ #include #include -#include "graphics.h" - -struct adf_surface_pdata { - GRSurface base; - int fence_fd; - int fd; - __u32 offset; - __u32 pitch; -}; - -struct adf_pdata { - minui_backend base; - int intf_fd; - adf_id_t eng_id; - __u32 format; +#include "minui/minui.h" - adf_device dev; +MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), n_surfaces(0), surfaces() {} - unsigned int current_surface; - unsigned int n_surfaces; - adf_surface_pdata surfaces[2]; -}; - -static GRSurface* adf_flip(minui_backend* backend); -static void adf_blank(minui_backend* backend, bool blank); - -static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surface_pdata* surf) { +int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) { *surf = {}; surf->fence_fd = -1; - surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, mode->vdisplay, - pdata->format, &surf->offset, &surf->pitch); + surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format, + &surf->offset, &surf->pitch); if (surf->fd < 0) { return surf->fd; } - surf->base.width = mode->hdisplay; - surf->base.height = mode->vdisplay; - surf->base.row_bytes = surf->pitch; - surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; + surf->width = mode->hdisplay; + surf->height = mode->vdisplay; + surf->row_bytes = surf->pitch; + surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4; - surf->base.data = static_cast(mmap(nullptr, surf->pitch * surf->base.height, PROT_WRITE, - MAP_SHARED, surf->fd, surf->offset)); - if (surf->base.data == MAP_FAILED) { + surf->data = static_cast( + mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset)); + if (surf->data == MAP_FAILED) { int saved_errno = errno; close(surf->fd); return -saved_errno; @@ -75,26 +55,26 @@ static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surfa return 0; } -static int adf_interface_init(adf_pdata* pdata) { +int MinuiBackendAdf::InterfaceInit() { adf_interface_data intf_data; - int err = adf_get_interface_data(pdata->intf_fd, &intf_data); + int err = adf_get_interface_data(intf_fd, &intf_data); if (err < 0) return err; int ret = 0; - err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); + err = SurfaceInit(&intf_data.current_mode, &surfaces[0]); if (err < 0) { fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); ret = err; goto done; } - err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[1]); + err = SurfaceInit(&intf_data.current_mode, &surfaces[1]); if (err < 0) { fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); - pdata->surfaces[1] = {}; - pdata->n_surfaces = 1; + surfaces[1] = {}; + n_surfaces = 1; } else { - pdata->n_surfaces = 2; + n_surfaces = 2; } done: @@ -102,37 +82,35 @@ done: return ret; } -static int adf_device_init(adf_pdata* pdata, adf_device* dev) { +int MinuiBackendAdf::DeviceInit(adf_device* dev) { adf_id_t intf_id; - int err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, &pdata->eng_id); + int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id); if (err < 0) return err; - err = adf_device_attach(dev, pdata->eng_id, intf_id); + err = adf_device_attach(dev, eng_id, intf_id); if (err < 0 && err != -EALREADY) return err; - pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); - if (pdata->intf_fd < 0) return pdata->intf_fd; + intf_fd = adf_interface_open(dev, intf_id, O_RDWR); + if (intf_fd < 0) return intf_fd; - err = adf_interface_init(pdata); + err = InterfaceInit(); if (err < 0) { - close(pdata->intf_fd); - pdata->intf_fd = -1; + close(intf_fd); + intf_fd = -1; } return err; } -static GRSurface* adf_init(minui_backend* backend) { - adf_pdata* pdata = reinterpret_cast(backend); - +GRSurface* MinuiBackendAdf::Init() { #if defined(RECOVERY_ABGR) - pdata->format = DRM_FORMAT_ABGR8888; + format = DRM_FORMAT_ABGR8888; #elif defined(RECOVERY_BGRA) - pdata->format = DRM_FORMAT_BGRA8888; + format = DRM_FORMAT_BGRA8888; #elif defined(RECOVERY_RGBX) - pdata->format = DRM_FORMAT_RGBX8888; + format = DRM_FORMAT_RGBX8888; #else - pdata->format = DRM_FORMAT_RGB565; + format = DRM_FORMAT_RGB565; #endif adf_id_t* dev_ids = nullptr; @@ -144,35 +122,35 @@ static GRSurface* adf_init(minui_backend* backend) { return nullptr; } - pdata->intf_fd = -1; + intf_fd = -1; - for (ssize_t i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { - int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev); + for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) { + int err = adf_device_open(dev_ids[i], O_RDWR, &dev); if (err < 0) { fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err)); continue; } - err = adf_device_init(pdata, &pdata->dev); + err = DeviceInit(&dev); if (err < 0) { fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err)); - adf_device_close(&pdata->dev); + adf_device_close(&dev); } } free(dev_ids); - if (pdata->intf_fd < 0) return nullptr; + if (intf_fd < 0) return nullptr; - GRSurface* ret = adf_flip(backend); + GRSurface* ret = Flip(); - adf_blank(backend, true); - adf_blank(backend, false); + Blank(true); + Blank(false); return ret; } -static void adf_sync(adf_surface_pdata* surf) { +void MinuiBackendAdf::Sync(GRSurfaceAdf* surf) { static constexpr unsigned int warningTimeout = 3000; if (surf == nullptr) return; @@ -188,52 +166,32 @@ static void adf_sync(adf_surface_pdata* surf) { } } -static GRSurface* adf_flip(minui_backend* backend) { - adf_pdata* pdata = reinterpret_cast(backend); - adf_surface_pdata* surf = &pdata->surfaces[pdata->current_surface]; +GRSurface* MinuiBackendAdf::Flip() { + GRSurfaceAdf* surf = &surfaces[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, surf->offset, surf->pitch, -1); + int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format, + surf->fd, surf->offset, surf->pitch, -1); if (fence_fd >= 0) surf->fence_fd = fence_fd; - pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; - adf_sync(&pdata->surfaces[pdata->current_surface]); - return &pdata->surfaces[pdata->current_surface].base; + current_surface = (current_surface + 1) % n_surfaces; + Sync(&surfaces[current_surface]); + return &surfaces[current_surface]; } -static void adf_blank(minui_backend* backend, bool blank) { - adf_pdata* pdata = reinterpret_cast(backend); - adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); +void MinuiBackendAdf::Blank(bool blank) { + adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); } -static void adf_surface_destroy(adf_surface_pdata* surf) { - munmap(surf->base.data, surf->pitch * surf->base.height); +void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) { + munmap(surf->data, surf->pitch * surf->height); close(surf->fence_fd); close(surf->fd); } -static void adf_exit(minui_backend* backend) { - adf_pdata* pdata = reinterpret_cast(backend); - adf_device_close(&pdata->dev); - for (unsigned int i = 0; i < pdata->n_surfaces; i++) { - adf_surface_destroy(&pdata->surfaces[i]); +MinuiBackendAdf::~MinuiBackendAdf() { + adf_device_close(&dev); + for (unsigned int i = 0; i < n_surfaces; i++) { + SurfaceDestroy(&surfaces[i]); } - if (pdata->intf_fd >= 0) close(pdata->intf_fd); - free(pdata); -} - -minui_backend* open_adf() { - adf_pdata* pdata = static_cast(calloc(1, sizeof(*pdata))); - if (!pdata) { - perror("allocating adf backend failed"); - return nullptr; - } - - pdata->base.init = adf_init; - pdata->base.flip = adf_flip; - pdata->base.blank = adf_blank; - pdata->base.exit = adf_exit; - - return &pdata->base; + if (intf_fd >= 0) close(intf_fd); } diff --git a/minui/graphics_adf.h b/minui/graphics_adf.h new file mode 100644 index 000000000..2f019ed0b --- /dev/null +++ b/minui/graphics_adf.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 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. + */ + +#ifndef _GRAPHICS_ADF_H_ +#define _GRAPHICS_ADF_H_ + +#include + +#include "graphics.h" + +class GRSurfaceAdf : public GRSurface { + private: + int fence_fd; + int fd; + __u32 offset; + __u32 pitch; + + friend class MinuiBackendAdf; +}; + +class MinuiBackendAdf : public MinuiBackend { + public: + GRSurface* Init() override; + GRSurface* Flip() override; + void Blank(bool) override; + ~MinuiBackendAdf() override; + MinuiBackendAdf(); + + private: + int SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf); + int InterfaceInit(); + int DeviceInit(adf_device* dev); + void SurfaceDestroy(GRSurfaceAdf* surf); + void Sync(GRSurfaceAdf* surf); + + int intf_fd; + adf_id_t eng_id; + __u32 format; + adf_device dev; + unsigned int current_surface; + unsigned int n_surfaces; + GRSurfaceAdf surfaces[2]; +}; + +#endif // _GRAPHICS_ADF_H_ diff --git a/minui/graphics_drm.cpp b/minui/graphics_drm.cpp index 2eeff5800..e7d4b38ef 100644 --- a/minui/graphics_drm.cpp +++ b/minui/graphics_drm.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "graphics_drm.h" + #include #include #include @@ -26,25 +28,13 @@ #include #include "minui/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; +MinuiBackendDrm::MinuiBackendDrm() + : GRSurfaceDrms(), main_monitor_crtc(nullptr), main_monitor_connector(nullptr), drm_fd(-1) {} -static int drm_fd = -1; - -static void drm_disable_crtc(int drm_fd, drmModeCrtc *crtc) { +void MinuiBackendDrm::DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc) { if (crtc) { drmModeSetCrtc(drm_fd, crtc->crtc_id, 0, // fb_id @@ -55,7 +45,7 @@ static void drm_disable_crtc(int drm_fd, drmModeCrtc *crtc) { } } -static void drm_enable_crtc(int drm_fd, drmModeCrtc* crtc, struct drm_surface* surface) { +void MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, GRSurfaceDrm* surface) { int32_t ret = drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y &main_monitor_connector->connector_id, 1, // connector_count @@ -66,19 +56,19 @@ static void drm_enable_crtc(int drm_fd, drmModeCrtc* crtc, struct drm_surface* s } } -static void drm_blank(minui_backend* backend __unused, bool blank) { +void MinuiBackendDrm::Blank(bool blank) { if (blank) { - drm_disable_crtc(drm_fd, main_monitor_crtc); + DrmDisableCrtc(drm_fd, main_monitor_crtc); } else { - drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[current_buffer]); + DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[current_buffer]); } } -static void drm_destroy_surface(struct drm_surface *surface) { +void MinuiBackendDrm::DrmDestroySurface(GRSurfaceDrm* surface) { if (!surface) return; - if (surface->base.data) { - munmap(surface->base.data, surface->base.row_bytes * surface->base.height); + if (surface->data) { + munmap(surface->data, surface->row_bytes * surface->height); } if (surface->fb_id) { @@ -98,7 +88,7 @@ static void drm_destroy_surface(struct drm_surface *surface) { } } - free(surface); + delete surface; } static int drm_format_to_bpp(uint32_t format) { @@ -118,12 +108,9 @@ static int drm_format_to_bpp(uint32_t format) { } } -static drm_surface *drm_create_surface(int width, int height) { - drm_surface* surface = static_cast(calloc(1, sizeof(*surface))); - if (!surface) { - printf("Can't allocate memory\n"); - return nullptr; - } +GRSurfaceDrm* MinuiBackendDrm::DrmCreateSurface(int width, int height) { + GRSurfaceDrm* surface = new GRSurfaceDrm; + *surface = {}; uint32_t format; #if defined(RECOVERY_ABGR) @@ -145,7 +132,7 @@ static drm_surface *drm_create_surface(int width, int height) { int 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); + DrmDestroySurface(surface); return nullptr; } surface->handle = create_dumb.handle; @@ -160,7 +147,7 @@ static drm_surface *drm_create_surface(int width, int height) { 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); + DrmDestroySurface(surface); return nullptr; } @@ -169,20 +156,20 @@ static drm_surface *drm_create_surface(int width, int height) { 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); + DrmDestroySurface(surface); return nullptr; } - 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 = - static_cast(mmap(nullptr, surface->base.height * surface->base.row_bytes, - PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, map_dumb.offset)); - if (surface->base.data == MAP_FAILED) { + surface->height = height; + surface->width = width; + surface->row_bytes = create_dumb.pitch; + surface->pixel_bytes = create_dumb.bpp / 8; + surface->data = static_cast(mmap(nullptr, surface->height * surface->row_bytes, + PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, + map_dumb.offset)); + if (surface->data == MAP_FAILED) { perror("mmap() failed"); - drm_destroy_surface(surface); + DrmDestroySurface(surface); return nullptr; } @@ -256,7 +243,8 @@ static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* reso return nullptr; } -static drmModeConnector* find_main_monitor(int fd, drmModeRes* resources, uint32_t* mode_index) { +drmModeConnector* MinuiBackendDrm::FindMainMonitor(int fd, drmModeRes* resources, + uint32_t* mode_index) { /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */ static constexpr unsigned kConnectorPriority[] = { DRM_MODE_CONNECTOR_LVDS, @@ -290,18 +278,18 @@ static drmModeConnector* find_main_monitor(int fd, drmModeRes* resources, uint32 return main_monitor_connector; } -static void disable_non_main_crtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) { +void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) { for (int i = 0; i < resources->count_connectors; i++) { drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]); drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector); if (crtc->crtc_id != main_crtc->crtc_id) { - drm_disable_crtc(fd, crtc); + DrmDisableCrtc(fd, crtc); } drmModeFreeCrtc(crtc); } } -static GRSurface* drm_init(minui_backend* backend __unused) { +GRSurface* MinuiBackendDrm::Init() { drmModeRes* res = nullptr; /* Consider DRM devices in order. */ @@ -344,7 +332,7 @@ static GRSurface* drm_init(minui_backend* backend __unused) { } uint32_t selected_mode; - main_monitor_connector = find_main_monitor(drm_fd, res, &selected_mode); + main_monitor_connector = FindMainMonitor(drm_fd, res, &selected_mode); if (!main_monitor_connector) { printf("main_monitor_connector not found\n"); @@ -362,7 +350,7 @@ static GRSurface* drm_init(minui_backend* backend __unused) { return nullptr; } - disable_non_main_crtcs(drm_fd, res, main_monitor_crtc); + DisableNonMainCrtcs(drm_fd, res, main_monitor_crtc); main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode]; @@ -371,51 +359,37 @@ static GRSurface* drm_init(minui_backend* backend __unused) { 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); + GRSurfaceDrms[0] = DrmCreateSurface(width, height); + GRSurfaceDrms[1] = DrmCreateSurface(width, height); + if (!GRSurfaceDrms[0] || !GRSurfaceDrms[1]) { + // GRSurfaceDrms and drm_fd should be freed in d'tor. return nullptr; } current_buffer = 0; - drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[1]); + DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[1]); - return &(drm_surfaces[0]->base); + return GRSurfaceDrms[0]; } -static GRSurface* drm_flip(minui_backend* backend __unused) { - int ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, drm_surfaces[current_buffer]->fb_id, - 0, nullptr); +GRSurface* MinuiBackendDrm::Flip() { + int ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, + GRSurfaceDrms[current_buffer]->fb_id, 0, nullptr); if (ret < 0) { printf("drmModePageFlip failed ret=%d\n", ret); return nullptr; } current_buffer = 1 - current_buffer; - return &(drm_surfaces[current_buffer]->base); + return GRSurfaceDrms[current_buffer]; } -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]); +MinuiBackendDrm::~MinuiBackendDrm() { + DrmDisableCrtc(drm_fd, main_monitor_crtc); + DrmDestroySurface(GRSurfaceDrms[0]); + DrmDestroySurface(GRSurfaceDrms[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_drm.h b/minui/graphics_drm.h new file mode 100644 index 000000000..de9621205 --- /dev/null +++ b/minui/graphics_drm.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 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. + */ + +#ifndef _GRAPHICS_DRM_H_ +#define _GRAPHICS_DRM_H_ + +#include + +#include + +#include "graphics.h" +#include "minui/minui.h" + +class GRSurfaceDrm : public GRSurface { + private: + uint32_t fb_id; + uint32_t handle; + + friend class MinuiBackendDrm; +}; + +class MinuiBackendDrm : public MinuiBackend { + public: + GRSurface* Init() override; + GRSurface* Flip() override; + void Blank(bool) override; + ~MinuiBackendDrm() override; + MinuiBackendDrm(); + + private: + void DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc); + void DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, GRSurfaceDrm* surface); + GRSurfaceDrm* DrmCreateSurface(int width, int height); + void DrmDestroySurface(GRSurfaceDrm* surface); + void DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc); + drmModeConnector* FindMainMonitor(int fd, drmModeRes* resources, uint32_t* mode_index); + + GRSurfaceDrm* GRSurfaceDrms[2]; + int current_buffer; + drmModeCrtc* main_monitor_crtc; + drmModeConnector* main_monitor_connector; + int drm_fd; +}; + +#endif // _GRAPHICS_DRM_H_ diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index dd4c9d465..746f42aaa 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "graphics_fbdev.h" + #include #include #include @@ -25,40 +27,15 @@ #include #include "minui/minui.h" -#include "graphics.h" - -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*); - -static GRSurface gr_framebuffer[2]; -static bool double_buffered; -static GRSurface* gr_draw = nullptr; -static int displayed_buffer; - -static fb_var_screeninfo vi; -static int fb_fd = -1; - -static minui_backend my_backend = { - .init = fbdev_init, - .flip = fbdev_flip, - .blank = fbdev_blank, - .exit = fbdev_exit, -}; - -minui_backend* open_fbdev() { - return &my_backend; -} -static void fbdev_blank(minui_backend* backend __unused, bool blank) { +MinuiBackendFbdev::MinuiBackendFbdev() : gr_draw(nullptr), fb_fd(-1) {} + +void MinuiBackendFbdev::Blank(bool blank) { int ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); - if (ret < 0) { - perror("ioctl(): blank"); - } + if (ret < 0) perror("ioctl(): blank"); } -static void set_displayed_framebuffer(unsigned n) { +void MinuiBackendFbdev::SetDisplayedFramebuffer(unsigned n) { if (n > 1 || !double_buffered) return; vi.yres_virtual = gr_framebuffer[0].height * 2; @@ -70,7 +47,7 @@ static void set_displayed_framebuffer(unsigned n) { displayed_buffer = n; } -static GRSurface* fbdev_init(minui_backend* backend) { +GRSurface* MinuiBackendFbdev::Init() { int fd = open("/dev/graphics/fb0", O_RDWR); if (fd == -1) { perror("cannot open fb0"); @@ -154,23 +131,23 @@ static GRSurface* fbdev_init(minui_backend* backend) { memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes); fb_fd = fd; - set_displayed_framebuffer(0); + SetDisplayedFramebuffer(0); printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); - fbdev_blank(backend, true); - fbdev_blank(backend, false); + Blank(true); + Blank(false); return gr_draw; } -static GRSurface* fbdev_flip(minui_backend* backend __unused) { +GRSurface* MinuiBackendFbdev::Flip() { if (double_buffered) { // Change gr_draw to point to the buffer currently displayed, // then flip the driver so we're displaying the other buffer // instead. gr_draw = gr_framebuffer + displayed_buffer; - set_displayed_framebuffer(1 - displayed_buffer); + SetDisplayedFramebuffer(1 - displayed_buffer); } else { // Copy from the in-memory surface to the framebuffer. memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes); @@ -178,7 +155,7 @@ static GRSurface* fbdev_flip(minui_backend* backend __unused) { return gr_draw; } -static void fbdev_exit(minui_backend* backend __unused) { +MinuiBackendFbdev::~MinuiBackendFbdev() { close(fb_fd); fb_fd = -1; diff --git a/minui/graphics_fbdev.h b/minui/graphics_fbdev.h new file mode 100644 index 000000000..107e19567 --- /dev/null +++ b/minui/graphics_fbdev.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2017 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. + */ + +#ifndef _GRAPHICS_FBDEV_H_ +#define _GRAPHICS_FBDEV_H_ + +#include + +#include "graphics.h" +#include "minui/minui.h" + +class MinuiBackendFbdev : public MinuiBackend { + public: + GRSurface* Init() override; + GRSurface* Flip() override; + void Blank(bool) override; + ~MinuiBackendFbdev() override; + MinuiBackendFbdev(); + + private: + void SetDisplayedFramebuffer(unsigned n); + + GRSurface gr_framebuffer[2]; + bool double_buffered; + GRSurface* gr_draw; + int displayed_buffer; + fb_var_screeninfo vi; + int fb_fd; +}; + +#endif // _GRAPHICS_FBDEV_H_ -- cgit v1.2.3 From 25a29d452e53f46766c2d26e4384c7378db3b571 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 23 Feb 2017 10:45:42 -0800 Subject: Add a missing #include for openat(2). Bug: https://code.google.com/p/android/issues/detail?id=64374 Test: builds Change-Id: I7d7650463197710657820a1adce51f71c1b01415 --- minui/events.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'minui') diff --git a/minui/events.cpp b/minui/events.cpp index 6dd60fe68..fa44033d2 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include #include -- cgit v1.2.3 From 9468fc0429cb076dc5ef7c4cda84f6efac3eb333 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 17 Mar 2017 00:57:37 -0700 Subject: Add the missing #include of . For the use of std::function and std::bind. They were relying on the transitive inclusion from . Test: mmma bootable/recovery Change-Id: Ia138e1cbdd035b11d6cdca9e16c5591303b6ee13 --- minui/events.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'minui') diff --git a/minui/events.cpp b/minui/events.cpp index fa44033d2..0e1fd44a0 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -24,6 +24,8 @@ #include #include +#include + #include "minui/minui.h" #define MAX_DEVICES 16 -- cgit v1.2.3 From 0a599567ce79a80d8d511505d34fa810b11e034e Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 28 Mar 2017 20:11:15 +0000 Subject: Merge "Add the missing sr-Latn into png files and rename the png locale header" am: 713d915636 am: dc235b5ab9 am: 5ec12126f0 Change-Id: Ia6b861c91958d3be23a4a7456d6d5d8e4a1607c8 (cherry picked from commit 9166f66eee883d6d6cc280a6c355e5528bb4a3f0) --- minui/Android.mk | 10 ++++++++-- minui/include/minui/minui.h | 3 ++- minui/resources.cpp | 33 +++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 13 deletions(-) (limited to 'minui') diff --git a/minui/Android.mk b/minui/Android.mk index 281f64912..4dfc65f8a 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -28,7 +28,10 @@ LOCAL_WHOLE_STATIC_LIBRARIES := \ libdrm \ libsync_recovery -LOCAL_STATIC_LIBRARIES := libpng +LOCAL_STATIC_LIBRARIES := \ + libpng \ + libbase + LOCAL_CFLAGS := -Werror LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include @@ -61,7 +64,10 @@ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libminui LOCAL_WHOLE_STATIC_LIBRARIES += libminui -LOCAL_SHARED_LIBRARIES := libpng +LOCAL_SHARED_LIBRARIES := \ + libpng \ + libbase + LOCAL_CFLAGS := -Werror LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h index a1749dfe6..78dd4cb98 100644 --- a/minui/include/minui/minui.h +++ b/minui/include/minui/minui.h @@ -20,6 +20,7 @@ #include #include +#include // // Graphics. @@ -93,7 +94,7 @@ int ev_get_epollfd(); // Resources // -bool matches_locale(const char* prefix, const char* locale); +bool matches_locale(const std::string& prefix, const std::string& locale); // res_create_*_surface() functions return 0 if no error, else // negative. diff --git a/minui/resources.cpp b/minui/resources.cpp index c0f9c5c85..86c731b02 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -25,8 +25,11 @@ #include #include +#include +#include #include +#include #include #include "minui/minui.h" @@ -371,16 +374,26 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface) { // This function tests if a locale string stored in PNG (prefix) matches // the locale string provided by the system (locale). -bool matches_locale(const char* prefix, const char* locale) { - if (locale == nullptr) { - return false; - } - - // Return true if the whole string of prefix matches the top part of - // locale. For instance, prefix == "en" matches locale == "en_US"; - // and prefix == "zh_CN" matches locale == "zh_CN_#Hans". - - return (strncmp(prefix, locale, strlen(prefix)) == 0); +bool matches_locale(const std::string& prefix, const std::string& locale) { + // According to the BCP 47 format, A locale string may consists of: + // language-{extlang}-{script}-{region}-{variant} + // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some + // android's system locale can have the format language-{script}-{region}. + + // Return true if the whole string of prefix matches the top part of locale. Otherwise try to + // match the locale string without the {script} section. + // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale + // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN". + if (android::base::StartsWith(locale, prefix.c_str())) { + return true; + } + + size_t separator = prefix.find('-'); + if (separator == std::string::npos) { + return false; + } + std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator)); + return std::regex_match(locale, loc_regex); } int res_create_localized_alpha_surface(const char* name, -- cgit v1.2.3