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