FrameProducer.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2022, 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. #ifndef _EGLSTREAM_FRAME_PRODUCER_H
  29. #define _EGLSTREAM_FRAME_PRODUCER_H
  30. #include "FrameBuf.h"
  31. #include "Argus/Types.h"
  32. namespace EGLStream
  33. {
  34. /**
  35. * A FrameProducer object acts as a producer point to an InputStream or
  36. * EGLStream, provided during creation, and exposes interfaces to return
  37. * Frame objects that provide various image buffer related interfaces.
  38. *
  39. * The FrameProducer is responsible for allocating and managing all FrameBuf objects
  40. * that are used in the stream. and these FrameBuf objects will exist in one of three states:
  41. *
  42. * Available: Unused FrameByuf objects that may be immediately retrieved for use with getFrame().
  43. * No other components hold references to Frame objects in this state.
  44. *
  45. * Pending: FrameBuf objects that have been returned by getFrame() and are currently being used
  46. * in libargus. FrameBuf objects in this state will be assigned to capture request
  47. * and should not be modified by the FrameProducer until presented/aborted.
  48. *
  49. * Presented: FrameBuf objects that have been presented to EGL Input Stream. FrameBuf objects
  50. * in this state are being used by the EGLStream and/or the consumer, and should not
  51. * be modified until the frame is returned by EGL Input Stream, and FrameBuf will be
  52. * locked while in this state.
  53. *
  54. * ===== FrameBuf State Diagram =====
  55. *
  56. * FrameProducer::create()
  57. * *
  58. * |
  59. * | allocate and register FrameBuf
  60. * |
  61. * v
  62. * +------------------------------------+
  63. * | |
  64. * | Available FrameBuf |----+
  65. * | | |
  66. * +------------------------------------+ |
  67. * | ^ |
  68. * getFrame() | | aborted FrameBuf |
  69. * v | |
  70. * +------------------------------------+ |
  71. * | | |
  72. * | Pending FrameBuf | | getFrame()
  73. * | | | [unlocks FrameBuf]
  74. * +------------------------------------+ |
  75. * | |
  76. * presentFrame() | |
  77. * [locks buffer] | |
  78. * v |
  79. * +------------------------------------+ |
  80. * | | |
  81. * | Presented FrameBuf |----+
  82. * | |
  83. * | These buffers have been presented |
  84. * | to the EGLStream and will be |
  85. * | returned by the consumer/EGL |
  86. * | |
  87. * +------------------------------------+
  88. */
  89. /**
  90. * Destroying a Producer will disconnect the producer from the EGLStream, but
  91. * Frame objects returned by IFrameProducer::presentFrame will persist until
  92. * the application explicitly destroys those objects.
  93. */
  94. class FrameProducer : public Argus::InterfaceProvider, public Argus::Destructable
  95. {
  96. public:
  97. /**
  98. * Creates a new FrameProducer to produce frames for an Argus InputStream.
  99. *
  100. * @param[in] inputStream The input stream to write into.
  101. * @param[in] phase The input stream raw bayer data phase.
  102. * @param[out] status An optional pointer to return an error status code.
  103. *
  104. * @returns A new FrameProducer object, or NULL on error.
  105. */
  106. static FrameProducer* create(Argus::InputStream* inputStream,
  107. const Argus::BayerPhase &phase,
  108. Argus::Status* status = NULL);
  109. /**
  110. * Creates a new FrameProducer to write frames into an EGLStream.
  111. *
  112. * @param[in] eglDisplay The EGLDisplay the stream belongs to.
  113. * @param[in] eglStream The EGLStream to connect to.
  114. * @param[in] size The EGLStream buffer size.
  115. * @param[in] format The EGLStream buffer pixel format.
  116. * @param[in] phase The input stream raw bayer data phase.
  117. * @param[out] status An optional pointer to return an error status code.
  118. *
  119. * @returns A new FrameProducer object, or NULL on error.
  120. */
  121. static FrameProducer* create(EGLDisplay eglDisplay,
  122. EGLStreamKHR eglStream,
  123. const Argus::Size2D<uint32_t> &size,
  124. const Argus::PixelFormat &format,
  125. const Argus::BayerPhase &phase,
  126. Argus::Status* status = NULL);
  127. protected:
  128. ~FrameProducer() {}
  129. };
  130. /**
  131. * @class IFrameProducer
  132. *
  133. * Exposes the methods used to present Frames from a FrameProducer.
  134. */
  135. DEFINE_UUID(Argus::InterfaceID, IID_FRAME_PRODUCER, b94a7bd1,c3c8,11e5,a837,08,00,20,0c,9a,66);
  136. class IFrameProducer : public Argus::Interface
  137. {
  138. public:
  139. static const Argus::InterfaceID& id() { return IID_FRAME_PRODUCER; }
  140. /**
  141. * Get a new frame from the EGL Stream, returning a Frame object. This Frame object
  142. * behaves as its own entity, and may persist even after the FrameProducer is destroyed.
  143. * It is the application's responsibility to destroy any Frame returned by this method.
  144. *
  145. * Destroying a Frame causes all resources held by that frame to be returned to the EGLStream
  146. * so that they may be used to produce another frame. If too many Frames are held
  147. * by the producer, or these frames are presented at a slower rate than the consumer is
  148. * consuming frames, it may be possible to stall the producer. Frame objects should always be
  149. * be destroyed as soon as possible to minimize resource overhead.
  150. *
  151. * If NULL is returned and the status code is STATUS_DISCONNECTED, the producer has
  152. * disconnected from the stream and no more frames can ever be acquired from this consumer.
  153. *
  154. * @param[in] timeout The timeout (in nanoseconds) to wait for a frame if one isn't available.
  155. * @param[out] status An optional pointer to return an error status code.
  156. *
  157. * @returns A pointer to the frame acquired from the stream, or NULL on error.
  158. */
  159. virtual Argus::Status getFrame(FrameBuf** frame,
  160. uint64_t timeout = Argus::TIMEOUT_INFINITE) = 0;
  161. /**
  162. * Presents a pending buffer to the EGLStream.
  163. *
  164. * @param[in] frame The buffer to present. This must have been previously
  165. * returned by getFrame.
  166. * @returns success/status of this call.
  167. */
  168. virtual Argus::Status presentFrame(FrameBuf *frame) = 0;
  169. /**
  170. * Return a aborted buffer to the free queue.
  171. *
  172. * @param[in] buffer The buffer to return. This must have been previously
  173. * returned by getFrame.
  174. */
  175. virtual Argus::Status returnAbortedFrame(FrameBuf *frame) = 0;
  176. protected:
  177. ~IFrameProducer() {}
  178. };
  179. } // namespace EGLStream
  180. #endif // _EGLSTREAM_FRAME_PRODUCER_H