Mathias Agopian | 1473f46 | 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 | |
Mathias Agopian | 67bbac8 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 18 | #define LOG_TAG "GraphicBufferAllocator" |
| 19 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 20 | #include <cutils/log.h> |
Mathias Agopian | a6b40ba | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 21 | |
| 22 | #include <utils/Singleton.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 23 | #include <utils/String8.h> |
| 24 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 25 | #include <ui/GraphicBufferAllocator.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 26 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | // --------------------------------------------------------------------------- |
| 29 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 30 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) |
Mathias Agopian | a6b40ba | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 32 | Mutex GraphicBufferAllocator::sLock; |
Mathias Agopian | 6f5f5a0 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 33 | KeyedVector<buffer_handle_t, |
| 34 | GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 36 | GraphicBufferAllocator::GraphicBufferAllocator() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | : mAllocDev(0) |
| 38 | { |
| 39 | hw_module_t const* module; |
| 40 | int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
| 41 | LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); |
| 42 | if (err == 0) { |
| 43 | gralloc_open(module, &mAllocDev); |
| 44 | } |
| 45 | } |
| 46 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 47 | GraphicBufferAllocator::~GraphicBufferAllocator() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 48 | { |
| 49 | gralloc_close(mAllocDev); |
| 50 | } |
| 51 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 52 | void GraphicBufferAllocator::dump(String8& result) const |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 53 | { |
| 54 | Mutex::Autolock _l(sLock); |
| 55 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 56 | size_t total = 0; |
Erik Gilling | 94720d7 | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 57 | const size_t SIZE = 4096; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 58 | char buffer[SIZE]; |
| 59 | snprintf(buffer, SIZE, "Allocated buffers:\n"); |
| 60 | result.append(buffer); |
| 61 | const size_t c = list.size(); |
| 62 | for (size_t i=0 ; i<c ; i++) { |
| 63 | const alloc_rec_t& rec(list.valueAt(i)); |
Mathias Agopian | 8bb8245 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 64 | if (rec.size) { |
| 65 | snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n", |
| 66 | list.keyAt(i), rec.size/1024.0f, |
| 67 | rec.w, rec.s, rec.h, rec.format, rec.usage); |
| 68 | } else { |
| 69 | snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n", |
| 70 | list.keyAt(i), |
| 71 | rec.w, rec.s, rec.h, rec.format, rec.usage); |
| 72 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 73 | result.append(buffer); |
| 74 | total += rec.size; |
| 75 | } |
Mathias Agopian | 8bb8245 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 76 | snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 77 | result.append(buffer); |
Erik Gilling | 94720d7 | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 78 | if (mAllocDev->common.version >= 1 && mAllocDev->dump) { |
| 79 | mAllocDev->dump(mAllocDev, buffer, SIZE); |
| 80 | result.append(buffer); |
| 81 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Mathias Agopian | e869aee | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 84 | void GraphicBufferAllocator::dumpToSystemLog() |
| 85 | { |
| 86 | String8 s; |
| 87 | GraphicBufferAllocator::getInstance().dump(s); |
| 88 | LOGD("%s", s.string()); |
| 89 | } |
| 90 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 91 | status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 92 | int usage, buffer_handle_t* handle, int32_t* stride) |
| 93 | { |
Mathias Agopian | 67bbac8 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 94 | // make sure to not allocate a N x 0 or 0 x N buffer, since this is |
| 95 | // allowed from an API stand-point allocate a 1x1 buffer instead. |
| 96 | if (!w || !h) |
| 97 | w = h = 1; |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 98 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 99 | // we have a h/w allocator and h/w buffer is requested |
Mathias Agopian | 6f5f5a0 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 100 | status_t err; |
| 101 | |
Mathias Agopian | 19f9eda | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 102 | err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 103 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 104 | LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", |
| 105 | w, h, format, usage, err, strerror(-err)); |
| 106 | |
| 107 | if (err == NO_ERROR) { |
| 108 | Mutex::Autolock _l(sLock); |
| 109 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
Mathias Agopian | 8bb8245 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 110 | int bpp = bytesPerPixel(format); |
| 111 | if (bpp < 0) { |
| 112 | // probably a HAL custom format. in any case, we don't know |
| 113 | // what its pixel size is. |
| 114 | bpp = 0; |
| 115 | } |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 116 | alloc_rec_t rec; |
| 117 | rec.w = w; |
| 118 | rec.h = h; |
Mathias Agopian | 67bbac8 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 119 | rec.s = *stride; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 120 | rec.format = format; |
| 121 | rec.usage = usage; |
Mathias Agopian | 8bb8245 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 122 | rec.size = h * stride[0] * bpp; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 123 | list.add(*handle, rec); |
| 124 | } |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 129 | status_t GraphicBufferAllocator::free(buffer_handle_t handle) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 130 | { |
Mathias Agopian | 6f5f5a0 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 131 | status_t err; |
Mathias Agopian | 19f9eda | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 132 | |
| 133 | err = mAllocDev->free(mAllocDev, handle); |
Mathias Agopian | 6f5f5a0 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 134 | |
Mathias Agopian | a6b40ba | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 135 | LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 136 | if (err == NO_ERROR) { |
| 137 | Mutex::Autolock _l(sLock); |
| 138 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 139 | list.removeItem(handle); |
| 140 | } |
| 141 | |
| 142 | return err; |
| 143 | } |
| 144 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 145 | // --------------------------------------------------------------------------- |
| 146 | }; // namespace android |