/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * format_convert.h - qcam - Convert buffer to RGB */ #ifndef __QCAM_FORMAT_CONVERTER_H__ #define __QCAM_FORMAT_CONVERTER_H__ #include #include #include class QImage; class FormatConverter { public: int configure(const libcamera::PixelFormat &format, const QSize &size); void convert(const unsigned char *src, size_t size, QImage *dst); private: enum FormatFamily { MJPEG, NV, RGB, YUV, }; void convertNV(const unsigned char *src, unsigned char *dst); void convertRGB(const unsigned char *src, unsigned char *dst); void convertYUV(const unsigned char *src, unsigned char *dst); libcamera::PixelFormat format_; unsigned int width_; unsigned int height_; enum FormatFamily formatFamily_; /* NV parameters */ unsigned int horzSubSample_; unsigned int vertSubSample_; bool nvSwap_; /* RGB parameters */ unsigned int bpp_; unsigned int r_pos_; unsigned int g_pos_; unsigned int b_pos_; /* YUV parameters */ unsigned int y_pos_; unsigned int cb_pos_; }; #endif /* __QCAM_FORMAT_CONVERTER_H__ */ hread * * The CameraWorker class wraps a Worker that runs on an internal thread * and schedules processing of CaptureRequest through it. */ CameraWorker::CameraWorker() { worker_.moveToThread(this); } void CameraWorker::start() { Thread::start(); } void CameraWorker::stop() { exit(); wait(); } void CameraWorker::run() { exec(); dispatchMessages(Message::Type::InvokeMessage); } void CameraWorker::queueRequest(CaptureRequest *request) { /* Async process the request on the worker which runs its own thread. */ worker_.invokeMethod(&Worker::processRequest, ConnectionTypeQueued, request); } /* * \class CameraWorker::Worker * \brief Process a CaptureRequest handling acquisition fences */ int CameraWorker::Worker::waitFence(int fence) { /* * \todo Better characterize the timeout. Currently equal to the one * used by the Rockchip Camera HAL on ChromeOS. */ constexpr unsigned int timeoutMs = 300; struct pollfd fds = { fence, POLLIN, 0 }; do { int ret = poll(&fds, 1, timeoutMs); if (ret == 0) return -ETIME; if (ret > 0) { if (fds.revents & (POLLERR | POLLNVAL)) return -EINVAL; return 0; } } while (errno == EINTR || errno == EAGAIN); return -errno; } void CameraWorker::Worker::processRequest(CaptureRequest *request) { /* Wait on all fences before queuing the Request. */ for (int fence : request->fences()) { if (fence == -1) continue; int ret = waitFence(fence); close(fence); if (ret < 0) { LOG(HAL, Error) << "Failed waiting for fence: " << fence << ": " << strerror(-ret); return; } } request->queue(); }