| 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 | 5629eb1 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 18 | #define LOG_TAG "GraphicBufferAllocator" |
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| Mathias Agopian | 5629eb1 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 20 | |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 21 | #include <cutils/log.h> |
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 22 | |
| 23 | #include <utils/Singleton.h> |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 24 | #include <utils/String8.h> |
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 25 | #include <utils/Trace.h> |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 26 | |
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 27 | #include <ui/GraphicBufferAllocator.h> |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 28 | |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | // --------------------------------------------------------------------------- |
| 31 | |
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 32 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) |
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 33 | |
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 34 | Mutex GraphicBufferAllocator::sLock; |
| Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 35 | KeyedVector<buffer_handle_t, |
| 36 | GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | |
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 38 | GraphicBufferAllocator::GraphicBufferAllocator() |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 39 | : mAllocDev(0) |
| 40 | { |
| 41 | hw_module_t const* module; |
| 42 | int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 43 | ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 44 | if (err == 0) { |
| 45 | gralloc_open(module, &mAllocDev); |
| 46 | } |
| 47 | } |
| 48 | |
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 49 | GraphicBufferAllocator::~GraphicBufferAllocator() |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 50 | { |
| 51 | gralloc_close(mAllocDev); |
| 52 | } |
| 53 | |
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 54 | void GraphicBufferAllocator::dump(String8& result) const |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 55 | { |
| 56 | Mutex::Autolock _l(sLock); |
| 57 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| 58 | size_t total = 0; |
| Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 59 | const size_t SIZE = 4096; |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 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)); |
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 66 | if (rec.size) { |
| 67 | snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n", |
| 68 | list.keyAt(i), rec.size/1024.0f, |
| 69 | rec.w, rec.s, rec.h, rec.format, rec.usage); |
| 70 | } else { |
| 71 | snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n", |
| 72 | list.keyAt(i), |
| 73 | rec.w, rec.s, rec.h, rec.format, rec.usage); |
| 74 | } |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 75 | result.append(buffer); |
| 76 | total += rec.size; |
| 77 | } |
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 78 | snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f); |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 79 | result.append(buffer); |
| Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 80 | if (mAllocDev->common.version >= 1 && mAllocDev->dump) { |
| 81 | mAllocDev->dump(mAllocDev, buffer, SIZE); |
| 82 | result.append(buffer); |
| 83 | } |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 86 | void GraphicBufferAllocator::dumpToSystemLog() |
| 87 | { |
| 88 | String8 s; |
| 89 | GraphicBufferAllocator::getInstance().dump(s); |
| Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 90 | ALOGD("%s", s.string()); |
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| Jamie Gennis | f53f9c6 | 2012-12-10 17:06:44 -0800 | [diff] [blame] | 93 | class BufferLiberatorThread : public Thread { |
| 94 | public: |
| 95 | |
| 96 | static void queueCaptiveBuffer(buffer_handle_t handle) { |
| 97 | size_t queueSize; |
| 98 | { |
| 99 | Mutex::Autolock lock(sMutex); |
| 100 | if (sThread == NULL) { |
| 101 | sThread = new BufferLiberatorThread; |
| 102 | sThread->run("BufferLiberator"); |
| 103 | } |
| 104 | |
| 105 | sThread->mQueue.push_back(handle); |
| 106 | sThread->mQueuedCondition.signal(); |
| 107 | queueSize = sThread->mQueue.size(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void waitForLiberation() { |
| 112 | Mutex::Autolock lock(sMutex); |
| 113 | |
| 114 | waitForLiberationLocked(); |
| 115 | } |
| 116 | |
| 117 | static void maybeWaitForLiberation() { |
| 118 | Mutex::Autolock lock(sMutex); |
| 119 | if (sThread != NULL) { |
| 120 | if (sThread->mQueue.size() > 8) { |
| 121 | waitForLiberationLocked(); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | |
| 128 | BufferLiberatorThread() {} |
| 129 | |
| 130 | virtual bool threadLoop() { |
| 131 | buffer_handle_t handle; |
| 132 | { // Scope for mutex |
| 133 | Mutex::Autolock lock(sMutex); |
| 134 | while (mQueue.isEmpty()) { |
| 135 | mQueuedCondition.wait(sMutex); |
| 136 | } |
| 137 | handle = mQueue[0]; |
| 138 | } |
| 139 | |
| 140 | status_t err; |
| 141 | GraphicBufferAllocator& gba(GraphicBufferAllocator::get()); |
| 142 | { // Scope for tracing |
| 143 | ATRACE_NAME("gralloc::free"); |
| 144 | err = gba.mAllocDev->free(gba.mAllocDev, handle); |
| 145 | } |
| 146 | ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); |
| 147 | |
| 148 | if (err == NO_ERROR) { |
| 149 | Mutex::Autolock _l(GraphicBufferAllocator::sLock); |
| 150 | KeyedVector<buffer_handle_t, GraphicBufferAllocator::alloc_rec_t>& |
| 151 | list(GraphicBufferAllocator::sAllocList); |
| 152 | list.removeItem(handle); |
| 153 | } |
| 154 | |
| 155 | { // Scope for mutex |
| 156 | Mutex::Autolock lock(sMutex); |
| 157 | mQueue.removeAt(0); |
| 158 | mFreedCondition.broadcast(); |
| 159 | } |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | static void waitForLiberationLocked() { |
| 165 | if (sThread == NULL) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | const nsecs_t timeout = 500 * 1000 * 1000; |
| 170 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 171 | nsecs_t timeToStop = now + timeout; |
| 172 | while (!sThread->mQueue.isEmpty() && now < timeToStop) { |
| 173 | sThread->mFreedCondition.waitRelative(sMutex, timeToStop - now); |
| 174 | now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 175 | } |
| 176 | |
| 177 | if (!sThread->mQueue.isEmpty()) { |
| 178 | ALOGW("waitForLiberationLocked timed out"); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static Mutex sMutex; |
| 183 | static sp<BufferLiberatorThread> sThread; |
| 184 | Vector<buffer_handle_t> mQueue; |
| 185 | Condition mQueuedCondition; |
| 186 | Condition mFreedCondition; |
| 187 | }; |
| 188 | |
| 189 | Mutex BufferLiberatorThread::sMutex; |
| 190 | sp<BufferLiberatorThread> BufferLiberatorThread::sThread; |
| 191 | |
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 192 | status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 193 | int usage, buffer_handle_t* handle, int32_t* stride) |
| 194 | { |
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 195 | ATRACE_CALL(); |
| Mathias Agopian | 5629eb1 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 196 | // make sure to not allocate a N x 0 or 0 x N buffer, since this is |
| 197 | // allowed from an API stand-point allocate a 1x1 buffer instead. |
| 198 | if (!w || !h) |
| 199 | w = h = 1; |
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 200 | |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 201 | // we have a h/w allocator and h/w buffer is requested |
| Jamie Gennis | f53f9c6 | 2012-12-10 17:06:44 -0800 | [diff] [blame] | 202 | status_t err; |
| 203 | |
| 204 | // If too many async frees are queued up then wait for some of them to |
| 205 | // complete before attempting to allocate more memory. This is exercised |
| 206 | // by the android.opengl.cts.GLSurfaceViewTest CTS test. |
| 207 | BufferLiberatorThread::maybeWaitForLiberation(); |
| 208 | |
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 209 | err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride); |
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 210 | |
| Jamie Gennis | f53f9c6 | 2012-12-10 17:06:44 -0800 | [diff] [blame] | 211 | if (err != NO_ERROR) { |
| 212 | ALOGW("WOW! gralloc alloc failed, waiting for pending frees!"); |
| 213 | BufferLiberatorThread::waitForLiberation(); |
| 214 | err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride); |
| 215 | } |
| 216 | |
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 217 | ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 218 | w, h, format, usage, err, strerror(-err)); |
| Jamie Gennis | f53f9c6 | 2012-12-10 17:06:44 -0800 | [diff] [blame] | 219 | |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 220 | if (err == NO_ERROR) { |
| 221 | Mutex::Autolock _l(sLock); |
| 222 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); |
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 223 | int bpp = bytesPerPixel(format); |
| 224 | if (bpp < 0) { |
| 225 | // probably a HAL custom format. in any case, we don't know |
| 226 | // what its pixel size is. |
| 227 | bpp = 0; |
| 228 | } |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 229 | alloc_rec_t rec; |
| 230 | rec.w = w; |
| 231 | rec.h = h; |
| Mathias Agopian | 5629eb1 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 232 | rec.s = *stride; |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 233 | rec.format = format; |
| 234 | rec.usage = usage; |
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 235 | rec.size = h * stride[0] * bpp; |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 236 | list.add(*handle, rec); |
| 237 | } |
| 238 | |
| 239 | return err; |
| 240 | } |
| 241 | |
| Jamie Gennis | 2e59d2c | 2012-12-07 00:38:36 -0800 | [diff] [blame] | 242 | |
| 243 | status_t GraphicBufferAllocator::free(buffer_handle_t handle) |
| 244 | { |
| 245 | BufferLiberatorThread::queueCaptiveBuffer(handle); |
| 246 | return NO_ERROR; |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 249 | // --------------------------------------------------------------------------- |
| 250 | }; // namespace android |