2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sys/types.h>
27 #include <cutils/log.h>
28 #include <cutils/atomic.h>
30 #include <hardware/hardware.h>
31 #include <hardware/gralloc.h>
33 #include "gralloc_priv.h"
34 #include "exynos_format.h"
37 #include <linux/ion.h>
39 /*****************************************************************************/
41 static int gralloc_map(gralloc_module_t
const* module
, buffer_handle_t handle
)
43 private_handle_t
* hnd
= (private_handle_t
*)handle
;
45 void* mappedAddress
= mmap(0, hnd
->size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
47 if (mappedAddress
== MAP_FAILED
) {
48 ALOGE("%s: could not mmap %s", __func__
, strerror(errno
));
51 ALOGV("%s: base %p %d %d %d %d\n", __func__
, mappedAddress
, hnd
->size
,
52 hnd
->width
, hnd
->height
, hnd
->stride
);
53 hnd
->base
= mappedAddress
;
57 static int gralloc_unmap(gralloc_module_t
const* module
, buffer_handle_t handle
)
59 private_handle_t
* hnd
= (private_handle_t
*)handle
;
61 if (hnd
->base
!= NULL
&& munmap(hnd
->base
, hnd
->size
) < 0) {
62 ALOGE("%s :could not unmap %s %p %d", __func__
, strerror(errno
),
63 hnd
->base
, hnd
->size
);
66 if (hnd
->base1
!= NULL
&& hnd
->format
== HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED
) {
67 // unmap plane-2 of mapped NV12 TILED format
68 size_t chroma_vstride
= ALIGN(hnd
->height
/ 2, 32);
69 size_t chroma_size
= chroma_vstride
* hnd
->stride
;
70 if (munmap(hnd
->base1
, chroma_size
) < 0) {
71 ALOGE("%s :could not unmap %s %p %zd", __func__
, strerror(errno
),
72 hnd
->base1
, chroma_size
);
76 ALOGV("%s: base %p %d %d %d %d\n", __func__
, hnd
->base
, hnd
->size
,
77 hnd
->width
, hnd
->height
, hnd
->stride
);
82 static int gralloc_map_yuv(gralloc_module_t
const* module
, buffer_handle_t handle
)
84 private_handle_t
* hnd
= (private_handle_t
*)handle
;
86 // only support NV12 TILED format
87 if (hnd
->format
!= HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED
) {
91 size_t chroma_vstride
= ALIGN(hnd
->height
/ 2, 32);
92 size_t chroma_size
= chroma_vstride
* hnd
->stride
;
93 ALOGI("map_yuv: size=%d/%zu", hnd
->size
, chroma_size
);
95 if (hnd
->base
== NULL
) {
96 int err
= gralloc_map(module
, handle
);
102 // map plane-2 for NV12 TILED format
103 void* mappedAddress
= mmap(0, chroma_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
105 if (mappedAddress
== MAP_FAILED
) {
106 ALOGE("%s: could not mmap %s", __func__
, strerror(errno
));
107 gralloc_unmap(module
, handle
);
111 ALOGV("%s: chroma %p %d %d %d %d\n", __func__
, mappedAddress
, hnd
->size
,
112 hnd
->width
, hnd
->height
, hnd
->stride
);
113 hnd
->base1
= mappedAddress
;
117 /*****************************************************************************/
119 int getIonFd(gralloc_module_t
const *module
)
121 private_module_t
* m
= const_cast<private_module_t
*>(reinterpret_cast<const private_module_t
*>(module
));
123 m
->ionfd
= ion_open();
127 static pthread_mutex_t sMapLock
= PTHREAD_MUTEX_INITIALIZER
;
129 /*****************************************************************************/
131 int gralloc_register_buffer(gralloc_module_t
const* module
,
132 buffer_handle_t handle
)
134 if (private_handle_t::validate(handle
) < 0)
137 private_handle_t
* hnd
= (private_handle_t
*)handle
;
138 ALOGV("%s: base %p %d %d %d %d\n", __func__
, hnd
->base
, hnd
->size
,
139 hnd
->width
, hnd
->height
, hnd
->stride
);
142 ret
= ion_import(getIonFd(module
), hnd
->fd
, &hnd
->handle
);
144 ALOGE("error importing handle %d %x\n", hnd
->fd
, hnd
->format
);
146 ret
= ion_import(getIonFd(module
), hnd
->fd1
, &hnd
->handle1
);
148 ALOGE("error importing handle1 %d %x\n", hnd
->fd1
, hnd
->format
);
151 ret
= ion_import(getIonFd(module
), hnd
->fd2
, &hnd
->handle2
);
153 ALOGE("error importing handle2 %d %x\n", hnd
->fd2
, hnd
->format
);
159 int gralloc_unregister_buffer(gralloc_module_t
const* module
,
160 buffer_handle_t handle
)
162 if (private_handle_t::validate(handle
) < 0)
165 private_handle_t
* hnd
= (private_handle_t
*)handle
;
166 ALOGV("%s: base %p %d %d %d %d\n", __func__
, hnd
->base
, hnd
->size
,
167 hnd
->width
, hnd
->height
, hnd
->stride
);
169 gralloc_unmap(module
, handle
);
172 ion_free(getIonFd(module
), hnd
->handle
);
174 ion_free(getIonFd(module
), hnd
->handle1
);
176 ion_free(getIonFd(module
), hnd
->handle2
);
181 int gralloc_lock(gralloc_module_t
const* module
,
182 buffer_handle_t handle
, int usage
,
183 int l
, int t
, int w
, int h
,
186 // this is called when a buffer is being locked for software
187 // access. in thin implementation we have nothing to do since
188 // not synchronization with the h/w is needed.
189 // typically this is used to wait for the h/w to finish with
190 // this buffer if relevant. the data cache may need to be
191 // flushed or invalidated depending on the usage bits and the
194 if (private_handle_t::validate(handle
) < 0)
197 private_handle_t
* hnd
= (private_handle_t
*)handle
;
199 gralloc_map(module
, hnd
);
200 *vaddr
= (void*)hnd
->base
;
204 int gralloc_lock_ycbcr(gralloc_module_t
const* module
,
205 buffer_handle_t handle
, int usage
,
206 int l
, int t
, int w
, int h
,
207 struct android_ycbcr
*ycbcr
)
209 // This is called when a YUV buffer is being locked for software
210 // access. In this implementation we have nothing to do since
211 // no synchronization with the HW is needed.
212 // Typically this is used to wait for the h/w to finish with
213 // this buffer if relevant. The data cache may need to be
214 // flushed or invalidated depending on the usage bits and the
217 if (private_handle_t::validate(handle
) < 0)
220 private_handle_t
* hnd
= (private_handle_t
*)handle
;
221 ALOGV("lock_ycbcr for fmt=%d %dx%d %dx%d %d", hnd
->format
, hnd
->width
, hnd
->height
,
222 hnd
->stride
, hnd
->vstride
, hnd
->size
);
223 switch (hnd
->format
) {
224 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED
:
227 if (hnd
->base1
== NULL
|| hnd
->base
== NULL
) {
228 err
= gralloc_map_yuv(module
, hnd
);
231 ycbcr
->y
= (void*)hnd
->base
;
232 ycbcr
->cb
= (void*)hnd
->base1
;
233 ycbcr
->cr
= (void*)((uint8_t *)hnd
->base
+ 1);
234 ycbcr
->ystride
= hnd
->stride
;
235 ycbcr
->cstride
= hnd
->stride
;
236 ycbcr
->chroma_step
= 2;
237 memset(ycbcr
->reserved
, 0, sizeof(ycbcr
->reserved
));
246 int gralloc_perform(struct gralloc_module_t
const* module
, int operation
, ... )
248 // dummy implementation required to implement lock_ycbcr
252 int gralloc_unlock(gralloc_module_t
const* module
,
253 buffer_handle_t handle
)
255 // we're done with a software buffer. nothing to do in this
256 // implementation. typically this is used to flush the data cache.
257 private_handle_t
* hnd
= (private_handle_t
*)handle
;
258 ion_sync_fd(getIonFd(module
), hnd
->fd
);
260 ion_sync_fd(getIonFd(module
), hnd
->fd1
);
262 ion_sync_fd(getIonFd(module
), hnd
->fd2
);
264 if (private_handle_t::validate(handle
) < 0)