summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiklas Söderlund <niklas.soderlund@ragnatech.se>2020-03-16 02:40:37 +0100
committerNiklas Söderlund <niklas.soderlund@ragnatech.se>2020-03-27 16:27:28 +0100
commit417e3143a99493d89fbb5a3f3ccd82a716f5d53a (patch)
tree24f34538f4f4251ef4c876933c571bb2de75a62d
parent38f2efb05cef01af711a3caa2d2456d905164ba5 (diff)
libcamera: FrameBuffer: Add a method to copy buffer content
This method may be used to memory copy a whole FrameBuffer content from another buffer. The operation is not fast and should not be used without great care by pipelines. The intended use-case is to have an option to copy out RAW buffers from the middle of a pipeline. Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
-rw-r--r--include/libcamera/buffer.h1
-rw-r--r--src/libcamera/buffer.cpp68
2 files changed, 69 insertions, 0 deletions
diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h
index 8e5ec699..ef3a3b36 100644
--- a/include/libcamera/buffer.h
+++ b/include/libcamera/buffer.h
@@ -57,6 +57,7 @@ public:
unsigned int cookie() const { return cookie_; }
void setCookie(unsigned int cookie) { cookie_ = cookie; }
+ int copyFrom(const FrameBuffer *src);
private:
friend class Request; /* Needed to update request_. */
friend class V4L2VideoDevice; /* Needed to update metadata_. */
diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp
index 673a63d3..93057568 100644
--- a/src/libcamera/buffer.cpp
+++ b/src/libcamera/buffer.cpp
@@ -211,4 +211,72 @@ FrameBuffer::FrameBuffer(const std::vector<Plane> &planes, unsigned int cookie)
* core never modifies the buffer cookie.
*/
+/**
+ * \brief Copy the contents from another buffer
+ * \param[in] src Buffer to copy
+ *
+ * Copy the buffer contents and metadata from \a src to this buffer. The
+ * destination FrameBuffer shall have the same number of planes as the source
+ * buffer, and each destination plane shall be larger than or equal to the
+ * corresponding source plane.
+ *
+ * The complete metadata of the source buffer is copied to the destination
+ * buffer. If an error occurs during the copy, the destination buffer's metadata
+ * status is set to FrameMetadata::FrameError, and other metadata fields are not
+ * modified.
+ *
+ * The operation is performed using memcpy() so is very slow, users needs to
+ * consider this before copying buffers.
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int FrameBuffer::copyFrom(const FrameBuffer *src)
+{
+ if (planes_.size() != src->planes_.size()) {
+ LOG(Buffer, Error) << "Different number of planes";
+ metadata_.status = FrameMetadata::FrameError;
+ return -EINVAL;
+ }
+
+ for (unsigned int i = 0; i < planes_.size(); i++) {
+ if (planes_[i].length < src->planes_[i].length) {
+ LOG(Buffer, Error) << "Plane " << i << " is too small";
+ metadata_.status = FrameMetadata::FrameError;
+ return -EINVAL;
+ }
+ }
+
+ for (unsigned int i = 0; i < planes_.size(); i++) {
+ void *dstmem = mmap(nullptr, planes_[i].length, PROT_WRITE,
+ MAP_SHARED, planes_[i].fd.fd(), 0);
+
+ if (dstmem == MAP_FAILED) {
+ LOG(Buffer, Error)
+ << "Failed to map destination plane " << i;
+ metadata_.status = FrameMetadata::FrameError;
+ return -EINVAL;
+ }
+
+ void *srcmem = mmap(nullptr, src->planes_[i].length, PROT_READ,
+ MAP_SHARED, src->planes_[i].fd.fd(), 0);
+
+ if (srcmem == MAP_FAILED) {
+ munmap(dstmem, planes_[i].length);
+ LOG(Buffer, Error)
+ << "Failed to map source plane " << i;
+ metadata_.status = FrameMetadata::FrameError;
+ return -EINVAL;
+ }
+
+ memcpy(dstmem, srcmem, src->planes_[i].length);
+
+ munmap(srcmem, src->planes_[i].length);
+ munmap(dstmem, planes_[i].length);
+ }
+
+ metadata_ = src->metadata_;
+
+ return 0;
+}
+
} /* namespace libcamera */
d='n223' href='#n223'>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 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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * unixsocket.cpp - Unix socket IPC test
 */

#include <algorithm>
#include <fcntl.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <libcamera/event_dispatcher.h>
#include <libcamera/timer.h>

#include "ipc_unixsocket.h"
#include "test.h"
#include "thread.h"
#include "utils.h"

#define CMD_CLOSE	0
#define CMD_REVERSE	1
#define CMD_LEN_CALC	2
#define CMD_LEN_CMP	3
#define CMD_JOIN	4

using namespace std;
using namespace libcamera;

int calculateLength(int fd)
{
	lseek(fd, 0, 0);
	int size = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, 0);

	return size;
}

class UnixSocketTestSlave
{
public:
	UnixSocketTestSlave()
		: exitCode_(EXIT_FAILURE), exit_(false)
	{
		dispatcher_ = Thread::current()->eventDispatcher();
		ipc_.readyRead.connect(this, &UnixSocketTestSlave::readyRead);
	}

	int run(int fd)
	{
		if (ipc_.bind(fd)) {
			cerr << "Failed to connect to IPC channel" << endl;
			return EXIT_FAILURE;
		}

		while (!exit_)
			dispatcher_->processEvents();

		ipc_.close();

		return exitCode_;
	}

private:
	void readyRead(IPCUnixSocket *ipc)
	{
		IPCUnixSocket::Payload message, response;
		int ret;

		ret = ipc->receive(&message);
		if (ret) {
			cerr << "Receive message failed: " << ret << endl;
			return;
		}

		const uint8_t cmd = message.data[0];

		switch (cmd) {
		case CMD_CLOSE:
			stop(0);
			break;

		case CMD_REVERSE: {
			response.data = message.data;
			std::reverse(response.data.begin() + 1, response.data.end());

			ret = ipc_.send(response);
			if (ret < 0) {
				cerr << "Reverse failed" << endl;
				stop(ret);
			}
			break;
		}

		case CMD_LEN_CALC: {
			int size = 0;
			for (int fd : message.fds)
				size += calculateLength(fd);

			response.data.resize(1 + sizeof(size));
			response.data[0] = cmd;
			memcpy(response.data.data() + 1, &size, sizeof(size));

			ret = ipc_.send(response);
			if (ret < 0) {
				cerr << "Calc failed" << endl;
				stop(ret);
			}
			break;
		}

		case CMD_LEN_CMP: {
			int size = 0;
			for (int fd : message.fds)
				size += calculateLength(fd);

			int cmp;
			memcpy(&cmp, message.data.data() + 1, sizeof(cmp));

			if (cmp != size) {
				cerr << "Compare failed" << endl;
				stop(-ERANGE);
			}
			break;
		}

		case CMD_JOIN: {
			int outfd = open("/tmp", O_TMPFILE | O_RDWR,
					 S_IRUSR | S_IWUSR);
			if (outfd < 0) {
				cerr << "Create out file failed" << endl;
				stop(outfd);
				return;
			}

			for (int fd : message.fds) {
				while (true) {
					char buf[32];
					ssize_t num = read(fd, &buf, sizeof(buf));

					if (num < 0) {
						cerr << "Read failed" << endl;
						stop(-EIO);
						return;
					} else if (!num)
						break;

					if (write(outfd, buf, num) < 0) {
						cerr << "Write failed" << endl;
						stop(-EIO);
						return;
					}
				}

				close(fd);
			}

			lseek(outfd, 0, 0);
			response.data.push_back(CMD_JOIN);
			response.fds.push_back(outfd);

			ret = ipc_.send(response);
			if (ret < 0) {
				cerr << "Join failed" << endl;
				stop(ret);
			}

			close(outfd);

			break;
		}

		default:
			cerr << "Unknown command " << cmd << endl;
			stop(-EINVAL);
			break;
		}
	}

	void stop(int code)
	{
		exitCode_ = code;
		exit_ = true;
	}

	IPCUnixSocket ipc_;
	EventDispatcher *dispatcher_;
	int exitCode_;
	bool exit_;
};

class UnixSocketTest : public Test
{
protected:
	int slaveStart(int fd)
	{
		pid_ = fork();

		if (pid_ == -1)
			return TestFail;

		if (!pid_) {
			std::string arg = std::to_string(fd);
			execl("/proc/self/exe", "/proc/self/exe",
			      arg.c_str(), nullptr);

			/* Only get here if exec fails. */
			exit(TestFail);
		}