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 /* desktop Linux needs a little help with gettid() */
37 #if defined(ARCH_X86) && !defined(HAVE_ANDROID_OS)
39 # include <linux/unistd.h>
40 pid_t gettid() { return syscall(__NR_gettid);}
44 /*****************************************************************************/
46 static int gralloc_map(gralloc_module_t const* module,
47 buffer_handle_t handle,
50 private_handle_t* hnd = (private_handle_t*)handle;
51 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
52 size_t size = hnd->size;
53 void* mappedAddress = mmap(0, size,
54 PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
55 if (mappedAddress == MAP_FAILED) {
56 LOGE("Could not mmap %s", strerror(errno));
59 hnd->base = intptr_t(mappedAddress) + hnd->offset;
60 //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
61 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
63 *vaddr = (void*)hnd->base;
67 static int gralloc_unmap(gralloc_module_t const* module,
68 buffer_handle_t handle)
70 private_handle_t* hnd = (private_handle_t*)handle;
71 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
72 void* base = (void*)hnd->base;
73 size_t size = hnd->size;
74 //LOGD("unmapping from %p, size=%d", base, size);
75 if (munmap(base, size) < 0) {
76 LOGE("Could not unmap %s", strerror(errno));
83 /*****************************************************************************/
85 static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
87 /*****************************************************************************/
89 int gralloc_register_buffer(gralloc_module_t const* module,
90 buffer_handle_t handle)
92 if (private_handle_t::validate(handle) < 0)
95 // if this handle was created in this process, then we keep it as is.
97 private_handle_t* hnd = (private_handle_t*)handle;
98 if (hnd->pid != getpid()) {
100 err = gralloc_map(module, handle, &vaddr);
105 int gralloc_unregister_buffer(gralloc_module_t const* module,
106 buffer_handle_t handle)
108 if (private_handle_t::validate(handle) < 0)
111 // never unmap buffers that were created in this process
112 private_handle_t* hnd = (private_handle_t*)handle;
113 if (hnd->pid != getpid()) {
115 gralloc_unmap(module, handle);
121 int mapBuffer(gralloc_module_t const* module,
122 private_handle_t* hnd)
125 return gralloc_map(module, hnd, &vaddr);
128 int terminateBuffer(gralloc_module_t const* module,
129 private_handle_t* hnd)
132 // this buffer was mapped, unmap it now
133 gralloc_unmap(module, hnd);
139 int gralloc_lock(gralloc_module_t const* module,
140 buffer_handle_t handle, int usage,
141 int l, int t, int w, int h,
144 // this is called when a buffer is being locked for software
145 // access. in thin implementation we have nothing to do since
146 // not synchronization with the h/w is needed.
147 // typically this is used to wait for the h/w to finish with
148 // this buffer if relevant. the data cache may need to be
149 // flushed or invalidated depending on the usage bits and the
152 if (private_handle_t::validate(handle) < 0)
155 private_handle_t* hnd = (private_handle_t*)handle;
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.
166 if (private_handle_t::validate(handle) < 0)