blob: 3f958ba68f52c4c4ffd97170402c49885ab862ba [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>
Chia-I Wu9ba189d2016-09-22 17:13:08 +080037#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039namespace android {
40// ---------------------------------------------------------------------------
41
Yiwei Zhang5434a782018-12-05 18:06:32 -080042using base::StringAppendF;
43
Mathias Agopian3330b202009-10-05 17:07:12 -070044ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070045
Mathias Agopian3330b202009-10-05 17:07:12 -070046Mutex GraphicBufferAllocator::sLock;
Mathias Agopianb26af232009-10-05 18:19:57 -070047KeyedVector<buffer_handle_t,
48 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049
Marissa Wall925bf7f2018-12-29 14:27:11 -080050GraphicBufferAllocator::GraphicBufferAllocator() : mMapper(GraphicBufferMapper::getInstance()) {
Marissa Wall87c8ba72019-06-20 14:20:52 -070051 mAllocator = std::make_unique<const Gralloc4Allocator>(
52 reinterpret_cast<const Gralloc4Mapper&>(mMapper.getGrallocMapper()));
53 if (mAllocator->isLoaded()) {
54 return;
55 }
Marissa Wall925bf7f2018-12-29 14:27:11 -080056 mAllocator = std::make_unique<const Gralloc3Allocator>(
57 reinterpret_cast<const Gralloc3Mapper&>(mMapper.getGrallocMapper()));
Marissa Wall87c8ba72019-06-20 14:20:52 -070058 if (mAllocator->isLoaded()) {
59 return;
60 }
61 mAllocator = std::make_unique<const Gralloc2Allocator>(
62 reinterpret_cast<const Gralloc2Mapper&>(mMapper.getGrallocMapper()));
63 if (mAllocator->isLoaded()) {
64 return;
Marissa Wall925bf7f2018-12-29 14:27:11 -080065 }
66
Marissa Wall87c8ba72019-06-20 14:20:52 -070067 LOG_ALWAYS_FATAL("gralloc-allocator is missing");
Marissa Wall925bf7f2018-12-29 14:27:11 -080068}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069
Dan Stoza8deb4da2016-06-01 18:21:44 -070070GraphicBufferAllocator::~GraphicBufferAllocator() {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071
Tapani Pälli42b60ba2019-10-21 09:54:44 +030072uint64_t GraphicBufferAllocator::getTotalSize() const {
Dan Stoza45de5ba2019-04-25 14:12:09 -070073 Mutex::Autolock _l(sLock);
Tapani Pälli42b60ba2019-10-21 09:54:44 +030074 uint64_t total = 0;
Dan Stoza45de5ba2019-04-25 14:12:09 -070075 for (size_t i = 0; i < sAllocList.size(); ++i) {
76 total += sAllocList.valueAt(i).size;
77 }
78 return total;
79}
80
Marissa Wall22b2de12019-12-02 18:11:43 -080081void GraphicBufferAllocator::dump(std::string& result, bool less) const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070082 Mutex::Autolock _l(sLock);
83 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Tapani Pälli42b60ba2019-10-21 09:54:44 +030084 uint64_t total = 0;
Marissa Wall22b2de12019-12-02 18:11:43 -080085 result.append("GraphicBufferAllocator buffers:\n");
Marin Shalamanovbbf0b4d2020-09-03 18:26:21 +020086 const size_t count = list.size();
87 StringAppendF(&result, "%10s | %11s | %18s | %s | %8s | %10s | %s\n", "Handle", "Size",
88 "W (Stride) x H", "Layers", "Format", "Usage", "Requestor");
89 for (size_t i = 0; i < count; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 const alloc_rec_t& rec(list.valueAt(i));
Marin Shalamanovbbf0b4d2020-09-03 18:26:21 +020091 std::string sizeStr = (rec.size)
92 ? base::StringPrintf("%7.2f KiB", static_cast<double>(rec.size) / 1024.0)
93 : "unknown";
94 StringAppendF(&result, "%10p | %11s | %4u (%4u) x %4u | %6u | %8X | 0x%8" PRIx64 " | %s\n",
95 list.keyAt(i), sizeStr.c_str(), rec.width, rec.stride, rec.height,
96 rec.layerCount, rec.format, rec.usage, rec.requestorName.c_str());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070097 total += rec.size;
98 }
Marissa Wall22b2de12019-12-02 18:11:43 -080099 StringAppendF(&result, "Total allocated by GraphicBufferAllocator (estimate): %.2f KB\n",
100 static_cast<double>(total) / 1024.0);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800101
Marissa Wall22b2de12019-12-02 18:11:43 -0800102 result.append(mAllocator->dumpDebugInfo(less));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103}
104
Marissa Wall22b2de12019-12-02 18:11:43 -0800105void GraphicBufferAllocator::dumpToSystemLog(bool less) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800106 std::string s;
Marissa Wall22b2de12019-12-02 18:11:43 -0800107 GraphicBufferAllocator::getInstance().dump(s, less);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800108 ALOGD("%s", s.c_str());
Mathias Agopian678bdd62010-12-03 17:33:09 -0800109}
110
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800111status_t GraphicBufferAllocator::allocateHelper(uint32_t width, uint32_t height, PixelFormat format,
112 uint32_t layerCount, uint64_t usage,
113 buffer_handle_t* handle, uint32_t* stride,
114 std::string requestorName, bool importBuffer) {
Mathias Agopiancf563192012-02-29 20:43:29 -0800115 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800116
Mathias Agopian5629eb12010-04-15 14:57:39 -0700117 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
118 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800119 if (!width || !height)
120 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700121
Valerie Haufb4c9862019-07-22 15:24:37 -0700122 const uint32_t bpp = bytesPerPixel(format);
123 if (std::numeric_limits<size_t>::max() / width / height < static_cast<size_t>(bpp)) {
124 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
125 "usage %" PRIx64 ": Requesting too large a buffer size",
126 width, height, layerCount, format, usage);
127 return BAD_VALUE;
128 }
129
Craig Donner6ebc46a2016-10-21 15:23:44 -0700130 // Ensure that layerCount is valid.
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600131 if (layerCount < 1) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700132 layerCount = 1;
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600133 }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700134
Chia-I Wu12ca5272018-11-05 09:41:30 -0800135 // TODO(b/72323293, b/72703005): Remove these invalid bits from callers
136 usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13));
137
Marissa Wall22b2de12019-12-02 18:11:43 -0800138 status_t error = mAllocator->allocate(requestorName, width, height, format, layerCount, usage,
139 1, stride, handle, importBuffer);
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800140 if (error != NO_ERROR) {
141 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
142 "usage %" PRIx64 ": %d",
143 width, height, layerCount, format, usage, error);
Tim Van Pattenaa8b7ac2021-06-02 16:01:38 -0600144 return error;
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800145 }
146
147 if (!importBuffer) {
148 return NO_ERROR;
149 }
Valerie Hau35493e72019-10-18 13:25:27 -0700150 size_t bufSize;
151
152 // if stride has no meaning or is too large,
153 // approximate size with the input width instead
Valerie Haue64b8772019-10-29 10:00:37 -0700154 if ((*stride) != 0 &&
155 std::numeric_limits<size_t>::max() / height / (*stride) < static_cast<size_t>(bpp)) {
Valerie Hau35493e72019-10-18 13:25:27 -0700156 bufSize = static_cast<size_t>(width) * height * bpp;
157 } else {
158 bufSize = static_cast<size_t>((*stride)) * height * bpp;
159 }
160
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800161 Mutex::Autolock _l(sLock);
162 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
163 alloc_rec_t rec;
164 rec.width = width;
165 rec.height = height;
166 rec.stride = *stride;
167 rec.format = format;
168 rec.layerCount = layerCount;
169 rec.usage = usage;
170 rec.size = bufSize;
171 rec.requestorName = std::move(requestorName);
172 list.add(*handle, rec);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800174 return NO_ERROR;
175}
176status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
177 uint32_t layerCount, uint64_t usage,
178 buffer_handle_t* handle, uint32_t* stride,
179 std::string requestorName) {
180 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
181 true);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182}
183
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800184status_t GraphicBufferAllocator::allocateRawHandle(uint32_t width, uint32_t height,
185 PixelFormat format, uint32_t layerCount,
186 uint64_t usage, buffer_handle_t* handle,
187 uint32_t* stride, std::string requestorName) {
188 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
189 false);
190}
191
192// DEPRECATED
Marissa Wall67514c22019-11-25 11:15:08 -0800193status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
194 uint32_t layerCount, uint64_t usage,
195 buffer_handle_t* handle, uint32_t* stride,
196 uint64_t /*graphicBufferId*/, std::string requestorName) {
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800197 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
198 true);
Marissa Wall67514c22019-11-25 11:15:08 -0800199}
200
Mathias Agopian3330b202009-10-05 17:07:12 -0700201status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202{
Mathias Agopiancf563192012-02-29 20:43:29 -0800203 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800204
Chia-I Wucb8405e2017-04-17 15:20:19 -0700205 // We allocated a buffer from the allocator and imported it into the
206 // mapper to get the handle. We just need to free the handle now.
207 mMapper.freeBuffer(handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208
Dan Stoza8deb4da2016-06-01 18:21:44 -0700209 Mutex::Autolock _l(sLock);
210 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
211 list.removeItem(handle);
212
213 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214}
215
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216// ---------------------------------------------------------------------------
217}; // namespace android