summaryrefslogtreecommitdiff
path: root/src/android/mm/cros_camera_buffer.cpp
diff options
context:
space:
mode:
authorJacopo Mondi <jacopo@jmondi.org>2021-02-17 16:23:34 +0100
committerJacopo Mondi <jacopo@jmondi.org>2021-03-03 09:50:44 +0100
commitd8d6a78f223e14e3da7adf73ee54730ba0f1740b (patch)
tree5b8e64f12e8a760f6e7aa04780d7903a80fbc64e /src/android/mm/cros_camera_buffer.cpp
parent1427d124e86a134076f6abdd4a11b037e005fede (diff)
android: Introduce Chromium OS buffer manager
Introduce the CameraBuffer backend for the Chromium OS operating system and the associated meson option. The Chromium OS CameraBuffer implementation uses the cros::CameraBufferManager class to perform mapping of 1 plane and multiplane buffers and to retrieve size information. Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'src/android/mm/cros_camera_buffer.cpp')
-rw-r--r--src/android/mm/cros_camera_buffer.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/android/mm/cros_camera_buffer.cpp b/src/android/mm/cros_camera_buffer.cpp
new file mode 100644
index 00000000..7df4f47c
--- /dev/null
+++ b/src/android/mm/cros_camera_buffer.cpp
@@ -0,0 +1,128 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2021, Google Inc.
+ *
+ * cros_camera_buffer.cpp - Chromium OS buffer backend using CameraBufferManager
+ */
+
+#include "../camera_buffer.h"
+
+#include "libcamera/internal/log.h"
+
+#include "cros-camera/camera_buffer_manager.h"
+
+using namespace libcamera;
+
+LOG_DECLARE_CATEGORY(HAL)
+
+class CameraBuffer::Private : public Extensible::Private
+{
+ LIBCAMERA_DECLARE_PUBLIC(CameraBuffer)
+
+public:
+ Private(CameraBuffer *cameraBuffer,
+ buffer_handle_t camera3Buffer, int flags);
+ ~Private();
+
+ bool isValid() const { return valid_; }
+
+ unsigned int numPlanes() const;
+
+ Span<uint8_t> plane(unsigned int plane);
+
+ size_t jpegBufferSize(size_t maxJpegBufferSize) const;
+
+private:
+ cros::CameraBufferManager *bufferManager_;
+ buffer_handle_t handle_;
+ unsigned int numPlanes_;
+ bool valid_;
+ union {
+ void *addr;
+ android_ycbcr ycbcr;
+ } mem;
+
+ const uint8_t *planeAddr(unsigned int plane) const;
+ uint8_t *planeAddr(unsigned int plane);
+};
+
+CameraBuffer::Private::Private(CameraBuffer *cameraBuffer,
+ buffer_handle_t camera3Buffer, int flags)
+ : Extensible::Private(cameraBuffer), handle_(camera3Buffer),
+ numPlanes_(0), valid_(false)
+{
+ bufferManager_ = cros::CameraBufferManager::GetInstance();
+
+ bufferManager_->Register(camera3Buffer);
+
+ numPlanes_ = bufferManager_->GetNumPlanes(camera3Buffer);
+ switch (numPlanes_) {
+ case 1: {
+ int ret = bufferManager_->Lock(handle_, 0, 0, 0, 0, 0, &mem.addr);
+ if (ret) {
+ LOG(HAL, Error) << "Single plane buffer mapping failed";
+ return;
+ }
+ break;
+ }
+ case 2:
+ case 3: {
+ int ret = bufferManager_->LockYCbCr(handle_, 0, 0, 0, 0, 0,
+ &mem.ycbcr);
+ if (ret) {
+ LOG(HAL, Error) << "YCbCr buffer mapping failed";
+ return;
+ }
+ break;
+ }
+ default:
+ LOG(HAL, Error) << "Invalid number of planes: " << numPlanes_;
+ return;
+ }
+
+ valid_ = true;
+}
+
+CameraBuffer::Private::~Private()
+{
+ bufferManager_->Unlock(handle_);
+ bufferManager_->Deregister(handle_);
+}
+
+unsigned int CameraBuffer::Private::numPlanes() const
+{
+ return bufferManager_->GetNumPlanes(handle_);
+}
+
+Span<uint8_t> CameraBuffer::Private::plane(unsigned int plane)
+{
+ void *addr;
+
+ switch (numPlanes()) {
+ case 1:
+ addr = mem.addr;
+ break;
+ default:
+ switch (plane) {
+ case 1:
+ addr = mem.ycbcr.y;
+ break;
+ case 2:
+ addr = mem.ycbcr.cb;
+ break;
+ case 3:
+ addr = mem.ycbcr.cr;
+ break;
+ }
+ }
+
+ return { static_cast<uint8_t *>(addr),
+ bufferManager_->GetPlaneSize(handle_, plane) };
+}
+
+size_t CameraBuffer::Private::jpegBufferSize(size_t maxJpegBufferSize) const
+{
+ return bufferManager_->GetPlaneSize(handle_, 0);
+}
+
+PUBLIC_CAMERA_BUFFER_IMPLEMENTATION