Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2009, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <sys/mman.h> |
| 19 | #include <utils/CallStack.h> |
| 20 | #include <cutils/ashmem.h> |
| 21 | #include <cutils/log.h> |
| 22 | #include <utils/String8.h> |
| 23 | |
| 24 | #include <ui/BufferMapper.h> |
| 25 | |
| 26 | #include "BufferAllocator.h" |
| 27 | |
| 28 | // FIXME: ANDROID_GRALLOC_DEBUG must only be used with *our* gralloc |
| 29 | #define ANDROID_GRALLOC_DEBUG 1 |
| 30 | |
| 31 | |
| 32 | namespace android { |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
| 35 | Mutex BufferAllocator::sLock; |
| 36 | KeyedVector<buffer_handle_t, BufferAllocator::alloc_rec_t> BufferAllocator::sAllocList; |
| 37 | |
| 38 | BufferAllocator::BufferAllocator() |
| 39 | : mAllocDev(0) |
| 40 | { |
| 41 | hw_module_t const* module; |
| 42 | int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
| 43 | LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); |
| 44 | if (err == 0) { |
| 45 | gralloc_open(module, &mAllocDev); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | BufferAllocator::~BufferAllocator() |
| 50 | { |
| 51 | gralloc_close(mAllocDev); |
| 52 | } |
| 53 | |
| 54 | void BufferAllocator::dump(String8& result) const |
| 55 | { |
| 56 | Mutex::Autolock _l(sLock); |
| 57 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 58 | size_t total = 0; |
| 59 | const size_t SIZE = 512; |
| 60 | char buffer[SIZE]; |
| 61 | snprintf(buffer, SIZE, "Allocated buffers:\n"); |
| 62 | result.append(buffer); |
| 63 | const size_t c = list.size(); |
| 64 | for (size_t i=0 ; i<c ; i++) { |
| 65 | const alloc_rec_t& rec(list.valueAt(i)); |
| 66 | snprintf(buffer, SIZE, "%10p: %10p | %7.2f KB | %4u x %4u | %2d | 0x%08x\n", |
| 67 | list.keyAt(i), rec.vaddr, rec.size/1024.0f, |
| 68 | rec.w, rec.h, rec.format, rec.usage); |
| 69 | result.append(buffer); |
| 70 | total += rec.size; |
| 71 | } |
| 72 | snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f); |
| 73 | result.append(buffer); |
| 74 | } |
| 75 | |
| 76 | status_t BufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, |
| 77 | int usage, buffer_handle_t* handle, int32_t* stride) |
| 78 | { |
| 79 | Mutex::Autolock _l(mLock); |
| 80 | |
| 81 | // we have a h/w allocator and h/w buffer is requested |
| 82 | status_t err = mAllocDev->alloc(mAllocDev, |
| 83 | w, h, format, usage, handle, stride); |
| 84 | LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", |
| 85 | w, h, format, usage, err, strerror(-err)); |
| 86 | |
| 87 | if (err == NO_ERROR) { |
| 88 | Mutex::Autolock _l(sLock); |
| 89 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 90 | alloc_rec_t rec; |
| 91 | rec.w = w; |
| 92 | rec.h = h; |
| 93 | rec.format = format; |
| 94 | rec.usage = usage; |
| 95 | rec.vaddr = 0; |
| 96 | rec.size = h * stride[0] * bytesPerPixel(format); |
| 97 | list.add(*handle, rec); |
| 98 | } |
| 99 | |
| 100 | return err; |
| 101 | } |
| 102 | |
| 103 | status_t BufferAllocator::free(buffer_handle_t handle) |
| 104 | { |
| 105 | Mutex::Autolock _l(mLock); |
| 106 | |
Mathias Agopian | 8b765b7 | 2009-04-10 20:34:46 -0700 | [diff] [blame^] | 107 | #if ANDROID_GRALLOC_DEBUG |
| 108 | void* base = (void*)(handle->data[2]); |
| 109 | if (base) { |
| 110 | CallStack s; |
| 111 | s.update(); |
| 112 | s.dump(""); |
| 113 | BufferMapper::get().dump(handle); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 114 | } |
| 115 | #endif |
| 116 | |
| 117 | status_t err = mAllocDev->free(mAllocDev, handle); |
| 118 | LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); |
| 119 | |
| 120 | if (err == NO_ERROR) { |
| 121 | Mutex::Autolock _l(sLock); |
| 122 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 123 | list.removeItem(handle); |
| 124 | } |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | status_t BufferAllocator::map(buffer_handle_t handle, void** addr) |
| 130 | { |
| 131 | Mutex::Autolock _l(mLock); |
| 132 | status_t err = BufferMapper::get().map(handle, addr); |
| 133 | if (err == NO_ERROR) { |
| 134 | Mutex::Autolock _l(sLock); |
| 135 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 136 | ssize_t idx = list.indexOfKey(handle); |
| 137 | if (idx >= 0) |
| 138 | list.editValueAt(idx).vaddr = addr; |
| 139 | } |
| 140 | |
| 141 | return err; |
| 142 | } |
| 143 | |
| 144 | status_t BufferAllocator::unmap(buffer_handle_t handle) |
| 145 | { |
| 146 | Mutex::Autolock _l(mLock); |
| 147 | gralloc_module_t* mod = (gralloc_module_t*)mAllocDev->common.module; |
Mathias Agopian | 8b765b7 | 2009-04-10 20:34:46 -0700 | [diff] [blame^] | 148 | status_t err = BufferMapper::get().unmap(handle); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 149 | if (err == NO_ERROR) { |
| 150 | Mutex::Autolock _l(sLock); |
| 151 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 152 | ssize_t idx = list.indexOfKey(handle); |
| 153 | if (idx >= 0) |
| 154 | list.editValueAt(idx).vaddr = 0; |
| 155 | } |
| 156 | |
| 157 | return err; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | // --------------------------------------------------------------------------- |
| 162 | }; // namespace android |