summaryrefslogtreecommitdiff
path: root/gralloc_module_ion.cpp
blob: edf83fd83695011c5991177a7f8c4810738a07d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2013 ARM Limited. All rights reserved.
 *
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <pthread.h>

#include <cutils/log.h>
#include <cutils/atomic.h>
#include <hardware/hardware.h>
#include <hardware/gralloc.h>

#include "gralloc_priv.h"
#include "alloc_device.h"
#include "framebuffer_device.h"

#include <linux/ion.h>
#include <ion/ion.h>
#include <sys/mman.h>

int gralloc_backend_register(private_handle_t* hnd)
{
	int retval = -EINVAL;

	switch (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_UMP |
	                      private_handle_t::PRIV_FLAGS_USES_ION))
	{
	case private_handle_t::PRIV_FLAGS_USES_UMP:
		AERR("Gralloc does not support UMP. Unable to register UMP memory for handle %p", hnd );
		break;
	case private_handle_t::PRIV_FLAGS_USES_ION:
		unsigned char *mappedAddress;
		size_t size = hnd->size;
		hw_module_t * pmodule = NULL;
		private_module_t *m=NULL;
		if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
		{
			m = reinterpret_cast<private_module_t *>(pmodule);
		}
		else
		{
			AERR("Could not get gralloc module for handle: %p", hnd);
			retval = -errno;
			break;
		}
		/* the test condition is set to m->ion_client <= 0 here, because:
		 * 1) module structure are initialized to 0 if no initial value is applied
		 * 2) a second user process should get a ion fd greater than 0.
		 */
		if (m->ion_client <= 0)
		{
			/* a second user process must obtain a client handle first via ion_open before it can obtain the shared ion buffer*/	
			m->ion_client = ion_open();
			
			if (m->ion_client < 0) 
			{
				AERR( "Could not open ion device for handle: %p", hnd );
				retval = -errno;
				break;
			}
		}

		mappedAddress = (unsigned char*)mmap( NULL, size, PROT_READ | PROT_WRITE,
		                                      MAP_SHARED, hnd->share_fd, 0 );
		
		if ( MAP_FAILED == mappedAddress )
		{
			AERR( "mmap( share_fd:%d ) failed with %s",  hnd->share_fd, strerror( errno ) );
			retval = -errno;
			break;
		}
		
		hnd->base = (void*)(uintptr_t(mappedAddress) + hnd->offset);
		retval = 0;
		break;
	}

	return retval;
}

void gralloc_backend_unregister(private_handle_t* hnd)
{
	switch (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_UMP |
	                      private_handle_t::PRIV_FLAGS_USES_ION))
	{
	case private_handle_t::PRIV_FLAGS_USES_UMP:
		AERR( "Can't unregister UMP buffer for handle %p. Not supported", hnd );
		break;
	case private_handle_t::PRIV_FLAGS_USES_ION:
		void* base = (void*)hnd->base;
		size_t size = hnd->size;

		if ( munmap( base,size ) < 0 )
		{
			AERR("Could not munmap base:%p size:%zd '%s'", base, size, strerror(errno));
		}
		break;
	}
}

void gralloc_backend_sync(private_handle_t* hnd)
{
	switch (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_UMP |
	                      private_handle_t::PRIV_FLAGS_USES_ION))
	{
	case private_handle_t::PRIV_FLAGS_USES_UMP:
		AERR( "Buffer %p is UMP type but it is not supported", hnd );
		break;
	case private_handle_t::PRIV_FLAGS_USES_ION:
		hw_module_t * pmodule = NULL;
		private_module_t *m=NULL;
		if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
		{
			m = reinterpret_cast<private_module_t *>(pmodule);
			ion_sync_fd(m->ion_client, hnd->share_fd);
		}
		else
		{
			AERR("Could not get gralloc module for handle %p\n", hnd);
		}
		break;
	}
}