• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef OMX_NODE_INSTANCE_H_
18 
19 #define OMX_NODE_INSTANCE_H_
20 
21 #include "OMX.h"
22 
23 #include <utils/RefBase.h>
24 #include <utils/SortedVector.h>
25 #include <utils/threads.h>
26 
27 namespace android {
28 
29 class IOMXObserver;
30 struct OMXMaster;
31 class GraphicBufferSource;
32 
33 struct OMXNodeInstance {
34     OMXNodeInstance(
35             OMX *owner, const sp<IOMXObserver> &observer, const char *name);
36 
37     void setHandle(OMX::node_id node_id, OMX_HANDLETYPE handle);
38 
39     OMX *owner();
40     sp<IOMXObserver> observer();
41     OMX::node_id nodeID();
42 
43     status_t freeNode(OMXMaster *master);
44 
45     status_t sendCommand(OMX_COMMANDTYPE cmd, OMX_S32 param);
46     status_t getParameter(OMX_INDEXTYPE index, void *params, size_t size);
47 
48     status_t setParameter(
49             OMX_INDEXTYPE index, const void *params, size_t size);
50 
51     status_t getConfig(OMX_INDEXTYPE index, void *params, size_t size);
52     status_t setConfig(OMX_INDEXTYPE index, const void *params, size_t size);
53 
54     status_t getState(OMX_STATETYPE* state);
55 
56     status_t enableNativeBuffers(OMX_U32 portIndex, OMX_BOOL graphic, OMX_BOOL enable);
57 
58     status_t getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage);
59 
60     status_t storeMetaDataInBuffers(
61             OMX_U32 portIndex, OMX_BOOL enable, MetadataBufferType *type);
62 
63     status_t prepareForAdaptivePlayback(
64             OMX_U32 portIndex, OMX_BOOL enable,
65             OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight);
66 
67     status_t configureVideoTunnelMode(
68             OMX_U32 portIndex, OMX_BOOL tunneled,
69             OMX_U32 audioHwSync, native_handle_t **sidebandHandle);
70 
71     status_t useBuffer(
72             OMX_U32 portIndex, const sp<IMemory> &params,
73             OMX::buffer_id *buffer, OMX_U32 allottedSize);
74 
75     status_t useGraphicBuffer(
76             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
77             OMX::buffer_id *buffer);
78 
79     status_t updateGraphicBufferInMeta(
80             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
81             OMX::buffer_id buffer);
82 
83     status_t updateNativeHandleInMeta(
84             OMX_U32 portIndex, const sp<NativeHandle> &nativeHandle,
85             OMX::buffer_id buffer);
86 
87     status_t createInputSurface(
88             OMX_U32 portIndex, android_dataspace dataSpace,
89             sp<IGraphicBufferProducer> *bufferProducer,
90             MetadataBufferType *type);
91 
92     static status_t createPersistentInputSurface(
93             sp<IGraphicBufferProducer> *bufferProducer,
94             sp<IGraphicBufferConsumer> *bufferConsumer);
95 
96     status_t setInputSurface(
97             OMX_U32 portIndex, const sp<IGraphicBufferConsumer> &bufferConsumer,
98             MetadataBufferType *type);
99 
100     status_t signalEndOfInputStream();
101 
102     void signalEvent(OMX_EVENTTYPE event, OMX_U32 arg1, OMX_U32 arg2);
103 
104     status_t allocateSecureBuffer(
105             OMX_U32 portIndex, size_t size, OMX::buffer_id *buffer,
106             void **buffer_data, sp<NativeHandle> *native_handle);
107 
108     status_t allocateBufferWithBackup(
109             OMX_U32 portIndex, const sp<IMemory> &params,
110             OMX::buffer_id *buffer, OMX_U32 allottedSize);
111 
112     status_t freeBuffer(OMX_U32 portIndex, OMX::buffer_id buffer);
113 
114     status_t fillBuffer(OMX::buffer_id buffer, int fenceFd);
115 
116     status_t emptyBuffer(
117             OMX::buffer_id buffer,
118             OMX_U32 rangeOffset, OMX_U32 rangeLength,
119             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd);
120 
121     status_t emptyGraphicBuffer(
122             OMX_BUFFERHEADERTYPE *header, const sp<GraphicBuffer> &buffer,
123             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd);
124 
125     status_t getExtensionIndex(
126             const char *parameterName, OMX_INDEXTYPE *index);
127 
128     status_t setInternalOption(
129             OMX_U32 portIndex,
130             IOMX::InternalOptionType type,
131             const void *data,
132             size_t size);
133 
isSecureOMXNodeInstance134     bool isSecure() const {
135         return mIsSecure;
136     }
137 
138     // handles messages and removes them from the list
139     void onMessages(std::list<omx_message> &messages);
140     void onMessage(const omx_message &msg);
141     void onObserverDied(OMXMaster *master);
142     void onGetHandleFailed();
143     void onEvent(OMX_EVENTTYPE event, OMX_U32 arg1, OMX_U32 arg2);
144 
145     static OMX_CALLBACKTYPE kCallbacks;
146 
147 private:
148     Mutex mLock;
149 
150     OMX *mOwner;
151     OMX::node_id mNodeID;
152     OMX_HANDLETYPE mHandle;
153     sp<IOMXObserver> mObserver;
154     bool mDying;
155     bool mSailed;  // configuration is set (no more meta-mode changes)
156     bool mQueriedProhibitedExtensions;
157     SortedVector<OMX_INDEXTYPE> mProhibitedExtensions;
158     bool mIsSecure;
159 
160     // Lock only covers mGraphicBufferSource.  We can't always use mLock
161     // because of rare instances where we'd end up locking it recursively.
162     Mutex mGraphicBufferSourceLock;
163     // Access this through getGraphicBufferSource().
164     sp<GraphicBufferSource> mGraphicBufferSource;
165 
166 
167     struct ActiveBuffer {
168         OMX_U32 mPortIndex;
169         OMX::buffer_id mID;
170     };
171     Vector<ActiveBuffer> mActiveBuffers;
172     // for buffer ptr to buffer id translation
173     Mutex mBufferIDLock;
174     uint32_t mBufferIDCount;
175     KeyedVector<OMX::buffer_id, OMX_BUFFERHEADERTYPE *> mBufferIDToBufferHeader;
176     KeyedVector<OMX_BUFFERHEADERTYPE *, OMX::buffer_id> mBufferHeaderToBufferID;
177 
178     // metadata and secure buffer type tracking
179     MetadataBufferType mMetadataType[2];
180     enum SecureBufferType {
181         kSecureBufferTypeUnknown,
182         kSecureBufferTypeOpaque,
183         kSecureBufferTypeNativeHandle,
184     };
185     SecureBufferType mSecureBufferType[2];
186 
187     // For debug support
188     char *mName;
189     int DEBUG;
190     size_t mNumPortBuffers[2];  // modified under mLock, read outside for debug
191     Mutex mDebugLock;
192     // following are modified and read under mDebugLock
193     int DEBUG_BUMP;
194     SortedVector<OMX_BUFFERHEADERTYPE *> mInputBuffersWithCodec, mOutputBuffersWithCodec;
195     size_t mDebugLevelBumpPendingBuffers[2];
196     void bumpDebugLevel_l(size_t numInputBuffers, size_t numOutputBuffers);
197     void unbumpDebugLevel_l(size_t portIndex);
198 
199     ~OMXNodeInstance();
200 
201     void addActiveBuffer(OMX_U32 portIndex, OMX::buffer_id id);
202     void removeActiveBuffer(OMX_U32 portIndex, OMX::buffer_id id);
203     void freeActiveBuffers();
204 
205     // For buffer id management
206     OMX::buffer_id makeBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
207     OMX_BUFFERHEADERTYPE *findBufferHeader(OMX::buffer_id buffer, OMX_U32 portIndex);
208     OMX::buffer_id findBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
209     void invalidateBufferID(OMX::buffer_id buffer);
210 
211     bool isProhibitedIndex_l(OMX_INDEXTYPE index);
212 
213     status_t useGraphicBuffer2_l(
214             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
215             OMX::buffer_id *buffer);
216     static OMX_ERRORTYPE OnEvent(
217             OMX_IN OMX_HANDLETYPE hComponent,
218             OMX_IN OMX_PTR pAppData,
219             OMX_IN OMX_EVENTTYPE eEvent,
220             OMX_IN OMX_U32 nData1,
221             OMX_IN OMX_U32 nData2,
222             OMX_IN OMX_PTR pEventData);
223 
224     static OMX_ERRORTYPE OnEmptyBufferDone(
225             OMX_IN OMX_HANDLETYPE hComponent,
226             OMX_IN OMX_PTR pAppData,
227             OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
228 
229     static OMX_ERRORTYPE OnFillBufferDone(
230             OMX_IN OMX_HANDLETYPE hComponent,
231             OMX_IN OMX_PTR pAppData,
232             OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
233 
234     status_t storeMetaDataInBuffers_l(
235             OMX_U32 portIndex, OMX_BOOL enable, MetadataBufferType *type);
236 
237     // Stores fence into buffer if it is ANWBuffer type and has enough space.
238     // otherwise, waits for the fence to signal.  Takes ownership of |fenceFd|.
239     status_t storeFenceInMeta_l(
240             OMX_BUFFERHEADERTYPE *header, int fenceFd, OMX_U32 portIndex);
241 
242     // Retrieves the fence from buffer if ANWBuffer type and has enough space. Otherwise, returns -1
243     int retrieveFenceFromMeta_l(
244             OMX_BUFFERHEADERTYPE *header, OMX_U32 portIndex);
245 
246     status_t emptyBuffer_l(
247             OMX_BUFFERHEADERTYPE *header,
248             OMX_U32 flags, OMX_TICKS timestamp, intptr_t debugAddr, int fenceFd);
249 
250     // Updates the graphic buffer handle in the metadata buffer for |buffer| and |header| to
251     // |graphicBuffer|'s handle. If |updateCodecBuffer| is true, the update will happen in
252     // the actual codec buffer (use this if not using emptyBuffer (with no _l) later to
253     // pass the buffer to the codec, as only emptyBuffer copies the backup buffer to the codec
254     // buffer.)
255     status_t updateGraphicBufferInMeta_l(
256             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
257             OMX::buffer_id buffer, OMX_BUFFERHEADERTYPE *header, bool updateCodecBuffer);
258 
259     status_t createGraphicBufferSource(
260             OMX_U32 portIndex, sp<IGraphicBufferConsumer> consumer /* nullable */,
261             MetadataBufferType *type);
262     sp<GraphicBufferSource> getGraphicBufferSource();
263     void setGraphicBufferSource(const sp<GraphicBufferSource>& bufferSource);
264 
265     // Handles |msg|, and may modify it. Returns true iff completely handled it and
266     // |msg| does not need to be sent to the event listener.
267     bool handleMessage(omx_message &msg);
268 
269     OMXNodeInstance(const OMXNodeInstance &);
270     OMXNodeInstance &operator=(const OMXNodeInstance &);
271 };
272 
273 }  // namespace android
274 
275 #endif  // OMX_NODE_INSTANCE_H_
276