Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Composer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, 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 #ifndef COMPOSER_H
30 #define COMPOSER_H
31 
32 #include <EGL/egl.h>
33 #include <EGL/eglext.h>
34 
35 #include <list>
36 
37 #include "Window.h"
38 #include "Thread.h"
39 #include "Mutex.h"
40 #include "ConditionVariable.h"
41 
42 #include "Ordered.h"
43 #include "GLContext.h"
44 
45 namespace ArgusSamples
46 {
47 
48 class StreamConsumer;
49 
50 /**
51  * The composer is used to render multiple EGL streams into the windows. The streams are arranged
52  * into a regular grid.
53  * Streams are only composed if the client indicates that by calling reCompose(). This updates
54  * a client sequence count and wakes up the composer thread.
55  * All functions modifying the stream configuration (bind/unbind/setActive/setAspectRatio) need to
56  * trigger a re-compose.
57  */
58 class Composer : public Thread, public Window::IResizeObserver
59 {
60 public:
61  Composer();
62  ~Composer();
63 
64  bool initialize();
65  bool shutdown();
66 
67  /**
68  * Bind an EGL stream. A bound and active stream is rendered. Newly bound streams are inactive.
69  *
70  * @param eglStream [in]
71  */
72  bool bindStream(EGLStreamKHR eglStream);
73 
74  /**
75  * Unbind a bound EGL stream.
76  *
77  * @param eglStream [in]
78  */
79  bool unbindStream(EGLStreamKHR eglStream);
80 
81  /**
82  * Set the active state of the stream, only active streams are rendered
83  *
84  * @param eglStream [in]
85  * @param active [in]
86  */
87  bool setStreamActive(EGLStreamKHR eglStream, bool active);
88 
89  /**
90  * Set the stream aspect ratio
91  *
92  * @param eglStream [in]
93  * @param aspectRatio [in] aspect ration of the images transported by the stream
94  */
95  bool setStreamAspectRatio(EGLStreamKHR eglStream, float aspectRatio);
96 
97  /**
98  * Get the GL context the composer is rendering into
99  */
100  const GLContext& getContext() const
101  {
102  return m_context;
103  }
104 
105  /**
106  * Trigger a re-compose. Called when new images arrived in a stream or the stream configuration
107  * changed.
108  */
109  bool reCompose();
110 
111 private:
112  /** @name Thread methods */
113  /**@{*/
114  virtual bool threadInitialize();
115  virtual bool threadExecute();
116  virtual bool threadShutdown();
117  /**@}*/
118 
119  /** @name IResizeObserver methods */
120  /**@{*/
121  virtual bool onResize(uint32_t width, uint32_t height);
122  /**@}*/
123 
124  bool m_initialized; ///< set if initialized
125  GLContext m_context; ///< GL context
126  uint32_t m_program; ///< program to render streams
127  uint32_t m_windowWidth; ///< window width
128  uint32_t m_windowHeight; ///< window height
129  float m_windowAspectRatio; ///< window aspect ratio
130 
131  Mutex m_mutex; ///< to protect access to the stream array
132 
133  Mutex m_sequenceMutex; ///< sequence mutex
134  ConditionVariable m_sequenceCond; ///< sequence condition variable
135  Ordered<uint32_t> m_clientSequence; ///< client sequence counter
136 
137  uint32_t m_composerSequence; ///< composer sequence counter
138 
139  /**
140  * Each bound EGL stream has a stream consumer and can be active or inactive.
141  */
142  class Stream
143  {
144  public:
145  explicit Stream(StreamConsumer *consumer)
146  : m_consumer(consumer)
147  , m_active(false)
148  {
149  }
150 
151  StreamConsumer *m_consumer; ///< the stream consumer
152  bool m_active; ///< if set then the stream is active and rendered
153  };
154 
155  typedef std::list<Stream> StreamList; ///< a list of streams
156  StreamList m_streams; ///< the list of composed streams
157 };
158 
159 }; // namespace ArgusSamples
160 
161 #endif // COMPOSER_H