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 class 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 enableNativeBuffers(OMX_U32 portIndex, OMX_BOOL graphic, OMX_BOOL enable); 56 57 status_t getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage); 58 59 status_t storeMetaDataInBuffers( 60 OMX_U32 portIndex, OMX_BOOL enable, MetadataBufferType *type); 61 62 status_t prepareForAdaptivePlayback( 63 OMX_U32 portIndex, OMX_BOOL enable, 64 OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight); 65 66 status_t configureVideoTunnelMode( 67 OMX_U32 portIndex, OMX_BOOL tunneled, 68 OMX_U32 audioHwSync, native_handle_t **sidebandHandle); 69 70 status_t useBuffer( 71 OMX_U32 portIndex, const sp<IMemory> ¶ms, 72 OMX::buffer_id *buffer, OMX_U32 allottedSize); 73 74 status_t useGraphicBuffer( 75 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 76 OMX::buffer_id *buffer); 77 78 status_t updateGraphicBufferInMeta( 79 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 80 OMX::buffer_id buffer); 81 82 status_t updateNativeHandleInMeta( 83 OMX_U32 portIndex, const sp<NativeHandle> &nativeHandle, 84 OMX::buffer_id buffer); 85 86 status_t createInputSurface( 87 OMX_U32 portIndex, android_dataspace dataSpace, 88 sp<IGraphicBufferProducer> *bufferProducer, 89 MetadataBufferType *type); 90 91 static status_t createPersistentInputSurface( 92 sp<IGraphicBufferProducer> *bufferProducer, 93 sp<IGraphicBufferConsumer> *bufferConsumer); 94 95 status_t setInputSurface( 96 OMX_U32 portIndex, const sp<IGraphicBufferConsumer> &bufferConsumer, 97 MetadataBufferType *type); 98 99 status_t signalEndOfInputStream(); 100 101 void signalEvent(OMX_EVENTTYPE event, OMX_U32 arg1, OMX_U32 arg2); 102 103 status_t allocateSecureBuffer( 104 OMX_U32 portIndex, size_t size, OMX::buffer_id *buffer, 105 void **buffer_data, sp<NativeHandle> *native_handle); 106 107 status_t allocateBufferWithBackup( 108 OMX_U32 portIndex, const sp<IMemory> ¶ms, 109 OMX::buffer_id *buffer, OMX_U32 allottedSize); 110 111 status_t freeBuffer(OMX_U32 portIndex, OMX::buffer_id buffer); 112 113 status_t fillBuffer(OMX::buffer_id buffer, int fenceFd); 114 115 status_t emptyBuffer( 116 OMX::buffer_id buffer, 117 OMX_U32 rangeOffset, OMX_U32 rangeLength, 118 OMX_U32 flags, OMX_TICKS timestamp, int fenceFd); 119 120 status_t emptyGraphicBuffer( 121 OMX_BUFFERHEADERTYPE *header, const sp<GraphicBuffer> &buffer, 122 OMX_U32 flags, OMX_TICKS timestamp, int fenceFd); 123 124 status_t getExtensionIndex( 125 const char *parameterName, OMX_INDEXTYPE *index); 126 127 status_t setInternalOption( 128 OMX_U32 portIndex, 129 IOMX::InternalOptionType type, 130 const void *data, 131 size_t size); 132 isSecureOMXNodeInstance133 bool isSecure() const { 134 return mIsSecure; 135 } 136 137 // handles messages and removes them from the list 138 void onMessages(std::list<omx_message> &messages); 139 void onMessage(const omx_message &msg); 140 void onObserverDied(OMXMaster *master); 141 void onGetHandleFailed(); 142 void onEvent(OMX_EVENTTYPE event, OMX_U32 arg1, OMX_U32 arg2); 143 144 static OMX_CALLBACKTYPE kCallbacks; 145 146 private: 147 Mutex mLock; 148 149 OMX *mOwner; 150 OMX::node_id mNodeID; 151 OMX_HANDLETYPE mHandle; 152 sp<IOMXObserver> mObserver; 153 bool mDying; 154 bool mIsSecure; 155 156 // Lock only covers mGraphicBufferSource. We can't always use mLock 157 // because of rare instances where we'd end up locking it recursively. 158 Mutex mGraphicBufferSourceLock; 159 // Access this through getGraphicBufferSource(). 160 sp<GraphicBufferSource> mGraphicBufferSource; 161 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 status_t useGraphicBuffer2_l( 208 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 209 OMX::buffer_id *buffer); 210 static OMX_ERRORTYPE OnEvent( 211 OMX_IN OMX_HANDLETYPE hComponent, 212 OMX_IN OMX_PTR pAppData, 213 OMX_IN OMX_EVENTTYPE eEvent, 214 OMX_IN OMX_U32 nData1, 215 OMX_IN OMX_U32 nData2, 216 OMX_IN OMX_PTR pEventData); 217 218 static OMX_ERRORTYPE OnEmptyBufferDone( 219 OMX_IN OMX_HANDLETYPE hComponent, 220 OMX_IN OMX_PTR pAppData, 221 OMX_IN OMX_BUFFERHEADERTYPE *pBuffer); 222 223 static OMX_ERRORTYPE OnFillBufferDone( 224 OMX_IN OMX_HANDLETYPE hComponent, 225 OMX_IN OMX_PTR pAppData, 226 OMX_IN OMX_BUFFERHEADERTYPE *pBuffer); 227 228 status_t storeMetaDataInBuffers_l( 229 OMX_U32 portIndex, OMX_BOOL enable, MetadataBufferType *type); 230 231 // Stores fence into buffer if it is ANWBuffer type and has enough space. 232 // otherwise, waits for the fence to signal. Takes ownership of |fenceFd|. 233 status_t storeFenceInMeta_l( 234 OMX_BUFFERHEADERTYPE *header, int fenceFd, OMX_U32 portIndex); 235 236 // Retrieves the fence from buffer if ANWBuffer type and has enough space. Otherwise, returns -1 237 int retrieveFenceFromMeta_l( 238 OMX_BUFFERHEADERTYPE *header, OMX_U32 portIndex); 239 240 status_t emptyBuffer_l( 241 OMX_BUFFERHEADERTYPE *header, 242 OMX_U32 flags, OMX_TICKS timestamp, intptr_t debugAddr, int fenceFd); 243 244 // Updates the graphic buffer handle in the metadata buffer for |buffer| and |header| to 245 // |graphicBuffer|'s handle. If |updateCodecBuffer| is true, the update will happen in 246 // the actual codec buffer (use this if not using emptyBuffer (with no _l) later to 247 // pass the buffer to the codec, as only emptyBuffer copies the backup buffer to the codec 248 // buffer.) 249 status_t updateGraphicBufferInMeta_l( 250 OMX_U32 portIndex, const sp<GraphicBuffer> &graphicBuffer, 251 OMX::buffer_id buffer, OMX_BUFFERHEADERTYPE *header, bool updateCodecBuffer); 252 253 status_t createGraphicBufferSource( 254 OMX_U32 portIndex, sp<IGraphicBufferConsumer> consumer /* nullable */, 255 MetadataBufferType *type); 256 sp<GraphicBufferSource> getGraphicBufferSource(); 257 void setGraphicBufferSource(const sp<GraphicBufferSource>& bufferSource); 258 259 // Handles |msg|, and may modify it. Returns true iff completely handled it and 260 // |msg| does not need to be sent to the event listener. 261 bool handleMessage(omx_message &msg); 262 263 OMXNodeInstance(const OMXNodeInstance &); 264 OMXNodeInstance &operator=(const OMXNodeInstance &); 265 }; 266 267 } // namespace android 268 269 #endif // OMX_NODE_INSTANCE_H_ 270