| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 1 | /* | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 2 | ** | 
|  | 3 | ** Copyright 2009, The Android Open Source Project | 
|  | 4 | ** | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 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 | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 8 | ** | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 10 | ** | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 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 | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 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> | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 28 | #include <ui/Gralloc1On0Adapter.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 29 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 30 | namespace android { | 
|  | 31 | // --------------------------------------------------------------------------- | 
|  | 32 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 33 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) | 
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 34 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 35 | Mutex GraphicBufferAllocator::sLock; | 
| Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 36 | KeyedVector<buffer_handle_t, | 
|  | 37 | GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 38 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 39 | GraphicBufferAllocator::GraphicBufferAllocator() | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 40 | : mLoader(std::make_unique<Gralloc1::Loader>()), | 
|  | 41 | mDevice(mLoader->getDevice()) {} | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 42 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 43 | GraphicBufferAllocator::~GraphicBufferAllocator() {} | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 44 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 45 | void GraphicBufferAllocator::dump(String8& result) const | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 46 | { | 
|  | 47 | Mutex::Autolock _l(sLock); | 
|  | 48 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
|  | 49 | size_t total = 0; | 
| Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 50 | const size_t SIZE = 4096; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 51 | char buffer[SIZE]; | 
|  | 52 | snprintf(buffer, SIZE, "Allocated buffers:\n"); | 
|  | 53 | result.append(buffer); | 
|  | 54 | const size_t c = list.size(); | 
|  | 55 | for (size_t i=0 ; i<c ; i++) { | 
|  | 56 | const alloc_rec_t& rec(list.valueAt(i)); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 57 | if (rec.size) { | 
|  | 58 | snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n", | 
|  | 59 | list.keyAt(i), rec.size/1024.0f, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 60 | rec.width, rec.stride, rec.height, rec.format, rec.usage); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 61 | } else { | 
|  | 62 | snprintf(buffer, SIZE, "%10p: unknown     | %4u (%4u) x %4u | %8X | 0x%08x\n", | 
|  | 63 | list.keyAt(i), | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 64 | rec.width, rec.stride, rec.height, rec.format, rec.usage); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 65 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 66 | result.append(buffer); | 
|  | 67 | total += rec.size; | 
|  | 68 | } | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 69 | snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 70 | result.append(buffer); | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 71 | std::string deviceDump = mDevice->dump(); | 
|  | 72 | result.append(deviceDump.c_str(), deviceDump.size()); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 75 | void GraphicBufferAllocator::dumpToSystemLog() | 
|  | 76 | { | 
|  | 77 | String8 s; | 
|  | 78 | GraphicBufferAllocator::getInstance().dump(s); | 
| Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 79 | ALOGD("%s", s.string()); | 
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 82 | status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 83 | PixelFormat format, uint32_t usage, buffer_handle_t* handle, | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 84 | uint32_t* stride, uint64_t graphicBufferId) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 85 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 86 | ATRACE_CALL(); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 87 |  | 
| Mathias Agopian | 5629eb1 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 88 | // make sure to not allocate a N x 0 or 0 x N buffer, since this is | 
|  | 89 | // allowed from an API stand-point allocate a 1x1 buffer instead. | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 90 | if (!width || !height) | 
|  | 91 | width = height = 1; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 92 |  | 
| Dan Stoza | 24fa67f | 2015-05-29 12:48:04 -0700 | [diff] [blame] | 93 | // Filter out any usage bits that should not be passed to the gralloc module | 
|  | 94 | usage &= GRALLOC_USAGE_ALLOC_MASK; | 
|  | 95 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 96 | auto descriptor = mDevice->createDescriptor(); | 
|  | 97 | auto error = descriptor->setDimensions(width, height); | 
|  | 98 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 99 | ALOGE("Failed to set dimensions to (%u, %u): %d", width, height, error); | 
|  | 100 | return BAD_VALUE; | 
|  | 101 | } | 
|  | 102 | error = descriptor->setFormat(static_cast<android_pixel_format_t>(format)); | 
|  | 103 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 104 | ALOGE("Failed to set format to %d: %d", format, error); | 
|  | 105 | return BAD_VALUE; | 
|  | 106 | } | 
|  | 107 | error = descriptor->setProducerUsage( | 
|  | 108 | static_cast<gralloc1_producer_usage_t>(usage)); | 
|  | 109 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 110 | ALOGE("Failed to set producer usage to %u: %d", usage, error); | 
|  | 111 | return BAD_VALUE; | 
|  | 112 | } | 
|  | 113 | error = descriptor->setConsumerUsage( | 
|  | 114 | static_cast<gralloc1_consumer_usage_t>(usage)); | 
|  | 115 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 116 | ALOGE("Failed to set consumer usage to %u: %d", usage, error); | 
|  | 117 | return BAD_VALUE; | 
|  | 118 | } | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 119 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 120 | error = mDevice->allocate(descriptor, graphicBufferId, handle); | 
|  | 121 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 122 | ALOGE("Failed to allocate (%u x %u) format %d usage %u: %d", | 
|  | 123 | width, height, format, usage, error); | 
|  | 124 | return NO_MEMORY; | 
|  | 125 | } | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 126 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 127 | error = mDevice->getStride(*handle, stride); | 
|  | 128 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 129 | ALOGW("Failed to get stride from buffer: %d", error); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | if (error == NO_ERROR) { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 133 | Mutex::Autolock _l(sLock); | 
|  | 134 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 135 | uint32_t bpp = bytesPerPixel(format); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 136 | alloc_rec_t rec; | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 137 | rec.width = width; | 
|  | 138 | rec.height = height; | 
|  | 139 | rec.stride = *stride; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 140 | rec.format = format; | 
|  | 141 | rec.usage = usage; | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 142 | rec.size = static_cast<size_t>(height * (*stride) * bpp); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 143 | list.add(*handle, rec); | 
|  | 144 | } | 
|  | 145 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 146 | return NO_ERROR; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 149 | status_t GraphicBufferAllocator::free(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 150 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 151 | ATRACE_CALL(); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 152 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 153 | auto error = mDevice->release(handle); | 
|  | 154 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 155 | ALOGE("Failed to free buffer: %d", error); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 158 | Mutex::Autolock _l(sLock); | 
|  | 159 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
|  | 160 | list.removeItem(handle); | 
|  | 161 |  | 
|  | 162 | return NO_ERROR; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 165 | // --------------------------------------------------------------------------- | 
|  | 166 | }; // namespace android |