summaryrefslogtreecommitdiff
path: root/include/libcamera/buffer.h
blob: 0b95e41a2dd205b21daf444f6ec2bdf13039d511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * buffer.h - Buffer handling
 */
#ifndef __LIBCAMERA_BUFFER_H__
#define __LIBCAMERA_BUFFER_H__

#include <array>
#include <stdint.h>
#include <vector>

namespace libcamera {

class Request;
class Stream;

struct FrameMetadata {
	enum Status {
		FrameSuccess,
		FrameError,
		FrameCancelled,
	};

	struct Plane {
		unsigned int bytesused;
	};

	Status status;
	unsigned int sequence;
	uint64_t timestamp;
	std::vector<Plane> planes;
};

class Plane final
{
public:
	Plane();
	~Plane();

	int dmabuf() const { return fd_; }
	int setDmabuf(int fd, unsigned int length);

	void *mem();
	unsigned int length() const { return length_; }

private:
	int mmap();
	int munmap();

	int fd_;
	unsigned int length_;
	void *mem_;
};

class BufferMemory final
{
public:
	const std::vector<Plane> &planes() const { return planes_; }
	std::vector<Plane> &planes() { return planes_; }

private:
	std::vector<Plane> planes_;
};

class BufferPool final
{
public:
	~BufferPool();

	void createBuffers(unsigned int count);
	void destroyBuffers();

	unsigned int count() const { return buffers_.size(); }
	std::vector<BufferMemory> &buffers() { return buffers_; }

private:
	std::vector<BufferMemory> buffers_;
};

class Buffer final
{
public:
	enum Status {
		BufferSuccess,
		BufferError,
		BufferCancelled,
	};

	Buffer(unsigned int index = -1, const Buffer *metadata = nullptr);
	Buffer(const Buffer &) = delete;
	Buffer &operator=(const Buffer &) = delete;

	unsigned int index() const { return index_; }
	const std::array<int, 3> &dmabufs() const { return dmabuf_; }
	BufferMemory *mem() { return mem_; }

	unsigned int bytesused() const { return bytesused_; }
	uint64_t timestamp() const { return timestamp_; }
	unsigned int sequence() const { return sequence_; }

	Status status() const { return status_; }
	Request *request() const { return request_; }
	Stream *stream() const { return stream_; }

private:
	friend class Camera;
	friend class Request;
	friend class Stream;
	friend class V4L2VideoDevice;

	void cancel();

	void setRequest(Request *request) { request_ = request; }

	unsigned int index_;
	std::array<int, 3> dmabuf_;
	BufferMemory *mem_;

	unsigned int bytesused_;
	uint64_t timestamp_;
	unsigned int sequence_;

	Status status_;
	Request *request_;
	Stream *stream_;
};

} /* namespace libcamera */

#endif /* __LIBCAMERA_BUFFER_H__ */