blob: b2b9680b26c24fa911eba61e7a2b82c056c4d764 [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
Dan Stoza45de5ba2019-04-25 14:12:09 -070072size_t GraphicBufferAllocator::getTotalSize() const {
73 Mutex::Autolock _l(sLock);
74 size_t total = 0;
75 for (size_t i = 0; i < sAllocList.size(); ++i) {
76 total += sAllocList.valueAt(i).size;
77 }
78 return total;
79}
80
Yiwei Zhang5434a782018-12-05 18:06:32 -080081void GraphicBufferAllocator::dump(std::string& result) const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070082 Mutex::Autolock _l(sLock);
83 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
84 size_t total = 0;
Yiwei Zhang5434a782018-12-05 18:06:32 -080085 result.append("Allocated buffers:\n");
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 const size_t c = list.size();
87 for (size_t i=0 ; i<c ; i++) {
88 const alloc_rec_t& rec(list.valueAt(i));
Mathias Agopiana947de82011-07-29 16:35:41 -070089 if (rec.size) {
Yiwei Zhang5434a782018-12-05 18:06:32 -080090 StringAppendF(&result,
91 "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64 " | %s\n",
Nick Desaulniersea6c7132019-10-15 19:14:39 -070092 list.keyAt(i), static_cast<double>(rec.size) / 1024.0, rec.width, rec.stride, rec.height,
Yiwei Zhang5434a782018-12-05 18:06:32 -080093 rec.layerCount, rec.format, rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070094 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -080095 StringAppendF(&result,
96 "%10p: unknown | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64 " | %s\n",
97 list.keyAt(i), rec.width, rec.stride, rec.height, rec.layerCount,
98 rec.format, rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070099 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100 total += rec.size;
101 }
Nick Desaulniersea6c7132019-10-15 19:14:39 -0700102 StringAppendF(&result, "Total allocated (estimate): %.2f KB\n", static_cast<double>(total) / 1024.0);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800103
Yiwei Zhang5434a782018-12-05 18:06:32 -0800104 result.append(mAllocator->dumpDebugInfo());
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700105}
106
Mathias Agopian678bdd62010-12-03 17:33:09 -0800107void GraphicBufferAllocator::dumpToSystemLog()
108{
Yiwei Zhang5434a782018-12-05 18:06:32 -0800109 std::string s;
Mathias Agopian678bdd62010-12-03 17:33:09 -0800110 GraphicBufferAllocator::getInstance().dump(s);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800111 ALOGD("%s", s.c_str());
Mathias Agopian678bdd62010-12-03 17:33:09 -0800112}
113
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800114status_t GraphicBufferAllocator::allocateHelper(uint32_t width, uint32_t height, PixelFormat format,
115 uint32_t layerCount, uint64_t usage,
116 buffer_handle_t* handle, uint32_t* stride,
117 std::string requestorName, bool importBuffer) {
Mathias Agopiancf563192012-02-29 20:43:29 -0800118 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800119
Mathias Agopian5629eb12010-04-15 14:57:39 -0700120 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
121 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800122 if (!width || !height)
123 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700124
Valerie Haufb4c9862019-07-22 15:24:37 -0700125 const uint32_t bpp = bytesPerPixel(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 width, height, layerCount, format, usage);
130 return BAD_VALUE;
131 }
132
Craig Donner6ebc46a2016-10-21 15:23:44 -0700133 // Ensure that layerCount is valid.
134 if (layerCount < 1)
135 layerCount = 1;
136
Chia-I Wu12ca5272018-11-05 09:41:30 -0800137 // TODO(b/72323293, b/72703005): Remove these invalid bits from callers
138 usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13));
139
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800140 status_t error = mAllocator->allocate(width, height, format, layerCount, usage, 1, stride,
141 handle, importBuffer);
142 if (error != NO_ERROR) {
143 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
144 "usage %" PRIx64 ": %d",
145 width, height, layerCount, format, usage, error);
146 return NO_MEMORY;
147 }
148
149 if (!importBuffer) {
150 return NO_ERROR;
151 }
Valerie Hau35493e72019-10-18 13:25:27 -0700152 size_t bufSize;
153
154 // if stride has no meaning or is too large,
155 // approximate size with the input width instead
Valerie Haue64b8772019-10-29 10:00:37 -0700156 if ((*stride) != 0 &&
157 std::numeric_limits<size_t>::max() / height / (*stride) < static_cast<size_t>(bpp)) {
Valerie Hau35493e72019-10-18 13:25:27 -0700158 bufSize = static_cast<size_t>(width) * height * bpp;
159 } else {
160 bufSize = static_cast<size_t>((*stride)) * height * bpp;
161 }
162
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800163 Mutex::Autolock _l(sLock);
164 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
165 alloc_rec_t rec;
166 rec.width = width;
167 rec.height = height;
168 rec.stride = *stride;
169 rec.format = format;
170 rec.layerCount = layerCount;
171 rec.usage = usage;
172 rec.size = bufSize;
173 rec.requestorName = std::move(requestorName);
174 list.add(*handle, rec);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800176 return NO_ERROR;
177}
178status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
179 uint32_t layerCount, uint64_t usage,
180 buffer_handle_t* handle, uint32_t* stride,
181 std::string requestorName) {
182 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
183 true);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184}
185
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800186status_t GraphicBufferAllocator::allocateRawHandle(uint32_t width, uint32_t height,
187 PixelFormat format, uint32_t layerCount,
188 uint64_t usage, buffer_handle_t* handle,
189 uint32_t* stride, std::string requestorName) {
190 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
191 false);
192}
193
194// DEPRECATED
Marissa Wall67514c22019-11-25 11:15:08 -0800195status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
196 uint32_t layerCount, uint64_t usage,
197 buffer_handle_t* handle, uint32_t* stride,
198 uint64_t /*graphicBufferId*/, std::string requestorName) {
Marissa Wallbfcf81f2019-11-27 10:36:29 -0800199 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
200 true);
Marissa Wall67514c22019-11-25 11:15:08 -0800201}
202
Mathias Agopian3330b202009-10-05 17:07:12 -0700203status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700204{
Mathias Agopiancf563192012-02-29 20:43:29 -0800205 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800206
Chia-I Wucb8405e2017-04-17 15:20:19 -0700207 // We allocated a buffer from the allocator and imported it into the
208 // mapper to get the handle. We just need to free the handle now.
209 mMapper.freeBuffer(handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210
Dan Stoza8deb4da2016-06-01 18:21:44 -0700211 Mutex::Autolock _l(sLock);
212 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
213 list.removeItem(handle);
214
215 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700216}
217
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700218// ---------------------------------------------------------------------------
219}; // namespace android