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/threads.h>
25 
26 namespace android {
27 
28 class IOMXObserver;
29 struct OMXMaster;
30 struct GraphicBufferSource;
31 
32 struct OMXNodeInstance {
33     OMXNodeInstance(
34             OMX *owner, const sp<IOMXObserver> &observer, const char *name);
35 
36     void setHandle(OMX::node_id node_id, OMX_HANDLETYPE handle);
37 
38     OMX *owner();
39     sp<IOMXObserver> observer();
40     OMX::node_id nodeID();
41 
42     status_t freeNode(OMXMaster *master);
43 
44     status_t sendCommand(OMX_COMMANDTYPE cmd, OMX_S32 param);
45     status_t getParameter(OMX_INDEXTYPE index, void *params, size_t size);
46 
47     status_t setParameter(
48             OMX_INDEXTYPE index, const void *params, size_t size);
49 
50     status_t getConfig(OMX_INDEXTYPE index, void *params, size_t size);
51     status_t setConfig(OMX_INDEXTYPE index, const void *params, size_t size);
52 
53     status_t getState(OMX_STATETYPE* state);
54 
55     status_t enableGraphicBuffers(OMX_U32 portIndex, OMX_BOOL enable);
56 
57     status_t getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage);
58 
59     status_t storeMetaDataInBuffers(OMX_U32 portIndex, OMX_BOOL enable);
60 
61     status_t prepareForAdaptivePlayback(
62             OMX_U32 portIndex, OMX_BOOL enable,
63             OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight);
64 
65     status_t configureVideoTunnelMode(
66             OMX_U32 portIndex, OMX_BOOL tunneled,
67             OMX_U32 audioHwSync, native_handle_t **sidebandHandle);
68 
69     status_t useBuffer(
70             OMX_U32 portIndex, const sp<IMemory> &params,
71             OMX::buffer_id *buffer);
72 
73     status_t useGraphicBuffer(
74             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
75             OMX::buffer_id *buffer);
76 
77     status_t updateGraphicBufferInMeta(
78             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
79             OMX::buffer_id buffer);
80 
81     status_t createInputSurface(
82             OMX_U32 portIndex, sp<IGraphicBufferProducer> *bufferProducer);
83 
84     status_t signalEndOfInputStream();
85 
86     status_t allocateBuffer(
87             OMX_U32 portIndex, size_t size, OMX::buffer_id *buffer,
88             void **buffer_data);
89 
90     status_t allocateBufferWithBackup(
91             OMX_U32 portIndex, const sp<IMemory> &params,
92             OMX::buffer_id *buffer);
93 
94     status_t freeBuffer(OMX_U32 portIndex, OMX::buffer_id buffer);
95 
96     status_t fillBuffer(OMX::buffer_id buffer);
97 
98     status_t emptyBuffer(
99             OMX::buffer_id buffer,
100             OMX_U32 rangeOffset, OMX_U32 rangeLength,
101             OMX_U32 flags, OMX_TICKS timestamp);
102 
103     status_t emptyDirectBuffer(
104             OMX_BUFFERHEADERTYPE *header,
105             OMX_U32 rangeOffset, OMX_U32 rangeLength,
106             OMX_U32 flags, OMX_TICKS timestamp);
107 
108     status_t getExtensionIndex(
109             const char *parameterName, OMX_INDEXTYPE *index);
110 
111     status_t setInternalOption(
112             OMX_U32 portIndex,
113             IOMX::InternalOptionType type,
114             const void *data,
115             size_t size);
116 
117     void onMessage(const omx_message &msg);
118     void onObserverDied(OMXMaster *master);
119     void onGetHandleFailed();
120     void onEvent(OMX_EVENTTYPE event, OMX_U32 arg1, OMX_U32 arg2);
121 
122     static OMX_CALLBACKTYPE kCallbacks;
123 
124 private:
125     Mutex mLock;
126 
127     OMX *mOwner;
128     OMX::node_id mNodeID;
129     OMX_HANDLETYPE mHandle;
130     sp<IOMXObserver> mObserver;
131     bool mDying;
132 
133     // Lock only covers mGraphicBufferSource.  We can't always use mLock
134     // because of rare instances where we'd end up locking it recursively.
135     Mutex mGraphicBufferSourceLock;
136     // Access this through getGraphicBufferSource().
137     sp<GraphicBufferSource> mGraphicBufferSource;
138 
139 
140     struct ActiveBuffer {
141         OMX_U32 mPortIndex;
142         OMX::buffer_id mID;
143     };
144     Vector<ActiveBuffer> mActiveBuffers;
145 #ifdef __LP64__
146     Mutex mBufferIDLock;
147     uint32_t mBufferIDCount;
148     KeyedVector<OMX::buffer_id, OMX_BUFFERHEADERTYPE *> mBufferIDToBufferHeader;
149     KeyedVector<OMX_BUFFERHEADERTYPE *, OMX::buffer_id> mBufferHeaderToBufferID;
150 #endif
151 
152     // For debug support
153     char *mName;
154     int DEBUG;
155     size_t mNumPortBuffers[2];  // modified under mLock, read outside for debug
156     Mutex mDebugLock;
157     // following are modified and read under mDebugLock
158     int DEBUG_BUMP;
159     SortedVector<OMX_BUFFERHEADERTYPE *> mInputBuffersWithCodec, mOutputBuffersWithCodec;
160     size_t mDebugLevelBumpPendingBuffers[2];
161     void bumpDebugLevel_l(size_t numInputBuffers, size_t numOutputBuffers);
162     void unbumpDebugLevel_l(size_t portIndex);
163 
164     ~OMXNodeInstance();
165 
166     void addActiveBuffer(OMX_U32 portIndex, OMX::buffer_id id);
167     void removeActiveBuffer(OMX_U32 portIndex, OMX::buffer_id id);
168     void freeActiveBuffers();
169 
170     // For buffer id management
171     OMX::buffer_id makeBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
172     OMX_BUFFERHEADERTYPE *findBufferHeader(OMX::buffer_id buffer);
173     OMX::buffer_id findBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
174     void invalidateBufferID(OMX::buffer_id buffer);
175 
176     status_t useGraphicBuffer2_l(
177             OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
178             OMX::buffer_id *buffer);
179     static OMX_ERRORTYPE OnEvent(
180             OMX_IN OMX_HANDLETYPE hComponent,
181             OMX_IN OMX_PTR pAppData,
182             OMX_IN OMX_EVENTTYPE eEvent,
183             OMX_IN OMX_U32 nData1,
184             OMX_IN OMX_U32 nData2,
185             OMX_IN OMX_PTR pEventData);
186 
187     static OMX_ERRORTYPE OnEmptyBufferDone(
188             OMX_IN OMX_HANDLETYPE hComponent,
189             OMX_IN OMX_PTR pAppData,
190             OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
191 
192     static OMX_ERRORTYPE OnFillBufferDone(
193             OMX_IN OMX_HANDLETYPE hComponent,
194             OMX_IN OMX_PTR pAppData,
195             OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
196 
197     status_t storeMetaDataInBuffers_l(
198             OMX_U32 portIndex, OMX_BOOL enable,
199             OMX_BOOL useGraphicBuffer, OMX_BOOL *usingGraphicBufferInMeta);
200 
201     status_t emptyBuffer_l(
202             OMX_BUFFERHEADERTYPE *header,
203             OMX_U32 flags, OMX_TICKS timestamp, intptr_t debugAddr);
204 
205     sp<GraphicBufferSource> getGraphicBufferSource();
206     void setGraphicBufferSource(const sp<GraphicBufferSource>& bufferSource);
207 
208     OMXNodeInstance(const OMXNodeInstance &);
209     OMXNodeInstance &operator=(const OMXNodeInstance &);
210 };
211 
212 }  // namespace android
213 
214 #endif  // OMX_NODE_INSTANCE_H_
215