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"
36 #include <linux/ion.h>
38 /*****************************************************************************/
40 static int gralloc_map(gralloc_module_t
const* module
, buffer_handle_t handle
)
42 private_handle_t
* hnd
= (private_handle_t
*)handle
;
44 void* mappedAddress
= mmap(0, hnd
->size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
,
46 if (mappedAddress
== MAP_FAILED
) {
47 ALOGE("%s: could not mmap %s", __func__
, strerror(errno
));
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
;
56 static int gralloc_unmap(gralloc_module_t
const* module
, buffer_handle_t handle
)
58 private_handle_t
* hnd
= (private_handle_t
*)handle
;
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
);
67 ALOGV("%s: base %p %d %d %d %d\n", __func__
, hnd
->base
, hnd
->size
,
68 hnd
->width
, hnd
->height
, hnd
->stride
);
73 /*****************************************************************************/
75 int getIonFd(gralloc_module_t
const *module
)
77 private_module_t
* m
= const_cast<private_module_t
*>(reinterpret_cast<const private_module_t
*>(module
));
79 m
->ionfd
= ion_open();
83 static pthread_mutex_t sMapLock
= PTHREAD_MUTEX_INITIALIZER
;
85 /*****************************************************************************/
87 int gralloc_register_buffer(gralloc_module_t
const* module
,
88 buffer_handle_t handle
)
90 if (private_handle_t::validate(handle
) < 0)
93 private_handle_t
* hnd
= (private_handle_t
*)handle
;
94 ALOGV("%s: base %p %d %d %d %d\n", __func__
, hnd
->base
, hnd
->size
,
95 hnd
->width
, hnd
->height
, hnd
->stride
);
98 ret
= ion_import(getIonFd(module
), hnd
->fd
, &hnd
->handle
);
100 ALOGE("error importing handle %d %x\n", hnd
->fd
, hnd
->format
);
102 ret
= ion_import(getIonFd(module
), hnd
->fd1
, &hnd
->handle1
);
104 ALOGE("error importing handle1 %d %x\n", hnd
->fd1
, hnd
->format
);
107 ret
= ion_import(getIonFd(module
), hnd
->fd2
, &hnd
->handle2
);
109 ALOGE("error importing handle2 %d %x\n", hnd
->fd2
, hnd
->format
);
115 int gralloc_unregister_buffer(gralloc_module_t
const* module
,
116 buffer_handle_t handle
)
118 if (private_handle_t::validate(handle
) < 0)
121 private_handle_t
* hnd
= (private_handle_t
*)handle
;
122 ALOGV("%s: base %p %d %d %d %d\n", __func__
, hnd
->base
, hnd
->size
,
123 hnd
->width
, hnd
->height
, hnd
->stride
);
125 gralloc_unmap(module
, handle
);
128 ion_free(getIonFd(module
), hnd
->handle
);
130 ion_free(getIonFd(module
), hnd
->handle1
);
132 ion_free(getIonFd(module
), hnd
->handle2
);
137 int gralloc_lock(gralloc_module_t
const* module
,
138 buffer_handle_t handle
, int usage
,
139 int l
, int t
, int w
, int h
,
142 // this is called when a buffer is being locked for software
143 // access. in thin implementation we have nothing to do since
144 // not synchronization with the h/w is needed.
145 // typically this is used to wait for the h/w to finish with
146 // this buffer if relevant. the data cache may need to be
147 // flushed or invalidated depending on the usage bits and the
150 if (private_handle_t::validate(handle
) < 0)
153 private_handle_t
* hnd
= (private_handle_t
*)handle
;
155 gralloc_map(module
, hnd
);
156 *vaddr
= (void*)hnd
->base
;
160 int gralloc_unlock(gralloc_module_t
const* module
,
161 buffer_handle_t handle
)
163 // we're done with a software buffer. nothing to do in this
164 // implementation. typically this is used to flush the data cache.
165 private_handle_t
* hnd
= (private_handle_t
*)handle
;
166 ion_sync_fd(getIonFd(module
), hnd
->fd
);
168 ion_sync_fd(getIonFd(module
), hnd
->fd1
);
170 ion_sync_fd(getIonFd(module
), hnd
->fd2
);
172 if (private_handle_t::validate(handle
) < 0)