BufferStream.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @file
  30. * <b>Libargus API: BufferStream API</b>
  31. *
  32. * @b Description: Defines an OutputStream type used to write to application-managed buffers.
  33. */
  34. #ifndef _ARGUS_BUFFER_STREAM_H
  35. #define _ARGUS_BUFFER_STREAM_H
  36. namespace Argus
  37. {
  38. /**
  39. * @defgroup ArgusBufferOutputStream BufferOutputStream
  40. * @ingroup ArgusOutputStream
  41. * @ref ArgusOutputStream type that writes to application-managed buffers (STREAM_TYPE_BUFFER).
  42. *
  43. * Buffer-based OutputStream objects maintain a set of Buffer objects that are
  44. * created by the application to wrap native image buffers allocated and owned
  45. * by the application. These Buffer objects do not take possession of the native
  46. * resources, which continue to be owned by the application; rather, they are used
  47. * to control data access between libargus (as capture results are written) and the
  48. * application (as the capture results are read).
  49. *
  50. * Every Buffer stream is associated with a single BufferType, which corresponds
  51. * to the native resource type that is being wrapped by its Buffers. This BufferType
  52. * dictates which interfaces will be supported by the OutputStream and the child
  53. * BufferSettings and Buffer objects created by it, and is immutable after stream
  54. * creation.
  55. *
  56. * In addition to image data, Buffer objects may be optionally used to transport
  57. * sync information between libargus and the application in order to support hardware
  58. * level synchronization and pipelining across the API boundary. The type of sync
  59. * information, and the sync interfaces supported by a Buffer, is controlled by
  60. * the SyncType.
  61. */
  62. /**
  63. * @defgroup ArgusBufferOutputStreamSettings BufferOutputStreamSettings
  64. * @ingroup ArgusOutputStreamSettings
  65. * Settings type used to configure/create @ref ArgusBufferOutputStream streams (STREAM_TYPE_BUFFER).
  66. */
  67. /**
  68. * @ref ArgusOutputStream type that writes to application-managed Buffers.
  69. * @ingroup ArgusOutputStreamSettings
  70. */
  71. DEFINE_UUID(StreamType, STREAM_TYPE_BUFFER, c723d960,5231,11e7,9598,18,00,20,0c,9a,66);
  72. /**
  73. * @defgroup ArgusBufferBuffer Buffer Types
  74. * @ingroup ArgusBuffer
  75. * The buffer type describes the type of the image resource being wrapped by the @ref ArgusBuffer.
  76. */
  77. /**
  78. * @defgroup ArgusBufferBufferSettings Buffer Types
  79. * @ingroup ArgusBufferSettings
  80. * Provides buffer type specific configuration settings.
  81. */
  82. DEFINE_NAMED_UUID_CLASS(BufferType);
  83. DEFINE_UUID(BufferType, BUFFER_TYPE_NONE, c723d961,5231,11e7,9598,18,00,20,0c,9a,66);
  84. /**
  85. * @defgroup ArgusBufferSync Sync Types
  86. * @ingroup ArgusBuffer
  87. * The sync type describes the type of sync object to use with the @ref ArgusBuffer.
  88. */
  89. /**
  90. * @defgroup ArgusBufferBufferSettings Buffer Types
  91. * @ingroup ArgusBufferSettings
  92. * Provides sync type specific configuration settings.
  93. */
  94. DEFINE_NAMED_UUID_CLASS(SyncType);
  95. DEFINE_UUID(SyncType, SYNC_TYPE_NONE, c723d962,5231,11e7,9598,18,00,20,0c,9a,66);
  96. /**
  97. * Object that wraps an application-managed buffer for use as a capture request destination.
  98. *
  99. * Every Buffer is associated with a single BufferType, which corresponds to the
  100. * native resource type that is being wrapped by it, and dictates which interfaces
  101. * it will support.
  102. *
  103. * In addition to image data, Buffer objects may optionally transport sync
  104. * information between libargus and the application in order to support hardware
  105. * level synchronization and pipelining across the API boundary. The type of sync
  106. * information, and the sync interfaces supported by a Buffer, is controlled by
  107. * the SyncType.
  108. *
  109. * All Buffer objects will support the IBuffer interface in order to query the
  110. * core BufferType and SyncType.
  111. *
  112. * @defgroup ArgusBuffer Buffer
  113. * @ingroup ArgusObjects
  114. */
  115. class Buffer : public InterfaceProvider, public Destructable
  116. {
  117. protected:
  118. ~Buffer() {}
  119. };
  120. /**
  121. * Container for settings used to configure/create a @ref ArgusBuffer.
  122. *
  123. * These objects are created by IBufferOutputStream::createBufferSettings, and
  124. * are used to configure the various parameters required for Buffer creation.
  125. * Since the Buffer OutputStream which creates this object uses a single
  126. * BufferType and SyncType, the interfaces supported by BufferSettings objects
  127. * are dictated by these types.
  128. *
  129. * @defgroup ArgusBufferSettings BufferSettings
  130. * @ingroup ArgusObjects
  131. */
  132. class BufferSettings : public InterfaceProvider, public Destructable
  133. {
  134. protected:
  135. ~BufferSettings() {}
  136. };
  137. /**
  138. * @class IBufferOutputStreamSettings
  139. *
  140. * Interface that exposes the configuration available to Buffer-based OutputStreams.
  141. *
  142. * @ingroup ArgusBufferOutputStreamSettings
  143. */
  144. DEFINE_UUID(InterfaceID, IID_BUFFER_OUTPUT_STREAM_SETTINGS,
  145. c723d963,5231,11e7,9598,18,00,20,0c,9a,66);
  146. class IBufferOutputStreamSettings : public Interface
  147. {
  148. public:
  149. static const InterfaceID& id() { return IID_BUFFER_OUTPUT_STREAM_SETTINGS; }
  150. /**
  151. * Sets the BufferType for the stream. This controls which type of native buffer
  152. * type will be wrapped by this OutputStream, and thus will dictate which interfaces
  153. * are supported by the OutputStream and child BufferSettings and Buffer objects.
  154. * This value defaults to BUFFER_TYPE_NONE and must be set by the application
  155. * to a BufferType supported by this libargus implementation.
  156. *
  157. * @param[in] type The BufferType to use for the new OutputStream.
  158. */
  159. virtual Status setBufferType(const BufferType& type) = 0;
  160. /**
  161. * Returns the BufferType to be used for the stream.
  162. */
  163. virtual BufferType getBufferType() const = 0;
  164. /**
  165. * Sets the SyncType for the stream. This controls which type of native sync
  166. * information will be attached to Buffers for sync support between libargus
  167. * and the application.
  168. * This value defaults to SYNC_TYPE_NONE, which means that no sync information
  169. * will be supported. In this case, both the application and libargus are
  170. * expected to be done all read and/or write operations before passing the
  171. * Buffer to the other.
  172. *
  173. * @param[in] type The SyncType to use for the new OutputStream.
  174. */
  175. virtual Status setSyncType(const SyncType& type) = 0;
  176. /**
  177. * Returns the SyncType to be used for the stream.
  178. */
  179. virtual SyncType getSyncType() const = 0;
  180. /**
  181. * Sets the metadata enable for the stream. When metadata is enabled, a CaptureMetadata
  182. * object may be attached to each Buffer when it is output from libargus as the result
  183. * of a successful capture request (see IBuffer::getMetadata).
  184. *
  185. * @param[in] enable Whether or not metadata is enabled for the stream.
  186. */
  187. virtual void setMetadataEnable(bool enable) = 0;
  188. /**
  189. * Returns the metadata enable.
  190. */
  191. virtual bool getMetadataEnable() const = 0;
  192. protected:
  193. ~IBufferOutputStreamSettings() {}
  194. };
  195. /**
  196. * @class IBufferOutputStream
  197. *
  198. * Interface that provides the methods used with Buffer-based OutputStreams.
  199. *
  200. * @ingroup ArgusBufferOutputStream
  201. */
  202. DEFINE_UUID(InterfaceID, IID_BUFFER_OUTPUT_STREAM, c723d964,5231,11e7,9598,18,00,20,0c,9a,66);
  203. class IBufferOutputStream : public Interface
  204. {
  205. public:
  206. static const InterfaceID& id() { return IID_BUFFER_OUTPUT_STREAM; }
  207. /**
  208. * Returns the BufferType of the stream.
  209. */
  210. virtual BufferType getBufferType() const = 0;
  211. /**
  212. * @returns the SyncType of the stream.
  213. */
  214. virtual SyncType getSyncType() const = 0;
  215. /**
  216. * Creates a BufferSettings object. This Destructable object is used to configure
  217. * the settings for a new Buffer object, including things like the native buffer
  218. * handle that is to be wrapped by the Buffer. The interfaces and settings that
  219. * are supported by the new BufferSettings object are dictated by the BufferType
  220. * and SyncType of the creating OutputStream.
  221. *
  222. * @param[out] status An optional pointer to return success/status.
  223. *
  224. * @returns a new BufferSettings, or NULL on failure (error code written to 'status').
  225. */
  226. virtual BufferSettings* createBufferSettings(Status* status = NULL) = 0;
  227. /**
  228. * Creates a Buffer object. All of the settings used to configure Buffer creation
  229. * are provided by the BufferSettings object (which continues to be owned by the
  230. * application and can be reused until destroyed).
  231. *
  232. * New Buffer objects are returned to the application in the "acquired" state,
  233. * meaning that the application must call releaseBuffer on the Buffer before it
  234. * may be used by libargus.
  235. *
  236. * @param[in] settings the buffer settings to use for Buffer creation.
  237. * @param[out] status An optional pointer to return success/status.
  238. *
  239. * @returns a new BufferSettings, or NULL on failure (error code written to 'status').
  240. */
  241. virtual Buffer* createBuffer(const BufferSettings* settings, Status* status = NULL) = 0;
  242. /**
  243. * Acquires a Buffer from the stream that was written to by a libargus capture request.
  244. *
  245. * Buffers are acquired from the stream in FIFO order relative to when they are
  246. * produced by libargus (which may not match the original request submission order).
  247. * If a non-zero timeout is provided, this operation will block until a new Buffer
  248. * is produced by libargus or the timeout period is exceeded.
  249. *
  250. * Once a Buffer has been acquired, the application will have exclusive access to the
  251. * Buffer's image data, which it will retain until the Buffer is released back to the
  252. * stream for further capture request use via releaseBuffer. Buffers may also be
  253. * destroyed while acquired; doing so prevents any further use of the Buffer object
  254. * within the Stream and releases any buffer resources or references held by the Buffer
  255. * object.
  256. *
  257. * If sync support has been enabled for this Stream/Buffer (ie. SyncType is not
  258. * STREAM_TYPE_NONE), hardware synchronization capabilities may be used to allow hardware
  259. * operations on a Buffer to still be pending when it is acquired from or released back to
  260. * libargus. In this case, the returned Buffer will contain the output sync information
  261. * provided by libargus which the application must obey before accessing the Buffer's
  262. * image data. Similarly, the application may need to write input sync information to
  263. * the Buffer before calling releaseBuffer such that libargus will obey the sync before
  264. * the Buffer is written to by a new capture request. The exact mechanism used for reading
  265. * and writing this sync state depends on and is documented by the various SyncTypes and
  266. * their corresponding interfaces.
  267. *
  268. * @param[in] timeout The amount of time to allow for the acquire.
  269. * @param[out] status An optional pointer to return success/status.
  270. *
  271. * @returns A Buffer that has been written to by a capture request
  272. */
  273. virtual Buffer* acquireBuffer(uint64_t timeout = TIMEOUT_INFINITE, Status* status = NULL) = 0;
  274. /**
  275. * Release a Buffer back to the stream to make it available for a future capture request.
  276. *
  277. * Once a Buffer has been released to the Stream, libargus will have exclusive access to
  278. * the Buffer's image resources until it is once again acquired by the application via
  279. * acquireBuffer. Any buffer access outside of libargus during this time may lead to
  280. * undefined results.
  281. *
  282. * While it is often the case that Buffers may be used by libargus in the order they are
  283. * released, this is not a requirement; libargus may reuse Buffers in any order once they
  284. * have been released.
  285. *
  286. * If sync support has been enabled for this StreamBuffer (ie. SyncType is not
  287. * STREAM_TYPE_NONE), sync information may need to be written to the Buffer by the client
  288. * before releaseBuffer is called. The exact mechanism used for writing this sync state
  289. * depends on and is documented by the various SyncTypes and their corresponding interfaces.
  290. *
  291. * Note that while it is safe to destroy a Buffer object while it has been released to
  292. * libargus, it is possible that pending requests may be using this Buffer and may still
  293. * output Events that reference the Buffer object, and so the application is responsible
  294. * for making sure that it does not use any Buffer object that it has previously destroyed.
  295. * If there are no pending requests using a particular Stream, destroying any of its
  296. * released Buffers will prevent them from ever being used or returned by libargus again.
  297. *
  298. * @param[in] buffer The Buffer to release back to the stream.
  299. */
  300. virtual Status releaseBuffer(Buffer* buffer) = 0;
  301. /**
  302. * Signals the end of the stream.
  303. *
  304. * Once the end of stream has been signalled on a stream, any call made to acquireBuffer
  305. * will immediately (ignoring the timeout parameter) return NULL with a STATUS_END_OF_STREAM
  306. * status when the following is true:
  307. * 1) There are no Buffers immediately available to be acquired, and
  308. * 2) There are no capture requests pending writes to the stream.
  309. * This implies that no pending or completed frames will be lost, and that all pending or
  310. * completed frames must be acquired before an END_OF_STREAM status is returned.
  311. *
  312. * If any thread is blocked in acquireBuffer when the end of stream is signalled, and the
  313. * above conditions are met, then that thread will unblock and return END_OF_STREAM immediately.
  314. */
  315. virtual Status endOfStream() = 0;
  316. protected:
  317. ~IBufferOutputStream() {}
  318. };
  319. /**
  320. * @class IBuffer
  321. *
  322. * Interface that provides the core methods for Buffer objects.
  323. *
  324. * @ingroup ArgusBuffer
  325. */
  326. DEFINE_UUID(InterfaceID, IID_BUFFER, c723d965,5231,11e7,9598,18,00,20,0c,9a,66);
  327. class IBuffer : public Interface
  328. {
  329. public:
  330. static const InterfaceID& id() { return IID_BUFFER; }
  331. /**
  332. * Returns the BufferType of the Buffer.
  333. */
  334. virtual BufferType getBufferType() const = 0;
  335. /**
  336. * Returns the SyncType of the Buffer.
  337. */
  338. virtual SyncType getSyncType() const = 0;
  339. /**
  340. * Sets the client data for the Buffer.
  341. * This is provided as a convenience for applications to be able to map Buffers to
  342. * other client-managed data. It is not used at all by the libargus implementation,
  343. * and is returned as-is by getClientData.
  344. * Default value: NULL
  345. *
  346. * @param[in] clientData The client data pointer to set in the buffer.
  347. */
  348. virtual void setClientData(const void* clientData) = 0;
  349. /**
  350. * Returns the client data from the Buffer.
  351. */
  352. virtual const void* getClientData() const = 0;
  353. /**
  354. * Returns the CaptureMetadata object that was attached to this Buffer when it was last
  355. * output to the stream from the result of a successful capture request.
  356. *
  357. * This method should only ever be called while the Buffer is in an acquired state; ie. the
  358. * time between when the Buffer was acquired by IBufferOutputStream::acquireBuffer and when
  359. * it was released by IBufferOutputStream::releaseBuffer. If called outside of the acquired
  360. * state, NULL will be returned. Similarly, the returned object will only remain valid so
  361. * long as the Buffer is acquired -- if this object or any of its interfaces are accessed
  362. * outside of the acquired state, undefined results or abnormal process termination may occur.
  363. *
  364. * Metadata will only be written if metadata is enabled for the stream (see
  365. * IBufferOutputStreamSettings::setMetadataEnable). NULL may also still be returned if there
  366. * were any capture errors or metadata is otherwise unavailable.
  367. */
  368. virtual const CaptureMetadata* getMetadata() const = 0;
  369. protected:
  370. ~IBuffer() {}
  371. };
  372. } // namespace Argus
  373. #endif // _ARGUS_BUFFER_STREAM_H