//static int gralloc_alloc_buffer(alloc_device_t* dev, size_t size, int usage, buffer_handle_t* pHandle, bool reserve)
static int gralloc_alloc_buffer(alloc_device_t* dev, size_t size, int usage, buffer_handle_t* pHandle, int reserve)
{
#if GRALLOC_ARM_DMA_BUF_MODULE
	{
		private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
		ion_user_handle_t ion_hnd;
		unsigned char *cpu_ptr;
		int shared_fd;
		int ret;
		unsigned int heap_mask;
		int Ion_type;
		bool Ishwc = false;
        int Ion_flag = 0;   
        if(usage == (GRALLOC_USAGE_HW_COMPOSER|GRALLOC_USAGE_HW_RENDER))
            Ishwc = true;

		//ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &ion_hnd);
        #ifdef USE_X86	
        
        if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
            Ion_flag = (ION_FLAG_CACHED|ION_FLAG_CACHED_NEEDS_SYNC);

        if(is_out_log())
            ALOGD("usage=%x,protect=%x,ion_flag=%x,mmu=%d",usage,GRALLOC_USAGE_PROTECTED,Ion_flag,g_MMU_stat);
        if (usage & GRALLOC_USAGE_PROTECTED)  //secrue memery
        {
            unsigned long phys;
            ret = ion_secure_alloc(m->ion_client, size,&phys);
            //ALOGD("secure_alloc ret=%d,phys=%x",ret,(int)phys);
            if(ret != 0)
            {
                AERR("Failed to ion_alloc from ion_client:%d, size: %d", m->ion_client, size);
                return -1;
            }    
	        private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_ION, usage, size, 0, 0);

    		if (NULL != hnd)
    		{
    			hnd->share_fd = 0;
    			hnd->ion_hnd = 0;
    			hnd->type = 0;
    			hnd->phy_addr = (int)phys;
    			*pHandle = hnd;
                if(is_out_log())
                    ALOGD("secure_alloc_ok phy=%x",usage,hnd->phy_addr);
    			
    			return 0;
    		}
    		else
    		{
    			AERR("Gralloc out of mem for ion_client:%d", m->ion_client);
    		}

    		close(shared_fd);

    		return -1;
        }
        #endif
		//ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &ion_hnd);
        #ifdef USE_X86		
		if(g_MMU_stat
		    && ((usage&GRALLOC_USAGE_HW_CAMERA_WRITE)==0)
		    && !(usage & GRALLOC_USAGE_PRIVATE_2)
		    && !Ishwc)
        #else
		if(g_MMU_stat)
		#endif
		{
		    heap_mask = ION_HEAP(ION_VMALLOC_HEAP_ID);
            #ifdef USE_X86		
		    if (usage & GRALLOC_USAGE_PRIVATE_2)
		    {
		        heap_mask |=  ION_HEAP(ION_SECURE_HEAP_ID);
		    }
            #endif
		    ret = ion_alloc(m->ion_client, size, 0, heap_mask, Ion_flag, &ion_hnd);
		    Ion_type = 1;
		} else {
		    heap_mask = ION_HEAP(ION_CMA_HEAP_ID);
            #ifdef USE_X86		
		    if (usage & GRALLOC_USAGE_PRIVATE_2)
		    {
		        heap_mask |=  ION_HEAP(ION_SECURE_HEAP_ID);
		    }
            #endif

		    if (usage == (GRALLOC_USAGE_HW_CAMERA_WRITE|GRALLOC_USAGE_SW_READ_OFTEN)) {
                     ret = ion_alloc(m->ion_client, size, 0,heap_mask, 
                        (ION_FLAG_CACHED|ION_FLAG_CACHED_NEEDS_SYNC), &ion_hnd);   
		    } else {
                     ret = ion_alloc(m->ion_client, size, 0,heap_mask, Ion_flag, &ion_hnd);
		    }
            #ifdef USE_X86		
		    if(g_MMU_stat && Ishwc)
		    {
		        Ion_type = 1;
 
		    }    
		    else    
		    #endif
		        Ion_type = 0;
		}

		if (ret != 0)
		{
            if( (heap_mask & ION_HEAP(ION_CMA_HEAP_ID))
#ifdef USE_X86
            && !Ishwc
#endif
            )
            {
#ifdef BOARD_WITH_IOMMU
                heap_mask = ION_HEAP(ION_VMALLOC_HEAP_ID);
#else
                heap_mask = ION_HEAP(ION_CARVEOUT_HEAP_ID);
#endif
                ret = ion_alloc(m->ion_client, size, 0, heap_mask, 0, &ion_hnd );
                {
                    if( ret != 0)
                    {
                        AERR("Force to VMALLOC fail ion_client:%d", m->ion_client);
                        return -1;
                    }
                    else
                    {
                        ALOGD("Force to VMALLOC sucess !");
                        Ion_type = 1;
                    }
                }
            }
            else
            {
                AERR("Failed to ion_alloc from ion_client:%d, size: %d", m->ion_client, size);
                return -1;
            }
		}

		ret = ion_share(m->ion_client, ion_hnd, &shared_fd);

		if (ret != 0)
		{
			AERR("ion_share( %d ) failed", m->ion_client);

			if (0 != ion_free(m->ion_client, ion_hnd))
			{
				AERR("ion_free( %d ) failed", m->ion_client);
			}

			return -1;
		}
		cpu_ptr = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_fd, 0);
		#ifdef USE_X86
		//memset(cpu_ptr, 0, size);
		#endif
		if (MAP_FAILED == cpu_ptr)
		{
			AERR("ion_map( %d ) failed", m->ion_client);

			if (0 != ion_free(m->ion_client, ion_hnd))
			{
				AERR("ion_free( %d ) failed", m->ion_client);
			}

			close(shared_fd);
			return -1;
		}

		private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_ION, usage, size, (int)cpu_ptr, private_handle_t::LOCK_STATE_MAPPED);

		if (NULL != hnd)
		{
		    unsigned long cma_phys = 0;
			hnd->share_fd = shared_fd;
			hnd->ion_hnd = ion_hnd;
			hnd->type = Ion_type;
			if(!Ion_type)
			{
			    int pret;
			    pret = ion_get_phys(m->ion_client, ion_hnd, &cma_phys);
			    //ALOGD("ion_get_phy ret=%d,cma_phys=%x",pret,cma_phys);
			}    
			    
			hnd->phy_addr = (int)cma_phys;
			*pHandle = hnd;
            if(is_out_log())
                ALOGD("alloc_info fd[%d],type=%d,phy=%x",hnd->share_fd,hnd->type,hnd->phy_addr);
			
			return 0;
		}
		else
		{
			AERR("Gralloc out of mem for ion_client:%d", m->ion_client);
		}

		close(shared_fd);
		ret = munmap(cpu_ptr, size);

		if (0 != ret)
		{
			AERR("munmap failed for base:%p size: %d", cpu_ptr, size);
		}

		ret = ion_free(m->ion_client, ion_hnd);

		if (0 != ret)
		{
			AERR("ion_free( %d ) failed", m->ion_client);
		}

		return -1;
	}
#endif

#if GRALLOC_ARM_UMP_MODULE
	{
		ump_handle ump_mem_handle;
		void *cpu_ptr;
		ump_secure_id ump_id;
		int constraints;

		size = round_up_to_page_size(size);

		if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
		{
			constraints =  UMP_REF_DRV_CONSTRAINT_USE_CACHE;
		}
		else
		{
			constraints = UMP_REF_DRV_CONSTRAINT_NONE;
		}
	    if ( reserve & 0x01)
		{
		
		    constraints |= UMP_REF_DRV_CONSTRAINT_PRE_RESERVE;
		}
		
		if( reserve & 0x02)
		{
            constraints |= UMP_REF_DRV_UK_CONSTRAINT_MEM_SWITCH;

		}
#ifdef GRALLOC_SIMULATE_FAILURES
		/* if the failure condition matches, fail this iteration */
		if (__ump_alloc_should_fail())
		{
			ump_mem_handle = UMP_INVALID_MEMORY_HANDLE;
		}
		else
#endif
		{
			ump_mem_handle = ump_ref_drv_allocate(size, (ump_alloc_constraints)constraints);

			if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle)
			{
				cpu_ptr = ump_mapped_pointer_get(ump_mem_handle);

				if (NULL != cpu_ptr)
				{
					ump_id = ump_secure_id_get(ump_mem_handle);

					if (UMP_INVALID_SECURE_ID != ump_id)
					{
						private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_UMP, usage, size, (int)cpu_ptr,
						private_handle_t::LOCK_STATE_MAPPED, ump_id, ump_mem_handle);

						if (NULL != hnd)
						{
						#ifdef  USE_LCDC_COMPOSER
            		 		if( reserve & 0x02)
					  		{
	                    		hnd->phy_addr = 0;   
					  		}
					  		else
					  		{
                    		    hnd->phy_addr = ump_phy_addr_get(ump_mem_handle);        
                    		}   
                    	#endif
							*pHandle = hnd;
							return 0;
						}
						else
						{
							AERR("gralloc_alloc_buffer() failed to allocate handle. ump_handle = %p, ump_id = %d", ump_mem_handle, ump_id);
						}
					}
					else
					{
						AERR("gralloc_alloc_buffer() failed to retrieve valid secure id. ump_handle = %p", ump_mem_handle);
					}

					ump_mapped_pointer_release(ump_mem_handle);
				}
				else
				{
					AERR("gralloc_alloc_buffer() failed to map UMP memory. ump_handle = %p", ump_mem_handle);
				}

				ump_reference_release(ump_mem_handle);
			}
			else
			{
				AERR("gralloc_alloc_buffer() failed to allocate UMP memory. size:%d constraints: %d", size, constraints);
			}
		}
		return -1;
	}
#endif
static int gralloc_alloc_buffer(alloc_device_t *dev, size_t size, int usage, buffer_handle_t *pHandle)
{
#if GRALLOC_ARM_DMA_BUF_MODULE
	{
		private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
		ion_user_handle_t ion_hnd;
		unsigned char *cpu_ptr;
		int shared_fd;
		int ret;
		unsigned int ion_flags = 0;

		if( (usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN )
			ion_flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
		if (usage & GRALLOC_USAGE_PRIVATE_1) {
			ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_CARVEOUT_MASK, ion_flags, &ion_hnd);
		} else {
			ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, ion_flags, &ion_hnd);
		}

		if (ret != 0)
		{
			AERR("Failed to ion_alloc from ion_client:%d", m->ion_client);
			return -1;
		}

		ret = ion_share(m->ion_client, ion_hnd, &shared_fd);

		if (ret != 0)
		{
			AERR("ion_share( %d ) failed", m->ion_client);

			if (0 != ion_free(m->ion_client, ion_hnd))
			{
				AERR("ion_free( %d ) failed", m->ion_client);
			}

			return -1;
		}

		cpu_ptr = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_fd, 0);

		if (MAP_FAILED == cpu_ptr)
		{
			AERR("ion_map( %d ) failed", m->ion_client);

			if (0 != ion_free(m->ion_client, ion_hnd))
			{
				AERR("ion_free( %d ) failed", m->ion_client);
			}

			close(shared_fd);
			return -1;
		}

		private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_ION, usage, size, (int)cpu_ptr, private_handle_t::LOCK_STATE_MAPPED);

		if (NULL != hnd)
		{
			hnd->share_fd = shared_fd;
			hnd->ion_hnd = ion_hnd;
			*pHandle = hnd;
			return 0;
		}
		else
		{
			AERR("Gralloc out of mem for ion_client:%d", m->ion_client);
		}

		close(shared_fd);
		ret = munmap(cpu_ptr, size);

		if (0 != ret)
		{
			AERR("munmap failed for base:%p size: %d", cpu_ptr, size);
		}

		ret = ion_free(m->ion_client, ion_hnd);

		if (0 != ret)
		{
			AERR("ion_free( %d ) failed", m->ion_client);
		}

		return -1;
	}
#endif

#if GRALLOC_ARM_UMP_MODULE
	{
		ump_handle ump_mem_handle;
		void *cpu_ptr;
		ump_secure_id ump_id;
		ump_alloc_constraints constraints;

		size = round_up_to_page_size(size);

		if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
		{
			constraints =  UMP_REF_DRV_CONSTRAINT_USE_CACHE;
		}
		else
		{
			constraints = UMP_REF_DRV_CONSTRAINT_NONE;
		}

#ifdef GRALLOC_SIMULATE_FAILURES
		/* if the failure condition matches, fail this iteration */
		if (__ump_alloc_should_fail())
		{
			ump_mem_handle = UMP_INVALID_MEMORY_HANDLE;
		}
		else
#endif
		{
			ump_mem_handle = ump_ref_drv_allocate(size, constraints);

			if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle)
			{
				cpu_ptr = ump_mapped_pointer_get(ump_mem_handle);

				if (NULL != cpu_ptr)
				{
					ump_id = ump_secure_id_get(ump_mem_handle);

					if (UMP_INVALID_SECURE_ID != ump_id)
					{
						private_handle_t *hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_UMP, usage, size, (int)cpu_ptr,
						private_handle_t::LOCK_STATE_MAPPED, ump_id, ump_mem_handle);

						if (NULL != hnd)
						{
							*pHandle = hnd;
							return 0;
						}
						else
						{
							AERR("gralloc_alloc_buffer() failed to allocate handle. ump_handle = %p, ump_id = %d", ump_mem_handle, ump_id);
						}
					}
					else
					{
						AERR("gralloc_alloc_buffer() failed to retrieve valid secure id. ump_handle = %p", ump_mem_handle);
					}

					ump_mapped_pointer_release(ump_mem_handle);
				}
				else
				{
					AERR("gralloc_alloc_buffer() failed to map UMP memory. ump_handle = %p", ump_mem_handle);
				}

				ump_reference_release(ump_mem_handle);
			}
			else
			{
				AERR("gralloc_alloc_buffer() failed to allocate UMP memory. size:%d constraints: %d", size, constraints);
			}
		}
		return -1;
	}
#endif

}
static int gralloc_alloc_buffer(alloc_device_t* dev, size_t size, int usage, buffer_handle_t* pHandle)
{
	ump_handle ump_mem_handle;
	void *cpu_ptr;
	ump_secure_id ump_id;
	ump_alloc_constraints constraints;

	size = round_up_to_page_size(size);

	if( (usage&GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN )
	{
		constraints =  UMP_REF_DRV_CONSTRAINT_USE_CACHE;
	}
	else
	{
		constraints = UMP_REF_DRV_CONSTRAINT_NONE;
	}

#ifdef GRALLOC_SIMULATE_FAILURES
	/* if the failure condition matches, fail this iteration */
	if (__ump_alloc_should_fail())
	{
		ump_mem_handle = UMP_INVALID_MEMORY_HANDLE;
	}
	else
#endif
	ump_mem_handle = ump_ref_drv_allocate(size, constraints);
	if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle)
	{
		cpu_ptr = ump_mapped_pointer_get(ump_mem_handle);
		if (NULL != cpu_ptr)
		{
			ump_id = ump_secure_id_get(ump_mem_handle);
			if (UMP_INVALID_SECURE_ID != ump_id)
			{
				private_handle_t* hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_UMP, size, (int)cpu_ptr,
				                                             private_handle_t::LOCK_STATE_MAPPED, ump_id, ump_mem_handle);
				if (NULL != hnd)
				{
					*pHandle = hnd;
					return 0;
				}
				else
				{
					LOGE("gralloc_alloc_buffer() failed to allocate handle");
				}
			}
			else
			{
				LOGE("gralloc_alloc_buffer() failed to retrieve valid secure id");
			}
			
			ump_mapped_pointer_release(ump_mem_handle);
		}
		else
		{
			LOGE("gralloc_alloc_buffer() failed to map UMP memory");
		}

		ump_reference_release(ump_mem_handle);
	}
	else
	{
		LOGE("gralloc_alloc_buffer() failed to allocate UMP memory");
	}

	return -1;
}