int _ion_alloc_test(int fd, struct ion_handle **handle)
{
	int ret;

	if (tiler_test)
		ret = ion_alloc_tiler(fd, width, height, fmt, alloc_flags,
					  handle, &stride);
	else
		ret = ion_alloc(fd, len, align, alloc_flags, handle);

	if (ret)
		printf("%s() failed: %s\n", __func__, strerror(ret));
	return ret;
}
MEMPLUGIN_ERRORTYPE MemPlugin_ION_Alloc(void *pMemPluginHandle, OMX_U32 nClient,
                                    MEMPLUGIN_BUFFER_PARAMS *pIonBufferParams,
                                    MEMPLUGIN_BUFFER_PROPERTIES *pIonBufferProp)
{
    OMX_S16 ret;
    struct ion_handle *temp;
    size_t stride;
    MEMPLUGIN_ERRORTYPE eError = MEMPLUGIN_ERROR_NONE;
    MEMPLUGIN_ION_PARAMS sIonParams;
    MEMPLUGIN_OBJECT    *pMemPluginHdl = (MEMPLUGIN_OBJECT *)pMemPluginHandle;

    if(pIonBufferParams->nWidth <= 0)
    {
        eError = MEMPLUGIN_ERROR_BADPARAMETER;
        DOMX_ERROR("%s: width should be positive %d", __FUNCTION__,pIonBufferParams->nWidth);
        goto EXIT;
    }

    if(pMemPluginHdl->pPluginExtendedInfo == NULL)
    {
        MEMPLUGIN_ION_PARAMS_INIT(&sIonParams);
    }
    else
    {
        MEMPLUGIN_ION_PARAMS_COPY(((MEMPLUGIN_ION_PARAMS *)pMemPluginHdl->pPluginExtendedInfo),sIonParams);
    }
    if(pIonBufferParams->eBuffer_type == DEFAULT)
    {
        ret = (OMX_S16)ion_alloc(nClient,
                                    pIonBufferParams->nWidth,
                                    sIonParams.nAlign,
                                    sIonParams.alloc_flags,
                                    &temp);
        if(ret || (int)temp == -ENOMEM)
        {
            if(sIonParams.alloc_flags != OMAP_ION_HEAP_SECURE_INPUT)
            {
               //for non default types of allocation - no retry with tiler 1d - throw error
//STARGO: ducati secure heap is too small, need to allocate from heap
#if 0
               DOMX_ERROR("FAILED to allocate secure buffer of size=%d. ret=0x%x",pIonBufferParams->nWidth, ret);
               eError = MEMPLUGIN_ERROR_NORESOURCES;
               goto EXIT;
#endif
               DOMX_ERROR("FAILED to allocate secure buffer of size=%d. ret=0x%x - trying tiler 1d space",pIonBufferParams->nWidth, ret);
               pIonBufferParams->eBuffer_type = TILER1D;
               pIonBufferParams->eTiler_format = MEMPLUGIN_TILER_FORMAT_PAGE;
               sIonParams.alloc_flags = OMAP_ION_HEAP_TILER_MASK;
               sIonParams.nAlign = -1;
            }
            else
            {
                // for default non tiler (OMAP_ION_HEAP_SECURE_INPUT) retry allocating from tiler 1D
                DOMX_DEBUG("FAILED to allocate from non tiler space - trying tiler 1d space");
                pIonBufferParams->eBuffer_type = TILER1D;
                pIonBufferParams->eTiler_format = MEMPLUGIN_TILER_FORMAT_PAGE;
                sIonParams.alloc_flags = OMAP_ION_HEAP_TILER_MASK;
                sIonParams.nAlign = -1;
            }
        }
    }
    if(pIonBufferParams->eBuffer_type == TILER1D)
    {
        ret = (OMX_S16)ion_alloc_tiler(nClient,
                                        pIonBufferParams->nWidth,
                                        pIonBufferParams->nHeight,
                                        pIonBufferParams->eTiler_format,
                                        sIonParams.alloc_flags,
                                        &temp,
                                        &(pIonBufferProp->nStride));

         if (ret || ((int)temp == -ENOMEM))
         {
               DOMX_ERROR("FAILED to allocate buffer of size=%d. ret=0x%x",pIonBufferParams->nWidth, ret);
               eError = MEMPLUGIN_ERROR_NORESOURCES;
               goto EXIT;
         }
    }
    else if(pIonBufferParams->eBuffer_type == TILER2D)
    {
        DOMX_ERROR("Tiler 2D not implemented");
        eError = MEMPLUGIN_ERROR_NOTIMPLEMENTED;
        goto EXIT;
    }
    else if(!temp)
    {
        DOMX_ERROR("Undefined option for buffer type");
        eError = MEMPLUGIN_ERROR_UNDEFINED;
        goto EXIT;
    }
    pIonBufferProp->sBuffer_accessor.pBufferHandle = (OMX_PTR)temp;
    pIonBufferProp->nStride =  stride;

    if(pIonBufferParams->bMap == OMX_TRUE)
    {
        ret = (OMX_S16) ion_map(nClient,
                                pIonBufferProp->sBuffer_accessor.pBufferHandle,
                                pIonBufferParams->nWidth*pIonBufferParams->nHeight,
                                sIonParams.prot,
                                sIonParams.map_flags,
                                sIonParams.nOffset,
                                (unsigned char **) &(pIonBufferProp->sBuffer_accessor.pBufferMappedAddress),
                                &(pIonBufferProp->sBuffer_accessor.bufferFd));

        if(ret < 0)
        {
                DOMX_ERROR("userspace mapping of ION buffers returned error");
                eError = MEMPLUGIN_ERROR_NORESOURCES;
                goto EXIT;
        }
    }
    else
    {
        ret = (OMX_S16) ion_share(nClient,
                                    pIonBufferProp->sBuffer_accessor.pBufferHandle,
                                    &(pIonBufferProp->sBuffer_accessor.bufferFd));
        if(ret < 0)
        {
                DOMX_ERROR("ION share returned error");
                eError = MEMPLUGIN_ERROR_NORESOURCES;
                goto EXIT;
        }
    }
EXIT:
      if (eError != MEMPLUGIN_ERROR_NONE) {
          DOMX_EXIT("%s exited with error 0x%x",__FUNCTION__,eError);
         return eError;
      }
      else {
          DOMX_EXIT("%s executed successfully",__FUNCTION__);
         return MEMPLUGIN_ERROR_NONE;
      }
}