merge in jb-mr1-release history after reset to jb-mr1-dev
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos5.git] / gralloc / mapper.cpp
1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <limits.h>
18 #include <errno.h>
19 #include <pthread.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <cutils/log.h>
28 #include <cutils/atomic.h>
29
30 #include <hardware/hardware.h>
31 #include <hardware/gralloc.h>
32
33 #include "gralloc_priv.h"
34
35 #include <ion/ion.h>
36 #include <linux/ion.h>
37
38 /*****************************************************************************/
39
40 static int gralloc_map(gralloc_module_t const* module, buffer_handle_t handle)
41 {
42 private_handle_t* hnd = (private_handle_t*)handle;
43
44 void* mappedAddress = mmap(0, hnd->size, PROT_READ|PROT_WRITE, MAP_SHARED,
45 hnd->fd, 0);
46 if (mappedAddress == MAP_FAILED) {
47 ALOGE("%s: could not mmap %s", __func__, strerror(errno));
48 return -errno;
49 }
50 ALOGV("%s: base %p %d %d %d %d\n", __func__, mappedAddress, hnd->size,
51 hnd->width, hnd->height, hnd->stride);
52 hnd->base = mappedAddress;
53 return 0;
54 }
55
56 static int gralloc_unmap(gralloc_module_t const* module, buffer_handle_t handle)
57 {
58 private_handle_t* hnd = (private_handle_t*)handle;
59
60 if (!hnd->base)
61 return 0;
62
63 if (munmap(hnd->base, hnd->size) < 0) {
64 ALOGE("%s :could not unmap %s %p %d", __func__, strerror(errno),
65 hnd->base, hnd->size);
66 }
67 ALOGV("%s: base %p %d %d %d %d\n", __func__, hnd->base, hnd->size,
68 hnd->width, hnd->height, hnd->stride);
69 hnd->base = 0;
70 return 0;
71 }
72
73 /*****************************************************************************/
74
75 int grallocMap(gralloc_module_t const* module, private_handle_t *hnd)
76 {
77 return gralloc_map(module, hnd);
78 }
79
80 int grallocUnmap(gralloc_module_t const* module, private_handle_t *hnd)
81 {
82 return gralloc_unmap(module, hnd);
83 }
84
85 int getIonFd(gralloc_module_t const *module)
86 {
87 private_module_t* m = const_cast<private_module_t*>(reinterpret_cast<const private_module_t*>(module));
88 if (m->ionfd == -1)
89 m->ionfd = ion_open();
90 return m->ionfd;
91 }
92
93 static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
94
95 /*****************************************************************************/
96
97 int gralloc_register_buffer(gralloc_module_t const* module,
98 buffer_handle_t handle)
99 {
100 int err;
101 if (private_handle_t::validate(handle) < 0)
102 return -EINVAL;
103
104 err = gralloc_map(module, handle);
105
106 private_handle_t* hnd = (private_handle_t*)handle;
107 ALOGV("%s: base %p %d %d %d %d\n", __func__, hnd->base, hnd->size,
108 hnd->width, hnd->height, hnd->stride);
109
110 int ret;
111 ret = ion_import(getIonFd(module), hnd->fd, &hnd->handle);
112 if (ret)
113 ALOGE("error importing handle %d %x\n", hnd->fd, hnd->format);
114 if (hnd->fd1 >= 0) {
115 ret = ion_import(getIonFd(module), hnd->fd1, &hnd->handle1);
116 if (ret)
117 ALOGE("error importing handle1 %d %x\n", hnd->fd1, hnd->format);
118 }
119 if (hnd->fd2 >= 0) {
120 ret = ion_import(getIonFd(module), hnd->fd2, &hnd->handle2);
121 if (ret)
122 ALOGE("error importing handle2 %d %x\n", hnd->fd2, hnd->format);
123 }
124
125 return err;
126 }
127
128 int gralloc_unregister_buffer(gralloc_module_t const* module,
129 buffer_handle_t handle)
130 {
131 if (private_handle_t::validate(handle) < 0)
132 return -EINVAL;
133
134 private_handle_t* hnd = (private_handle_t*)handle;
135 ALOGV("%s: base %p %d %d %d %d\n", __func__, hnd->base, hnd->size,
136 hnd->width, hnd->height, hnd->stride);
137
138 gralloc_unmap(module, handle);
139
140 if (hnd->handle)
141 ion_free(getIonFd(module), hnd->handle);
142 if (hnd->handle1)
143 ion_free(getIonFd(module), hnd->handle1);
144 if (hnd->handle2)
145 ion_free(getIonFd(module), hnd->handle2);
146
147 return 0;
148 }
149
150 int gralloc_lock(gralloc_module_t const* module,
151 buffer_handle_t handle, int usage,
152 int l, int t, int w, int h,
153 void** vaddr)
154 {
155 // this is called when a buffer is being locked for software
156 // access. in thin implementation we have nothing to do since
157 // not synchronization with the h/w is needed.
158 // typically this is used to wait for the h/w to finish with
159 // this buffer if relevant. the data cache may need to be
160 // flushed or invalidated depending on the usage bits and the
161 // hardware.
162
163 if (private_handle_t::validate(handle) < 0)
164 return -EINVAL;
165
166 private_handle_t* hnd = (private_handle_t*)handle;
167 *vaddr = (void*)hnd->base;
168 return 0;
169 }
170
171 int gralloc_unlock(gralloc_module_t const* module,
172 buffer_handle_t handle)
173 {
174 // we're done with a software buffer. nothing to do in this
175 // implementation. typically this is used to flush the data cache.
176
177 if (private_handle_t::validate(handle) < 0)
178 return -EINVAL;
179 return 0;
180 }