libcamera: camera: Handle camera objects through shared pointers

The Camera class is explicitly reference-counted to manage the lifetime
of camera objects. Replace this open-coded implementation with usage of
the std::shared_ptr<> class.

This API change prevents pipeline handlers from subclassing the Camera
class. This isn't deemed to be an issue. Mark the class final to make
this explicit.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
Laurent Pinchart 2019-01-17 16:23:25 +02:00
parent f3695e9b09
commit 21ff749a79
6 changed files with 54 additions and 45 deletions

View file

@ -7,22 +7,25 @@
#ifndef __LIBCAMERA_CAMERA_H__ #ifndef __LIBCAMERA_CAMERA_H__
#define __LIBCAMERA_CAMERA_H__ #define __LIBCAMERA_CAMERA_H__
#include <memory>
#include <string> #include <string>
namespace libcamera { namespace libcamera {
class Camera class Camera final
{ {
public: public:
Camera(const std::string &name); static std::shared_ptr<Camera> create(const std::string &name);
Camera(const Camera &) = delete;
void operator=(const Camera &) = delete;
const std::string &name() const; const std::string &name() const;
void get();
void put();
private: private:
virtual ~Camera() { }; explicit Camera(const std::string &name);
int ref_; ~Camera();
std::string name_; std::string name_;
}; };

View file

@ -24,10 +24,10 @@ public:
int start(); int start();
void stop(); void stop();
const std::vector<Camera *> &cameras() const { return cameras_; } const std::vector<std::shared_ptr<Camera>> &cameras() const { return cameras_; }
Camera *get(const std::string &name); std::shared_ptr<Camera> get(const std::string &name);
void addCamera(Camera *camera); void addCamera(std::shared_ptr<Camera> camera);
static CameraManager *instance(); static CameraManager *instance();
@ -42,7 +42,7 @@ private:
std::unique_ptr<DeviceEnumerator> enumerator_; std::unique_ptr<DeviceEnumerator> enumerator_;
std::vector<PipelineHandler *> pipes_; std::vector<PipelineHandler *> pipes_;
std::vector<Camera *> cameras_; std::vector<std::shared_ptr<Camera>> cameras_;
std::unique_ptr<EventDispatcher> dispatcher_; std::unique_ptr<EventDispatcher> dispatcher_;
}; };

View file

@ -41,19 +41,36 @@ namespace libcamera {
* streams from a single image source. It provides the main interface to * streams from a single image source. It provides the main interface to
* configuring and controlling the device, and capturing image streams. It is * configuring and controlling the device, and capturing image streams. It is
* the central object exposed by libcamera. * the central object exposed by libcamera.
*
* To support the central nature of Camera objects, libcamera manages the
* lifetime of camera instances with std::shared_ptr<>. Instances shall be
* created with the create() function which returns a shared pointer. The
* Camera constructors and destructor are private, to prevent instances from
* being constructed and destroyed manually.
*/ */
/** /**
* \brief Construct a named camera device * \brief Create a camera instance
* \param[in] name The name of the camera device
* *
* \param[in] name The name to set on the camera device * The caller is responsible for guaranteeing unicity of the camera name.
* *
* The caller is responsible for guaranteeing unicity of the camera * \return A shared pointer to the newly created camera object
* device name.
*/ */
Camera::Camera(const std::string &name) std::shared_ptr<Camera> Camera::create(const std::string &name)
: ref_(1), name_(name)
{ {
struct Allocator : std::allocator<Camera> {
void construct(void *p, const std::string &name)
{
::new(p) Camera(name);
}
void destroy(Camera *p)
{
p->~Camera();
}
};
return std::allocate_shared<Camera>(Allocator(), name);
} }
/** /**
@ -66,24 +83,13 @@ const std::string &Camera::name() const
return name_; return name_;
} }
/** Camera::Camera(const std::string &name)
* \brief Acquire a reference to the camera : name_(name)
*/
void Camera::get()
{ {
ref_++;
} }
/** Camera::~Camera()
* \brief Release a reference to the camera
*
* When the last reference is released the camera device is deleted. Callers
* shall not access the camera device after calling this function.
*/
void Camera::put()
{ {
if (--ref_ == 0)
delete this;
} }
} /* namespace libcamera */ } /* namespace libcamera */

View file

@ -40,9 +40,11 @@ namespace libcamera {
* This will enumerate all the cameras present in the system, which can then be * This will enumerate all the cameras present in the system, which can then be
* listed with list() and retrieved with get(). * listed with list() and retrieved with get().
* *
* Cameras are reference-counted, and shall be returned to the camera manager * Cameras are shared through std::shared_ptr<>, ensuring that a camera will
* with Camera::put() after being used. Once all cameras have been returned to * stay valid until the last reference is released without requiring any special
* the manager, it can be stopped with stop(). * action from the application. Once the application has released all the
* references it held to cameras, the camera manager can be stopped with
* stop().
* *
* \todo Add ability to add and remove media devices based on hot-(un)plug * \todo Add ability to add and remove media devices based on hot-(un)plug
* events coming from the device enumerator. * events coming from the device enumerator.
@ -148,15 +150,13 @@ void CameraManager::stop()
* \param[in] name Name of camera to get * \param[in] name Name of camera to get
* *
* Before calling this function the caller is responsible for ensuring that * Before calling this function the caller is responsible for ensuring that
* the camera manger is running. A camera fetched this way shall be * the camera manger is running.
* released by the user with the put() method of the Camera object once
* it is done using the camera.
* *
* \return Pointer to Camera object or nullptr if camera not found * \return Shared pointer to Camera object or nullptr if camera not found
*/ */
Camera *CameraManager::get(const std::string &name) std::shared_ptr<Camera> CameraManager::get(const std::string &name)
{ {
for (Camera *camera : cameras_) { for (std::shared_ptr<Camera> camera : cameras_) {
if (camera->name() == name) if (camera->name() == name)
return camera; return camera;
} }
@ -172,9 +172,9 @@ Camera *CameraManager::get(const std::string &name)
* handle with the camera manager. Registered cameras are immediately made * handle with the camera manager. Registered cameras are immediately made
* available to the system. * available to the system.
*/ */
void CameraManager::addCamera(Camera *camera) void CameraManager::addCamera(std::shared_ptr<Camera> camera)
{ {
for (Camera *c : cameras_) { for (std::shared_ptr<Camera> c : cameras_) {
if (c->name() == camera->name()) { if (c->name() == camera->name()) {
LOG(Warning) << "Registering camera with duplicate name '" LOG(Warning) << "Registering camera with duplicate name '"
<< camera->name() << "'"; << camera->name() << "'";
@ -182,7 +182,7 @@ void CameraManager::addCamera(Camera *camera)
} }
} }
cameras_.push_back(camera); cameras_.push_back(std::move(camera));
} }
/** /**

View file

@ -64,8 +64,8 @@ bool PipeHandlerVimc::match(CameraManager *manager, DeviceEnumerator *enumerator
* will be chosen depends on how the Camera * will be chosen depends on how the Camera
* object is modeled. * object is modeled.
*/ */
Camera *camera = new Camera("Dummy VIMC Camera"); std::shared_ptr<Camera> camera = Camera::create("Dummy VIMC Camera");
manager->addCamera(camera); manager->addCamera(std::move(camera));
return true; return true;
} }

View file

@ -30,7 +30,7 @@ protected:
{ {
unsigned int count = 0; unsigned int count = 0;
for (Camera *camera : cm->cameras()) { for (const std::shared_ptr<Camera> &camera : cm->cameras()) {
cout << "- " << camera->name() << endl; cout << "- " << camera->name() << endl;
count++; count++;
} }