summaryrefslogtreecommitdiff
path: root/src/qcam/assets/shader
AgeCommit message (Collapse)Author
2020-11-07qcam: viewfinder_gl: Add support for RGB formatsLaurent Pinchart
Add support for 24-bit and 32-bit RGB formats. The fragment samples the texture and reorders the components, using a pattern set through the RGB_PATTERN macro. The pattern stores the shader vec4 element indices (named {r, g, b, a} by convention, for elements 0 to 3) to be extracted from the texture samples, when interpreted by OpenGL as RGBA. Note that, as textures are created with GL_UNSIGNED_BYTE, the RGBA order corresponds to bytes in memory, while the libcamera formats are named based on the components order in a 32-bit word stored in memory in little endian format. An alternative to manual reordering in the shader would be to set the texture swizzling mask. This is however not available in OpenGL ES before version 3.0, which we don't mandate at the moment. Only the BGR888 and RGB888 formats have been tested, with the vimc pipeline handler. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Andrey Konovalov <andrey.konovalov@linaro.org> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-11-07qcam: viewfinder_gl: Rename YUV.vert to identity.vertLaurent Pinchart
In preparation for RGB formats support, rename the identity vertex shader from YUV.vert to identity.vert. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Andrey Konovalov <andrey.konovalov@linaro.org> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-10-04qcam: viewfinder_gl: Add shader to render packed YUV formatsLaurent Pinchart
The shader supports all 4 packed 8-bit YUV variants. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Acked-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-10-04qcam: viewfinder_gl: Merge the semi-planar UV and VU shadersLaurent Pinchart
Use macros to select the U and V pattern, to avoid code duplication between the two semi-planar shaders. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-10-04qcam: viewfinder_gl: Rename shader filesLaurent Pinchart
Rename shader files to prepare for packed YUYV support: - The NV prefix isn't a good match for packed (or for 3-planar) formats, replace it with a YUV prefix - Use .frag and .vert extensions to differentiate between fragment and vertex shaders Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-10-04qcam: Remove unneeded './' file prefix in *.qrcLaurent Pinchart
There's no need to prefix files in the local directory with './'. Drop it. While at it, add indentation to make the *.qrc files more readable. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
2020-09-15qcam: Add OpenGL shader code as Qt resourceShow Liu
Add OpenGL fragment and vertex shaders to convert two- and tri-planar YUV formats to RGB. This will be used to accelerate YUV image rendering. Signed-off-by: Show Liu <show.liu@linaro.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
'#n170'>170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * v4l2_videodevice.h - V4L2 Video Device
 */
#ifndef __LIBCAMERA_V4L2_VIDEODEVICE_H__
#define __LIBCAMERA_V4L2_VIDEODEVICE_H__

#include <atomic>
#include <memory>
#include <stdint.h>
#include <string>
#include <vector>

#include <linux/videodev2.h>

#include <libcamera/buffer.h>
#include <libcamera/geometry.h>
#include <libcamera/pixelformats.h>
#include <libcamera/signal.h>

#include "formats.h"
#include "log.h"
#include "v4l2_device.h"
#include "v4l2_pixelformat.h"

namespace libcamera {

class EventNotifier;
class FileDescriptor;
class MediaDevice;
class MediaEntity;

struct V4L2Capability final : v4l2_capability {
	const char *driver() const
	{
		return reinterpret_cast<const char *>(v4l2_capability::driver);
	}
	const char *card() const
	{
		return reinterpret_cast<const char *>(v4l2_capability::card);
	}
	const char *bus_info() const
	{
		return reinterpret_cast<const char *>(v4l2_capability::bus_info);
	}
	unsigned int device_caps() const
	{
		return capabilities & V4L2_CAP_DEVICE_CAPS
				    ? v4l2_capability::device_caps
				    : v4l2_capability::capabilities;
	}
	bool isMultiplanar() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
					V4L2_CAP_VIDEO_OUTPUT_MPLANE |
					V4L2_CAP_VIDEO_M2M_MPLANE);
	}
	bool isCapture() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
					V4L2_CAP_VIDEO_CAPTURE_MPLANE |
					V4L2_CAP_META_CAPTURE);
	}
	bool isOutput() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_OUTPUT |
					V4L2_CAP_VIDEO_OUTPUT_MPLANE |
					V4L2_CAP_META_OUTPUT);
	}
	bool isVideo() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
					V4L2_CAP_VIDEO_CAPTURE_MPLANE |
					V4L2_CAP_VIDEO_OUTPUT |
					V4L2_CAP_VIDEO_OUTPUT_MPLANE);
	}
	bool isM2M() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_M2M |
					V4L2_CAP_VIDEO_M2M_MPLANE);
	}
	bool isMeta() const
	{
		return device_caps() & (V4L2_CAP_META_CAPTURE |
					V4L2_CAP_META_OUTPUT);
	}
	bool isVideoCapture() const
	{
		return isVideo() && isCapture();
	}
	bool isVideoOutput() const
	{
		return isVideo() && isOutput();
	}
	bool isMetaCapture() const
	{
		return isMeta() && isCapture();
	}
	bool isMetaOutput() const
	{
		return isMeta() && isOutput();
	}
	bool hasStreaming() const
	{
		return device_caps() & V4L2_CAP_STREAMING;
	}
};

class V4L2BufferCache
{
public:
	V4L2BufferCache(unsigned int numEntries);
	V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers);
	~V4L2BufferCache();

	int get(const FrameBuffer &buffer);
	void put(unsigned int index);

private:
	class Entry
	{
	public:
		Entry();
		Entry(bool free, uint64_t lastUsed, const FrameBuffer &buffer);

		bool operator==(const FrameBuffer &buffer) const;

		bool free;
		uint64_t lastUsed;

	private:
		struct Plane {
			Plane(const FrameBuffer::Plane &plane)
				: fd(plane.fd.fd()), length(plane.length)
			{
			}

			int fd;
			unsigned int length;
		};

		std::vector<Plane> planes_;
	};

	std::atomic<uint64_t> lastUsedCounter_;
	std::vector<Entry> cache_;
	/* \todo Expose the miss counter through an instrumentation API. */
	unsigned int missCounter_;
};

class V4L2DeviceFormat
{
public:
	V4L2PixelFormat fourcc;
	Size size;

	struct {
		uint32_t size;
		uint32_t bpl;
	} planes[3];
	unsigned int planesCount;

	const std::string toString() const;
};

class V4L2VideoDevice : public V4L2Device
{
public:
	explicit V4L2VideoDevice(const std::string &deviceNode);
	explicit V4L2VideoDevice(const MediaEntity *entity);
	V4L2VideoDevice(const V4L2VideoDevice &) = delete;
	~V4L2VideoDevice();

	V4L2VideoDevice &operator=(const V4L2VideoDevice &) = delete;

	int open();
	int open(int handle, enum v4l2_buf_type type);
	void close();

	const char *driverName() const { return caps_.driver(); }
	const char *deviceName() const { return caps_.card(); }
	const char *busName() const { return caps_.bus_info(); }

	const V4L2Capability &caps() const { return caps_; }

	int getFormat(V4L2DeviceFormat *format);
	int setFormat(V4L2DeviceFormat *format);
	std::map<V4L2PixelFormat, std::vector<SizeRange>> formats(uint32_t code = 0);

	int setSelection(unsigned int target, Rectangle *rect);

	int allocateBuffers(unsigned int count,
			    std::vector<std::unique_ptr<FrameBuffer>> *buffers);
	int exportBuffers(unsigned int count,
			  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
	int importBuffers(unsigned int count);
	int releaseBuffers();

	int queueBuffer(FrameBuffer *buffer);
	Signal<FrameBuffer *> bufferReady;

	int setFrameStartEnabled(bool enable);
	Signal<uint32_t> frameStart;

	int streamOn();
	int streamOff();

	static V4L2VideoDevice *fromEntityName(const MediaDevice *media,
					       const std::string &entity);

	V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat);

protected:
	std::string logPrefix() const;

private:
	int getFormatMeta(V4L2DeviceFormat *format);
	int setFormatMeta(V4L2DeviceFormat *format);

	int getFormatMultiplane(V4L2DeviceFormat *format);
	int setFormatMultiplane(V4L2DeviceFormat *format);

	int getFormatSingleplane(V4L2DeviceFormat *format);
	int setFormatSingleplane(V4L2DeviceFormat *format);

	std::vector<V4L2PixelFormat> enumPixelformats(uint32_t code);
	std::vector<SizeRange> enumSizes(V4L2PixelFormat pixelFormat);

	int requestBuffers(unsigned int count, enum v4l2_memory memoryType);
	int createBuffers(unsigned int count,
			  std::vector<std::unique_ptr<FrameBuffer>> *buffers);
	std::unique_ptr<FrameBuffer> createBuffer(unsigned int index);
	FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);

	void bufferAvailable(EventNotifier *notifier);
	FrameBuffer *dequeueBuffer();

	void eventAvailable(EventNotifier *notifier);

	V4L2Capability caps_;

	enum v4l2_buf_type bufferType_;
	enum v4l2_memory memoryType_;

	V4L2BufferCache *cache_;
	std::map<unsigned int, FrameBuffer *> queuedBuffers_;

	EventNotifier *fdBufferNotifier_;
	EventNotifier *fdEventNotifier_;

	bool frameStartEnabled_;
};

class V4L2M2MDevice
{
public:
	V4L2M2MDevice(const std::string &deviceNode);
	~V4L2M2MDevice();

	int open();
	void close();

	V4L2VideoDevice *output() { return output_; }
	V4L2VideoDevice *capture() { return capture_; }

private:
	std::string deviceNode_;

	V4L2VideoDevice *output_;
	V4L2VideoDevice *capture_;
};

} /* namespace libcamera */

#endif /* __LIBCAMERA_V4L2_VIDEODEVICE_H__ */