blob: c0abec23e031d2fcca682bd5439e7269e36a5fbb [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();
92 StringAppendF(&result, "%10s | %11s | %18s | %s | %8s | %10s | %s\n", "Handle", "Size",
93 "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";
99 StringAppendF(&result, "%10p | %11s | %4u (%4u) x %4u | %6u | %8X | 0x%8" PRIx64 " | %s\n",
100 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
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800116status_t GraphicBufferAllocator::allocateHelper(uint32_t width, uint32_t height, PixelFormat format,
117 uint32_t layerCount, uint64_t usage,
118 buffer_handle_t* handle, uint32_t* stride,
119 std::string requestorName, bool importBuffer) {
Mathias Agopiancf563192012-02-29 20:43:29 -0800120 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800121
Mathias Agopian5629eb12010-04-15 14:57:39 -0700122 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
123 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800124 if (!width || !height)
125 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700126
Valerie Haufb4c9862019-07-22 15:24:37 -0700127 const uint32_t bpp = bytesPerPixel(format);
128 if (std::numeric_limits<size_t>::max() / width / height < static_cast<size_t>(bpp)) {
129 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
130 "usage %" PRIx64 ": Requesting too large a buffer size",
131 width, height, layerCount, format, usage);
132 return BAD_VALUE;
133 }
134
Craig Donner6ebc46a2016-10-21 15:23:44 -0700135 // Ensure that layerCount is valid.
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600136 if (layerCount < 1) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700137 layerCount = 1;
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600138 }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700139
Chia-I Wu12ca5272018-11-05 09:41:30 -0800140 // TODO(b/72323293, b/72703005): Remove these invalid bits from callers
141 usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13));
142
Marissa Wall22b2de12019-12-02 18:11:43 -0800143 status_t error = mAllocator->allocate(requestorName, width, height, format, layerCount, usage,
144 1, stride, handle, importBuffer);
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800145 if (error != NO_ERROR) {
146 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
147 "usage %" PRIx64 ": %d",
148 width, height, layerCount, format, usage, error);
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600149 return error;
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800150 }
151
152 if (!importBuffer) {
153 return NO_ERROR;
154 }
Valerie Hau35493e72019-10-18 13:25:27 -0700155 size_t bufSize;
156
157 // if stride has no meaning or is too large,
158 // approximate size with the input width instead
Valerie Haue64b8772019-10-29 10:00:37 -0700159 if ((*stride) != 0 &&
160 std::numeric_limits<size_t>::max() / height / (*stride) < static_cast<size_t>(bpp)) {
Valerie Hau35493e72019-10-18 13:25:27 -0700161 bufSize = static_cast<size_t>(width) * height * bpp;
162 } else {
163 bufSize = static_cast<size_t>((*stride)) * height * bpp;
164 }
165
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800166 Mutex::Autolock _l(sLock);
167 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
168 alloc_rec_t rec;
169 rec.width = width;
170 rec.height = height;
171 rec.stride = *stride;
172 rec.format = format;
173 rec.layerCount = layerCount;
174 rec.usage = usage;
175 rec.size = bufSize;
176 rec.requestorName = std::move(requestorName);
177 list.add(*handle, rec);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800179 return NO_ERROR;
180}
181status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
182 uint32_t layerCount, uint64_t usage,
183 buffer_handle_t* handle, uint32_t* stride,
184 std::string requestorName) {
185 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
186 true);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187}
188
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800189status_t GraphicBufferAllocator::allocateRawHandle(uint32_t width, uint32_t height,
190 PixelFormat format, uint32_t layerCount,
191 uint64_t usage, buffer_handle_t* handle,
192 uint32_t* stride, std::string requestorName) {
193 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
194 false);
195}
196
197// DEPRECATED
Marissa Wall67514c22019-11-25 11:15:08 -0800198status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
199 uint32_t layerCount, uint64_t usage,
200 buffer_handle_t* handle, uint32_t* stride,
201 uint64_t /*graphicBufferId*/, std::string requestorName) {
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800202 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
203 true);
Marissa Wall67514c22019-11-25 11:15:08 -0800204}
205
Mathias Agopian3330b202009-10-05 17:07:12 -0700206status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207{
Mathias Agopiancf563192012-02-29 20:43:29 -0800208 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800209
Chia-I Wucb8405e2017-04-17 15:20:19 -0700210 // We allocated a buffer from the allocator and imported it into the
211 // mapper to get the handle. We just need to free the handle now.
212 mMapper.freeBuffer(handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213
Dan Stoza8deb4da2016-06-01 18:21:44 -0700214 Mutex::Autolock _l(sLock);
215 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
216 list.removeItem(handle);
217
218 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700219}
220
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221// ---------------------------------------------------------------------------
222}; // namespace android