android: mm: generic: Use GraphicBufferAllocator instead of gralloc.h

gralloc.h is a very old API that has been deprecated at least since
Android P (9).

Switch over to a higher level abstraction of gralloc from libui, which
is compatible with Android 11 and up.
Libui:
* is provided in the VNDK (so it's available to vendors).
* is also used in the camera vts test named VtsAidlHalCameraProvider_TargetTest.

Drop the libhardware stub since we no longer need it.

Notes:
* GraphicsBufferAllocator being a Singleton, buffer lifecycle
  management is easier.
* The imported headers from Android generate the -Wextra-semi warning.
  To avoid patching the files, a pragma has been added before inclusion.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>
This commit is contained in:
Mattijs Korpershoek
2023-09-23 18:23:34 +02:00
committed by Konsta
parent 8691ade614
commit fe37826bf4
3 changed files with 20 additions and 66 deletions

View File

@@ -16,8 +16,11 @@
#include "libcamera/internal/framebuffer.h"
#include <hardware/camera3.h>
#include <hardware/gralloc.h>
#include <hardware/hardware.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wextra-semi"
#include <ui/GraphicBufferAllocator.h>
#pragma GCC diagnostic pop
#include <utils/Errors.h>
#include "../camera_device.h"
#include "../frame_buffer_allocator.h"
@@ -33,35 +36,28 @@ class GenericFrameBufferData : public FrameBuffer::Private
LIBCAMERA_DECLARE_PUBLIC(FrameBuffer)
public:
GenericFrameBufferData(struct alloc_device_t *allocDevice,
GenericFrameBufferData(android::GraphicBufferAllocator &allocDevice,
buffer_handle_t handle,
Span<const FrameBuffer::Plane> planes)
: FrameBuffer::Private(planes), allocDevice_(allocDevice),
handle_(handle)
{
ASSERT(allocDevice_);
ASSERT(handle_);
}
~GenericFrameBufferData() override
{
/*
* allocDevice_ is used to destroy handle_. allocDevice_ is
* owned by PlatformFrameBufferAllocator::Private.
* GenericFrameBufferData must be destroyed before it is
* destroyed.
*
* \todo Consider managing alloc_device_t with std::shared_ptr
* if this is difficult to maintain.
*
* \todo Thread safety against alloc_device_t is not documented.
* Is it no problem to call alloc/free in parallel?
*/
allocDevice_->free(allocDevice_, handle_);
android::status_t status = allocDevice_.free(handle_);
if (status != android::NO_ERROR)
LOG(HAL, Error) << "Error freeing framebuffer: " << status;
}
private:
struct alloc_device_t *allocDevice_;
android::GraphicBufferAllocator &allocDevice_;
const buffer_handle_t handle_;
};
} /* namespace */
@@ -73,50 +69,33 @@ class PlatformFrameBufferAllocator::Private : public Extensible::Private
public:
Private(CameraDevice *const cameraDevice)
: cameraDevice_(cameraDevice),
hardwareModule_(nullptr),
allocDevice_(nullptr)
allocDevice_(android::GraphicBufferAllocator::get())
{
hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hardwareModule_);
ASSERT(hardwareModule_);
}
~Private() override;
~Private() = default;
std::unique_ptr<HALFrameBuffer>
allocate(int halPixelFormat, const libcamera::Size &size, uint32_t usage);
private:
const CameraDevice *const cameraDevice_;
const struct hw_module_t *hardwareModule_;
struct alloc_device_t *allocDevice_;
android::GraphicBufferAllocator &allocDevice_;
};
PlatformFrameBufferAllocator::Private::~Private()
{
if (allocDevice_)
gralloc_close(allocDevice_);
dlclose(hardwareModule_->dso);
}
std::unique_ptr<HALFrameBuffer>
PlatformFrameBufferAllocator::Private::allocate(int halPixelFormat,
const libcamera::Size &size,
uint32_t usage)
{
if (!allocDevice_) {
int ret = gralloc_open(hardwareModule_, &allocDevice_);
if (ret) {
LOG(HAL, Fatal) << "gralloc_open() failed: " << ret;
return nullptr;
}
}
int stride = 0;
uint32_t stride = 0;
buffer_handle_t handle = nullptr;
int ret = allocDevice_->alloc(allocDevice_, size.width, size.height,
halPixelFormat, usage, &handle, &stride);
if (ret) {
LOG(HAL, Error) << "failed buffer allocation: " << ret;
android::status_t status = allocDevice_.allocate(size.width, size.height, halPixelFormat,
1 /*layerCount*/, usage, &handle, &stride,
"libcameraHAL");
if (status != android::NO_ERROR) {
LOG(HAL, Error) << "failed buffer allocation: " << status;
return nullptr;
}
if (!handle) {

View File

@@ -1,17 +0,0 @@
/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright (C) 2023, Ideas on Board
*
* Android libhardware stub for test compilation
*/
#include <errno.h>
#include <hardware/hardware.h>
int hw_get_module(const char *id __attribute__((__unused__)),
const struct hw_module_t **module)
{
*module = NULL;
return -ENOTSUP;
}

View File

@@ -4,14 +4,6 @@ platform = get_option('android_platform')
if platform == 'generic'
android_hal_sources += files(['generic_camera_buffer.cpp',
'generic_frame_buffer_allocator.cpp'])
android_deps += [libdl]
libhardware = dependency('libhardware', required : false)
if libhardware.found()
android_deps += [libhardware]
else
android_hal_sources += files(['libhardware_stub.c'])
endif
libui = dependency('libui', required : false)
if libui.found()