PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/aries-common/sec_mm/sec_omx/sec_osal/SEC_OSAL_Buffer.cpp

https://github.com/scrizz/vibrant_ics
C++ | 457 lines | 342 code | 85 blank | 30 comment | 55 complexity | 5b994213f06c0d2ab919056e881238b9 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /*
  2. *
  3. * Copyright 2010 Samsung Electronics S.LSI Co. LTD
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * @file SEC_OSAL_Buffer.c
  19. * @brief
  20. * @author SeungBeom Kim (sbcrux.kim@samsung.com)
  21. * Jinsung Yang (jsgood.yang@samsung.com)
  22. * @version 1.0.2
  23. * @history
  24. * 2011.5.15 : Create
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "SEC_OMX_Def.h"
  33. #include "SEC_OMX_Macros.h"
  34. #include "SEC_OSAL_Memory.h"
  35. #include "SEC_OSAL_Semaphore.h"
  36. #include "SEC_OSAL_Buffer.h"
  37. #include "SEC_OMX_Basecomponent.h"
  38. #define SEC_LOG_OFF
  39. #include "SEC_OSAL_Log.h"
  40. #ifdef __cplusplus
  41. }
  42. #endif
  43. #include <cutils/properties.h>
  44. #include <ui/android_native_buffer.h>
  45. #include <ui/GraphicBuffer.h>
  46. #include <ui/GraphicBufferMapper.h>
  47. #include <ui/Rect.h>
  48. #include <media/stagefright/HardwareAPI.h>
  49. #include <hardware/hardware.h>
  50. #include <media/stagefright/MetadataBufferType.h>
  51. #include "hal_public.h"
  52. #define HAL_PIXEL_FORMAT_C110_NV12 0x100
  53. using namespace android;
  54. struct AndroidNativeBuffersParams {
  55. OMX_U32 nSize;
  56. OMX_VERSIONTYPE nVersion;
  57. OMX_U32 nPortIndex;
  58. };
  59. #ifdef USE_ANDROID_EXTENSION
  60. OMX_ERRORTYPE checkVersionANB(OMX_PTR ComponentParameterStructure)
  61. {
  62. OMX_ERRORTYPE ret = OMX_ErrorNone;
  63. OMX_VERSIONTYPE* version = NULL;
  64. AndroidNativeBuffersParams *pANBP;
  65. pANBP = (AndroidNativeBuffersParams *)ComponentParameterStructure;
  66. version = (OMX_VERSIONTYPE*)((char*)pANBP + sizeof(OMX_U32));
  67. if (*((OMX_U32*)pANBP) <= sizeof(AndroidNativeBuffersParams)) {
  68. ret = OMX_ErrorBadParameter;
  69. goto EXIT;
  70. }
  71. if (version->s.nVersionMajor != VERSIONMAJOR_NUMBER ||
  72. version->s.nVersionMinor != VERSIONMINOR_NUMBER) {
  73. ret = OMX_ErrorVersionMismatch;
  74. goto EXIT;
  75. }
  76. ret = OMX_ErrorNone;
  77. EXIT:
  78. return ret;
  79. }
  80. OMX_U32 checkPortIndexANB(OMX_PTR ComponentParameterStructure)
  81. {
  82. AndroidNativeBuffersParams *pANBP;
  83. pANBP = (AndroidNativeBuffersParams *)ComponentParameterStructure;
  84. return pANBP->nPortIndex;
  85. }
  86. OMX_U32 getMetadataBufferType(const uint8_t *ptr)
  87. {
  88. OMX_U32 type = *(OMX_U32 *) ptr;
  89. SEC_OSAL_Log(SEC_LOG_TRACE, "getMetadataBufferType: %ld", type);
  90. return type;
  91. }
  92. OMX_U32 getVADDRfromANB(OMX_PTR pUnreadableBuffer, OMX_U32 Width, OMX_U32 Height, void *pVirAddrs[])
  93. {
  94. OMX_U32 ret = 0;
  95. android_native_buffer_t *buf;
  96. void *readableBuffer;
  97. GraphicBufferMapper &mapper = GraphicBufferMapper::get();
  98. Rect bounds(Width, Height);
  99. FunctionIn();
  100. buf = (android_native_buffer_t *)pUnreadableBuffer;
  101. SEC_OSAL_Log(SEC_LOG_TRACE, "pUnreadableBuffer:0x%x, buf:0x%x, buf->handle:0x%x",
  102. pUnreadableBuffer, buf, buf->handle);
  103. ret = mapper.lock(buf->handle, GRALLOC_USAGE_SW_WRITE_OFTEN, bounds, pVirAddrs);
  104. if (ret != 0) {
  105. SEC_OSAL_Log(SEC_LOG_ERROR, "mapper.lock Error, Error code:%d", ret);
  106. }
  107. FunctionOut();
  108. return ret;
  109. }
  110. OMX_U32 putVADDRtoANB(OMX_PTR pUnreadableBuffer)
  111. {
  112. android_native_buffer_t *buf;
  113. void *readableBuffer;
  114. int ret = 0;
  115. GraphicBufferMapper &mapper = GraphicBufferMapper::get();
  116. FunctionIn();
  117. buf = (android_native_buffer_t *)pUnreadableBuffer;
  118. FunctionOut();
  119. return mapper.unlock(buf->handle);
  120. }
  121. OMX_ERRORTYPE enableAndroidNativeBuffer(OMX_HANDLETYPE hComponent, OMX_PTR ComponentParameterStructure)
  122. {
  123. OMX_ERRORTYPE ret = OMX_ErrorNone;
  124. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  125. SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
  126. SEC_OMX_BASEPORT *pSECPort = NULL;
  127. EnableAndroidNativeBuffersParams *peanbp;
  128. FunctionIn();
  129. if (hComponent == NULL) {
  130. ret = OMX_ErrorBadParameter;
  131. goto EXIT;
  132. }
  133. pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  134. if (pOMXComponent->pComponentPrivate == NULL) {
  135. ret = OMX_ErrorBadParameter;
  136. goto EXIT;
  137. }
  138. pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
  139. peanbp = (EnableAndroidNativeBuffersParams *)ComponentParameterStructure;
  140. pSECPort = &pSECComponent->pSECPort[peanbp->nPortIndex];
  141. if (peanbp->enable == OMX_FALSE) {
  142. SEC_OSAL_Log(SEC_LOG_TRACE, "disable AndroidNativeBuffer");
  143. pSECPort->bUseAndroidNativeBuffer = OMX_FALSE;
  144. } else {
  145. SEC_OSAL_Log(SEC_LOG_TRACE, "enable AndroidNativeBuffer");
  146. pSECPort->bUseAndroidNativeBuffer = OMX_TRUE;
  147. pSECPort->portDefinition.format.video.eColorFormat = (OMX_COLOR_FORMATTYPE)OMX_SEC_COLOR_FormatANBYUV420SemiPlanar;
  148. }
  149. ret = OMX_ErrorNone;
  150. EXIT:
  151. FunctionOut();
  152. return ret;
  153. }
  154. OMX_ERRORTYPE getAndroidNativeBuffer(OMX_HANDLETYPE hComponent, OMX_PTR ComponentParameterStructure)
  155. {
  156. OMX_ERRORTYPE ret = OMX_ErrorNone;
  157. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  158. SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
  159. SEC_OMX_BASEPORT *pSECPort = NULL;
  160. GetAndroidNativeBufferUsageParams *pganbp;
  161. FunctionIn();
  162. pganbp = (GetAndroidNativeBufferUsageParams *)ComponentParameterStructure;
  163. pganbp->nUsage = GRALLOC_USAGE_SW_WRITE_OFTEN;
  164. ret = OMX_ErrorNone;
  165. EXIT:
  166. FunctionOut();
  167. return ret;
  168. }
  169. OMX_ERRORTYPE UseBufferANB(
  170. OMX_IN OMX_HANDLETYPE hComponent,
  171. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
  172. OMX_IN OMX_U32 nPortIndex,
  173. OMX_IN OMX_PTR pAppPrivate,
  174. OMX_IN OMX_U32 nSizeBytes,
  175. OMX_IN OMX_U8 *pBuffer)
  176. {
  177. OMX_ERRORTYPE ret = OMX_ErrorNone;
  178. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  179. SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
  180. SEC_OMX_BASEPORT *pSECPort = NULL;
  181. OMX_BUFFERHEADERTYPE *temp_bufferHeader = NULL;
  182. int i = 0;
  183. FunctionIn();
  184. if (hComponent == NULL) {
  185. ret = OMX_ErrorBadParameter;
  186. goto EXIT;
  187. }
  188. pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  189. if (pOMXComponent->pComponentPrivate == NULL) {
  190. ret = OMX_ErrorBadParameter;
  191. goto EXIT;
  192. }
  193. pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
  194. pSECPort = &pSECComponent->pSECPort[nPortIndex];
  195. if (nPortIndex >= pSECComponent->portParam.nPorts) {
  196. ret = OMX_ErrorBadPortIndex;
  197. goto EXIT;
  198. }
  199. if (pSECPort->portState != OMX_StateIdle) {
  200. ret = OMX_ErrorIncorrectStateOperation;
  201. goto EXIT;
  202. }
  203. if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort)) {
  204. ret = OMX_ErrorBadPortIndex;
  205. goto EXIT;
  206. }
  207. temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)SEC_OSAL_Malloc(sizeof(OMX_BUFFERHEADERTYPE));
  208. if (temp_bufferHeader == NULL) {
  209. ret = OMX_ErrorInsufficientResources;
  210. goto EXIT;
  211. }
  212. SEC_OSAL_Memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  213. for (i = 0; i < pSECPort->portDefinition.nBufferCountActual; i++) {
  214. if (pSECPort->bufferStateAllocate[i] == BUFFER_STATE_FREE) {
  215. pSECPort->bufferHeader[i] = temp_bufferHeader;
  216. pSECPort->bufferStateAllocate[i] = (BUFFER_STATE_ASSIGNED | HEADER_STATE_ALLOCATED);
  217. INIT_SET_SIZE_VERSION(temp_bufferHeader, OMX_BUFFERHEADERTYPE);
  218. temp_bufferHeader->pBuffer = pBuffer;
  219. temp_bufferHeader->nAllocLen = nSizeBytes;
  220. temp_bufferHeader->pAppPrivate = pAppPrivate;
  221. if (nPortIndex == INPUT_PORT_INDEX)
  222. temp_bufferHeader->nInputPortIndex = INPUT_PORT_INDEX;
  223. else
  224. temp_bufferHeader->nOutputPortIndex = OUTPUT_PORT_INDEX;
  225. pSECPort->assignedBufferNum++;
  226. if (pSECPort->assignedBufferNum == pSECPort->portDefinition.nBufferCountActual) {
  227. pSECPort->portDefinition.bPopulated = OMX_TRUE;
  228. /* SEC_OSAL_MutexLock(pSECComponent->compMutex); */
  229. SEC_OSAL_SemaphorePost(pSECPort->loadedResource);
  230. /* SEC_OSAL_MutexUnlock(pSECComponent->compMutex); */
  231. }
  232. *ppBufferHdr = temp_bufferHeader;
  233. ret = OMX_ErrorNone;
  234. goto EXIT;
  235. }
  236. }
  237. SEC_OSAL_Free(temp_bufferHeader);
  238. ret = OMX_ErrorInsufficientResources;
  239. EXIT:
  240. FunctionOut();
  241. return ret;
  242. }
  243. OMX_ERRORTYPE useAndroidNativeBuffer(OMX_HANDLETYPE hComponent, OMX_PTR ComponentParameterStructure)
  244. {
  245. OMX_ERRORTYPE ret = OMX_ErrorNone;
  246. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  247. SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
  248. SEC_OMX_BASEPORT *pSECPort = NULL;
  249. OMX_U32 frameSize = 0;
  250. OMX_U32 bufWidth, bufHeight;
  251. UseAndroidNativeBufferParams *puanbp;
  252. FunctionIn();
  253. puanbp = (UseAndroidNativeBufferParams *)ComponentParameterStructure;
  254. OMX_PTR buffer = (void *)puanbp->nativeBuffer.get();
  255. android_native_buffer_t *buf = (android_native_buffer_t *)buffer;
  256. bufWidth = ((buf->width + 15) / 16) * 16;
  257. bufHeight = ((buf->height + 15) / 16) * 16;
  258. frameSize = (bufWidth * bufHeight * 3) / 2;
  259. SEC_OSAL_Log(SEC_LOG_TRACE, "buffer:0x%x, buf:0x%x, buf->handle:0x%x", buffer, buf, buf->handle);
  260. ret = UseBufferANB(hComponent, puanbp->bufferHeader, puanbp->nPortIndex,
  261. puanbp->pAppPrivate, frameSize, (OMX_U8 *)buffer);
  262. EXIT:
  263. FunctionOut();
  264. return ret;
  265. }
  266. OMX_ERRORTYPE enableStoreMetaDataInBuffers(OMX_HANDLETYPE hComponent, OMX_PTR ComponentParameterStructure)
  267. {
  268. OMX_ERRORTYPE ret = OMX_ErrorNone;
  269. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  270. SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
  271. SEC_OMX_BASEPORT *pSECPort = NULL;
  272. StoreMetaDataInBuffersParams *pStoreMetaData;
  273. FunctionIn();
  274. if (hComponent == NULL) {
  275. ret = OMX_ErrorBadParameter;
  276. goto EXIT;
  277. }
  278. pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  279. if (pOMXComponent->pComponentPrivate == NULL) {
  280. ret = OMX_ErrorBadParameter;
  281. goto EXIT;
  282. }
  283. pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
  284. pStoreMetaData = (StoreMetaDataInBuffersParams*)ComponentParameterStructure;
  285. pSECPort = &pSECComponent->pSECPort[pStoreMetaData->nPortIndex];
  286. if (pStoreMetaData->bStoreMetaData == OMX_FALSE) {
  287. SEC_OSAL_Log(SEC_LOG_TRACE, "disable StoreMetaDataInBuffers");
  288. pSECPort->bStoreMetaDataInBuffer = OMX_FALSE;
  289. } else {
  290. SEC_OSAL_Log(SEC_LOG_TRACE, "enable StoreMetaDataInBuffers");
  291. pSECPort->bStoreMetaDataInBuffer = OMX_TRUE;
  292. }
  293. EXIT:
  294. FunctionOut();
  295. return ret;
  296. }
  297. OMX_BOOL isMetadataBufferTypeGrallocSource(OMX_BYTE pInputDataBuffer)
  298. {
  299. OMX_U32 type = getMetadataBufferType(pInputDataBuffer);
  300. if (type == kMetadataBufferTypeGrallocSource)
  301. return OMX_TRUE;
  302. else
  303. return OMX_FALSE;
  304. }
  305. OMX_ERRORTYPE preprocessMetaDataInBuffers(OMX_HANDLETYPE hComponent, OMX_BYTE pInputDataBuffer, BUFFER_ADDRESS_INFO *pInputInfo)
  306. {
  307. OMX_ERRORTYPE ret = OMX_ErrorNone;
  308. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  309. SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
  310. SEC_OMX_BASEPORT *pSECPort = NULL;
  311. OMX_U32 type = 0;
  312. FunctionIn();
  313. if (hComponent == NULL) {
  314. ret = OMX_ErrorBadParameter;
  315. goto EXIT;
  316. }
  317. pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  318. if (pOMXComponent->pComponentPrivate == NULL) {
  319. ret = OMX_ErrorBadParameter;
  320. goto EXIT;
  321. }
  322. pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
  323. pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
  324. type = getMetadataBufferType(pInputDataBuffer);
  325. if (type == kMetadataBufferTypeCameraSource) {
  326. SEC_OSAL_Memcpy(&pInputInfo->YPhyAddr, pInputDataBuffer + 4, sizeof(void *));
  327. SEC_OSAL_Memcpy(&pInputInfo->CPhyAddr, pInputDataBuffer + 4 + sizeof(void *), sizeof(void *));
  328. } else if (type == kMetadataBufferTypeGrallocSource){
  329. IMG_gralloc_module_public_t *module = (IMG_gralloc_module_public_t *)pSECPort->pIMGGrallocModule;
  330. OMX_PTR pUnreadableBuffer = NULL;
  331. OMX_PTR pReadableBuffer = NULL;
  332. OMX_PTR pVirAddrs[3];
  333. int err = 0;
  334. pVirAddrs[0] = pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YVirAddr;
  335. pVirAddrs[1] = pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CVirAddr;
  336. pVirAddrs[2] = NULL;
  337. if (module == NULL) {
  338. err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&module);
  339. if(err) {
  340. SEC_OSAL_Log(SEC_LOG_ERROR, "hw_get_module failed (err=%d)\n", err);
  341. ret = OMX_ErrorUndefined;
  342. goto EXIT;
  343. }
  344. pSECPort->pIMGGrallocModule = (OMX_PTR)module;
  345. }
  346. /**************************************/
  347. /* IMG CSC RGB to NV12 */
  348. /**************************************/
  349. buffer_handle_t buf = *((buffer_handle_t *) (pInputDataBuffer + 4));
  350. SEC_OSAL_Log(SEC_LOG_TRACE, "buffer handle %p)\n", buf);
  351. err = module->Blit(module, buf, pVirAddrs, HAL_PIXEL_FORMAT_C110_NV12);
  352. if(err) {
  353. SEC_OSAL_Log(SEC_LOG_ERROR, "module->Blit() failed (err=%d)\n", err);
  354. ret = OMX_ErrorUndefined;
  355. goto EXIT;
  356. }
  357. pInputInfo->YPhyAddr = pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YPhyAddr;
  358. pInputInfo->CPhyAddr = pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CPhyAddr;
  359. } else {
  360. ret = OMX_ErrorNotImplemented;
  361. goto EXIT;
  362. }
  363. EXIT:
  364. FunctionOut();
  365. return ret;
  366. }
  367. int isTvOutEnabled() {
  368. char value[PROPERTY_VALUE_MAX];
  369. property_get("init.svc.tvouthack", value, "");
  370. return (strcmp(value, "running") == 0);
  371. }
  372. #endif