libcamera/src/android/mm/cros_frame_buffer_allocator.cpp
Laurent Pinchart c20d3f5575 libcamera: framebuffer: Move remaining private data to Private class
Private members of the FrameBuffer class are split between FrameBuffer
and FrameBuffer::Private. There was no real justification for this
split, and keeping some members private in the FrameBuffer class causes
multiple issues:

- Future modifications of the FrameBuffer class without breaking the ABI
  may be more difficult.
- Mutable access to members that should not be modified by applications
  require a friend statement, or going through the Private class.

Move all remaining private members to the Private class to address the
first issue, and add a Private::metadata() function to address the
second problem.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
Tested-by: Naushir Patuck <naush@raspberrypi.com>
2022-10-10 17:49:49 +03:00

88 lines
2.3 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2021, Google Inc.
*
* cros_frame_buffer.cpp - Allocate FrameBuffer for Chromium OS using
* CameraBufferManager
*/
#include <memory>
#include <vector>
#include <libcamera/base/log.h>
#include <libcamera/base/shared_fd.h>
#include "libcamera/internal/framebuffer.h"
#include "../camera_device.h"
#include "../frame_buffer_allocator.h"
#include "cros-camera/camera_buffer_manager.h"
using namespace libcamera;
LOG_DECLARE_CATEGORY(HAL)
namespace {
class CrosFrameBufferData : public FrameBuffer::Private
{
LIBCAMERA_DECLARE_PUBLIC(FrameBuffer)
public:
CrosFrameBufferData(cros::ScopedBufferHandle scopedHandle,
const std::vector<FrameBuffer::Plane> &planes)
: FrameBuffer::Private(planes), scopedHandle_(std::move(scopedHandle))
{
}
private:
cros::ScopedBufferHandle scopedHandle_;
};
} /* namespace */
class PlatformFrameBufferAllocator::Private : public Extensible::Private
{
LIBCAMERA_DECLARE_PUBLIC(PlatformFrameBufferAllocator)
public:
Private([[maybe_unused]] CameraDevice *const cameraDevice)
{
}
std::unique_ptr<libcamera::FrameBuffer>
allocate(int halPixelFormat, const libcamera::Size &size, uint32_t usage);
};
std::unique_ptr<libcamera::FrameBuffer>
PlatformFrameBufferAllocator::Private::allocate(int halPixelFormat,
const libcamera::Size &size,
uint32_t usage)
{
cros::ScopedBufferHandle scopedHandle =
cros::CameraBufferManager::AllocateScopedBuffer(
size.width, size.height, halPixelFormat, usage);
if (!scopedHandle) {
LOG(HAL, Error) << "Failed to allocate buffer handle";
return nullptr;
}
buffer_handle_t handle = *scopedHandle;
SharedFD fd{ handle->data[0] };
if (!fd.isValid()) {
LOG(HAL, Fatal) << "Invalid fd";
return nullptr;
}
/* This code assumes all the planes are located in the same buffer. */
const size_t numPlanes = cros::CameraBufferManager::GetNumPlanes(handle);
std::vector<FrameBuffer::Plane> planes(numPlanes);
for (auto [i, plane] : utils::enumerate(planes)) {
plane.fd = fd;
plane.offset = cros::CameraBufferManager::GetPlaneOffset(handle, i);
plane.length = cros::CameraBufferManager::GetPlaneSize(handle, i);
}
return std::make_unique<FrameBuffer>(
std::make_unique<CrosFrameBufferData>(std::move(scopedHandle), planes));
}
PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION