| 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 |  | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 21 | #include <log/log.h> | 
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 22 | #include <utils/Singleton.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 23 | #include <utils/String8.h> | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 24 | #include <utils/Trace.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 25 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 26 | #include <ui/GraphicBufferAllocator.h> | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 27 | #include <ui/Gralloc1On0Adapter.h> | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 28 | #include <ui/GrallocAllocator.h> | 
|  | 29 | #include <ui/GrallocMapper.h> | 
|  | 30 | #include <ui/GraphicBufferMapper.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 31 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | namespace android { | 
|  | 33 | // --------------------------------------------------------------------------- | 
|  | 34 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 35 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) | 
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 36 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 37 | Mutex GraphicBufferAllocator::sLock; | 
| Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 38 | KeyedVector<buffer_handle_t, | 
|  | 39 | GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 40 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 41 | GraphicBufferAllocator::GraphicBufferAllocator() | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 42 | : mAllocator(std::make_unique<Gralloc2::Allocator>()), | 
|  | 43 | mMapper(GraphicBufferMapper::getInstance()) | 
|  | 44 | { | 
|  | 45 | if (!mAllocator->valid()) { | 
|  | 46 | mLoader = std::make_unique<Gralloc1::Loader>(); | 
|  | 47 | mDevice = mLoader->getDevice(); | 
|  | 48 | } | 
|  | 49 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 50 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 51 | GraphicBufferAllocator::~GraphicBufferAllocator() {} | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 52 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 53 | void GraphicBufferAllocator::dump(String8& result) const | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 54 | { | 
|  | 55 | Mutex::Autolock _l(sLock); | 
|  | 56 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
|  | 57 | size_t total = 0; | 
| Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 58 | const size_t SIZE = 4096; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 59 | char buffer[SIZE]; | 
|  | 60 | snprintf(buffer, SIZE, "Allocated buffers:\n"); | 
|  | 61 | result.append(buffer); | 
|  | 62 | const size_t c = list.size(); | 
|  | 63 | for (size_t i=0 ; i<c ; i++) { | 
|  | 64 | const alloc_rec_t& rec(list.valueAt(i)); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 65 | if (rec.size) { | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 66 | snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%08x | %s\n", | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 67 | list.keyAt(i), rec.size/1024.0f, | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 68 | rec.width, rec.stride, rec.height, rec.layerCount, rec.format, | 
|  | 69 | rec.usage, rec.requestorName.c_str()); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 70 | } else { | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 71 | snprintf(buffer, SIZE, "%10p: unknown     | %4u (%4u) x %4u | %4u | %8X | 0x%08x | %s\n", | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 72 | list.keyAt(i), | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 73 | rec.width, rec.stride, rec.height, rec.layerCount, rec.format, | 
|  | 74 | rec.usage, rec.requestorName.c_str()); | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 75 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 76 | result.append(buffer); | 
|  | 77 | total += rec.size; | 
|  | 78 | } | 
| Mathias Agopian | a947de8 | 2011-07-29 16:35:41 -0700 | [diff] [blame] | 79 | snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 80 | result.append(buffer); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 81 |  | 
|  | 82 | std::string deviceDump; | 
|  | 83 | if (mAllocator->valid()) { | 
|  | 84 | deviceDump = mAllocator->dumpDebugInfo(); | 
|  | 85 | } else { | 
|  | 86 | deviceDump = mDevice->dump(); | 
|  | 87 | } | 
|  | 88 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 89 | result.append(deviceDump.c_str(), deviceDump.size()); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 92 | void GraphicBufferAllocator::dumpToSystemLog() | 
|  | 93 | { | 
|  | 94 | String8 s; | 
|  | 95 | GraphicBufferAllocator::getInstance().dump(s); | 
| Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 96 | ALOGD("%s", s.string()); | 
| Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 99 | namespace { | 
|  | 100 |  | 
|  | 101 | class HalBuffer { | 
|  | 102 | public: | 
|  | 103 | HalBuffer(const Gralloc2::Allocator* allocator, | 
|  | 104 | uint32_t width, uint32_t height, | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 105 | PixelFormat format, uint32_t layerCount, uint32_t usage) | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 106 | : mAllocator(allocator), mBufferValid(false) | 
|  | 107 | { | 
| Chia-I Wu | b018bf0 | 2016-11-22 13:29:49 +0800 | [diff] [blame] | 108 | Gralloc2::IAllocatorClient::BufferDescriptorInfo info = {}; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 109 | info.width = width; | 
|  | 110 | info.height = height; | 
|  | 111 | info.format = static_cast<Gralloc2::PixelFormat>(format); | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 112 | info.layerCount = layerCount; | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 113 | info.producerUsageMask = usage; | 
|  | 114 | info.consumerUsageMask = usage; | 
|  | 115 |  | 
|  | 116 | Gralloc2::BufferDescriptor descriptor; | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 117 | auto error = mAllocator->createBufferDescriptor(info, &descriptor); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 118 | if (error != Gralloc2::Error::NONE) { | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 119 | ALOGE("Failed to create desc (%u x %u) layerCount %u format %d usage %u: %d", | 
|  | 120 | width, height, layerCount, format, usage, error); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 121 | return; | 
|  | 122 | } | 
|  | 123 |  | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 124 | error = mAllocator->allocate(descriptor, &mBuffer); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 125 | if (error == Gralloc2::Error::NOT_SHARED) { | 
|  | 126 | error = Gralloc2::Error::NONE; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | if (error != Gralloc2::Error::NONE) { | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 130 | ALOGE("Failed to allocate (%u x %u) layerCount %u format %d usage %u: %d", | 
|  | 131 | width, height, layerCount, format, usage, error); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 132 | mAllocator->destroyBufferDescriptor(descriptor); | 
|  | 133 | return; | 
|  | 134 | } | 
|  | 135 |  | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 136 | error = mAllocator->exportHandle(descriptor, mBuffer, &mHandle); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 137 | if (error != Gralloc2::Error::NONE) { | 
|  | 138 | ALOGE("Failed to export handle"); | 
|  | 139 | mAllocator->free(mBuffer); | 
|  | 140 | mAllocator->destroyBufferDescriptor(descriptor); | 
|  | 141 | return; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | mAllocator->destroyBufferDescriptor(descriptor); | 
|  | 145 |  | 
|  | 146 | mBufferValid = true; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | ~HalBuffer() | 
|  | 150 | { | 
|  | 151 | if (mBufferValid) { | 
|  | 152 | if (mHandle) { | 
|  | 153 | native_handle_close(mHandle); | 
|  | 154 | native_handle_delete(mHandle); | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | mAllocator->free(mBuffer); | 
|  | 158 | } | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | bool exportHandle(GraphicBufferMapper& mapper, | 
|  | 162 | buffer_handle_t* handle, uint32_t* stride) | 
|  | 163 | { | 
|  | 164 | if (!mBufferValid) { | 
|  | 165 | return false; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | if (mapper.registerBuffer(mHandle)) { | 
|  | 169 | return false; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | *handle = mHandle; | 
|  | 173 |  | 
| Chia-I Wu | 67e376d | 2016-12-19 11:36:22 +0800 | [diff] [blame] | 174 | auto error = mapper.getGrallocMapper().getStride(mHandle, stride); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 175 | if (error != Gralloc2::Error::NONE) { | 
|  | 176 | ALOGW("Failed to get stride from buffer: %d", error); | 
|  | 177 | *stride = 0; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | mHandle = nullptr; | 
|  | 181 | mAllocator->free(mBuffer); | 
|  | 182 | mBufferValid = false; | 
|  | 183 |  | 
|  | 184 | return true; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | private: | 
|  | 188 | const Gralloc2::Allocator* mAllocator; | 
|  | 189 |  | 
|  | 190 | bool mBufferValid; | 
|  | 191 | Gralloc2::Buffer mBuffer; | 
|  | 192 | native_handle_t* mHandle; | 
|  | 193 | }; | 
|  | 194 |  | 
|  | 195 | } // namespace | 
|  | 196 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 197 | status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 198 | PixelFormat format, uint32_t layerCount, uint32_t usage, | 
|  | 199 | buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId, | 
|  | 200 | std::string requestorName) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 201 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 202 | ATRACE_CALL(); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 203 |  | 
| Mathias Agopian | 5629eb1 | 2010-04-15 14:57:39 -0700 | [diff] [blame] | 204 | // make sure to not allocate a N x 0 or 0 x N buffer, since this is | 
|  | 205 | // allowed from an API stand-point allocate a 1x1 buffer instead. | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 206 | if (!width || !height) | 
|  | 207 | width = height = 1; | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 208 |  | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 209 | // Ensure that layerCount is valid. | 
|  | 210 | if (layerCount < 1) | 
|  | 211 | layerCount = 1; | 
|  | 212 |  | 
| Dan Stoza | 24fa67f | 2015-05-29 12:48:04 -0700 | [diff] [blame] | 213 | // Filter out any usage bits that should not be passed to the gralloc module | 
|  | 214 | usage &= GRALLOC_USAGE_ALLOC_MASK; | 
|  | 215 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 216 | gralloc1_error_t error; | 
|  | 217 | if (mAllocator->valid()) { | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 218 | HalBuffer buffer(mAllocator.get(), width, height, format, layerCount, | 
|  | 219 | usage); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 220 | if (!buffer.exportHandle(mMapper, handle, stride)) { | 
|  | 221 | return NO_MEMORY; | 
|  | 222 | } | 
|  | 223 | error = GRALLOC1_ERROR_NONE; | 
|  | 224 | } else { | 
|  | 225 | auto descriptor = mDevice->createDescriptor(); | 
|  | 226 | error = descriptor->setDimensions(width, height); | 
|  | 227 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 228 | ALOGE("Failed to set dimensions to (%u, %u): %d", | 
|  | 229 | width, height, error); | 
|  | 230 | return BAD_VALUE; | 
|  | 231 | } | 
|  | 232 | error = descriptor->setFormat( | 
|  | 233 | static_cast<android_pixel_format_t>(format)); | 
|  | 234 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 235 | ALOGE("Failed to set format to %d: %d", format, error); | 
|  | 236 | return BAD_VALUE; | 
|  | 237 | } | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 238 | if (mDevice->hasCapability(GRALLOC1_CAPABILITY_LAYERED_BUFFERS)) { | 
|  | 239 | error = descriptor->setLayerCount(layerCount); | 
|  | 240 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 241 | ALOGE("Failed to set layer count to %u: %d", layerCount, error); | 
|  | 242 | return BAD_VALUE; | 
|  | 243 | } | 
|  | 244 | } else if (layerCount > 1) { | 
|  | 245 | ALOGE("Failed to set layer count to %u: capability unsupported", | 
|  | 246 | layerCount); | 
|  | 247 | return BAD_VALUE; | 
|  | 248 | } | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 249 | error = descriptor->setProducerUsage( | 
|  | 250 | static_cast<gralloc1_producer_usage_t>(usage)); | 
|  | 251 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 252 | ALOGE("Failed to set producer usage to %u: %d", usage, error); | 
|  | 253 | return BAD_VALUE; | 
|  | 254 | } | 
|  | 255 | error = descriptor->setConsumerUsage( | 
|  | 256 | static_cast<gralloc1_consumer_usage_t>(usage)); | 
|  | 257 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 258 | ALOGE("Failed to set consumer usage to %u: %d", usage, error); | 
|  | 259 | return BAD_VALUE; | 
|  | 260 | } | 
| Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 261 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 262 | error = mDevice->allocate(descriptor, graphicBufferId, handle); | 
|  | 263 | if (error != GRALLOC1_ERROR_NONE) { | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 264 | ALOGE("Failed to allocate (%u x %u) layerCount %u format %d usage %u: %d", | 
|  | 265 | width, height, layerCount, format, usage, error); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 266 | return NO_MEMORY; | 
|  | 267 | } | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 268 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 269 | error = mDevice->getStride(*handle, stride); | 
|  | 270 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 271 | ALOGW("Failed to get stride from buffer: %d", error); | 
|  | 272 | } | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 273 | } | 
|  | 274 |  | 
|  | 275 | if (error == NO_ERROR) { | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 276 | Mutex::Autolock _l(sLock); | 
|  | 277 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 278 | uint32_t bpp = bytesPerPixel(format); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 279 | alloc_rec_t rec; | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 280 | rec.width = width; | 
|  | 281 | rec.height = height; | 
|  | 282 | rec.stride = *stride; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 283 | rec.format = format; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 284 | rec.layerCount = layerCount; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 285 | rec.usage = usage; | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 286 | rec.size = static_cast<size_t>(height * (*stride) * bpp); | 
| Dan Stoza | 024e931 | 2016-08-24 12:17:29 -0700 | [diff] [blame] | 287 | rec.requestorName = std::move(requestorName); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 288 | list.add(*handle, rec); | 
|  | 289 | } | 
|  | 290 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 291 | return NO_ERROR; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 292 | } | 
|  | 293 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 294 | status_t GraphicBufferAllocator::free(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 295 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 296 | ATRACE_CALL(); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 297 |  | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 298 | gralloc1_error_t error; | 
|  | 299 | if (mAllocator->valid()) { | 
|  | 300 | error = static_cast<gralloc1_error_t>( | 
|  | 301 | mMapper.unregisterBuffer(handle)); | 
|  | 302 | } else { | 
|  | 303 | error = mDevice->release(handle); | 
|  | 304 | } | 
|  | 305 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 306 | if (error != GRALLOC1_ERROR_NONE) { | 
|  | 307 | ALOGE("Failed to free buffer: %d", error); | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 308 | } | 
|  | 309 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 310 | Mutex::Autolock _l(sLock); | 
|  | 311 | KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); | 
|  | 312 | list.removeItem(handle); | 
|  | 313 |  | 
|  | 314 | return NO_ERROR; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 317 | // --------------------------------------------------------------------------- | 
|  | 318 | }; // namespace android |