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 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 18 | #include <cutils/log.h> |
Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 19 | |
| 20 | #include <utils/Singleton.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 21 | #include <utils/String8.h> |
| 22 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 23 | #include <ui/GraphicBufferAllocator.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 24 | |
Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 25 | #include <private/ui/sw_gralloc_handle.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | // --------------------------------------------------------------------------- |
| 29 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 30 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) |
Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 32 | Mutex GraphicBufferAllocator::sLock; |
Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 33 | KeyedVector<buffer_handle_t, |
| 34 | GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 36 | GraphicBufferAllocator::GraphicBufferAllocator() |
Mathias Agopian | 076b1cc | 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 | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 47 | GraphicBufferAllocator::~GraphicBufferAllocator() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 48 | { |
| 49 | gralloc_close(mAllocDev); |
| 50 | } |
| 51 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 52 | void GraphicBufferAllocator::dump(String8& result) const |
Mathias Agopian | 076b1cc | 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; |
| 57 | const size_t SIZE = 512; |
| 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 | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 64 | snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u x %4u | %2d | 0x%08x\n", |
| 65 | list.keyAt(i), rec.size/1024.0f, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 66 | rec.w, rec.h, rec.format, rec.usage); |
| 67 | result.append(buffer); |
| 68 | total += rec.size; |
| 69 | } |
| 70 | snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f); |
| 71 | result.append(buffer); |
| 72 | } |
| 73 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 74 | static inline uint32_t clamp(uint32_t c) { |
| 75 | return c>0 ? c : 1; |
| 76 | } |
| 77 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 78 | status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 79 | int usage, buffer_handle_t* handle, int32_t* stride) |
| 80 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 81 | // make sure to not allocate a 0 x 0 buffer |
| 82 | w = clamp(w); |
| 83 | h = clamp(h); |
| 84 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 85 | // we have a h/w allocator and h/w buffer is requested |
Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 86 | status_t err; |
| 87 | |
| 88 | if (usage & GRALLOC_USAGE_HW_MASK) { |
| 89 | err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride); |
| 90 | } else { |
| 91 | err = sw_gralloc_handle_t::alloc(w, h, format, usage, handle, stride); |
| 92 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 93 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 94 | LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", |
| 95 | w, h, format, usage, err, strerror(-err)); |
| 96 | |
| 97 | if (err == NO_ERROR) { |
| 98 | Mutex::Autolock _l(sLock); |
| 99 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 100 | alloc_rec_t rec; |
| 101 | rec.w = w; |
| 102 | rec.h = h; |
| 103 | rec.format = format; |
| 104 | rec.usage = usage; |
| 105 | rec.vaddr = 0; |
| 106 | rec.size = h * stride[0] * bytesPerPixel(format); |
| 107 | list.add(*handle, rec); |
Mathias Agopian | 737e786 | 2009-09-27 18:44:09 -0700 | [diff] [blame] | 108 | } else { |
| 109 | String8 s; |
| 110 | dump(s); |
| 111 | LOGD("%s", s.string()); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return err; |
| 115 | } |
| 116 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 117 | status_t GraphicBufferAllocator::free(buffer_handle_t handle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 118 | { |
Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 119 | status_t err; |
| 120 | if (sw_gralloc_handle_t::validate(handle) < 0) { |
| 121 | err = mAllocDev->free(mAllocDev, handle); |
| 122 | } else { |
| 123 | err = sw_gralloc_handle_t::free((sw_gralloc_handle_t*)handle); |
| 124 | } |
| 125 | |
Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 126 | LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 127 | if (err == NO_ERROR) { |
| 128 | Mutex::Autolock _l(sLock); |
| 129 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 130 | list.removeItem(handle); |
| 131 | } |
| 132 | |
| 133 | return err; |
| 134 | } |
| 135 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 136 | // --------------------------------------------------------------------------- |
| 137 | }; // namespace android |