Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 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> |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 20 | #include <unistd.h> |
Marco Nelissen | a4b587c | 2009-07-07 09:29:00 -0700 | [diff] [blame] | 21 | #include <string.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 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 | |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 35 | |
Marco Nelissen | a4b587c | 2009-07-07 09:29:00 -0700 | [diff] [blame] | 36 | /* desktop Linux needs a little help with gettid() */ |
| 37 | #if defined(ARCH_X86) && !defined(HAVE_ANDROID_OS) |
| 38 | #define __KERNEL__ |
| 39 | # include <linux/unistd.h> |
| 40 | pid_t gettid() { return syscall(__NR_gettid);} |
| 41 | #undef __KERNEL__ |
| 42 | #endif |
| 43 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 44 | /*****************************************************************************/ |
| 45 | |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 46 | static int gralloc_map(gralloc_module_t const* module, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 47 | buffer_handle_t handle, |
| 48 | void** vaddr) |
| 49 | { |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 50 | private_handle_t* hnd = (private_handle_t*)handle; |
| 51 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 52 | size_t size = hnd->size; |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 53 | void* mappedAddress = mmap(0, size, |
| 54 | PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0); |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 55 | if (mappedAddress == MAP_FAILED) { |
Steve Block | 60d056b | 2012-01-08 10:17:53 +0000 | [diff] [blame] | 56 | ALOGE("Could not mmap %s", strerror(errno)); |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 57 | return -errno; |
| 58 | } |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 59 | hnd->base = intptr_t(mappedAddress) + hnd->offset; |
Steve Block | cee8501 | 2011-12-20 16:25:12 +0000 | [diff] [blame] | 60 | //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p", |
Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 61 | // hnd->fd, hnd->offset, hnd->size, mappedAddress); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 62 | } |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 63 | *vaddr = (void*)hnd->base; |
| 64 | return 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 67 | static int gralloc_unmap(gralloc_module_t const* module, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 68 | buffer_handle_t handle) |
| 69 | { |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 70 | private_handle_t* hnd = (private_handle_t*)handle; |
| 71 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 72 | void* base = (void*)hnd->base; |
| 73 | size_t size = hnd->size; |
Steve Block | cee8501 | 2011-12-20 16:25:12 +0000 | [diff] [blame] | 74 | //ALOGD("unmapping from %p, size=%d", base, size); |
Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 75 | if (munmap(base, size) < 0) { |
Steve Block | 60d056b | 2012-01-08 10:17:53 +0000 | [diff] [blame] | 76 | ALOGE("Could not unmap %s", strerror(errno)); |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 77 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 78 | } |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 79 | hnd->base = 0; |
| 80 | return 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 83 | /*****************************************************************************/ |
| 84 | |
Jesse Hall | c71b6ca | 2013-03-28 11:04:16 -0700 | [diff] [blame] | 85 | static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 86 | |
| 87 | /*****************************************************************************/ |
| 88 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 89 | int gralloc_register_buffer(gralloc_module_t const* module, |
| 90 | buffer_handle_t handle) |
| 91 | { |
| 92 | if (private_handle_t::validate(handle) < 0) |
| 93 | return -EINVAL; |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 94 | |
Jesse Hall | 0d8f81a | 2013-03-28 16:51:25 -0700 | [diff] [blame^] | 95 | // *** WARNING WARNING WARNING *** |
| 96 | // |
| 97 | // If a buffer handle is passed from the process that allocated it to a |
| 98 | // different process, and then back to the allocator process, we will |
| 99 | // create a second mapping of the buffer. If the process reads and writes |
| 100 | // through both mappings, normal memory ordering guarantees may be |
| 101 | // violated, depending on the processor cache implementation*. |
| 102 | // |
| 103 | // If you are deriving a new gralloc implementation from this code, don't |
| 104 | // do this. A "real" gralloc should provide a single reference-counted |
| 105 | // mapping for each buffer in a process. |
| 106 | // |
| 107 | // In the current system, there is one case that needs a buffer to be |
| 108 | // registered in the same process that allocated it. The SurfaceFlinger |
| 109 | // process acts as the IGraphicBufferAlloc Binder provider, so all gralloc |
| 110 | // allocations happen in its process. After returning the buffer handle to |
| 111 | // the IGraphicBufferAlloc client, SurfaceFlinger free's its handle to the |
| 112 | // buffer (unmapping it from the SurfaceFlinger process). If |
| 113 | // SurfaceFlinger later acts as the producer end of the buffer queue the |
| 114 | // buffer belongs to, it will get a new handle to the buffer in response |
| 115 | // to IGraphicBufferProducer::requestBuffer(). Like any buffer handle |
| 116 | // received through Binder, the SurfaceFlinger process will register it. |
| 117 | // Since it already freed its original handle, it will only end up with |
| 118 | // one mapping to the buffer and there will be no problem. |
| 119 | // |
| 120 | // Currently SurfaceFlinger only acts as a buffer producer for a remote |
| 121 | // consumer when taking screenshots and when using virtual displays. |
| 122 | // |
| 123 | // Eventually, each application should be allowed to make its own gralloc |
| 124 | // allocations, solving the problem. Also, this ashmem-based gralloc |
| 125 | // should go away, replaced with a real ion-based gralloc. |
| 126 | // |
| 127 | // * Specifically, associative virtually-indexed caches are likely to have |
| 128 | // problems. Most modern L1 caches fit that description. |
| 129 | |
| 130 | private_handle_t* hnd = (private_handle_t*)handle; |
| 131 | ALOGD_IF(hnd->pid == getpid(), |
| 132 | "Registering a buffer in the process that created it. " |
| 133 | "This may cause memory ordering problems."); |
| 134 | |
Jesse Hall | c71b6ca | 2013-03-28 11:04:16 -0700 | [diff] [blame] | 135 | void *vaddr; |
| 136 | return gralloc_map(module, handle, &vaddr); |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
| 140 | buffer_handle_t handle) |
| 141 | { |
| 142 | if (private_handle_t::validate(handle) < 0) |
| 143 | return -EINVAL; |
| 144 | |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 145 | private_handle_t* hnd = (private_handle_t*)handle; |
Jesse Hall | c71b6ca | 2013-03-28 11:04:16 -0700 | [diff] [blame] | 146 | if (hnd->base) |
| 147 | gralloc_unmap(module, handle); |
| 148 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 151 | |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 152 | int mapBuffer(gralloc_module_t const* module, |
| 153 | private_handle_t* hnd) |
| 154 | { |
| 155 | void* vaddr; |
| 156 | return gralloc_map(module, hnd, &vaddr); |
| 157 | } |
| 158 | |
Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 159 | int terminateBuffer(gralloc_module_t const* module, |
| 160 | private_handle_t* hnd) |
| 161 | { |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 162 | if (hnd->base) { |
Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 163 | // this buffer was mapped, unmap it now |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 164 | gralloc_unmap(module, hnd); |
Mathias Agopian | bd80b38 | 2009-07-07 17:53:43 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 170 | int gralloc_lock(gralloc_module_t const* module, |
| 171 | buffer_handle_t handle, int usage, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 172 | int l, int t, int w, int h, |
| 173 | void** vaddr) |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 174 | { |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 175 | // this is called when a buffer is being locked for software |
| 176 | // access. in thin implementation we have nothing to do since |
| 177 | // not synchronization with the h/w is needed. |
| 178 | // typically this is used to wait for the h/w to finish with |
| 179 | // this buffer if relevant. the data cache may need to be |
| 180 | // flushed or invalidated depending on the usage bits and the |
| 181 | // hardware. |
| 182 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 183 | if (private_handle_t::validate(handle) < 0) |
| 184 | return -EINVAL; |
| 185 | |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 186 | private_handle_t* hnd = (private_handle_t*)handle; |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 187 | *vaddr = (void*)hnd->base; |
| 188 | return 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Jesse Hall | c71b6ca | 2013-03-28 11:04:16 -0700 | [diff] [blame] | 191 | int gralloc_unlock(gralloc_module_t const* module, |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 192 | buffer_handle_t handle) |
| 193 | { |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 194 | // we're done with a software buffer. nothing to do in this |
| 195 | // implementation. typically this is used to flush the data cache. |
| 196 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 197 | if (private_handle_t::validate(handle) < 0) |
| 198 | return -EINVAL; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | } |