/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2021, Ideas on Board Oy * * KMS Sink */ #include "kms_sink.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "drm.h" KMSSink::KMSSink(const std::string &connectorName) : connector_(nullptr), crtc_(nullptr), plane_(nullptr), mode_(nullptr) { int ret = dev_.init(); if (ret < 0) return; /* * Find the requested connector. If no specific connector is requested, * pick the first connected connector or, if no connector is connected, * the first connector with unknown status. */ for (const DRM::Connector &conn : dev_.connectors()) { if (!connectorName.empty()) { if (conn.name() != connectorName) continue; connector_ = &conn; break; } if (conn.status() == DRM::Connector::Connected) { connector_ = &conn; break; } if (!connector_ && conn.status() == DRM::Connector::Unknown) connector_ = &conn; } if (!connector_) { if (!connectorName.empty()) std::cerr << "Connector " << connectorName << " not found" << std::endl; else std::cerr << "No connected connector found" << std::endl; return; } dev_.requestComplete.connect(this, &KMSSink::requestComplete); } void KMSSink::mapBuffer(libcamera::FrameBuffer *buffer) { std::array strides = {}; /* \todo Should libcamera report per-plane strides ? */ unsigned int uvStrideMultiplier; switch (format_) { case libcamera::formats::NV24: case libcamera::formats::NV42: uvStrideMultiplier = 4; break; case libcamera::formats::YUV420: case libcamera::formats::YVU420: case libcamera::formats::YUV422: uvStrideMultiplier = 1; break; default: uvStrideMultiplier = 2; break; } strides[0] = stride_; for (unsigned int i = 1; i < buffer->planes().size(); ++i) strides[i] = stride_ * uvStrideMultiplier / 2; std::unique_ptr drmBuffer = dev_.createFrameBuffer(*buffer, format_, size_, strides); if (!drmBuffer) return; buffers_.emplace(std::piecewise_construct, std::forward_as_tuple(buffer), std::forward_as_tuple(std::move(drmBuffer))); } int KMSSink::configure(const libcamera::CameraConfiguration &config) { if (!connector_) return -EINVAL; crtc_ = nullptr; plane_ = nullptr; mode_ = nullptr; const libcamera::StreamConfiguration &cfg = config.at(0); /* Find the best mode for the stream size. */ const std::vector &modes = connector_->modes(); unsigned int cfgArea = cfg.size.width * cfg.size.height; unsigned int bestDistance = UINT_MAX; for (const DRM::Mode &mode : modes) { unsigned int modeArea = mode.hdisplay * mode.vdisplay; unsigned int distance = modeArea > cfgArea ? modeArea - cfgArea : cfgArea - modeArea; if (distance < bestDistance) { mode_ = &mode; bestDistance = distance; /* * If the sizes match exactly, there will be no better * match. */ if (distance == 0) break; } } if (!mode_) { std::cerr << "No modes\n"; return -EINVAL; } int ret = configurePipeline(cfg.pixelFormat); if (ret < 0) return ret; size_ = cfg.size; stride_ = cfg.stride; /* Configure color space. */ colorEncoding_ = std::nullopt; colorRange_ = std::nullopt; if (cfg.colorSpace->ycbcrEncoding == libcamera::ColorSpace::YcbcrEncoding::None) return 0; /* * The encoding and range enums are defined in the kernel but not * exposed in public headers. */ enum drm_color_encoding { DRM_COLOR_YCBCR_BT601, DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_BT2020, }; enum drm_color_range { DRM_COLOR_YCBCR_LIMITED_RANGE, DRM_COLOR_YCBCR_FULL_RANGE, }; const DRM::Property *colorEncoding = plane_->property("COLOR_ENCODING"); const DRM::Property *colorRange = plane_->property("COLOR_RANGE"); if (colorEncoding) { drm_color_encoding encoding; switch (cfg.colorSpace->ycbcrEncoding) { case libcamera::ColorSpace::YcbcrEncoding::Rec601: default: encoding = DRM_COLOR_YCBCR_BT601; break; case libcamera::ColorSpace::YcbcrEncoding::Rec709: encoding = DRM_COLOR_YCBCR_BT709; break; case libcamera::ColorSpace::YcbcrEncoding::Rec2020: encoding = DRM_COLOR_YCBCR_BT2020; break; } for (const auto &[id, name] : colorEncoding->enums()) { if (id == encoding) { colorEncoding_ = encoding; break; } } } if (colorRange) { drm_color_range range; switch (cfg.colorSpace->range) { case libcamera::ColorSpace::Range::Limited: default: range = DRM_COLOR_YCBCR_LIMITED_RANGE; break; case libcamera::ColorSpace::Range::Full: range = DRM_COLOR_YCBCR_FULL_RANGE; break; } for (const auto &[id, name] : colorRange->enums()) { if (id == range) { colorRange_ = range; break; } } } if (!colorEncoding_ || !colorRange_) std::cerr << "Color space " << cfg.colorSpace->toString() << " not supported by the display device." << " Colors may be wrong." << std::endl; return 0; } int KMSSink::selectPipeline(const libcamera::PixelFormat &format) { /* * If the requested format has an alpha channel, also consider the X * variant. */ libcamera::PixelFormat xFormat; switch (format) { case libcamera::formats::ABGR8888: xFormat = libcamera::formats::XBGR8888; break; case libcamera::formats::ARGB8888: xFormat = libcamera::formats::XRGB8888; break; case libcamera::formats::BGRA8888: xFormat = libcamera::formats::BGRX8888; break; case libcamera::formats::RGBA8888: xFormat = libcamera::formats::RGBX8888; break; } /* * Find a CRTC and plane suitable for the request format and the * connector at the end of the pipeline. Restrict the search to primary * planes for now. */ for (const DRM::Encoder *encoder : connector_->encoders()) { for (const DRM::Crtc *crtc : encoder->possibleCrtcs()) { for (const DRM::Plane *plane : crtc->planes()) { if (plane->type() != DRM::Plane::TypePrimary) continue; if (plane->supportsFormat(format)) { crtc_ = crtc; plane_ = plane; format_ = format; return 0; } if (plane->supportsFormat(xFormat)) { crtc_ = crtc; plane_ = plane; format_ = xFormat; return 0; } } } } return -EPIPE; } int KMSSink::configurePipeline(const libcamera::PixelFormat &format) { const int ret = selectPipeline(format); if (ret) { std::cerr << "Unable to find display pipeline for format " << format << std::endl; return ret; } std::cout << "Using KMS plane " << plane_->id() << ", CRTC " << crtc_->id() << ", connector " << connector_->name() << " (" << connector_->id() << "), mode " << mode_->hdisplay << "x" << mode_->vdisplay << "@" << mode_->vrefresh << std::endl; return 0; } int KMSSink::start() { int ret = FrameSink::start(); if (ret < 0) return ret; /* Disable all CRTCs and planes to start from a known valid state. */ DRM::AtomicRequest request(&dev_); for (const DRM::Crtc &crtc : dev_.crtcs()) request.addProperty(&crtc, "ACTIVE", 0); for (const DRM::Plane &plane : dev_.planes()) { request.addProperty(&plane, "CRTC_ID", 0); request.addProperty(&plane, "FB_ID", 0); } ret = request.commit(DRM::AtomicRequest::FlagAllowModeset); if (ret < 0) { std::cerr << "Failed to disable CRTCs and planes: " << strerror(-ret) << std::endl; return ret; } return 0; } int KMSSink::stop() { /* Display pipeline. */ DRM::AtomicRequest request(&dev_); request.addProperty(connector_, "CRTC_ID", 0); request.addProperty(crtc_, "ACTIVE", 0); request.addProperty(crtc_, "MODE_ID", 0); request.addProperty(plane_, "CRTC_ID", 0); request.addProperty(plane_, "FB_ID", 0); int ret = request.commit(DRM::AtomicRequest::FlagAllowModeset); if (ret < 0) { std::cerr << "Failed to stop display pipeline: " << strerror(-ret) << std::endl; return ret; } /* Free all buffers. */ pending_.reset(); queued_.reset(); active_.reset(); buffers_.clear(); return FrameSink::stop(); } bool KMSSink::testModeSet(DRM::FrameBuffer *drmBuffer, const libcamera::Rectangle &src, const libcamera::Rectangle &dst) { DRM::AtomicRequest drmRequest{ &dev_ }; drmRequest.addProperty(connector_, "CRTC_ID", crtc_->id()); drmRequest.addProperty(crtc_, "ACTIVE", 1); drmRequest.addProperty(crtc_, "MODE_ID", mode_->toBlob(&dev_)); drmRequest.addProperty(plane_, "CRTC_ID", crtc_->id()); drmRequest.addProperty(plane_, "FB_ID", drmBuffer->id()); drmRequest.addProperty(plane_, "SRC_X", src.x << 16); drmRequest.addProperty(plane_, "SRC_Y", src.y << 16); drmRequest.addProperty(plane_, "SRC_W", src.width << 16); drmRequest.addProperty(plane_, "SRC_H", src.height << 16); drmRequest.addProperty(plane_, "CRTC_X", dst.x); drmRequest.addProperty(plane_, "CRTC_Y", dst.y); drmRequest.addProperty(plane_, "CRTC_W", dst.width); drmRequest.addProperty(plane_, "CRTC_H", dst.height); return !drmRequest.commit(DRM::AtomicRequest::FlagAllowModeset | DRM::AtomicRequest::FlagTestOnly); } bool KMSSink::setupComposition(DRM::FrameBuffer *drmBuffer) { /* * Test composition options, from most to least desirable, to select the * best one. */ const libcamera::Rectangle framebuffer{ size_ }; const libcamera::Rectangle display{ 0, 0, mode_->hdisplay, mode_->vdisplay }; /* 1. Scale the frame buffer to full screen, preserving aspect ratio. */ libcamera::Rectangle src = framebuffer; libcamera::Rectangle dst = display.size().boundedToAspectRatio(framebuffer.size()) .centeredTo(display.center()); if (testModeSet(drmBuffer, src, dst)) { std::cout << "KMS: full-screen scaled output, square pixels" << std::endl; src_ = src; dst_ = dst; return true; } /* * 2. Scale the frame buffer to full screen, without preserving aspect * ratio. */ src = framebuffer; dst = display; if (testModeSet(drmBuffer, src, dst)) { std::cout << "KMS: full-screen scaled output, non-square pixels" << std::endl; src_ = src; dst_ = dst; return true; } /* 3. Center the frame buffer on the display. */ src = display.size().centeredTo(framebuffer.center()).boundedTo(framebuffer); dst = framebuffer.size().centeredTo(display.center()).boundedTo(display); if (testModeSet(drmBuffer, src, dst)) { std::cout << "KMS: centered output" << std::endl; src_ = src; dst_ = dst; return true; } /* 4. Align the frame buffer on the top-left of the display. */ src = framebuffer.boundedTo(display); dst = display.boundedTo(framebuffer); if (testModeSet(drmBuffer, src, dst)) { std::cout << "KMS: top-left aligned output" << std::endl; src_ = src; dst_ = dst; return true; } return false; } bool KMSSink::processRequest(libcamera::Request *camRequest) { /* * Perform a very crude rate adaptation by simply dropping the request * if the display queue is full. */ if (pending_) return true; libcamera::FrameBuffer *buffer = camRequest->buffers().begin()->second; auto iter = buffers_.find(buffer); if (iter == buffers_.end()) return true; DRM::FrameBuffer *drmBuffer = iter->second.get(); unsigned int flags = DRM::AtomicRequest::FlagAsync; std::unique_ptr drmRequest = std::make_unique(&dev_); drmRequest->addProperty(plane_, "FB_ID", drmBuffer->id()); if (!active_ && !queued_) { /* Enable the display pipeline on the first frame. */ if (!setupComposition(drmBuffer)) { std::cerr << "Failed to setup composition" << std::endl; return true; } drmRequest->addProperty(connector_, "CRTC_ID", crtc_->id()); drmRequest->addProperty(crtc_, "ACTIVE", 1); drmRequest->addProperty(crtc_, "MODE_ID", mode_->toBlob(&dev_)); drmRequest->addProperty(plane_, "CRTC_ID", crtc_->id()); drmRequest->addProperty(plane_, "SRC_X", src_.x << 16); drmRequest->addProperty(plane_, "SRC_Y", src_.y << 16); drmRequest->addProperty(plane_, "SRC_W", src_.width << 16); drmRequest->addProperty(plane_, "SRC_H", src_.height << 16); drmRequest->addProperty(plane_, "CRTC_X", dst_.x); drmRequest->addProperty(plane_, "CRTC_Y", dst_.y); drmRequest->addProperty(plane_, "CRTC_W", dst_.width); drmRequest->addProperty(plane_, "CRTC_H", dst_.height); if (colorEncoding_) drmRequest->addProperty(plane_, "COLOR_ENCODING", *colorEncoding_); if (colorRange_) drmRequest->addProperty(plane_, "COLOR_RANGE", *colorRange_); flags |= DRM::AtomicRequest::FlagAllowModeset; } pending_ = std::make_unique(std::move(drmRequest), camRequest); std::lock_guard lock(lock_); if (!queued_) { int ret = pending_->drmRequest_->commit(flags); if (ret < 0) { std::cerr << "Failed to commit atomic request: " << strerror(-ret) << std::endl; /* \todo Implement error handling */ } queued_ = std::move(pending_); } return false; } void KMSSink::requestComplete([[maybe_unused]] DRM::AtomicRequest *request) { std::lock_guard lock(lock_); assert(queued_ && queued_->drmRequest_.get() == request); /* Complete the active request, if any. */ if (active_) requestProcessed.emit(active_->camRequest_); /* The queued request becomes active. */ active_ = std::move(queued_); /* Queue the pending request, if any. */ if (pending_) { pending_->drmRequest_->commit(DRM::AtomicRequest::FlagAsync); queued_ = std::move(pending_); } } 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * controls.h - Control handling
 */

#ifndef __LIBCAMERA_CONTROLS_H__
#define __LIBCAMERA_CONTROLS_H__

#include <assert.h>
#include <stdint.h>
#include <string>
#include <unordered_map>
#include <vector>

#include <libcamera/base/class.h>
#include <libcamera/base/span.h>

#include <libcamera/geometry.h>

namespace libcamera {

class ControlValidator;

enum ControlType {
	ControlTypeNone,
	ControlTypeBool,
	ControlTypeByte,
	ControlTypeInteger32,
	ControlTypeInteger64,
	ControlTypeFloat,
	ControlTypeString,
	ControlTypeRectangle,
	ControlTypeSize,
};

namespace details {

template<typename T>
struct control_type {
};

template<>
struct control_type<void> {
	static constexpr ControlType value = ControlTypeNone;
};

template<>
struct control_type<bool> {
	static constexpr ControlType value = ControlTypeBool;
};

template<>
struct control_type<uint8_t> {
	static constexpr ControlType value = ControlTypeByte;
};

template<>
struct control_type<int32_t> {
	static constexpr ControlType value = ControlTypeInteger32;
};

template<>
struct control_type<int64_t> {
	static constexpr ControlType value = ControlTypeInteger64;
};

template<>
struct control_type<float> {
	static constexpr ControlType value = ControlTypeFloat;
};

template<>
struct control_type<std::string> {
	static constexpr ControlType value = ControlTypeString;
};

template<>
struct control_type<Rectangle> {
	static constexpr ControlType value = ControlTypeRectangle;
};

template<>
struct control_type<Size> {
	static constexpr ControlType value = ControlTypeSize;
};

template<typename T, std::size_t N>
struct control_type<Span<T, N>> : public control_type<std::remove_cv_t<T>> {
};

} /* namespace details */

class ControlValue
{
public:
	ControlValue();

#ifndef __DOXYGEN__
	template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
						       details::control_type<T>::value &&
						       !std::is_same<std::string, std::remove_cv_t<T>>::value,
						       std::nullptr_t> = nullptr>
	ControlValue(const T &value)
		: type_(ControlTypeNone), numElements_(0)
	{
		set(details::control_type<std::remove_cv_t<T>>::value, false,
		    &value, 1, sizeof(T));
	}

	template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
						       std::is_same<std::string, std::remove_cv_t<T>>::value,
						       std::nullptr_t> = nullptr>
#else
	template<typename T>
#endif
	ControlValue(const T &value)
		: type_(ControlTypeNone), numElements_(0)
	{
		set(details::control_type<std::remove_cv_t<T>>::value, true,
		    value.data(), value.size(), sizeof(typename T::value_type));
	}

	~ControlValue();

	ControlValue(const ControlValue &other);
	ControlValue &operator=(const ControlValue &other);

	ControlType type() const { return type_; }
	bool isNone() const { return type_ == ControlTypeNone; }
	bool isArray() const { return isArray_; }
	std::size_t numElements() const { return numElements_; }
	Span<const uint8_t> data() const;
	Span<uint8_t> data();

	std::string toString() const;

	bool operator==(const ControlValue &other) const;
	bool operator!=(const ControlValue &other) const
	{
		return !(*this == other);
	}

#ifndef __DOXYGEN__
	template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
						       !std::is_same<std::string, std::remove_cv_t<T>>::value,
						       std::nullptr_t> = nullptr>
	T get() const
	{
		assert(type_ == details::control_type<std::remove_cv_t<T>>::value);
		assert(!isArray_);

		return *reinterpret_cast<const T *>(data().data());
	}

	template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
						       std::is_same<std::string, std::remove_cv_t<T>>::value,
						       std::nullptr_t> = nullptr>
#else
	template<typename T>
#endif
	T get() const
	{
		assert(type_ == details::control_type<std::remove_cv_t<T>>::value);
		assert(isArray_);

		using V = typename T::value_type;
		const V *value = reinterpret_cast<const V *>(data().data());
		return { value, numElements_ };
	}

#ifndef __DOXYGEN__
	template<typename T, typename std::enable_if_t<!details::is_span<T>::value &&
						       !std::is_same<std::string, std::remove_cv_t<T>>::value,
						       std::nullptr_t> = nullptr>
	void set(const T &value)
	{
		set(details::control_type<std::remove_cv_t<T>>::value, false,
		    reinterpret_cast<const void *>(&value), 1, sizeof(T));
	}

	template<typename T, typename std::enable_if_t<details::is_span<T>::value ||
						       std::is_same<std::string, std::remove_cv_t<T>>::value,
						       std::nullptr_t> = nullptr>
#else
	template<typename T>
#endif
	void set(const T &value)
	{
		set(details::control_type<std::remove_cv_t<T>>::value, true,
		    value.data(), value.size(), sizeof(typename T::value_type));
	}

	void reserve(ControlType type, bool isArray = false,
		     std::size_t numElements = 1);

private:
	ControlType type_ : 8;
	bool isArray_;
	std::size_t numElements_ : 32;
	union {
		uint64_t value_;
		void *storage_;
	};

	void release();
	void set(ControlType type, bool isArray, const void *data,
		 std::size_t numElements, std::size_t elementSize);
};

class ControlId
{
public:
	ControlId(unsigned int id, const std::string &name, ControlType type)
		: id_(id), name_(name), type_(type)
	{
	}

	unsigned int id() const { return id_; }
	const std::string &name() const { return name_; }
	ControlType type() const { return type_; }

private:
	LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId)

	unsigned int id_;
	std::string name_;
	ControlType type_;
};

static inline bool operator==(unsigned int lhs, const ControlId &rhs)
{
	return lhs == rhs.id();
}

static inline bool operator!=(unsigned int lhs, const ControlId &rhs)
{
	return !(lhs == rhs);
}

static inline bool operator==(const ControlId &lhs, unsigned int rhs)
{
	return lhs.id() == rhs;
}

static inline bool operator!=(const ControlId &lhs, unsigned int rhs)
{
	return !(lhs == rhs);
}

template<typename T>
class Control : public ControlId
{
public:
	using type = T;

	Control(unsigned int id, const char *name)
		: ControlId(id, name, details::control_type<std::remove_cv_t<T>>::value)
	{
	}

private:
	LIBCAMERA_DISABLE_COPY_AND_MOVE(Control)
};

class ControlInfo
{
public:
	explicit ControlInfo(const ControlValue &min = 0,
			     const ControlValue &max = 0,
			     const ControlValue &def = 0);
	explicit ControlInfo(Span<const ControlValue> values,
			     const ControlValue &def = {});

	const ControlValue &min() const { return min_; }
	const ControlValue &max() const { return max_; }
	const ControlValue &def() const { return def_; }
	const std::vector<ControlValue> &values() const { return values_; }

	std::string toString() const;

	bool operator==(const ControlInfo &other) const
	{
		return min_ == other.min_ && max_ == other.max_;
	}

	bool operator!=(const ControlInfo &other) const
	{
		return !(*this == other);
	}

private:
	ControlValue min_;
	ControlValue max_;
	ControlValue def_;
	std::vector<ControlValue> values_;
};

using ControlIdMap = std::unordered_map<unsigned int, const ControlId *>;

class ControlInfoMap : private std::unordered_map<const ControlId *, ControlInfo>
{
public:
	using Map = std::unordered_map<const ControlId *, ControlInfo>;

	ControlInfoMap() = default;
	ControlInfoMap(const ControlInfoMap &other) = default;
	ControlInfoMap(std::initializer_list<Map::value_type> init);
	ControlInfoMap(Map &&info);

	ControlInfoMap &operator=(const ControlInfoMap &other) = default;
	ControlInfoMap &operator=(std::initializer_list<Map::value_type> init);
	ControlInfoMap &operator=(Map &&info);

	using Map::key_type;
	using Map::mapped_type;
	using Map::value_type;
	using Map::size_type;
	using Map::iterator;
	using Map::const_iterator;

	using Map::begin;
	using Map::cbegin;
	using Map::end;
	using Map::cend;
	using Map::at;
	using Map::empty;
	using Map::size;
	using Map::count;
	using Map::find;

	mapped_type &at(unsigned int key);
	const mapped_type &at(unsigned int key) const;
	size_type count(unsigned int key) const;
	iterator find(unsigned int key);
	const_iterator find(unsigned int key) const;

	const ControlIdMap &idmap() const { return idmap_; }

private:
	void generateIdmap();

	ControlIdMap idmap_;
};

class ControlList
{
private:
	using ControlListMap = std::unordered_map<unsigned int, ControlValue>;

public:
	ControlList();
	ControlList(const ControlIdMap &idmap, ControlValidator *validator = nullptr);
	ControlList(const ControlInfoMap &infoMap, ControlValidator *validator = nullptr);

	using iterator = ControlListMap::iterator;
	using const_iterator = ControlListMap::const_iterator;

	iterator begin() { return controls_.begin(); }
	iterator end() { return controls_.end(); }
	const_iterator begin() const { return controls_.begin(); }
	const_iterator end() const { return controls_.end(); }

	bool empty() const { return controls_.empty(); }
	std::size_t size() const { return controls_.size(); }

	void clear() { controls_.clear(); }
	void merge(const ControlList &source);

	bool contains(const ControlId &id) const;
	bool contains(unsigned int id) const;

	template<typename T>
	T get(const Control<T> &ctrl) const
	{
		const ControlValue *val = find(ctrl.id());
		if (!val)
			return T{};

		return val->get<T>();
	}

	template<typename T, typename V>
	void set(const Control<T> &ctrl, const V &value)
	{
		ControlValue *val = find(ctrl.id());
		if (!val)
			return;

		val->set<T>(value);
	}

	template<typename T, typename V>