blob: a9f7cedab652b9e17c5a9b9b12d2a2eba36b6e89 [file] [log] [blame]
Dan Stozad3182402014-11-17 12:03:59 -08001/*
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002**
3** Copyright 2009, The Android Open Source Project
4**
Dan Stozad3182402014-11-17 12:03:59 -08005** 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 Agopian076b1cc2009-04-10 14:24:30 -07008**
Dan Stozad3182402014-11-17 12:03:59 -08009** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian076b1cc2009-04-10 14:24:30 -070010**
Dan Stozad3182402014-11-17 12:03:59 -080011** 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 Agopian076b1cc2009-04-10 14:24:30 -070015** limitations under the License.
16*/
17
Mathias Agopian5629eb12010-04-15 14:57:39 -070018#define LOG_TAG "GraphicBufferAllocator"
Mathias Agopiancf563192012-02-29 20:43:29 -080019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopian5629eb12010-04-15 14:57:39 -070020
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080021#include <ui/GraphicBufferAllocator.h>
22
Valerie Haufb4c9862019-07-22 15:24:37 -070023#include <limits.h>
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080024#include <stdio.h>
25
Chia-I Wu5bac7f32017-04-06 12:34:32 -070026#include <grallocusage/GrallocUsageConversion.h>
27
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070029#include <log/log.h>
Mathias Agopian4243e662009-04-15 18:34:24 -070030#include <utils/Singleton.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080031#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032
Marissa Walld380e2c2018-12-29 14:17:29 -080033#include <ui/Gralloc.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070034#include <ui/Gralloc2.h>
Marissa Wall925bf7f2018-12-29 14:27:11 -080035#include <ui/Gralloc3.h>
Marissa Wall87c8ba72019-06-20 14:20:52 -070036#include <ui/Gralloc4.h>
John Reck0ff95c92022-12-08 11:45:29 -050037#include <ui/Gralloc5.h>
Chia-I Wu9ba189d2016-09-22 17:13:08 +080038#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040namespace android {
41// ---------------------------------------------------------------------------
42
Yiwei Zhang5434a782018-12-05 18:06:32 -080043using base::StringAppendF;
44
Mathias Agopian3330b202009-10-05 17:07:12 -070045ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070046
Mathias Agopian3330b202009-10-05 17:07:12 -070047Mutex GraphicBufferAllocator::sLock;
Mathias Agopianb26af232009-10-05 18:19:57 -070048KeyedVector<buffer_handle_t,
49 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050
Marissa Wall925bf7f2018-12-29 14:27:11 -080051GraphicBufferAllocator::GraphicBufferAllocator() : mMapper(GraphicBufferMapper::getInstance()) {
John Reck0ff95c92022-12-08 11:45:29 -050052 switch (mMapper.getMapperVersion()) {
53 case GraphicBufferMapper::GRALLOC_5:
54 mAllocator = std::make_unique<const Gralloc5Allocator>(
55 reinterpret_cast<const Gralloc5Mapper&>(mMapper.getGrallocMapper()));
56 break;
57 case GraphicBufferMapper::GRALLOC_4:
58 mAllocator = std::make_unique<const Gralloc4Allocator>(
59 reinterpret_cast<const Gralloc4Mapper&>(mMapper.getGrallocMapper()));
60 break;
61 case GraphicBufferMapper::GRALLOC_3:
62 mAllocator = std::make_unique<const Gralloc3Allocator>(
63 reinterpret_cast<const Gralloc3Mapper&>(mMapper.getGrallocMapper()));
64 break;
65 case GraphicBufferMapper::GRALLOC_2:
66 mAllocator = std::make_unique<const Gralloc2Allocator>(
67 reinterpret_cast<const Gralloc2Mapper&>(mMapper.getGrallocMapper()));
68 break;
Marissa Wall87c8ba72019-06-20 14:20:52 -070069 }
John Reck0ff95c92022-12-08 11:45:29 -050070 LOG_ALWAYS_FATAL_IF(!mAllocator->isLoaded(),
71 "Failed to load matching allocator for mapper version %d",
72 mMapper.getMapperVersion());
Marissa Wall925bf7f2018-12-29 14:27:11 -080073}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074
Dan Stoza8deb4da2016-06-01 18:21:44 -070075GraphicBufferAllocator::~GraphicBufferAllocator() {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076
Tapani Pälli42b60ba2019-10-21 09:54:44 +030077uint64_t GraphicBufferAllocator::getTotalSize() const {
Dan Stoza45de5ba2019-04-25 14:12:09 -070078 Mutex::Autolock _l(sLock);
Tapani Pälli42b60ba2019-10-21 09:54:44 +030079 uint64_t total = 0;
Dan Stoza45de5ba2019-04-25 14:12:09 -070080 for (size_t i = 0; i < sAllocList.size(); ++i) {
81 total += sAllocList.valueAt(i).size;
82 }
83 return total;
84}
85
Marissa Wall22b2de12019-12-02 18:11:43 -080086void GraphicBufferAllocator::dump(std::string& result, bool less) const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 Mutex::Autolock _l(sLock);
88 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Tapani Pälli42b60ba2019-10-21 09:54:44 +030089 uint64_t total = 0;
Marissa Wall22b2de12019-12-02 18:11:43 -080090 result.append("GraphicBufferAllocator buffers:\n");
Marin Shalamanovbbf0b4d2020-09-03 18:26:21 +020091 const size_t count = list.size();
Rahul Banerjeec65685b2024-10-14 10:29:18 -070092 StringAppendF(&result, "%18s | %12s | %18s | %s | %8s | %10s | %s\n", "Handle", "Size",
Marin Shalamanovbbf0b4d2020-09-03 18:26:21 +020093 "W (Stride) x H", "Layers", "Format", "Usage", "Requestor");
94 for (size_t i = 0; i < count; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070095 const alloc_rec_t& rec(list.valueAt(i));
Marin Shalamanovbbf0b4d2020-09-03 18:26:21 +020096 std::string sizeStr = (rec.size)
97 ? base::StringPrintf("%7.2f KiB", static_cast<double>(rec.size) / 1024.0)
98 : "unknown";
Rahul Banerjeec65685b2024-10-14 10:29:18 -070099 StringAppendF(&result, "%18p | %12s | %4u (%4u) x %4u | %6u | %8X | 0x%8" PRIx64 " | %s\n",
Marin Shalamanovbbf0b4d2020-09-03 18:26:21 +0200100 list.keyAt(i), sizeStr.c_str(), rec.width, rec.stride, rec.height,
101 rec.layerCount, rec.format, rec.usage, rec.requestorName.c_str());
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 total += rec.size;
103 }
Marissa Wall22b2de12019-12-02 18:11:43 -0800104 StringAppendF(&result, "Total allocated by GraphicBufferAllocator (estimate): %.2f KB\n",
105 static_cast<double>(total) / 1024.0);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800106
Marissa Wall22b2de12019-12-02 18:11:43 -0800107 result.append(mAllocator->dumpDebugInfo(less));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108}
109
Marissa Wall22b2de12019-12-02 18:11:43 -0800110void GraphicBufferAllocator::dumpToSystemLog(bool less) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800111 std::string s;
Marissa Wall22b2de12019-12-02 18:11:43 -0800112 GraphicBufferAllocator::getInstance().dump(s, less);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800113 ALOGD("%s", s.c_str());
Mathias Agopian678bdd62010-12-03 17:33:09 -0800114}
115
John Reckd727e9c2023-12-08 11:30:37 -0500116auto GraphicBufferAllocator::allocate(const AllocationRequest& request) -> AllocationResult {
117 ATRACE_CALL();
118 if (!request.width || !request.height) {
119 return AllocationResult(BAD_VALUE);
120 }
121
122 const auto width = request.width;
123 const auto height = request.height;
124
125 const uint32_t bpp = bytesPerPixel(request.format);
126 if (std::numeric_limits<size_t>::max() / width / height < static_cast<size_t>(bpp)) {
127 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
128 "usage %" PRIx64 ": Requesting too large a buffer size",
129 request.width, request.height, request.layerCount, request.format, request.usage);
130 return AllocationResult(BAD_VALUE);
131 }
132
133 if (request.layerCount < 1) {
134 return AllocationResult(BAD_VALUE);
135 }
136
137 auto result = mAllocator->allocate(request);
138 if (result.status == UNKNOWN_TRANSACTION) {
139 if (!request.extras.empty()) {
140 ALOGE("Failed to allocate with additional options, allocator version mis-match? "
141 "gralloc version = %d",
142 (int)mMapper.getMapperVersion());
143 return result;
144 }
145 // If there's no additional options, fall back to previous allocate version
146 result.status = mAllocator->allocate(request.requestorName, request.width, request.height,
147 request.format, request.layerCount, request.usage,
148 &result.stride, &result.handle, request.importBuffer);
149 }
150
151 if (result.status != NO_ERROR) {
152 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
153 "usage %" PRIx64 ": %d",
154 request.width, request.height, request.layerCount, request.format, request.usage,
155 result.status);
156 return result;
157 }
158
159 if (!request.importBuffer) {
160 return result;
161 }
162 size_t bufSize;
163
164 // if stride has no meaning or is too large,
165 // approximate size with the input width instead
166 if ((result.stride) != 0 &&
167 std::numeric_limits<size_t>::max() / height / (result.stride) < static_cast<size_t>(bpp)) {
168 bufSize = static_cast<size_t>(width) * height * bpp;
169 } else {
170 bufSize = static_cast<size_t>((result.stride)) * height * bpp;
171 }
172
173 Mutex::Autolock _l(sLock);
174 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
175 alloc_rec_t rec;
176 rec.width = width;
177 rec.height = height;
178 rec.stride = result.stride;
179 rec.format = request.format;
180 rec.layerCount = request.layerCount;
181 rec.usage = request.usage;
182 rec.size = bufSize;
183 rec.requestorName = request.requestorName;
184 list.add(result.handle, rec);
185
186 return result;
187}
188
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800189status_t GraphicBufferAllocator::allocateHelper(uint32_t width, uint32_t height, PixelFormat format,
190 uint32_t layerCount, uint64_t usage,
191 buffer_handle_t* handle, uint32_t* stride,
192 std::string requestorName, bool importBuffer) {
Mathias Agopiancf563192012-02-29 20:43:29 -0800193 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800194
Mathias Agopian5629eb12010-04-15 14:57:39 -0700195 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
196 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800197 if (!width || !height)
198 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700199
Valerie Haufb4c9862019-07-22 15:24:37 -0700200 const uint32_t bpp = bytesPerPixel(format);
201 if (std::numeric_limits<size_t>::max() / width / height < static_cast<size_t>(bpp)) {
202 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
203 "usage %" PRIx64 ": Requesting too large a buffer size",
204 width, height, layerCount, format, usage);
205 return BAD_VALUE;
206 }
207
Craig Donner6ebc46a2016-10-21 15:23:44 -0700208 // Ensure that layerCount is valid.
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600209 if (layerCount < 1) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700210 layerCount = 1;
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600211 }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700212
Chia-I Wu12ca5272018-11-05 09:41:30 -0800213 // TODO(b/72323293, b/72703005): Remove these invalid bits from callers
214 usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13));
215
Marissa Wall22b2de12019-12-02 18:11:43 -0800216 status_t error = mAllocator->allocate(requestorName, width, height, format, layerCount, usage,
John Reckd727e9c2023-12-08 11:30:37 -0500217 stride, handle, importBuffer);
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800218 if (error != NO_ERROR) {
219 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
220 "usage %" PRIx64 ": %d",
221 width, height, layerCount, format, usage, error);
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600222 return error;
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800223 }
224
225 if (!importBuffer) {
226 return NO_ERROR;
227 }
Valerie Hau35493e72019-10-18 13:25:27 -0700228 size_t bufSize;
229
230 // if stride has no meaning or is too large,
231 // approximate size with the input width instead
Valerie Haue64b8772019-10-29 10:00:37 -0700232 if ((*stride) != 0 &&
233 std::numeric_limits<size_t>::max() / height / (*stride) < static_cast<size_t>(bpp)) {
Valerie Hau35493e72019-10-18 13:25:27 -0700234 bufSize = static_cast<size_t>(width) * height * bpp;
235 } else {
236 bufSize = static_cast<size_t>((*stride)) * height * bpp;
237 }
238
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800239 Mutex::Autolock _l(sLock);
240 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
241 alloc_rec_t rec;
242 rec.width = width;
243 rec.height = height;
244 rec.stride = *stride;
245 rec.format = format;
246 rec.layerCount = layerCount;
247 rec.usage = usage;
248 rec.size = bufSize;
249 rec.requestorName = std::move(requestorName);
250 list.add(*handle, rec);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800252 return NO_ERROR;
253}
254status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
255 uint32_t layerCount, uint64_t usage,
256 buffer_handle_t* handle, uint32_t* stride,
257 std::string requestorName) {
258 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
259 true);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260}
261
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800262status_t GraphicBufferAllocator::allocateRawHandle(uint32_t width, uint32_t height,
263 PixelFormat format, uint32_t layerCount,
264 uint64_t usage, buffer_handle_t* handle,
265 uint32_t* stride, std::string requestorName) {
266 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
267 false);
268}
269
270// DEPRECATED
Marissa Wall67514c22019-11-25 11:15:08 -0800271status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
272 uint32_t layerCount, uint64_t usage,
273 buffer_handle_t* handle, uint32_t* stride,
274 uint64_t /*graphicBufferId*/, std::string requestorName) {
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800275 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
276 true);
Marissa Wall67514c22019-11-25 11:15:08 -0800277}
278
Mathias Agopian3330b202009-10-05 17:07:12 -0700279status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280{
Mathias Agopiancf563192012-02-29 20:43:29 -0800281 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800282
Chia-I Wucb8405e2017-04-17 15:20:19 -0700283 // We allocated a buffer from the allocator and imported it into the
284 // mapper to get the handle. We just need to free the handle now.
285 mMapper.freeBuffer(handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286
Dan Stoza8deb4da2016-06-01 18:21:44 -0700287 Mutex::Autolock _l(sLock);
288 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
289 list.removeItem(handle);
290
291 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700292}
293
John Reckdb164ff2024-04-03 16:59:28 -0400294bool GraphicBufferAllocator::supportsAdditionalOptions() const {
295 return mAllocator->supportsAdditionalOptions();
296}
297
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298// ---------------------------------------------------------------------------
299}; // namespace android