1a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian/* 2a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * Copyright (C) 2008 The Android Open Source Project 3a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * 4a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * you may not use this file except in compliance with the License. 6a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * You may obtain a copy of the License at 7a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * 8a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * 10a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * Unless required by applicable law or agreed to in writing, software 11a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * See the License for the specific language governing permissions and 14a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian * limitations under the License. 15a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian */ 16a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 17a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <limits.h> 18a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <errno.h> 19a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <pthread.h> 208c4ab1fa14cc13c324bde91c0271f9ab5f4663d3Mathias Agopian#include <unistd.h> 21a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen#include <string.h> 22a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 23a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <sys/mman.h> 24a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <sys/stat.h> 25a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <sys/types.h> 26a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 27a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <cutils/log.h> 28a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <cutils/atomic.h> 29a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 30a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <hardware/hardware.h> 31a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include <hardware/gralloc.h> 32a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 33a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian#include "gralloc_priv.h" 34a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 358c4ab1fa14cc13c324bde91c0271f9ab5f4663d3Mathias Agopian 36a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen/* desktop Linux needs a little help with gettid() */ 37a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen#if defined(ARCH_X86) && !defined(HAVE_ANDROID_OS) 38a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen#define __KERNEL__ 39a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen# include <linux/unistd.h> 40a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissenpid_t gettid() { return syscall(__NR_gettid);} 41a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen#undef __KERNEL__ 42a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen#endif 43a4b587cb063dfd1b11f0006b0149e5e3045cc873Marco Nelissen 44a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian/*****************************************************************************/ 45a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 465115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopianstatic int gralloc_map(gralloc_module_t const* module, 47a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian buffer_handle_t handle, 48a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian void** vaddr) 49a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian{ 505115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian private_handle_t* hnd = (private_handle_t*)handle; 515115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { 528c4ab1fa14cc13c324bde91c0271f9ab5f4663d3Mathias Agopian size_t size = hnd->size; 538c4ab1fa14cc13c324bde91c0271f9ab5f4663d3Mathias Agopian void* mappedAddress = mmap(0, size, 548c4ab1fa14cc13c324bde91c0271f9ab5f4663d3Mathias Agopian PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0); 555115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian if (mappedAddress == MAP_FAILED) { 5660d056bf2926357260592409dee31374fa8e301bSteve Block ALOGE("Could not mmap %s", strerror(errno)); 575115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian return -errno; 585115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian } 598c4ab1fa14cc13c324bde91c0271f9ab5f4663d3Mathias Agopian hnd->base = intptr_t(mappedAddress) + hnd->offset; 60cee8501c1623765d5287fcd440fbf8001b9de183Steve Block //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p", 618c4ab1fa14cc13c324bde91c0271f9ab5f4663d3Mathias Agopian // hnd->fd, hnd->offset, hnd->size, mappedAddress); 62a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian } 635115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian *vaddr = (void*)hnd->base; 645115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian return 0; 65a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian} 66a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 675115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopianstatic int gralloc_unmap(gralloc_module_t const* module, 68a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian buffer_handle_t handle) 69a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian{ 705115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian private_handle_t* hnd = (private_handle_t*)handle; 715115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { 72bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian void* base = (void*)hnd->base; 73bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian size_t size = hnd->size; 74cee8501c1623765d5287fcd440fbf8001b9de183Steve Block //ALOGD("unmapping from %p, size=%d", base, size); 75bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian if (munmap(base, size) < 0) { 7660d056bf2926357260592409dee31374fa8e301bSteve Block ALOGE("Could not unmap %s", strerror(errno)); 775115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian } 78a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian } 795115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian hnd->base = 0; 805115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian return 0; 81a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian} 82a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 835115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian/*****************************************************************************/ 845115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian 85c71b6caece3fb1b85327f05acdd1becae4eda137Jesse Hallstatic pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; 865115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian 875115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian/*****************************************************************************/ 885115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian 89988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopianint gralloc_register_buffer(gralloc_module_t const* module, 90988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian buffer_handle_t handle) 91988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian{ 92988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian if (private_handle_t::validate(handle) < 0) 93988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian return -EINVAL; 945115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian 950d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // *** WARNING WARNING WARNING *** 960d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // 970d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // If a buffer handle is passed from the process that allocated it to a 980d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // different process, and then back to the allocator process, we will 990d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // create a second mapping of the buffer. If the process reads and writes 1000d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // through both mappings, normal memory ordering guarantees may be 1010d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // violated, depending on the processor cache implementation*. 1020d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // 1030d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // If you are deriving a new gralloc implementation from this code, don't 1040d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // do this. A "real" gralloc should provide a single reference-counted 1050d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // mapping for each buffer in a process. 1060d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // 1070d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // In the current system, there is one case that needs a buffer to be 1080d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // registered in the same process that allocated it. The SurfaceFlinger 1090d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // process acts as the IGraphicBufferAlloc Binder provider, so all gralloc 1100d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // allocations happen in its process. After returning the buffer handle to 1110d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // the IGraphicBufferAlloc client, SurfaceFlinger free's its handle to the 1120d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // buffer (unmapping it from the SurfaceFlinger process). If 1130d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // SurfaceFlinger later acts as the producer end of the buffer queue the 1140d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // buffer belongs to, it will get a new handle to the buffer in response 1150d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // to IGraphicBufferProducer::requestBuffer(). Like any buffer handle 1160d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // received through Binder, the SurfaceFlinger process will register it. 1170d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // Since it already freed its original handle, it will only end up with 1180d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // one mapping to the buffer and there will be no problem. 1190d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // 1200d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // Currently SurfaceFlinger only acts as a buffer producer for a remote 1210d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // consumer when taking screenshots and when using virtual displays. 1220d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // 1230d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // Eventually, each application should be allowed to make its own gralloc 1240d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // allocations, solving the problem. Also, this ashmem-based gralloc 1250d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // should go away, replaced with a real ion-based gralloc. 1260d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // 1270d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // * Specifically, associative virtually-indexed caches are likely to have 1280d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall // problems. Most modern L1 caches fit that description. 1290d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall 1300d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall private_handle_t* hnd = (private_handle_t*)handle; 1310d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall ALOGD_IF(hnd->pid == getpid(), 1320d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall "Registering a buffer in the process that created it. " 1330d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall "This may cause memory ordering problems."); 1340d8f81a9848b47afd7f4a75cda9955a5cc77b465Jesse Hall 135c71b6caece3fb1b85327f05acdd1becae4eda137Jesse Hall void *vaddr; 136c71b6caece3fb1b85327f05acdd1becae4eda137Jesse Hall return gralloc_map(module, handle, &vaddr); 137988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian} 138988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian 139988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopianint gralloc_unregister_buffer(gralloc_module_t const* module, 140988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian buffer_handle_t handle) 141988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian{ 142988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian if (private_handle_t::validate(handle) < 0) 143988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian return -EINVAL; 144988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian 145f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian private_handle_t* hnd = (private_handle_t*)handle; 146c71b6caece3fb1b85327f05acdd1becae4eda137Jesse Hall if (hnd->base) 147c71b6caece3fb1b85327f05acdd1becae4eda137Jesse Hall gralloc_unmap(module, handle); 148c71b6caece3fb1b85327f05acdd1becae4eda137Jesse Hall 149988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian return 0; 150988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian} 151a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 152f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopianint mapBuffer(gralloc_module_t const* module, 153bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian private_handle_t* hnd) 154bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian{ 155f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian void* vaddr; 156f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian return gralloc_map(module, hnd, &vaddr); 157f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian} 158bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian 159f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopianint terminateBuffer(gralloc_module_t const* module, 160f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian private_handle_t* hnd) 161f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian{ 162f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian if (hnd->base) { 163bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian // this buffer was mapped, unmap it now 164f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian gralloc_unmap(module, hnd); 165bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian } 166bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian 167bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian return 0; 168bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian} 169bd80b38f2945ac918f66fb336c149b28b9dd030eMathias Agopian 170a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopianint gralloc_lock(gralloc_module_t const* module, 171a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian buffer_handle_t handle, int usage, 172988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian int l, int t, int w, int h, 173988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian void** vaddr) 174a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian{ 175f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // this is called when a buffer is being locked for software 176f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // access. in thin implementation we have nothing to do since 177f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // not synchronization with the h/w is needed. 178f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // typically this is used to wait for the h/w to finish with 179f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // this buffer if relevant. the data cache may need to be 180f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // flushed or invalidated depending on the usage bits and the 181f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // hardware. 182f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian 183988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian if (private_handle_t::validate(handle) < 0) 184988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian return -EINVAL; 185988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian 1865115665397e5f5be5a5d464bc22b70b023d243d9Mathias Agopian private_handle_t* hnd = (private_handle_t*)handle; 187f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian *vaddr = (void*)hnd->base; 188f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian return 0; 189a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian} 190a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian 191c71b6caece3fb1b85327f05acdd1becae4eda137Jesse Hallint gralloc_unlock(gralloc_module_t const* module, 192a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian buffer_handle_t handle) 193a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian{ 194f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // we're done with a software buffer. nothing to do in this 195f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian // implementation. typically this is used to flush the data cache. 196f96b2064d7753af7ab75f05eff9559f401e1a4adMathias Agopian 197988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian if (private_handle_t::validate(handle) < 0) 198988b8bd553180e8d71b4028ecb721f46312efe62Mathias Agopian return -EINVAL; 199a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian return 0; 200a8a75166a2d3c7639a7432a67075c98796165206Mathias Agopian} 201