Merge "minui: Clean up graphics_adf.cpp."

This commit is contained in:
Tao Bao 2017-02-09 20:07:08 +00:00 committed by Gerrit Code Review
commit d592e1d5e1

View file

@ -16,14 +16,10 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/cdefs.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h>
#include <adf/adf.h> #include <adf/adf.h>
#include <sync/sync.h> #include <sync/sync.h>
@ -55,21 +51,20 @@ static GRSurface* adf_flip(minui_backend *backend);
static void adf_blank(minui_backend* backend, bool blank); 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) { static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surface_pdata* surf) {
memset(surf, 0, sizeof(*surf)); *surf = {};
surf->fence_fd = -1; surf->fence_fd = -1;
surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, mode->vdisplay,
mode->vdisplay, pdata->format, &surf->offset, &surf->pitch); pdata->format, &surf->offset, &surf->pitch);
if (surf->fd < 0) if (surf->fd < 0) {
return surf->fd; return surf->fd;
}
surf->base.width = mode->hdisplay; surf->base.width = mode->hdisplay;
surf->base.height = mode->vdisplay; surf->base.height = mode->vdisplay;
surf->base.row_bytes = surf->pitch; surf->base.row_bytes = surf->pitch;
surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4;
surf->base.data = static_cast<uint8_t*>(mmap(NULL, surf->base.data = static_cast<uint8_t*>(mmap(nullptr, surf->pitch * surf->base.height, PROT_WRITE,
surf->pitch * surf->base.height, PROT_WRITE,
MAP_SHARED, surf->fd, surf->offset)); MAP_SHARED, surf->fd, surf->offset));
if (surf->base.data == MAP_FAILED) { if (surf->base.data == MAP_FAILED) {
close(surf->fd); close(surf->fd);
@ -79,16 +74,12 @@ static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surfa
return 0; return 0;
} }
static int adf_interface_init(adf_pdata *pdata) static int adf_interface_init(adf_pdata* pdata) {
{
adf_interface_data intf_data; adf_interface_data intf_data;
int err = adf_get_interface_data(pdata->intf_fd, &intf_data);
if (err < 0) return err;
int ret = 0; 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]); err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]);
if (err < 0) { if (err < 0) {
fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
@ -96,11 +87,10 @@ static int adf_interface_init(adf_pdata *pdata)
goto done; goto done;
} }
err = adf_surface_init(pdata, &intf_data.current_mode, err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[1]);
&pdata->surfaces[1]);
if (err < 0) { if (err < 0) {
fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
memset(&pdata->surfaces[1], 0, sizeof(pdata->surfaces[1])); pdata->surfaces[1] = {};
pdata->n_surfaces = 1; pdata->n_surfaces = 1;
} else { } else {
pdata->n_surfaces = 2; pdata->n_surfaces = 2;
@ -111,24 +101,16 @@ done:
return ret; return ret;
} }
static int adf_device_init(adf_pdata *pdata, adf_device *dev) static int adf_device_init(adf_pdata* pdata, adf_device* dev) {
{
adf_id_t intf_id; adf_id_t intf_id;
int intf_fd; int err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, &pdata->eng_id);
int err; 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); err = adf_device_attach(dev, pdata->eng_id, intf_id);
if (err < 0 && err != -EALREADY) if (err < 0 && err != -EALREADY) return err;
return err;
pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
if (pdata->intf_fd < 0) if (pdata->intf_fd < 0) return pdata->intf_fd;
return pdata->intf_fd;
err = adf_interface_init(pdata); err = adf_interface_init(pdata);
if (err < 0) { if (err < 0) {
@ -139,12 +121,8 @@ static int adf_device_init(adf_pdata *pdata, adf_device *dev)
return err; return err;
} }
static GRSurface* adf_init(minui_backend *backend) static GRSurface* adf_init(minui_backend* backend) {
{ adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
adf_pdata *pdata = (adf_pdata *)backend;
adf_id_t *dev_ids = NULL;
ssize_t n_dev_ids, i;
GRSurface* ret;
#if defined(RECOVERY_ABGR) #if defined(RECOVERY_ABGR)
pdata->format = DRM_FORMAT_ABGR8888; pdata->format = DRM_FORMAT_ABGR8888;
@ -156,40 +134,36 @@ static GRSurface* adf_init(minui_backend *backend)
pdata->format = DRM_FORMAT_RGB565; pdata->format = DRM_FORMAT_RGB565;
#endif #endif
n_dev_ids = adf_devices(&dev_ids); adf_id_t* dev_ids = nullptr;
ssize_t n_dev_ids = adf_devices(&dev_ids);
if (n_dev_ids == 0) { if (n_dev_ids == 0) {
return NULL; return nullptr;
} else if (n_dev_ids < 0) { } else if (n_dev_ids < 0) {
fprintf(stderr, "enumerating adf devices failed: %s\n", fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
strerror(-n_dev_ids)); return nullptr;
return NULL;
} }
pdata->intf_fd = -1; pdata->intf_fd = -1;
for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { 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); int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev);
if (err < 0) { if (err < 0) {
fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
strerror(-err));
continue; continue;
} }
err = adf_device_init(pdata, &pdata->dev); err = adf_device_init(pdata, &pdata->dev);
if (err < 0) { if (err < 0) {
fprintf(stderr, "initializing adf device %u failed: %s\n", fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
dev_ids[i], strerror(-err));
adf_device_close(&pdata->dev); adf_device_close(&pdata->dev);
} }
} }
free(dev_ids); free(dev_ids);
if (pdata->intf_fd < 0) if (pdata->intf_fd < 0) return nullptr;
return NULL;
ret = adf_flip(backend); GRSurface* ret = adf_flip(backend);
adf_blank(backend, true); adf_blank(backend, true);
adf_blank(backend, false); adf_blank(backend, false);
@ -197,77 +171,68 @@ static GRSurface* adf_init(minui_backend *backend)
return ret; return ret;
} }
static void adf_sync(adf_surface_pdata *surf) static void adf_sync(adf_surface_pdata* surf) {
{ static constexpr unsigned int warningTimeout = 3000;
unsigned int warningTimeout = 3000;
if (surf == NULL) if (surf == nullptr) return;
return;
if (surf->fence_fd >= 0) { if (surf->fence_fd >= 0) {
int err = sync_wait(surf->fence_fd, warningTimeout); int err = sync_wait(surf->fence_fd, warningTimeout);
if (err < 0) if (err < 0) {
perror("adf sync fence wait error\n"); perror("adf sync fence wait error\n");
}
close(surf->fence_fd); close(surf->fence_fd);
surf->fence_fd = -1; surf->fence_fd = -1;
} }
} }
static GRSurface* adf_flip(minui_backend *backend) static GRSurface* adf_flip(minui_backend* backend) {
{ adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
adf_pdata *pdata = (adf_pdata *)backend;
adf_surface_pdata* surf = &pdata->surfaces[pdata->current_surface]; adf_surface_pdata* surf = &pdata->surfaces[pdata->current_surface];
int fence_fd = adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, int fence_fd =
surf->base.width, surf->base.height, pdata->format, surf->fd, adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, surf->base.width, surf->base.height,
surf->offset, surf->pitch, -1); pdata->format, surf->fd, surf->offset, surf->pitch, -1);
if (fence_fd >= 0) if (fence_fd >= 0) surf->fence_fd = fence_fd;
surf->fence_fd = fence_fd;
pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces;
adf_sync(&pdata->surfaces[pdata->current_surface]); adf_sync(&pdata->surfaces[pdata->current_surface]);
return &pdata->surfaces[pdata->current_surface].base; return &pdata->surfaces[pdata->current_surface].base;
} }
static void adf_blank(minui_backend *backend, bool blank) static void adf_blank(minui_backend* backend, bool blank) {
{ adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
adf_pdata *pdata = (adf_pdata *)backend; adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
adf_interface_blank(pdata->intf_fd,
blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
} }
static void adf_surface_destroy(adf_surface_pdata *surf) static void adf_surface_destroy(adf_surface_pdata* surf) {
{
munmap(surf->base.data, surf->pitch * surf->base.height); munmap(surf->base.data, surf->pitch * surf->base.height);
close(surf->fence_fd); close(surf->fence_fd);
close(surf->fd); close(surf->fd);
} }
static void adf_exit(minui_backend *backend) static void adf_exit(minui_backend* backend) {
{ adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
adf_pdata *pdata = (adf_pdata *)backend;
unsigned int i;
adf_device_close(&pdata->dev); adf_device_close(&pdata->dev);
for (i = 0; i < pdata->n_surfaces; i++) for (unsigned int i = 0; i < pdata->n_surfaces; i++) {
adf_surface_destroy(&pdata->surfaces[i]); adf_surface_destroy(&pdata->surfaces[i]);
if (pdata->intf_fd >= 0) }
close(pdata->intf_fd); if (pdata->intf_fd >= 0) close(pdata->intf_fd);
free(pdata); free(pdata);
} }
minui_backend *open_adf() minui_backend* open_adf() {
{
adf_pdata* pdata = static_cast<adf_pdata*>(calloc(1, sizeof(*pdata))); adf_pdata* pdata = static_cast<adf_pdata*>(calloc(1, sizeof(*pdata)));
if (!pdata) { if (!pdata) {
perror("allocating adf backend failed"); perror("allocating adf backend failed");
return NULL; return nullptr;
} }
pdata->base.init = adf_init; pdata->base.init = adf_init;
pdata->base.flip = adf_flip; pdata->base.flip = adf_flip;
pdata->base.blank = adf_blank; pdata->base.blank = adf_blank;
pdata->base.exit = adf_exit; pdata->base.exit = adf_exit;
return &pdata->base; return &pdata->base;
} }