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 grallocMap(gralloc_module_t
const* module
, private_handle_t
*hnd
)
77 return gralloc_map(module
, hnd
);
80 int grallocUnmap(gralloc_module_t
const* module
, private_handle_t
*hnd
)
82 return gralloc_unmap(module
, hnd
);
85 int getIonFd(gralloc_module_t
const *module
)
87 private_module_t
* m
= const_cast<private_module_t
*>(reinterpret_cast<const private_module_t
*>(module
));
89 m
->ionfd
= ion_open();
93 static pthread_mutex_t sMapLock
= PTHREAD_MUTEX_INITIALIZER
;
95 /*****************************************************************************/
97 int gralloc_register_buffer(gralloc_module_t
const* module
,
98 buffer_handle_t handle
)
101 if (private_handle_t::validate(handle
) < 0)
104 err
= gralloc_map(module
, handle
);
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
);
111 ret
= ion_import(getIonFd(module
), hnd
->fd
, &hnd
->handle
);
113 ALOGE("error importing handle %d %x\n", hnd
->fd
, hnd
->format
);
115 ret
= ion_import(getIonFd(module
), hnd
->fd1
, &hnd
->handle1
);
117 ALOGE("error importing handle1 %d %x\n", hnd
->fd1
, hnd
->format
);
120 ret
= ion_import(getIonFd(module
), hnd
->fd2
, &hnd
->handle2
);
122 ALOGE("error importing handle2 %d %x\n", hnd
->fd2
, hnd
->format
);
128 int gralloc_unregister_buffer(gralloc_module_t
const* module
,
129 buffer_handle_t handle
)
131 if (private_handle_t::validate(handle
) < 0)
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
);
138 gralloc_unmap(module
, handle
);
141 ion_free(getIonFd(module
), hnd
->handle
);
143 ion_free(getIonFd(module
), hnd
->handle1
);
145 ion_free(getIonFd(module
), hnd
->handle2
);
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
,
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
163 if (private_handle_t::validate(handle
) < 0)
166 private_handle_t
* hnd
= (private_handle_t
*)handle
;
167 *vaddr
= (void*)hnd
->base
;
171 int gralloc_unlock(gralloc_module_t
const* module
,
172 buffer_handle_t handle
)
174 // we're done with a software buffer. nothing to do in this
175 // implementation. typically this is used to flush the data cache.
177 if (private_handle_t::validate(handle
) < 0)