• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * You may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <errno.h>
20 #include <pthread.h>
21 
22 #include <cutils/log.h>
23 #include <cutils/atomic.h>
24 #include <hardware/hardware.h>
25 #include <hardware/gralloc.h>
26 
27 #include "gralloc_priv.h"
28 #include "alloc_device.h"
29 #include "framebuffer_device.h"
30 
31 #include <linux/ion.h>
32 #include <ion/ion.h>
33 #include <sys/mman.h>
34 
gralloc_backend_register(private_handle_t * hnd)35 int gralloc_backend_register(private_handle_t* hnd)
36 {
37 	int retval = -EINVAL;
38 
39 	switch (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
40 	{
41 	case private_handle_t::PRIV_FLAGS_USES_ION:
42 		unsigned char *mappedAddress;
43 		size_t size = hnd->size;
44 		hw_module_t * pmodule = NULL;
45 		private_module_t *m=NULL;
46 		if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
47 		{
48 			m = reinterpret_cast<private_module_t *>(pmodule);
49 		}
50 		else
51 		{
52 			AERR("Could not get gralloc module for handle: %p", hnd);
53 			retval = -errno;
54 			break;
55 		}
56 		/* the test condition is set to m->ion_client <= 0 here, because:
57 		 * 1) module structure are initialized to 0 if no initial value is applied
58 		 * 2) a second user process should get a ion fd greater than 0.
59 		 */
60 		if (m->ion_client <= 0)
61 		{
62 			/* a second user process must obtain a client handle first via ion_open before it can obtain the shared ion buffer*/
63 			m->ion_client = ion_open();
64 
65 			if (m->ion_client < 0)
66 			{
67 				AERR( "Could not open ion device for handle: %p", hnd );
68 				retval = -errno;
69 				break;
70 			}
71 		}
72 
73 		mappedAddress = (unsigned char*)mmap( NULL, size, PROT_READ | PROT_WRITE,
74 		                                      MAP_SHARED, hnd->share_fd, 0 );
75 
76 		if ( MAP_FAILED == mappedAddress )
77 		{
78 			AERR( "mmap( share_fd:%d ) failed with %s",  hnd->share_fd, strerror( errno ) );
79 			retval = -errno;
80 			break;
81 		}
82 
83 		hnd->base = (void*)(uintptr_t(mappedAddress) + hnd->offset);
84 		retval = 0;
85 		break;
86 	}
87 
88 	return retval;
89 }
90 
gralloc_backend_unregister(private_handle_t * hnd)91 void gralloc_backend_unregister(private_handle_t* hnd)
92 {
93 	switch (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
94 	{
95 	case private_handle_t::PRIV_FLAGS_USES_ION:
96 		void* base = (void*)hnd->base;
97 		size_t size = hnd->size;
98 
99 		if ( munmap( base,size ) < 0 )
100 		{
101 			AERR("Could not munmap base:%p size:%zd '%s'", base, size, strerror(errno));
102 		}
103 		break;
104 	}
105 }
106 
gralloc_backend_sync(private_handle_t * hnd)107 void gralloc_backend_sync(private_handle_t* hnd)
108 {
109 	switch (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
110 	{
111 	case private_handle_t::PRIV_FLAGS_USES_ION:
112 		hw_module_t * pmodule = NULL;
113 		private_module_t *m=NULL;
114 		if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
115 		{
116 			if(!(hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION_DMA_HEAP))
117 			{
118 				m = reinterpret_cast<private_module_t *>(pmodule);
119 				ion_sync_fd(m->ion_client, hnd->share_fd);
120 			}
121 		}
122 		else
123 		{
124 			AERR("Could not get gralloc module for handle %p\n", hnd);
125 		}
126 		break;
127 	}
128 }
129