OMXNodeInstance.h revision 09fcc862f226df509cde12923d92cf6db53c4108
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
27namespace android {
28
29class IOMXObserver;
30struct OMXMaster;
31class GraphicBufferSource;
32
33struct 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    status_t allocateSecureBuffer(
103            OMX_U32 portIndex, size_t size, OMX::buffer_id *buffer,
104            void **buffer_data, sp<NativeHandle> *native_handle);
105
106    status_t allocateBufferWithBackup(
107            OMX_U32 portIndex, const sp<IMemory> &params,
108            OMX::buffer_id *buffer, OMX_U32 allottedSize);
109
110    status_t freeBuffer(OMX_U32 portIndex, OMX::buffer_id buffer);
111
112    status_t fillBuffer(OMX::buffer_id buffer, int fenceFd);
113
114    status_t emptyBuffer(
115            OMX::buffer_id buffer,
116            OMX_U32 rangeOffset, OMX_U32 rangeLength,
117            OMX_U32 flags, OMX_TICKS timestamp, int fenceFd);
118
119    status_t emptyGraphicBuffer(
120            OMX::buffer_id buffer, const sp<GraphicBuffer> &graphicBuffer,
121            OMX_U32 flags, OMX_TICKS timestamp, int fenceFd);
122
123    status_t getExtensionIndex(
124            const char *parameterName, OMX_INDEXTYPE *index);
125
126    status_t setInternalOption(
127            OMX_U32 portIndex,
128            IOMX::InternalOptionType type,
129            const void *data,
130            size_t size);
131
132    bool isSecure() const {
133        return mIsSecure;
134    }
135
136    // handles messages and removes them from the list
137    void onMessages(std::list<omx_message> &messages);
138    void onObserverDied(OMXMaster *master);
139    void onGetHandleFailed();
140    void onEvent(OMX_EVENTTYPE event, OMX_U32 arg1, OMX_U32 arg2);
141
142    static OMX_CALLBACKTYPE kCallbacks;
143
144private:
145    Mutex mLock;
146
147    OMX *mOwner;
148    OMX::node_id mNodeID;
149    OMX_HANDLETYPE mHandle;
150    sp<IOMXObserver> mObserver;
151    bool mDying;
152    bool mSailed;  // configuration is set (no more meta-mode changes)
153    bool mQueriedProhibitedExtensions;
154    SortedVector<OMX_INDEXTYPE> mProhibitedExtensions;
155    bool mIsSecure;
156
157    // Lock only covers mGraphicBufferSource.  We can't always use mLock
158    // because of rare instances where we'd end up locking it recursively.
159    Mutex mGraphicBufferSourceLock;
160    // Access this through getGraphicBufferSource().
161    sp<GraphicBufferSource> mGraphicBufferSource;
162
163    struct ActiveBuffer {
164        OMX_U32 mPortIndex;
165        OMX::buffer_id mID;
166    };
167    Vector<ActiveBuffer> mActiveBuffers;
168    // for buffer ptr to buffer id translation
169    Mutex mBufferIDLock;
170    uint32_t mBufferIDCount;
171    KeyedVector<OMX::buffer_id, OMX_BUFFERHEADERTYPE *> mBufferIDToBufferHeader;
172    KeyedVector<OMX_BUFFERHEADERTYPE *, OMX::buffer_id> mBufferHeaderToBufferID;
173
174    // metadata and secure buffer type tracking
175    MetadataBufferType mMetadataType[2];
176    enum SecureBufferType {
177        kSecureBufferTypeUnknown,
178        kSecureBufferTypeOpaque,
179        kSecureBufferTypeNativeHandle,
180    };
181    SecureBufferType mSecureBufferType[2];
182
183    // For debug support
184    char *mName;
185    int DEBUG;
186    size_t mNumPortBuffers[2];  // modified under mLock, read outside for debug
187    Mutex mDebugLock;
188    // following are modified and read under mDebugLock
189    int DEBUG_BUMP;
190    SortedVector<OMX_BUFFERHEADERTYPE *> mInputBuffersWithCodec, mOutputBuffersWithCodec;
191    size_t mDebugLevelBumpPendingBuffers[2];
192    void bumpDebugLevel_l(size_t numInputBuffers, size_t numOutputBuffers);
193    void unbumpDebugLevel_l(size_t portIndex);
194
195    ~OMXNodeInstance();
196
197    void addActiveBuffer(OMX_U32 portIndex, OMX::buffer_id id);
198    void removeActiveBuffer(OMX_U32 portIndex, OMX::buffer_id id);
199    void freeActiveBuffers();
200
201    // For buffer id management
202    OMX::buffer_id makeBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
203    OMX_BUFFERHEADERTYPE *findBufferHeader(OMX::buffer_id buffer, OMX_U32 portIndex);
204    OMX::buffer_id findBufferID(OMX_BUFFERHEADERTYPE *bufferHeader);
205    void invalidateBufferID(OMX::buffer_id buffer);
206
207    bool isProhibitedIndex_l(OMX_INDEXTYPE index);
208
209    status_t useGraphicBuffer2_l(
210            OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
211            OMX::buffer_id *buffer);
212    static OMX_ERRORTYPE OnEvent(
213            OMX_IN OMX_HANDLETYPE hComponent,
214            OMX_IN OMX_PTR pAppData,
215            OMX_IN OMX_EVENTTYPE eEvent,
216            OMX_IN OMX_U32 nData1,
217            OMX_IN OMX_U32 nData2,
218            OMX_IN OMX_PTR pEventData);
219
220    static OMX_ERRORTYPE OnEmptyBufferDone(
221            OMX_IN OMX_HANDLETYPE hComponent,
222            OMX_IN OMX_PTR pAppData,
223            OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
224
225    static OMX_ERRORTYPE OnFillBufferDone(
226            OMX_IN OMX_HANDLETYPE hComponent,
227            OMX_IN OMX_PTR pAppData,
228            OMX_IN OMX_BUFFERHEADERTYPE *pBuffer);
229
230    status_t storeMetaDataInBuffers_l(
231            OMX_U32 portIndex, OMX_BOOL enable, MetadataBufferType *type);
232
233    // Stores fence into buffer if it is ANWBuffer type and has enough space.
234    // otherwise, waits for the fence to signal.  Takes ownership of |fenceFd|.
235    status_t storeFenceInMeta_l(
236            OMX_BUFFERHEADERTYPE *header, int fenceFd, OMX_U32 portIndex);
237
238    // Retrieves the fence from buffer if ANWBuffer type and has enough space. Otherwise, returns -1
239    int retrieveFenceFromMeta_l(
240            OMX_BUFFERHEADERTYPE *header, OMX_U32 portIndex);
241
242    status_t emptyBuffer_l(
243            OMX_BUFFERHEADERTYPE *header,
244            OMX_U32 flags, OMX_TICKS timestamp, intptr_t debugAddr, int fenceFd);
245
246    // Updates the graphic buffer handle in the metadata buffer for |buffer| and |header| to
247    // |graphicBuffer|'s handle. If |updateCodecBuffer| is true, the update will happen in
248    // the actual codec buffer (use this if not using emptyBuffer (with no _l) later to
249    // pass the buffer to the codec, as only emptyBuffer copies the backup buffer to the codec
250    // buffer.)
251    status_t updateGraphicBufferInMeta_l(
252            OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer,
253            OMX::buffer_id buffer, OMX_BUFFERHEADERTYPE *header, bool updateCodecBuffer);
254
255    status_t createGraphicBufferSource(
256            OMX_U32 portIndex, const sp<IGraphicBufferConsumer> &consumer /* nullable */,
257            MetadataBufferType *type);
258    sp<GraphicBufferSource> getGraphicBufferSource();
259    void setGraphicBufferSource(const sp<GraphicBufferSource> &bufferSource);
260
261    // Handles |msg|, and may modify it. Returns true iff completely handled it and
262    // |msg| does not need to be sent to the event listener.
263    bool handleMessage(omx_message &msg);
264
265    bool handleDataSpaceChanged(omx_message &msg);
266
267    OMXNodeInstance(const OMXNodeInstance &);
268    OMXNodeInstance &operator=(const OMXNodeInstance &);
269};
270
271}  // namespace android
272
273#endif  // OMX_NODE_INSTANCE_H_
274