| 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 | fe2f54f | 2017-02-15 19:48:58 -0800 | [diff] [blame] | 21 | #include <ui/GraphicBufferAllocator.h> | 
|  | 22 |  | 
|  | 23 | #include <stdio.h> | 
|  | 24 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 25 | #include <grallocusage/GrallocUsageConversion.h> | 
|  | 26 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 28 | #include <log/log.h> | 
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 29 | #include <utils/Singleton.h> | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 30 | #include <utils/Trace.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 31 |  | 
| Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 32 | #include <ui/Gralloc.h> | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 33 | #include <ui/Gralloc2.h> | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 34 | #include <ui/Gralloc3.h> | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 35 | #include <ui/GraphicBufferMapper.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 36 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | namespace android { | 
|  | 38 | // --------------------------------------------------------------------------- | 
|  | 39 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 40 | using base::StringAppendF; | 
|  | 41 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 42 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) | 
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 43 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 44 | Mutex GraphicBufferAllocator::sLock; | 
| Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 45 | KeyedVector<buffer_handle_t, | 
|  | 46 | GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 47 |  | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 48 | GraphicBufferAllocator::GraphicBufferAllocator() : mMapper(GraphicBufferMapper::getInstance()) { | 
|  | 49 | mAllocator = std::make_unique<const Gralloc3Allocator>( | 
|  | 50 | reinterpret_cast<const Gralloc3Mapper&>(mMapper.getGrallocMapper())); | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 51 | if (!mAllocator->isLoaded()) { | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 52 | mAllocator = std::make_unique<const Gralloc2Allocator>( | 
|  | 53 | reinterpret_cast<const Gralloc2Mapper&>(mMapper.getGrallocMapper())); | 
|  | 54 | } | 
|  | 55 |  | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 56 | if (!mAllocator->isLoaded()) { | 
| Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 57 | LOG_ALWAYS_FATAL("gralloc-allocator is missing"); | 
|  | 58 | } | 
|  | 59 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 60 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 61 | GraphicBufferAllocator::~GraphicBufferAllocator() {} | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 62 |  | 
| Dan Stoza | 45de5ba | 2019-04-25 14:12:09 -0700 | [diff] [blame] | 63 | size_t GraphicBufferAllocator::getTotalSize() const { | 
|  | 64 | Mutex::Autolock _l(sLock); | 
|  | 65 | size_t total = 0; | 
|  | 66 | for (size_t i = 0; i < sAllocList.size(); ++i) { | 
|  | 67 | total += sAllocList.valueAt(i).size; | 
|  | 68 | } | 
|  | 69 | return total; | 
|  | 70 | } | 
|  | 71 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 72 | void GraphicBufferAllocator::dump(std::string& result) const { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 73 | Mutex::Autolock _l(sLock); | 
|  | 74 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
|  | 75 | size_t total = 0; | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 76 | result.append("Allocated buffers:\n"); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 77 | const size_t c = list.size(); | 
|  | 78 | for (size_t i=0 ; i<c ; i++) { | 
|  | 79 | const alloc_rec_t& rec(list.valueAt(i)); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 80 | if (rec.size) { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 81 | StringAppendF(&result, | 
|  | 82 | "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64 " | %s\n", | 
|  | 83 | list.keyAt(i), rec.size / 1024.0, rec.width, rec.stride, rec.height, | 
|  | 84 | rec.layerCount, rec.format, rec.usage, rec.requestorName.c_str()); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 85 | } else { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 86 | StringAppendF(&result, | 
|  | 87 | "%10p: unknown     | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64 " | %s\n", | 
|  | 88 | list.keyAt(i), rec.width, rec.stride, rec.height, rec.layerCount, | 
|  | 89 | rec.format, rec.usage, rec.requestorName.c_str()); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 90 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 91 | total += rec.size; | 
|  | 92 | } | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 93 | StringAppendF(&result, "Total allocated (estimate): %.2f KB\n", total / 1024.0); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 94 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 95 | result.append(mAllocator->dumpDebugInfo()); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 98 | void GraphicBufferAllocator::dumpToSystemLog() | 
|  | 99 | { | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 100 | std::string s; | 
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 101 | GraphicBufferAllocator::getInstance().dump(s); | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 102 | ALOGD("%s", s.c_str()); | 
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 105 | status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, | 
| Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 106 | PixelFormat format, uint32_t layerCount, uint64_t usage, | 
|  | 107 | buffer_handle_t* handle, uint32_t* stride, | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 108 | uint64_t /*graphicBufferId*/, std::string requestorName) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 109 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 110 | ATRACE_CALL(); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 111 |  | 
| Mathias Agopian | 5629eb1 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 112 | // make sure to not allocate a N x 0 or 0 x N buffer, since this is | 
|  | 113 | // allowed from an API stand-point allocate a 1x1 buffer instead. | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 114 | if (!width || !height) | 
|  | 115 | width = height = 1; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 116 |  | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 117 | // Ensure that layerCount is valid. | 
|  | 118 | if (layerCount < 1) | 
|  | 119 | layerCount = 1; | 
|  | 120 |  | 
| Chia-I Wu | 12ca527 | 2018-11-05 09:41:30 -0800 | [diff] [blame] | 121 | // TODO(b/72323293, b/72703005): Remove these invalid bits from callers | 
|  | 122 | usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13)); | 
|  | 123 |  | 
| Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 124 | status_t error = | 
|  | 125 | mAllocator->allocate(width, height, format, layerCount, usage, 1, stride, handle); | 
|  | 126 | if (error == NO_ERROR) { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 127 | Mutex::Autolock _l(sLock); | 
|  | 128 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 129 | uint32_t bpp = bytesPerPixel(format); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 130 | alloc_rec_t rec; | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 131 | rec.width = width; | 
|  | 132 | rec.height = height; | 
|  | 133 | rec.stride = *stride; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 134 | rec.format = format; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 135 | rec.layerCount = layerCount; | 
| Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 136 | rec.usage = usage; | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 137 | rec.size = static_cast<size_t>(height * (*stride) * bpp); | 
| Dan Stoza | 024e931 | 2016-08-24 12:17:29 -0700 | [diff] [blame] | 138 | rec.requestorName = std::move(requestorName); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 139 | list.add(*handle, rec); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 140 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 141 | return NO_ERROR; | 
|  | 142 | } else { | 
| Chia-I Wu | 0ffa62b | 2017-04-19 22:20:55 -0700 | [diff] [blame] | 143 | ALOGE("Failed to allocate (%u x %u) layerCount %u format %d " | 
| Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 144 | "usage %" PRIx64 ": %d", | 
|  | 145 | width, height, layerCount, format, usage, | 
|  | 146 | error); | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 147 | return NO_MEMORY; | 
|  | 148 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 151 | status_t GraphicBufferAllocator::free(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 152 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 153 | ATRACE_CALL(); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 154 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 155 | // We allocated a buffer from the allocator and imported it into the | 
|  | 156 | // mapper to get the handle.  We just need to free the handle now. | 
|  | 157 | mMapper.freeBuffer(handle); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 158 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 159 | Mutex::Autolock _l(sLock); | 
|  | 160 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
|  | 161 | list.removeItem(handle); | 
|  | 162 |  | 
|  | 163 | return NO_ERROR; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 166 | // --------------------------------------------------------------------------- | 
|  | 167 | }; // namespace android |