/**
* struct dma_buf_sync - Synchronize with CPU access.
*
* When a DMA buffer is accessed from the CPU via mmap, it is not always
* possible to guarantee coherency between the CPU-visible map and underlying
* memory. To manage coherency, DMA_BUF_IOCTL_SYNC must be used to bracket
* any CPU access to give the kernel the chance to shuffle memory around if
* needed.
*
* Prior to accessing the map, the client must call DMA_BUF_IOCTL_SYNC
* with DMA_BUF_SYNC_START and the appropriate read/write flags. Once the
* access is complete, the client should call DMA_BUF_IOCTL_SYNC with
* DMA_BUF_SYNC_END and the same read/write flags.
*
* The synchronization provided via DMA_BUF_IOCTL_SYNC only provides cache
* coherency. It does not prevent other processes or devices from
* accessing the memory at the same time. If synchronization with a GPU or
* other device driver is required, it is the client's responsibility to
* wait for buffer to be ready for reading or writing before calling this
* ioctl with DMA_BUF_SYNC_START. Likewise, the client must ensure that
* follow-up work is not submitted to GPU or other device driver until
* after this ioctl has been called with DMA_BUF_SYNC_END?
*
* If the driver or API with which the client is interacting uses implicit
* synchronization, waiting for prior work to complete can be done via
* poll() on the DMA buffer file descriptor. If the driver or API requires
* explicit synchronization, the client may have to wait on a sync_file or
* other synchronization primitive outside the scope of the DMA buffer API.
*/
struct dma_buf_sync {
/**
* @flags: Set of access flags
*
* DMA_BUF_SYNC_START:
* Indicates the start of a map access session.
*
* DMA_BUF_SYNC_END:
* Indicates the end of a map access session.
*
* DMA_BUF_SYNC_READ:
* Indicates that the mapped DMA buffer will be read by the
* client via the CPU map.
*
* DMA_BUF_SYNC_WRITE:
* Indicates that the mapped DMA buffer will be written by the
* client via the CPU map.
*
* DMA_BUF_SYNC_RW:
* An alias for DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE.
*/
__u64 flags;
};
#define DMA_BUF_SYNC_READ (1 << 0)
#define DMA_BUF_SYNC_WRITE (2 << 0)
#define DMA_BUF_SYNC_RW (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE)
#define DMA_BUF_SYNC_START (0 << 2)
#define DMA_BUF_SYNC_END (1 << 2)
#define DMA_BUF_SYNC_VALID_FLAGS_MASK \
(DMA_BUF_SYNC_RW | DMA_BUF_SYNC_END)
#define DMA_BUF_NAME_LEN 32
#define DMA_BUF_BASE 'b'
#define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
/* 32/64bitness of this uapi was botched in android, there's no difference
* between them in actual uapi, they're just different numbers.
*/
#define DMA_BUF_SET_NAME _IOW(DMA_BUF_BASE, 1, const char *)
#define DMA_BUF_SET_NAME_A _IOW(DMA_BUF_BASE, 1, u32)
#define DMA_BUF_SET_NAME_B _IOW(DMA_BUF_BASE, 1, u64)
#endif
0
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2018, Google Inc.
*
* camera_manager.h - Camera management
*/
#include <libcamera/camera_manager.h>
#include <map>
#include <libcamera/camera.h>
#include <libcamera/base/log.h>
#include <libcamera/base/mutex.h>
#include <libcamera/base/thread.h>
#include <libcamera/base/utils.h>
#include "libcamera/internal/device_enumerator.h"
#include "libcamera/internal/ipa_manager.h"
#include "libcamera/internal/pipeline_handler.h"
#include "libcamera/internal/process.h"
/**
* \file camera_manager.h
* \brief The camera manager
*/
/**
* \brief Top-level libcamera namespace
*/
namespace libcamera {
LOG_DEFINE_CATEGORY(Camera)
class CameraManager::Private : public Extensible::Private, public Thread
{
LIBCAMERA_DECLARE_PUBLIC(CameraManager)
public:
Private();
int start();
void addCamera(std::shared_ptr<Camera> camera,
const std::vector<dev_t> &devnums);
void removeCamera(Camera *camera);
/*
* This mutex protects
*
* - initialized_ and status_ during initialization
* - cameras_ and camerasByDevnum_ after initialization
*/
mutable Mutex mutex_;
std::vector<std::shared_ptr<Camera>> cameras_;
std::map<dev_t, std::weak_ptr<Camera>> camerasByDevnum_;
protected:
void run() override;
private:
int init();
void createPipelineHandlers();
void cleanup();
ConditionVariable cv_;
bool initialized_;
int status_;
std::unique_ptr<DeviceEnumerator> enumerator_;
IPAManager ipaManager_;
ProcessManager processManager_;
};
CameraManager::Private::Private()
: initialized_(false)
{
}
int CameraManager::Private::start()
{
int status;
/* Start the thread and wait for initialization to complete. */
Thread::start();
{
MutexLocker locker(mutex_);
cv_.wait(locker, [&] { return initialized_; });
status = status_;
}
/* If a failure happened during initialization, stop the thread. */
if (status < 0) {
exit();
wait();
return status;
}
return 0;
}
void CameraManager::Private::run()
{
LOG(Camera, Debug) << "Starting camera manager";
int ret = init();
mutex_.lock();
status_ = ret;
initialized_ = true;
mutex_.unlock();
cv_.notify_one();
if (ret < 0)
return;
/* Now start processing events and messages. */
exec();
cleanup();
}
int CameraManager::Private::init()
{
enumerator_ = DeviceEnumerator::create();
if (!enumerator_ || enumerator_->enumerate())
return -ENODEV;
createPipelineHandlers();
return 0;
}
void CameraManager::Private::createPipelineHandlers()
{
CameraManager *const o = LIBCAMERA_O_PTR();
/*
* \todo Try to read handlers and order from configuration
* file and only fallback on all handlers if there is no
* configuration file.
*/
std::vector<PipelineHandlerFactory *> &factories =
PipelineHandlerFactory::factories();
for (PipelineHandlerFactory *factory : factories) {
LOG(Camera, Debug)
<< "Found registered pipeline handler '"
<< factory->name() << "'";
/*
* Try each pipeline handler until it exhaust
* all pipelines it can provide.
*/
while (1) {
std::shared_ptr<PipelineHandler> pipe = factory->create(o);
if (!pipe->match(enumerator_.get()))
break;
LOG(Camera, Debug)
<< "Pipeline handler \"" << factory->name()
<< "\" matched";
}
}
enumerator_->devicesAdded.connect(this, &Private::createPipelineHandlers);
}
void CameraManager::Private::cleanup()
{
enumerator_->devicesAdded.disconnect(this);
/*
* Release all references to cameras to ensure they all get destroyed
* before the device enumerator deletes the media devices. Cameras are
* destroyed via Object::deleteLater() API, hence we need to explicitly
* process deletion requests from the thread's message queue as the event
* loop is not in action here.
*/
cameras_.clear();
dispatchMessages(Message::Type::DeferredDelete);
enumerator_.reset(nullptr);
}
void CameraManager::Private::addCamera(std::shared_ptr<Camera> camera,
const std::vector<dev_t> &devnums)
{
MutexLocker locker(mutex_);
for (const std::shared_ptr<Camera> &c : cameras_) {
if (c->id() == camera->id()) {
LOG(Camera, Fatal)
<< "Trying to register a camera with a duplicated ID '"
<< camera->id() << "'";
return;
}
}
cameras_.push_back(std::move(camera));
unsigned int index = cameras_.size() - 1;
for (dev_t devnum : devnums)
camerasByDevnum_[devnum] = cameras_[index];
}
void CameraManager::Private::removeCamera(Camera *camera)
{
MutexLocker locker(mutex_);
auto iter = std::find_if(cameras_.begin(), cameras_.end(),
[camera](std::shared_ptr<Camera> &c) {
return c.get() == camera;
});
if (iter == cameras_.end())
return;
LOG(Camera, Debug)
<< "Unregistering camera '" << camera->id() << "'";
auto iter_d = std::find_if(camerasByDevnum_.begin(), camerasByDevnum_.end(),
[camera](const std::pair<dev_t, std::weak_ptr<Camera>> &p) {
return p.second.lock().get() == camera;
});
if (iter_d != camerasByDevnum_.end())
camerasByDevnum_.erase(iter_d);
cameras_.erase(iter);
}
/**
* \class CameraManager
* \brief Provide access and manage all cameras in the system
*
* The camera manager is the entry point to libcamera. It enumerates devices,
* associates them with pipeline managers, and provides access to the cameras
* in the system to applications. The manager owns all Camera objects and
* handles hot-plugging and hot-unplugging to manage the lifetime of cameras.
*
* To interact with libcamera, an application starts by creating a camera
* manager instance. Only a single instance of the camera manager may exist at
* a time. Attempting to create a second instance without first deleting the
* existing instance results in undefined behaviour.
*
* The manager is initially stopped, and shall be started with start(). This
* will enumerate all the cameras present in the system, which can then be
* listed with list() and retrieved with get().
*
* Cameras are shared through std::shared_ptr<>, ensuring that a camera will
* stay valid until the last reference is released without requiring any special
* action from the application. Once the application has released all the
* references it held to cameras, the camera manager can be stopped with
* stop().
*/
CameraManager *CameraManager::self_ = nullptr;
CameraManager::CameraManager()
: Extensible(std::make_unique<CameraManager::Private>())
|