libcamera: request: Add cookie to make request tracking easier

Applications often have to map requests queued to a camera to external
resources. To make this easy, add a 64-bit integer cookie to the Request
class that is set when the request is created and can be retrieved at
any time, especially in the request completion handler. The cookie is
completely transparent for libcamera and is never modified.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
Laurent Pinchart 2019-07-12 09:37:51 +03:00
parent 185fe3d4b4
commit 33d3c4e204
4 changed files with 30 additions and 6 deletions

View file

@ -10,6 +10,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <set> #include <set>
#include <stdint.h>
#include <string> #include <string>
#include <libcamera/controls.h> #include <libcamera/controls.h>
@ -93,7 +94,7 @@ public:
int allocateBuffers(); int allocateBuffers();
int freeBuffers(); int freeBuffers();
Request *createRequest(); Request *createRequest(uint64_t cookie = 0);
int queueRequest(Request *request); int queueRequest(Request *request);
int start(); int start();

View file

@ -8,6 +8,7 @@
#define __LIBCAMERA_REQUEST_H__ #define __LIBCAMERA_REQUEST_H__
#include <map> #include <map>
#include <stdint.h>
#include <unordered_set> #include <unordered_set>
#include <libcamera/controls.h> #include <libcamera/controls.h>
@ -29,7 +30,7 @@ public:
RequestCancelled, RequestCancelled,
}; };
explicit Request(Camera *camera); Request(Camera *camera, uint64_t cookie = 0);
Request(const Request &) = delete; Request(const Request &) = delete;
Request &operator=(const Request &) = delete; Request &operator=(const Request &) = delete;
@ -38,6 +39,7 @@ public:
int setBuffers(const std::map<Stream *, Buffer *> &streamMap); int setBuffers(const std::map<Stream *, Buffer *> &streamMap);
Buffer *findBuffer(Stream *stream) const; Buffer *findBuffer(Stream *stream) const;
uint64_t cookie() const { return cookie_; }
Status status() const { return status_; } Status status() const { return status_; }
bool hasPendingBuffers() const { return !pending_.empty(); } bool hasPendingBuffers() const { return !pending_.empty(); }
@ -56,6 +58,7 @@ private:
std::map<Stream *, Buffer *> bufferMap_; std::map<Stream *, Buffer *> bufferMap_;
std::unordered_set<Buffer *> pending_; std::unordered_set<Buffer *> pending_;
const uint64_t cookie_;
Status status_; Status status_;
}; };

View file

@ -754,10 +754,16 @@ int Camera::freeBuffers()
/** /**
* \brief Create a request object for the camera * \brief Create a request object for the camera
* \param[in] cookie Opaque cookie for application use
* *
* This method creates an empty request for the application to fill with * This method creates an empty request for the application to fill with
* buffers and paramaters, and queue for capture. * buffers and paramaters, and queue for capture.
* *
* The \a cookie is stored in the request and is accessible through the
* Request::cookie() method at any time. It is typically used by applications
* to map the request to an external resource in the request completion
* handler, and is completely opaque to libcamera.
*
* The ownership of the returned request is passed to the caller, which is * The ownership of the returned request is passed to the caller, which is
* responsible for either queueing the request or deleting it. * responsible for either queueing the request or deleting it.
* *
@ -766,12 +772,12 @@ int Camera::freeBuffers()
* *
* \return A pointer to the newly created request, or nullptr on error * \return A pointer to the newly created request, or nullptr on error
*/ */
Request *Camera::createRequest() Request *Camera::createRequest(uint64_t cookie)
{ {
if (disconnected_ || !stateBetween(CameraPrepared, CameraRunning)) if (disconnected_ || !stateBetween(CameraPrepared, CameraRunning))
return nullptr; return nullptr;
return new Request(this); return new Request(this, cookie);
} }
/** /**

View file

@ -46,9 +46,17 @@ LOG_DEFINE_CATEGORY(Request)
/** /**
* \brief Create a capture request for a camera * \brief Create a capture request for a camera
* \param[in] camera The camera that creates the request * \param[in] camera The camera that creates the request
* \param[in] cookie Opaque cookie for application use
*
* The \a cookie is stored in the request and is accessible through the
* cookie() method at any time. It is typically used by applications to map the
* request to an external resource in the request completion handler, and is
* completely opaque to libcamera.
*
*/ */
Request::Request(Camera *camera) Request::Request(Camera *camera, uint64_t cookie)
: camera_(camera), controls_(camera), status_(RequestPending) : camera_(camera), controls_(camera), cookie_(cookie),
status_(RequestPending)
{ {
} }
@ -119,6 +127,12 @@ Buffer *Request::findBuffer(Stream *stream) const
return it->second; return it->second;
} }
/**
* \fn Request::cookie()
* \brief Retrieve the cookie set when the request was created
* \return The request cookie
*/
/** /**
* \fn Request::status() * \fn Request::status()
* \brief Retrieve the request completion status * \brief Retrieve the request completion status