summaryrefslogtreecommitdiff
path: root/test/signal-threads.cpp
AgeCommit message (Collapse)Author
2019-07-11test: Add test case for signal delivery across threadsLaurent Pinchart
The test case creates a receiver inheriting from Object, connects a signal to one of its slot, moves the receiver to a different thread, emits the signal and verifies that it gets delivered in the correct thread with the expected value. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
'#n30'>30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * generic_camera_buffer.cpp - Allocate FrameBuffer using gralloc API
 */

#include <dlfcn.h>
#include <memory>
#include <vector>

#include <libcamera/base/log.h>
#include <libcamera/base/shared_fd.h>

#include "libcamera/internal/formats.h"
#include "libcamera/internal/framebuffer.h"

#include <hardware/camera3.h>
#include <hardware/gralloc.h>
#include <hardware/hardware.h>

#include "../camera_device.h"
#include "../frame_buffer_allocator.h"
#include "../hal_framebuffer.h"

using namespace libcamera;

LOG_DECLARE_CATEGORY(HAL)

namespace {
class GenericFrameBufferData : public FrameBuffer::Private
{
	LIBCAMERA_DECLARE_PUBLIC(FrameBuffer)

public:
	GenericFrameBufferData(struct alloc_device_t *allocDevice,
			       buffer_handle_t handle,
			       const std::vector<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_);
	}

private:
	struct alloc_device_t *allocDevice_;
	const buffer_handle_t handle_;
};
} /* namespace */

class PlatformFrameBufferAllocator::Private : public Extensible::Private
{
	LIBCAMERA_DECLARE_PUBLIC(PlatformFrameBufferAllocator)

public:
	Private(CameraDevice *const cameraDevice)
		: cameraDevice_(cameraDevice),
		  hardwareModule_(nullptr),
		  allocDevice_(nullptr)
	{
		hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hardwareModule_);
		ASSERT(hardwareModule_);
	}

	~Private() override;

	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_;
};

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;
	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;
		return nullptr;
	}
	if (!handle) {
		LOG(HAL, Fatal) << "invalid buffer_handle_t";
		return nullptr;
	}

	/* This code assumes the planes are mapped consecutively. */
	const libcamera::PixelFormat pixelFormat =
		cameraDevice_->capabilities()->toPixelFormat(halPixelFormat);
	const auto &info = PixelFormatInfo::info(pixelFormat);
	std::vector<FrameBuffer::Plane> planes(info.numPlanes());

	SharedFD fd{ handle->data[0] };
	size_t offset = 0;
	for (auto [i, plane] : utils::enumerate(planes)) {
		const size_t planeSize = info.planeSize(size.height, i, stride);

		plane.fd = fd;
		plane.offset = offset;
		plane.length = planeSize;
		offset += planeSize;
	}

	return std::make_unique<HALFrameBuffer>(
		std::make_unique<GenericFrameBufferData>(
			allocDevice_, handle, planes),
		handle);
}

PUBLIC_FRAME_BUFFER_ALLOCATOR_IMPLEMENTATION