summaryrefslogtreecommitdiff
path: root/include/android/system
AgeCommit message (Collapse)Author
2019-08-12include: android: Add SPDX tagsJacopo Mondi
Add SPDX indentifier to Apache-2.0 licensed android headers. Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2019-08-12include: android: Add Android headers from CrosJacopo Mondi
Copy the Android Camera3 HAL headers from the ChromiumOS build system and define a new inclusion directive in the meson build system for them. The header files have been copied from: https://chromium.googlesource.com/chromiumos/platform2 at revision 9e65ddd2c496e712f005ada9715decd2ff8e4a03 and provide: 1) Android CameraHAL3 HAL headers in include/android/hardware/ 2) The Android system headers in include/android/system/ 3) The Android camera metadata headers in include/android/metadata/ The original path in the Cros platform2/ repository is, respectively: camera/android/header_files/include/hardware camera/android/header_files/include/system camera/android/libcamera_metadata/include/ Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
'#n52'>52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
 */

#include "py_camera_manager.h"

#include <errno.h>
#include <memory>
#include <sys/eventfd.h>
#include <system_error>
#include <unistd.h>
#include <vector>

#include "py_main.h"

namespace py = pybind11;

using namespace libcamera;

PyCameraManager::PyCameraManager()
{
	LOG(Python, Debug) << "PyCameraManager()";

	cameraManager_ = std::make_unique<CameraManager>();

	int fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
	if (fd == -1)
		throw std::system_error(errno, std::generic_category(),
					"Failed to create eventfd");

	eventFd_ = UniqueFD(fd);

	int ret = cameraManager_->start();
	if (ret)
		throw std::system_error(-ret, std::generic_category(),
					"Failed to start CameraManager");
}

PyCameraManager::~PyCameraManager()
{
	LOG(Python, Debug) << "~PyCameraManager()";
}

py::list PyCameraManager::cameras()
{
	/*
	 * Create a list of Cameras, where each camera has a keep-alive to
	 * CameraManager.
	 */
	py::list l;

	for (auto &camera : cameraManager_->cameras()) {
		py::object py_cm = py::cast(this);
		py::object py_cam = py::cast(camera);
		py::detail::keep_alive_impl(py_cam, py_cm);
		l.append(py_cam);
	}

	return l;
}

std::vector<py::object> PyCameraManager::getReadyRequests()
{
	int ret = readFd();

	if (ret == -EAGAIN)
		return std::vector<py::object>();

	if (ret != 0)
		throw std::system_error(-ret, std::generic_category());

	std::vector<py::object> py_reqs;

	for (Request *request : getCompletedRequests()) {
		py::object o = py::cast(request);
		/* Decrease the ref increased in Camera.queue_request() */
		o.dec_ref();
		py_reqs.push_back(o);
	}

	return py_reqs;
}

/* Note: Called from another thread */
void PyCameraManager::handleRequestCompleted(Request *req)
{
	pushRequest(req);
	writeFd();
}

void PyCameraManager::writeFd()
{
	uint64_t v = 1;

	size_t s = write(eventFd_.get(), &v, 8);
	/*
	 * We should never fail, and have no simple means to manage the error,
	 * so let's log a fatal error.
	 */
	if (s != 8)
		LOG(Python, Fatal) << "Unable to write to eventfd";
}

int PyCameraManager::readFd()
{
	uint8_t buf[8];

	ssize_t ret = read(eventFd_.get(), buf, 8);

	if (ret == 8)
		return 0;
	else if (ret < 0)
		return -errno;
	else
		return -EIO;
}

void PyCameraManager::pushRequest(Request *req)
{
	MutexLocker guard(completedRequestsMutex_);
	completedRequests_.push_back(req);
}

std::vector<Request *> PyCameraManager::getCompletedRequests()
{
	std::vector<Request *> v;
	MutexLocker guard(completedRequestsMutex_);
	swap(v, completedRequests_);
	return v;
}