2014-04-25 19:39:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 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.
|
|
|
|
*/
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
#include "graphics_adf.h"
|
|
|
|
|
2014-04-25 19:39:50 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mman.h>
|
2017-02-07 22:00:18 +01:00
|
|
|
#include <unistd.h>
|
2014-04-25 19:39:50 +02:00
|
|
|
|
|
|
|
#include <adf/adf.h>
|
2016-08-05 08:52:22 +02:00
|
|
|
#include <sync/sync.h>
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
#include "minui/minui.h"
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-04-29 00:05:33 +02:00
|
|
|
MinuiBackendAdf::MinuiBackendAdf()
|
|
|
|
: intf_fd(-1), dev(), current_surface(0), n_surfaces(0), surfaces() {}
|
2016-05-06 00:30:57 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) {
|
2017-02-07 22:00:18 +01:00
|
|
|
*surf = {};
|
|
|
|
surf->fence_fd = -1;
|
2017-02-07 21:51:00 +01:00
|
|
|
surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
|
|
|
|
&surf->offset, &surf->pitch);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (surf->fd < 0) {
|
|
|
|
return surf->fd;
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
surf->width = mode->hdisplay;
|
|
|
|
surf->height = mode->vdisplay;
|
|
|
|
surf->row_bytes = surf->pitch;
|
|
|
|
surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4;
|
2017-02-07 22:00:18 +01:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
surf->data = static_cast<uint8_t*>(
|
|
|
|
mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset));
|
|
|
|
if (surf->data == MAP_FAILED) {
|
2017-02-09 21:59:19 +01:00
|
|
|
int saved_errno = errno;
|
2017-02-07 22:00:18 +01:00
|
|
|
close(surf->fd);
|
2017-02-09 21:59:19 +01:00
|
|
|
return -saved_errno;
|
2017-02-07 22:00:18 +01:00
|
|
|
}
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 22:00:18 +01:00
|
|
|
return 0;
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
int MinuiBackendAdf::InterfaceInit() {
|
2017-02-07 22:00:18 +01:00
|
|
|
adf_interface_data intf_data;
|
2017-02-07 21:51:00 +01:00
|
|
|
int err = adf_get_interface_data(intf_fd, &intf_data);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0) return err;
|
|
|
|
|
|
|
|
int ret = 0;
|
2017-02-07 21:51:00 +01:00
|
|
|
err = SurfaceInit(&intf_data.current_mode, &surfaces[0]);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
|
|
|
|
ret = err;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
err = SurfaceInit(&intf_data.current_mode, &surfaces[1]);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
|
2017-02-07 21:51:00 +01:00
|
|
|
surfaces[1] = {};
|
|
|
|
n_surfaces = 1;
|
2017-02-07 22:00:18 +01:00
|
|
|
} else {
|
2017-02-07 21:51:00 +01:00
|
|
|
n_surfaces = 2;
|
2017-02-07 22:00:18 +01:00
|
|
|
}
|
2014-04-25 19:39:50 +02:00
|
|
|
|
|
|
|
done:
|
2017-02-07 22:00:18 +01:00
|
|
|
adf_free_interface_data(&intf_data);
|
|
|
|
return ret;
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
int MinuiBackendAdf::DeviceInit(adf_device* dev) {
|
2017-02-07 22:00:18 +01:00
|
|
|
adf_id_t intf_id;
|
2017-02-07 21:51:00 +01:00
|
|
|
int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0) return err;
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
err = adf_device_attach(dev, eng_id, intf_id);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0 && err != -EALREADY) return err;
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
|
|
|
|
if (intf_fd < 0) return intf_fd;
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
err = InterfaceInit();
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0) {
|
2017-02-07 21:51:00 +01:00
|
|
|
close(intf_fd);
|
|
|
|
intf_fd = -1;
|
2017-02-07 22:00:18 +01:00
|
|
|
}
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 22:00:18 +01:00
|
|
|
return err;
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
GRSurface* MinuiBackendAdf::Init() {
|
2015-02-05 14:25:56 +01:00
|
|
|
#if defined(RECOVERY_ABGR)
|
2017-02-07 21:51:00 +01:00
|
|
|
format = DRM_FORMAT_ABGR8888;
|
2015-02-05 14:25:56 +01:00
|
|
|
#elif defined(RECOVERY_BGRA)
|
2017-02-07 21:51:00 +01:00
|
|
|
format = DRM_FORMAT_BGRA8888;
|
2014-04-25 19:39:50 +02:00
|
|
|
#elif defined(RECOVERY_RGBX)
|
2017-02-07 21:51:00 +01:00
|
|
|
format = DRM_FORMAT_RGBX8888;
|
2014-04-25 19:39:50 +02:00
|
|
|
#else
|
2017-02-07 21:51:00 +01:00
|
|
|
format = DRM_FORMAT_RGB565;
|
2014-04-25 19:39:50 +02:00
|
|
|
#endif
|
|
|
|
|
2017-02-07 22:00:18 +01:00
|
|
|
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;
|
|
|
|
}
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
intf_fd = -1;
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) {
|
|
|
|
int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
|
|
|
|
continue;
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
err = DeviceInit(&dev);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
|
2017-02-07 21:51:00 +01:00
|
|
|
adf_device_close(&dev);
|
2017-02-07 22:00:18 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 22:00:18 +01:00
|
|
|
free(dev_ids);
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
if (intf_fd < 0) return nullptr;
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
GRSurface* ret = Flip();
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
Blank(true);
|
|
|
|
Blank(false);
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 22:00:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2016-08-05 08:52:22 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
void MinuiBackendAdf::Sync(GRSurfaceAdf* surf) {
|
2017-02-07 22:00:18 +01:00
|
|
|
static constexpr unsigned int warningTimeout = 3000;
|
2016-08-05 08:52:22 +02:00
|
|
|
|
2017-02-07 22:00:18 +01:00
|
|
|
if (surf == nullptr) return;
|
2016-08-05 08:52:22 +02:00
|
|
|
|
2017-02-07 22:00:18 +01:00
|
|
|
if (surf->fence_fd >= 0) {
|
|
|
|
int err = sync_wait(surf->fence_fd, warningTimeout);
|
|
|
|
if (err < 0) {
|
|
|
|
perror("adf sync fence wait error\n");
|
2016-08-05 08:52:22 +02:00
|
|
|
}
|
2017-02-07 22:00:18 +01:00
|
|
|
|
|
|
|
close(surf->fence_fd);
|
|
|
|
surf->fence_fd = -1;
|
|
|
|
}
|
2016-08-05 08:52:22 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
GRSurface* MinuiBackendAdf::Flip() {
|
|
|
|
GRSurfaceAdf* surf = &surfaces[current_surface];
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format,
|
|
|
|
surf->fd, surf->offset, surf->pitch, -1);
|
2017-02-07 22:00:18 +01:00
|
|
|
if (fence_fd >= 0) surf->fence_fd = fence_fd;
|
2014-04-25 19:39:50 +02:00
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
current_surface = (current_surface + 1) % n_surfaces;
|
|
|
|
Sync(&surfaces[current_surface]);
|
|
|
|
return &surfaces[current_surface];
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
void MinuiBackendAdf::Blank(bool blank) {
|
|
|
|
adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) {
|
|
|
|
munmap(surf->data, surf->pitch * surf->height);
|
2017-02-07 22:00:18 +01:00
|
|
|
close(surf->fence_fd);
|
|
|
|
close(surf->fd);
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:51:00 +01:00
|
|
|
MinuiBackendAdf::~MinuiBackendAdf() {
|
|
|
|
adf_device_close(&dev);
|
|
|
|
for (unsigned int i = 0; i < n_surfaces; i++) {
|
|
|
|
SurfaceDestroy(&surfaces[i]);
|
2017-02-07 22:00:18 +01:00
|
|
|
}
|
2017-02-07 21:51:00 +01:00
|
|
|
if (intf_fd >= 0) close(intf_fd);
|
2014-04-25 19:39:50 +02:00
|
|
|
}
|