summaryrefslogtreecommitdiff
path: root/include/android/metadata
diff options
context:
space:
mode:
Diffstat (limited to 'include/android/metadata')
0 files changed, 0 insertions, 0 deletions
href='#n36'>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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2018, Google Inc.
 *
 * vimc.cpp - Pipeline handler for the vimc device
 */

#include <libcamera/camera.h>
#include <libcamera/request.h>
#include <libcamera/stream.h>

#include "device_enumerator.h"
#include "log.h"
#include "media_device.h"
#include "pipeline_handler.h"
#include "v4l2_device.h"

namespace libcamera {

LOG_DEFINE_CATEGORY(VIMC)

class PipelineHandlerVimc : public PipelineHandler
{
public:
	PipelineHandlerVimc(CameraManager *manager);
	~PipelineHandlerVimc();

	std::map<Stream *, StreamConfiguration>
	streamConfiguration(Camera *camera,
			    std::vector<Stream *> &streams) override;
	int configureStreams(Camera *camera,
			     std::map<Stream *, StreamConfiguration> &config) override;

	int allocateBuffers(Camera *camera, Stream *stream) override;
	int freeBuffers(Camera *camera, Stream *stream) override;

	int start(const Camera *camera) override;
	void stop(const Camera *camera) override;

	int queueRequest(const Camera *camera, Request *request) override;

	bool match(DeviceEnumerator *enumerator);

private:
	std::shared_ptr<MediaDevice> media_;
	V4L2Device *video_;
	Stream stream_;
};

PipelineHandlerVimc::PipelineHandlerVimc(CameraManager *manager)
	: PipelineHandler(manager), media_(nullptr), video_(nullptr)
{
}

PipelineHandlerVimc::~PipelineHandlerVimc()
{
	delete video_;

	if (media_)
		media_->release();
}

std::map<Stream *, StreamConfiguration>
PipelineHandlerVimc::streamConfiguration(Camera *camera,
				     std::vector<Stream *> &streams)
{
	std::map<Stream *, StreamConfiguration> configs;

	StreamConfiguration config{};

	LOG(VIMC, Debug) << "Retrieving default format";
	config.width = 640;
	config.height = 480;
	config.pixelFormat = V4L2_PIX_FMT_RGB24;
	config.bufferCount = 4;

	configs[&stream_] = config;

	return configs;
}

int PipelineHandlerVimc::configureStreams(Camera *camera,
				      std::map<Stream *, StreamConfiguration> &config)
{
	StreamConfiguration *cfg = &config[&stream_];
	int ret;

	LOG(VIMC, Debug) << "Configure the camera for resolution "
			 << cfg->width << "x" << cfg->height;

	V4L2DeviceFormat format = {};
	format.width = cfg->width;
	format.height = cfg->height;
	format.fourcc = cfg->pixelFormat;

	ret = video_->setFormat(&format);
	if (ret)
		return ret;

	if (format.width != cfg->width ||
	    format.height != cfg->height ||
	    format.fourcc != cfg->pixelFormat)
		return -EINVAL;

	return 0;
}

int PipelineHandlerVimc::allocateBuffers(Camera *camera, Stream *stream)
{
	const StreamConfiguration &cfg = stream->configuration();

	LOG(VIMC, Debug) << "Requesting " << cfg.bufferCount << " buffers";

	return video_->exportBuffers(&stream->bufferPool());
}

int PipelineHandlerVimc::freeBuffers(Camera *camera, Stream *stream)
{
	return video_->releaseBuffers();
}

int PipelineHandlerVimc::start(const Camera *camera)
{
	return video_->streamOn();
}

void PipelineHandlerVimc::stop(const Camera *camera)
{
	video_->streamOff();
}

int PipelineHandlerVimc::queueRequest(const Camera *camera, Request *request)
{
	Buffer *buffer = request->findBuffer(&stream_);
	if (!buffer) {
		LOG(VIMC, Error)
			<< "Attempt to queue request with invalid stream";

		return -ENOENT;
	}

	video_->queueBuffer(buffer);

	return 0;
}

bool PipelineHandlerVimc::match(DeviceEnumerator *enumerator)
{
	DeviceMatch dm("vimc");

	dm.add("Raw Capture 0");
	dm.add("Raw Capture 1");
	dm.add("RGB/YUV Capture");
	dm.add("Sensor A");
	dm.add("Sensor B");
	dm.add("Debayer A");
	dm.add("Debayer B");
	dm.add("RGB/YUV Input");
	dm.add("Scaler");

	media_ = enumerator->search(dm);
	if (!media_)
		return false;

	media_->acquire();

	video_ = new V4L2Device(media_->getEntityByName("Raw Capture 1"));

	if (video_->open()) {
		media_->release();
		return false;
	}

	std::vector<Stream *> streams{ &stream_ };
	std::shared_ptr<Camera> camera = Camera::create(this, "VIMC Sensor B",
							streams);
	registerCamera(std::move(camera));

	return true;
}

REGISTER_PIPELINE_HANDLER(PipelineHandlerVimc);

} /* namespace libcamera */