/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2019-2021, Google Inc. * * camera_request.cpp - libcamera Android Camera Request Descriptor */ #include "camera_request.h" #include #include "camera_buffer.h" using namespace libcamera; /* * \class Camera3RequestDescriptor * * A utility class that groups information about a capture request to be later * reused at request complete time to notify the framework. * ******************************************************************************* * Lifetime of a Camera3RequestDescriptor tracking a capture request placed by * Android Framework ******************************************************************************* * * * Android Framework * │ * │ ┌──────────────────────────────────┐ * │ │camera3_capture_request_t │ * │ │ │ * │ │Requested output streams │ * │ │ stream1 stream2 stream3 ... │ * │ └──────────────────────────────────┘ * ▼ * ┌─────────────────────────────────────────────────────────────┐ * │ libcamera HAL │ * ├─────────────────────────────────────────────────────────────┤ * │ CameraDevice │ * │ │ * │ processCaptureRequest(camera3_capture_request_t request) │ * │ │ * │ - Create Camera3RequestDescriptor tracking this request │ * │ - Streams requiring post-processing are stored in the │ * │ pendingStreamsToProcess map │ * │ - Add this Camera3RequestDescriptor to descriptors' queue │ * │ CameraDevice::descriptors_ │ * │ │ ┌─────────────────────────┐ * │ - Queue the capture request to libcamera core ────────────┤►│libcamera core │ * │ │ ├─────────────────────────┤ * │ │ │- Capture from Camera │ * │ │ │ │ * │ │ │- Emit │ * │ │ │ Camera::requestComplete│ * │ requestCompleted(Request *request) ◄───────────────────────┼─┼──── │ * │ │ │ │ * │ - Check request completion status │ └─────────────────────────┘ * │ │ * │ - if (pendingStreamsToProcess > 0) │ * │ Queue all entries from pendingStreamsToProcess │ * │ else │ │ * │ completeDescriptor() │ └──────────────────────┐ * │ │ │ * │ ┌──────────────────────────┴───┬──────────────────┐ │ * │ │ │ │ │ * │ ┌──────────▼────────────┐ ┌───────────▼─────────┐ ▼ │ * │ │CameraStream1 │ │CameraStream2 │ .... │ * │ ├┬───┬───┬──────────────┤ ├┬───┬───┬────────────┤ │ * │ ││ │ │ │ ││ │ │ │ │ * │ │▼───▼───▼──────────────┤ │▼───▼───▼────────────┤ │ * │ │PostProcessorWorker │ │PostProcessorWorker │ │ * │ │ │ │ │ │ * │ │ +------------------+ │ │ +------------------+│ │ * │ │ | PostProcessor | │ │ | PostProcessor |│ │ * │ │ | process() | │ │ | process() |│ │ * │ │ | | │ │ | |│ │ * │ │ | Emit | │ │ | Emit |│ │ * │ │ | processComplete | │ │ | processComplete |│ │ * │ │ | | │ │ | |│ │ * │ │ +--------------│---+ │ │ +--------------│---+│ │ * │ │ │ │ │ │ │ │ * │ │ │ │ │ │ │ │ * │ └────────────────┼──────┘ └────────────────┼────┘ │ * │ │ │ │ * │ │ │ │ * │ │ │ │ * │ ▼ ▼ │ * │ +---------------------------------------+ +--------------+ │ * │ | CameraDevice | | | │ * │ | | | | │ * │ | streamProcessingComplete() | | | │ * │ | | | | │ * │ | - Check and set buffer status | | .... | │ * │ | - Remove post+processing entry | | | │ * │ | from pendingStreamsToProcess | | | │ * │ | | | | │ * │ | - if (pendingStreamsToProcess.empty())| | | │ * │ | completeDescriptor | | | │ * │ | | | | │ * │ +---------------------------------------+ +--------------+ │ * │ │ * └────────────────────────────────────────────────────────────────────────────────────┘ * * +-------------+ * | | - PostProcessorWorker's thread * | | * +-------------+ */ Camera3RequestDescriptor::Camera3RequestDescriptor( Camera *camera, const camera3_capture_request_t *camera3Request) { frameNumber_ = camera3Request->frame_number; /* Copy the camera3 request stream information for later access. */ const Span buffers{ camera3Request->output_buffers, camera3Request->num_output_buffers }; buffers_.reserve(buffers.size()); for (const camera3_stream_buffer_t &buffer : buffers) { CameraStream *stream = static_cast(buffer.stream->priv); buffers_.emplace_back(stream, buffer, this); } /* Clone the controls associated with the camera3 request. */ settings_ = CameraMetadata(camera3Request->settings); /* * Create the CaptureRequest, stored as a unique_ptr<> to tie its * lifetime to the descriptor. */ request_ = camera->createRequest(reinterpret_cast(this)); } Camera3RequestDescriptor::~Camera3RequestDescriptor() = default; /** * \struct Camera3RequestDescriptor::StreamBuffer * \brief Group information for per-stream buffer of Camera3RequestDescriptor * * A capture request placed to the libcamera HAL can contain multiple streams. * Each stream will have an associated buffer to be filled. StreamBuffer * tracks this buffer with contextual information which aids in the stream's * generation. The generation of the stream will depend on its type (refer to * the CameraStream::Type documentation). * * \var Camera3RequestDescriptor::StreamBuffer::stream * \brief Pointer to the corresponding CameraStream * * \var Camera3RequestDescriptor::StreamBuffer::camera3Buffer * \brief Native handle to the buffer * * \var Camera3RequestDescriptor::StreamBuffer::frameBuffer * \brief Encapsulate the dmabuf handle inside a libcamera::FrameBuffer for * direct streams * * \var Camera3RequestDescriptor::StreamBuffer::fence * \brief Acquire fence of the buffer * * \var Camera3RequestDescriptor::StreamBuffer::status * \brief Track the status of the buffer * * \var Camera3RequestDescriptor::StreamBuffer::internalBuffer * \brief Pointer to a buffer internally handled by CameraStream (if any) * * \var Camera3RequestDescriptor::StreamBuffer::srcBuffer * \brief Pointer to the source frame buffer used for post-processing * * \var Camera3RequestDescriptor::StreamBuffer::dstBuffer * \brief Pointer to the destination frame buffer used for post-processing * * \var Camera3RequestDescriptor::StreamBuffer::request * \brief Back pointer to the Camera3RequestDescriptor to which the StreamBuffer belongs */ Camera3RequestDescriptor::StreamBuffer::StreamBuffer( CameraStream *cameraStream, const camera3_stream_buffer_t &buffer, Camera3RequestDescriptor *requestDescriptor) : stream(cameraStream), camera3Buffer(buffer.buffer), fence(buffer.acquire_fence), request(requestDescriptor) { } Camera3RequestDescriptor::StreamBuffer::~StreamBuffer() = default; Camera3RequestDescriptor::StreamBuffer::StreamBuffer(StreamBuffer &&) = default; Camera3RequestDescriptor::StreamBuffer & Camera3RequestDescriptor::StreamBuffer::operator=(Camera3RequestDescriptor::StreamBuffer &&) = default; 50 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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * thread.cpp - Thread support
 */

#include "thread.h"

#include <atomic>
#include <condition_variable>
#include <list>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include <libcamera/event_dispatcher.h>

#include "event_dispatcher_poll.h"
#include "log.h"
#include "message.h"

/**
 * \page thread Thread Support
 *
 * libcamera supports multi-threaded applications through a threading model that
 * sets precise rules to guarantee thread-safe usage of the API. Additionally,
 * libcamera makes internal use of threads, and offers APIs that simplify
 * interactions with application threads. Careful compliance with the threading
 * model will ensure avoidance of race conditions.
 *
 * \section thread-objects Threads and Objects
 *
 * Instances of the Object class and all its derived classes are thread-aware
 * and are bound to the thread they are created in. They are said to *live* in
 * a thread, and they interact with the event loop of their thread for the
 * purpose of message passing and signal delivery. Messages posted to the
 * object with Object::postMessage() will be delivered from the event loop of
 * the thread that the object lives in. Signals delivered to the object, unless
 * explicitly connected with ConnectionTypeDirect, will also be delivered from
 * the object thread's event loop.
 *
 * All Object instances created by libcamera are bound to an internal thread,
 * and applications don't need to provide an event loop to support them. Object
 * instances created by applications require an event loop. It is the
 * responsibility of applications to provide that event loop, either explicitly
 * through CameraManager::setEventDispatcher(), or by running the default event
 * loop provided by CameraManager::eventDispatcher() in their main thread. The
 * main thread of an application is the one that calls CameraManager::start().
 *
 * \section thread-signals Threads and Signals
 *
 * When sent to a receiver that does not inherit from the Object class, signals
 * are delivered synchronously in the thread of the sender. When the receiver
 * inherits from the Object class, delivery is by default asynchronous if the
 * sender and receiver live in different threads. In that case, the signal is
 * posted to the receiver's message queue and will be delivered from the
 * receiver's event loop, running in the receiver's thread. This mechanism can
 * be overridden by selecting a different connection type when calling
 * Signal::connect().
 *
 * Asynchronous signal delivery is used internally in libcamera, but is also
 * available to applications if desired. To use this feature, applications
 * shall create receiver classes that inherit from the Object class, and
 * provide an event loop to the CameraManager as explained above. Note that
 * Object instances created by the application are limited to living in the
 * application's main thread. Creating Object instances from another thread of
 * an application causes undefined behaviour.
 *
 * \section thread-reentrancy Reentrancy and Thread-Safety
 *
 * Through the documentation, several terms are used to define how classes and
 * their member functions can be used from multiple threads.
 *
 * - A **reentrant** function may be called simultaneously from multiple
 *   threads if and only if each invocation uses a different instance of the
 *   class. This is the default for all member functions not explictly marked
 *   otherwise.
 *
 * - \anchor thread-safe A **thread-safe** function may be called
 *   simultaneously from multiple threads on the same instance of a class. A
 *   thread-safe function is thus reentrant. Thread-safe functions may also be
 *   called simultaneously with any other reentrant function of the same class
 *   on the same instance.
 *
 * - \anchor thread-bound A **thread-bound** function may be called only from