minui: Refactor GRSurfaceAdf.
Test: mmma -j bootable/recovery
Change-Id: I14514017aace4b7043a9db1f5a93ec130a6f89c4
diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp
index 6fc193f..9eea497 100644
--- a/minui/graphics_adf.cpp
+++ b/minui/graphics_adf.cpp
@@ -20,6 +20,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
@@ -28,51 +29,60 @@
#include "minui/minui.h"
-MinuiBackendAdf::MinuiBackendAdf()
- : intf_fd(-1), dev(), current_surface(0), n_surfaces(0), surfaces() {}
+GRSurfaceAdf::~GRSurfaceAdf() {
+ if (mmapped_buffer_) {
+ munmap(mmapped_buffer_, pitch * height);
+ }
+ if (fence_fd != -1) {
+ close(fence_fd);
+ }
+ if (fd != -1) {
+ close(fd);
+ }
+}
-int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) {
- *surf = {};
- surf->fence_fd = -1;
- 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;
+std::unique_ptr<GRSurfaceAdf> GRSurfaceAdf::Create(int intf_fd, const drm_mode_modeinfo* mode,
+ __u32 format, int* err) {
+ __u32 offset;
+ __u32 pitch;
+ auto fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
+ &offset, &pitch);
+
+ if (fd < 0) {
+ *err = fd;
+ return nullptr;
}
- surf->width = mode->hdisplay;
- surf->height = mode->vdisplay;
- surf->row_bytes = surf->pitch;
- surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4;
+ std::unique_ptr<GRSurfaceAdf> surf = std::unique_ptr<GRSurfaceAdf>(
+ new GRSurfaceAdf(mode->hdisplay, mode->vdisplay, pitch, (format == DRM_FORMAT_RGB565 ? 2 : 4),
+ offset, pitch, fd));
auto mmapped =
mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset);
if (mmapped == MAP_FAILED) {
- int saved_errno = errno;
- close(surf->fd);
- return -saved_errno;
+ *err = -errno;
+ return nullptr;
}
surf->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
- return 0;
+ return surf;
}
+MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), current_surface(0), n_surfaces(0) {}
+
int MinuiBackendAdf::InterfaceInit() {
adf_interface_data intf_data;
- int err = adf_get_interface_data(intf_fd, &intf_data);
- if (err < 0) return err;
+ if (int err = adf_get_interface_data(intf_fd, &intf_data); err < 0) return err;
- int ret = 0;
- err = SurfaceInit(&intf_data.current_mode, &surfaces[0]);
- if (err < 0) {
- fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
- ret = err;
+ int result = 0;
+ surfaces[0] = GRSurfaceAdf::Create(intf_fd, &intf_data.current_mode, format, &result);
+ if (!surfaces[0]) {
+ fprintf(stderr, "Failed to allocate surface 0: %s\n", strerror(-result));
goto done;
}
- err = SurfaceInit(&intf_data.current_mode, &surfaces[1]);
- if (err < 0) {
- fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
- surfaces[1] = {};
+ surfaces[1] = GRSurfaceAdf::Create(intf_fd, &intf_data.current_mode, format, &result);
+ if (!surfaces[1]) {
+ fprintf(stderr, "Failed to allocate surface 1: %s\n", strerror(-result));
n_surfaces = 1;
} else {
n_surfaces = 2;
@@ -80,7 +90,7 @@
done:
adf_free_interface_data(&intf_data);
- return ret;
+ return result;
}
int MinuiBackendAdf::DeviceInit(adf_device* dev) {
@@ -153,12 +163,12 @@
}
void MinuiBackendAdf::Sync(GRSurfaceAdf* surf) {
- static constexpr unsigned int warningTimeout = 3000;
+ static constexpr unsigned int kWarningTimeout = 3000;
if (surf == nullptr) return;
if (surf->fence_fd >= 0) {
- int err = sync_wait(surf->fence_fd, warningTimeout);
+ int err = sync_wait(surf->fence_fd, kWarningTimeout);
if (err < 0) {
perror("adf sync fence wait error\n");
}
@@ -169,33 +179,22 @@
}
GRSurface* MinuiBackendAdf::Flip() {
- GRSurfaceAdf* surf = &surfaces[current_surface];
+ const auto& surf = surfaces[current_surface];
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;
current_surface = (current_surface + 1) % n_surfaces;
- Sync(&surfaces[current_surface]);
- return &surfaces[current_surface];
+ Sync(surfaces[current_surface].get());
+ return surfaces[current_surface].get();
}
void MinuiBackendAdf::Blank(bool blank) {
adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
}
-void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) {
- if (surf->mmapped_buffer_) {
- munmap(surf->mmapped_buffer_, surf->pitch * surf->height);
- }
- close(surf->fence_fd);
- close(surf->fd);
-}
-
MinuiBackendAdf::~MinuiBackendAdf() {
adf_device_close(&dev);
- for (unsigned int i = 0; i < n_surfaces; i++) {
- SurfaceDestroy(&surfaces[i]);
- }
if (intf_fd >= 0) close(intf_fd);
}
diff --git a/minui/graphics_adf.h b/minui/graphics_adf.h
index 099d329..bf98428 100644
--- a/minui/graphics_adf.h
+++ b/minui/graphics_adf.h
@@ -17,6 +17,9 @@
#pragma once
#include <stdint.h>
+#include <sys/types.h>
+
+#include <memory>
#include <adf/adf.h>
@@ -25,6 +28,11 @@
class GRSurfaceAdf : public GRSurface {
public:
+ ~GRSurfaceAdf() override;
+
+ static std::unique_ptr<GRSurfaceAdf> Create(int intf_fd, const drm_mode_modeinfo* mode,
+ __u32 format, int* err);
+
uint8_t* data() override {
return mmapped_buffer_;
}
@@ -32,34 +40,36 @@
private:
friend class MinuiBackendAdf;
- int fence_fd;
- int fd;
- __u32 offset;
- __u32 pitch;
+ GRSurfaceAdf(int width, int height, int row_bytes, int pixel_bytes, __u32 offset, __u32 pitch,
+ int fd)
+ : GRSurface(width, height, row_bytes, pixel_bytes), offset(offset), pitch(pitch), fd(fd) {}
+ const __u32 offset;
+ const __u32 pitch;
+
+ int fd;
+ int fence_fd{ -1 };
uint8_t* mmapped_buffer_{ nullptr };
};
class MinuiBackendAdf : public MinuiBackend {
public:
+ MinuiBackendAdf();
+ ~MinuiBackendAdf() override;
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];
+ size_t current_surface;
+ size_t n_surfaces;
+ std::unique_ptr<GRSurfaceAdf> surfaces[2];
};