Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -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> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 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 | |
| 35 | #include <ion/ion.h> |
| 36 | #include <linux/ion.h> |
| 37 | |
| 38 | /*****************************************************************************/ |
| 39 | |
| 40 | static int gralloc_map(gralloc_module_t const* module, buffer_handle_t handle) |
| 41 | { |
| 42 | private_handle_t* hnd = (private_handle_t*)handle; |
| 43 | |
| 44 | void* mappedAddress = mmap(0, hnd->size, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 45 | hnd->fd, 0); |
| 46 | if (mappedAddress == MAP_FAILED) { |
| 47 | ALOGE("%s: could not mmap %s", __func__, strerror(errno)); |
| 48 | return -errno; |
| 49 | } |
| 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; |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int gralloc_unmap(gralloc_module_t const* module, buffer_handle_t handle) |
| 57 | { |
| 58 | private_handle_t* hnd = (private_handle_t*)handle; |
| 59 | |
| 60 | if (!hnd->base) |
| 61 | return 0; |
| 62 | |
| 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); |
| 66 | } |
| 67 | ALOGV("%s: base %p %d %d %d %d\n", __func__, hnd->base, hnd->size, |
| 68 | hnd->width, hnd->height, hnd->stride); |
| 69 | hnd->base = 0; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /*****************************************************************************/ |
| 74 | |
| 75 | int grallocMap(gralloc_module_t const* module, private_handle_t *hnd) |
| 76 | { |
| 77 | return gralloc_map(module, hnd); |
| 78 | } |
| 79 | |
| 80 | int grallocUnmap(gralloc_module_t const* module, private_handle_t *hnd) |
| 81 | { |
| 82 | return gralloc_unmap(module, hnd); |
| 83 | } |
| 84 | |
| 85 | static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; |
| 86 | |
| 87 | /*****************************************************************************/ |
| 88 | |
| 89 | int gralloc_register_buffer(gralloc_module_t const* module, |
| 90 | buffer_handle_t handle) |
| 91 | { |
| 92 | int err; |
| 93 | if (private_handle_t::validate(handle) < 0) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | err = gralloc_map(module, handle); |
| 97 | |
| 98 | private_handle_t* hnd = (private_handle_t*)handle; |
| 99 | ALOGV("%s: base %p %d %d %d %d\n", __func__, hnd->base, hnd->size, |
| 100 | hnd->width, hnd->height, hnd->stride); |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
| 105 | buffer_handle_t handle) |
| 106 | { |
| 107 | if (private_handle_t::validate(handle) < 0) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | private_handle_t* hnd = (private_handle_t*)handle; |
| 111 | ALOGV("%s: base %p %d %d %d %d\n", __func__, hnd->base, hnd->size, |
| 112 | hnd->width, hnd->height, hnd->stride); |
| 113 | |
| 114 | gralloc_unmap(module, handle); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int gralloc_lock(gralloc_module_t const* module, |
| 120 | buffer_handle_t handle, int usage, |
| 121 | int l, int t, int w, int h, |
| 122 | void** vaddr) |
| 123 | { |
| 124 | // this is called when a buffer is being locked for software |
| 125 | // access. in thin implementation we have nothing to do since |
| 126 | // not synchronization with the h/w is needed. |
| 127 | // typically this is used to wait for the h/w to finish with |
| 128 | // this buffer if relevant. the data cache may need to be |
| 129 | // flushed or invalidated depending on the usage bits and the |
| 130 | // hardware. |
| 131 | |
| 132 | if (private_handle_t::validate(handle) < 0) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | private_handle_t* hnd = (private_handle_t*)handle; |
| 136 | *vaddr = (void*)hnd->base; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int gralloc_unlock(gralloc_module_t const* module, |
| 141 | buffer_handle_t handle) |
| 142 | { |
| 143 | // we're done with a software buffer. nothing to do in this |
| 144 | // implementation. typically this is used to flush the data cache. |
| 145 | |
| 146 | if (private_handle_t::validate(handle) < 0) |
| 147 | return -EINVAL; |
| 148 | return 0; |
| 149 | } |