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 | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 18 | #include <cutils/log.h> |
Mathias Agopian | a6b40ba | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 19 | |
| 20 | #include <utils/Singleton.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 21 | #include <utils/String8.h> |
| 22 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 23 | #include <ui/GraphicBufferAllocator.h> |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 24 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | // --------------------------------------------------------------------------- |
| 28 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 29 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) |
Mathias Agopian | a6b40ba | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 30 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 31 | Mutex GraphicBufferAllocator::sLock; |
| 32 | KeyedVector<buffer_handle_t, GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 34 | GraphicBufferAllocator::GraphicBufferAllocator() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 35 | : mAllocDev(0) |
| 36 | { |
| 37 | hw_module_t const* module; |
| 38 | int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
| 39 | LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); |
| 40 | if (err == 0) { |
| 41 | gralloc_open(module, &mAllocDev); |
| 42 | } |
| 43 | } |
| 44 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 45 | GraphicBufferAllocator::~GraphicBufferAllocator() |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 46 | { |
| 47 | gralloc_close(mAllocDev); |
| 48 | } |
| 49 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 50 | void GraphicBufferAllocator::dump(String8& result) const |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 51 | { |
| 52 | Mutex::Autolock _l(sLock); |
| 53 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 54 | size_t total = 0; |
| 55 | const size_t SIZE = 512; |
| 56 | char buffer[SIZE]; |
| 57 | snprintf(buffer, SIZE, "Allocated buffers:\n"); |
| 58 | result.append(buffer); |
| 59 | const size_t c = list.size(); |
| 60 | for (size_t i=0 ; i<c ; i++) { |
| 61 | const alloc_rec_t& rec(list.valueAt(i)); |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 62 | snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u x %4u | %2d | 0x%08x\n", |
| 63 | list.keyAt(i), rec.size/1024.0f, |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 64 | rec.w, rec.h, rec.format, rec.usage); |
| 65 | result.append(buffer); |
| 66 | total += rec.size; |
| 67 | } |
| 68 | snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f); |
| 69 | result.append(buffer); |
| 70 | } |
| 71 | |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 72 | static inline uint32_t clamp(uint32_t c) { |
| 73 | return c>0 ? c : 1; |
| 74 | } |
| 75 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 76 | status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 77 | int usage, buffer_handle_t* handle, int32_t* stride) |
| 78 | { |
| 79 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 80 | |
| 81 | // make sure to not allocate a 0 x 0 buffer |
| 82 | w = clamp(w); |
| 83 | h = clamp(h); |
| 84 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 85 | // we have a h/w allocator and h/w buffer is requested |
| 86 | status_t err = mAllocDev->alloc(mAllocDev, |
| 87 | w, h, format, usage, handle, stride); |
Mathias Agopian | 9779b221 | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 88 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 89 | LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", |
| 90 | w, h, format, usage, err, strerror(-err)); |
| 91 | |
| 92 | if (err == NO_ERROR) { |
| 93 | Mutex::Autolock _l(sLock); |
| 94 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 95 | alloc_rec_t rec; |
| 96 | rec.w = w; |
| 97 | rec.h = h; |
| 98 | rec.format = format; |
| 99 | rec.usage = usage; |
| 100 | rec.vaddr = 0; |
| 101 | rec.size = h * stride[0] * bytesPerPixel(format); |
| 102 | list.add(*handle, rec); |
Mathias Agopian | c4646e6 | 2009-09-27 18:44:09 -0700 | [diff] [blame] | 103 | } else { |
| 104 | String8 s; |
| 105 | dump(s); |
| 106 | LOGD("%s", s.string()); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return err; |
| 110 | } |
| 111 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame^] | 112 | status_t GraphicBufferAllocator::free(buffer_handle_t handle) |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 113 | { |
| 114 | Mutex::Autolock _l(mLock); |
| 115 | |
Mathias Agopian | a6b40ba | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 116 | status_t err = mAllocDev->free(mAllocDev, handle); |
| 117 | LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); |
| 118 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 119 | if (err == NO_ERROR) { |
| 120 | Mutex::Autolock _l(sLock); |
| 121 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 122 | list.removeItem(handle); |
| 123 | } |
| 124 | |
| 125 | return err; |
| 126 | } |
| 127 | |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 128 | // --------------------------------------------------------------------------- |
| 129 | }; // namespace android |