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