summaryrefslogtreecommitdiff
path: root/src/android/mm/cros_camera_buffer.cpp
blob: e2a44a2a3437415ff01193212eab18acc9714dde (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
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) 2021, Google Inc.
 *
 * Chromium OS buffer backend using CameraBufferManager
 */

#include "../camera_buffer.h"

#include <libcamera/base/log.h>

#include "cros-camera/camera_buffer_manager.h"

using namespace libcamera;

LOG_DECLARE_CATEGORY(HAL)

class CameraBuffer::Private : public Extensible::Private
{
	LIBCAMERA_DECLARE_PUBLIC(CameraBuffer)

public:
	Private(CameraBuffer *cameraBuffer, buffer_handle_t camera3Buffer,
		PixelFormat pixelFormat, const Size &size,
		int flags);
	~Private();

	bool isValid() const { return registered_; }

	unsigned int numPlanes() const;

	Span<uint8_t> plane(unsigned int plane);

	unsigned int stride(unsigned int plane) const;
	unsigned int offset(unsigned int plane) const;
	unsigned int size(unsigned int plane) const;

	size_t jpegBufferSize(size_t maxJpegBufferSize) const;

private:
	void map();

	cros::CameraBufferManager *bufferManager_;
	buffer_handle_t handle_;
	unsigned int numPlanes_;
	bool mapped_;
	bool registered_;
	union {
		void *addr;
		android_ycbcr ycbcr;
	} mem;
};

CameraBuffer::Private::Private([[maybe_unused]] CameraBuffer *cameraBuffer,
			       buffer_handle_t camera3Buffer,
			       [[maybe_unused]] PixelFormat pixelFormat,
			       [[maybe_unused]] const Size &size,
			       [[maybe_unused]] int flags)
	: handle_(camera3Buffer), numPlanes_(0), mapped_(false),
	  registered_(false)
{
	bufferManager_ = cros::CameraBufferManager::GetInstance();
	if (!bufferManager_) {
		LOG(HAL, Fatal)
			<< "Failed to get cros CameraBufferManager instance";
		return;
	}

	int ret = bufferManager_->Register(camera3Buffer);
	if (ret) {
		LOG(HAL, Error) << "Failed registering a buffer: " << ret;
		return;
	}

	registered_ = true;
	numPlanes_ = bufferManager_->GetNumPlanes(camera3Buffer);
}

CameraBuffer::Private::~Private()
{
	int ret;
	if (mapped_) {
		ret = bufferManager_->Unlock(handle_);
		if (ret != 0)
			LOG(HAL, Error) << "Failed to unlock buffer: "
					<< strerror(-ret);
	}

	if (registered_) {
		ret = bufferManager_->Deregister(handle_);
		if (ret != 0)
			LOG(HAL, Error) << "Failed to deregister buffer: "
					<< strerror(-ret);
	}
}

unsigned int CameraBuffer::Private::numPlanes() const
{
	return bufferManager_->GetNumPlanes(handle_);
}

Span<uint8_t> CameraBuffer::Private::plane(unsigned int plane)
{
	if (!mapped_)
		map();
	if (!mapped_)
		return {};

	void *addr;

	switch (numPlanes()) {
	case 1:
		addr = mem.addr;
		break;
	default:
		switch (plane) {
		case 0:
			addr = mem.ycbcr.y;
			break;
		case 1:
			addr = mem.ycbcr.cb;
			break;
		case 2:
			addr = mem.ycbcr.cr;
			break;
		}
	}

	return { static_cast<uint8_t *>(addr),
		 bufferManager_->GetPlaneSize(handle_, plane) };
}

unsigned int CameraBuffer::Private::stride(unsigned int plane) const
{
	return cros::CameraBufferManager::GetPlaneStride(handle_, plane);
}

unsigned int CameraBuffer::Private::offset(unsigned int plane) const
{
	return cros::CameraBufferManager::GetPlaneOffset(handle_, plane);
}

unsigned int CameraBuffer::Private::size(unsigned int plane) const
{
	return cros::CameraBufferManager::GetPlaneSize(handle_, plane);
}

size_t CameraBuffer::Private::jpegBufferSize([[maybe_unused]] size_t maxJpegBufferSize) const
{
	return bufferManager_->GetPlaneSize(handle_, 0);
}

void CameraBuffer::Private::map()
{
	int ret;
	switch (numPlanes_) {
	case 1: {
		ret = bufferManager_->Lock(handle_, 0, 0, 0, 0, 0, &mem.addr);
		if (ret) {
			LOG(HAL, Error) << "Single plane buffer mapping failed";
			return;
		}
		break;
	}
	case 2:
	case 3: {
		ret = bufferManager_->LockYCbCr(handle_, 0, 0, 0, 0, 0,
						&mem.ycbcr);
		if (ret) {
			LOG(HAL, Error) << "YCbCr buffer mapping failed";
			return;
		}
		break;
	}
	default:
		LOG(HAL, Error) << "Invalid number of planes: " << numPlanes_;
		return;
	}

	mapped_ = true;
	return;
}

PUBLIC_CAMERA_BUFFER_IMPLEMENTATION
pan class="hl com"> * ByteStreamBuffer, and returns the new buffer. It operates identically to a * read or write access from the point of view of the current buffer, but allows * the new buffer to be read or written at a later time after other read or * write accesses on the current buffer. * * \return A newly created ByteStreamBuffer of \a size */ ByteStreamBuffer ByteStreamBuffer::carveOut(size_t size) { if (!size_ || overflow_) return ByteStreamBuffer(static_cast<const uint8_t *>(nullptr), 0); const uint8_t *curr = read_ ? read_ : write_; if (curr + size > base_ + size_) { LOG(Serialization, Error) << "Unable to reserve " << size << " bytes"; setOverflow(); return ByteStreamBuffer(static_cast<const uint8_t *>(nullptr), 0); } if (read_) { ByteStreamBuffer b(read_, size); b.parent_ = this; read_ += size; return b; } else { ByteStreamBuffer b(write_, size); b.parent_ = this; write_ += size; return b; } } /** * \brief Skip \a size bytes from the buffer * \param[in] size The number of bytes to skip * * This function skips the next \a size bytes from the buffer. * * \return 0 on success, a negative error code otherwise * \retval -ENOSPC no more space is available in the managed memory buffer */ int ByteStreamBuffer::skip(size_t size) { if (overflow_) return -ENOSPC; const uint8_t *curr = read_ ? read_ : write_; if (curr + size > base_ + size_) { LOG(Serialization, Error) << "Unable to skip " << size << " bytes"; setOverflow(); return -ENOSPC; } if (read_) { read_ += size; } else { memset(write_, 0, size); write_ += size; } return 0; } /** * \fn template<typename T> int ByteStreamBuffer::read(T *t) * \brief Read data from the managed memory buffer into \a t * \param[out] t Pointer to the memory containing the read data * \return 0 on success, a negative error code otherwise * \retval -EACCES attempting to read from a write buffer * \retval -ENOSPC no more space is available in the managed memory buffer */ /** * \fn template<typename T> int ByteStreamBuffer::read(const Span<T> &data) * \brief Read data from the managed memory buffer into Span \a data * \param[out] data Span representing the destination memory * \return 0 on success, a negative error code otherwise * \retval -EACCES attempting to read from a write buffer * \retval -ENOSPC no more space is available in the managed memory buffer */ /** * \fn template<typename T> const T *ByteStreamBuffer::read(size_t count) * \brief Read data from the managed memory buffer without performing a copy * \param[in] count Number of data items to read * * This function reads \a count elements of type \a T from the buffer. Unlike * the other read variants, it doesn't copy the data but returns a pointer to * the first element. If data can't be read for any reason (usually due to * reading more data than available), the function returns nullptr. * * \return A pointer to the data on success, or nullptr otherwise */ /** * \fn template<typename T> int ByteStreamBuffer::write(const T *t) * \brief Write \a t to the managed memory buffer * \param[in] t The data to write to memory * \return 0 on success, a negative error code otherwise * \retval -EACCES attempting to write to a read buffer * \retval -ENOSPC no more space is available in the managed memory buffer */ /** * \fn template<typename T> int ByteStreamBuffer::write(const Span<T> &data) * \brief Write \a data to the managed memory buffer * \param[in] data The data to write to memory * \return 0 on success, a negative error code otherwise * \retval -EACCES attempting to write to a read buffer * \retval -ENOSPC no more space is available in the managed memory buffer */ const uint8_t *ByteStreamBuffer::read(size_t size, size_t count) { if (!read_) return nullptr; if (overflow_) return nullptr; size_t bytes; if (__builtin_mul_overflow(size, count, &bytes)) { setOverflow(); return nullptr; } if (read_ + bytes > base_ + size_) { LOG(Serialization, Error) << "Unable to read " << bytes << " bytes: out of bounds"; setOverflow(); return nullptr; } const uint8_t *data = read_; read_ += bytes; return data; } int ByteStreamBuffer::read(uint8_t *data, size_t size) { if (!read_) return -EACCES; if (overflow_) return -ENOSPC; if (read_ + size > base_ + size_) { LOG(Serialization, Error) << "Unable to read " << size << " bytes: out of bounds"; setOverflow(); return -ENOSPC; } memcpy(data, read_, size); read_ += size; return 0; } int ByteStreamBuffer::write(const uint8_t *data, size_t size) { if (!write_) return -EACCES; if (overflow_) return -ENOSPC; if (write_ + size > base_ + size_) { LOG(Serialization, Error) << "Unable to write " << size << " bytes: no space left"; setOverflow(); return -ENOSPC; } memcpy(write_, data, size); write_ += size; return 0; } } /* namespace libcamera */