blob: 1c597f2a7390584ada0a0e7c5912b7cd7e767504 [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
23#include <stdio.h>
24
Chia-I Wu5bac7f32017-04-06 12:34:32 -070025#include <grallocusage/GrallocUsageConversion.h>
26
Yiwei Zhang5434a782018-12-05 18:06:32 -080027#include <android-base/stringprintf.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <log/log.h>
Mathias Agopian4243e662009-04-15 18:34:24 -070029#include <utils/Singleton.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080030#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031
Marissa Walld380e2c2018-12-29 14:17:29 -080032#include <ui/Gralloc.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070033#include <ui/Gralloc2.h>
Marissa Wall925bf7f2018-12-29 14:27:11 -080034#include <ui/Gralloc3.h>
Chia-I Wu9ba189d2016-09-22 17:13:08 +080035#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037namespace android {
38// ---------------------------------------------------------------------------
39
Yiwei Zhang5434a782018-12-05 18:06:32 -080040using base::StringAppendF;
41
Mathias Agopian3330b202009-10-05 17:07:12 -070042ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070043
Mathias Agopian3330b202009-10-05 17:07:12 -070044Mutex GraphicBufferAllocator::sLock;
Mathias Agopianb26af232009-10-05 18:19:57 -070045KeyedVector<buffer_handle_t,
46 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047
Marissa Wall925bf7f2018-12-29 14:27:11 -080048GraphicBufferAllocator::GraphicBufferAllocator() : mMapper(GraphicBufferMapper::getInstance()) {
49 mAllocator = std::make_unique<const Gralloc3Allocator>(
50 reinterpret_cast<const Gralloc3Mapper&>(mMapper.getGrallocMapper()));
Valerie Hau250c6542019-01-31 14:23:43 -080051 if (!mAllocator->isLoaded()) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080052 mAllocator = std::make_unique<const Gralloc2Allocator>(
53 reinterpret_cast<const Gralloc2Mapper&>(mMapper.getGrallocMapper()));
54 }
55
Valerie Hau250c6542019-01-31 14:23:43 -080056 if (!mAllocator->isLoaded()) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080057 LOG_ALWAYS_FATAL("gralloc-allocator is missing");
58 }
59}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060
Dan Stoza8deb4da2016-06-01 18:21:44 -070061GraphicBufferAllocator::~GraphicBufferAllocator() {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070062
Tapani Pälli42b60ba2019-10-21 09:54:44 +030063uint64_t GraphicBufferAllocator::getTotalSize() const {
Dan Stoza45de5ba2019-04-25 14:12:09 -070064 Mutex::Autolock _l(sLock);
Tapani Pälli42b60ba2019-10-21 09:54:44 +030065 uint64_t total = 0;
Dan Stoza45de5ba2019-04-25 14:12:09 -070066 for (size_t i = 0; i < sAllocList.size(); ++i) {
67 total += sAllocList.valueAt(i).size;
68 }
69 return total;
70}
71
Yiwei Zhang5434a782018-12-05 18:06:32 -080072void GraphicBufferAllocator::dump(std::string& result) const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 Mutex::Autolock _l(sLock);
74 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Tapani Pälli42b60ba2019-10-21 09:54:44 +030075 uint64_t total = 0;
Yiwei Zhang5434a782018-12-05 18:06:32 -080076 result.append("Allocated buffers:\n");
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077 const size_t c = list.size();
78 for (size_t i=0 ; i<c ; i++) {
79 const alloc_rec_t& rec(list.valueAt(i));
Mathias Agopiana947de82011-07-29 16:35:41 -070080 if (rec.size) {
Yiwei Zhang5434a782018-12-05 18:06:32 -080081 StringAppendF(&result,
82 "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64 " | %s\n",
Nick Desaulniersea6c7132019-10-15 19:14:39 -070083 list.keyAt(i), static_cast<double>(rec.size) / 1024.0, rec.width, rec.stride, rec.height,
Yiwei Zhang5434a782018-12-05 18:06:32 -080084 rec.layerCount, rec.format, rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070085 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -080086 StringAppendF(&result,
87 "%10p: unknown | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64 " | %s\n",
88 list.keyAt(i), rec.width, rec.stride, rec.height, rec.layerCount,
89 rec.format, rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070090 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 total += rec.size;
92 }
Nick Desaulniersea6c7132019-10-15 19:14:39 -070093 StringAppendF(&result, "Total allocated (estimate): %.2f KB\n", static_cast<double>(total) / 1024.0);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080094
Yiwei Zhang5434a782018-12-05 18:06:32 -080095 result.append(mAllocator->dumpDebugInfo());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096}
97
Mathias Agopian678bdd62010-12-03 17:33:09 -080098void GraphicBufferAllocator::dumpToSystemLog()
99{
Yiwei Zhang5434a782018-12-05 18:06:32 -0800100 std::string s;
Mathias Agopian678bdd62010-12-03 17:33:09 -0800101 GraphicBufferAllocator::getInstance().dump(s);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800102 ALOGD("%s", s.c_str());
Mathias Agopian678bdd62010-12-03 17:33:09 -0800103}
104
Dan Stoza8deb4da2016-06-01 18:21:44 -0700105status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
Chris Forbes82c04982017-04-19 14:29:54 -0700106 PixelFormat format, uint32_t layerCount, uint64_t usage,
107 buffer_handle_t* handle, uint32_t* stride,
Chia-I Wucb8405e2017-04-17 15:20:19 -0700108 uint64_t /*graphicBufferId*/, std::string requestorName)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109{
Mathias Agopiancf563192012-02-29 20:43:29 -0800110 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800111
Mathias Agopian5629eb12010-04-15 14:57:39 -0700112 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
113 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800114 if (!width || !height)
115 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700116
Craig Donner6ebc46a2016-10-21 15:23:44 -0700117 // Ensure that layerCount is valid.
118 if (layerCount < 1)
119 layerCount = 1;
120
Chia-I Wu12ca5272018-11-05 09:41:30 -0800121 // TODO(b/72323293, b/72703005): Remove these invalid bits from callers
122 usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13));
123
Marissa Wall1e779252018-12-29 12:01:57 -0800124 status_t error =
125 mAllocator->allocate(width, height, format, layerCount, usage, 1, stride, handle);
126 if (error == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700127 Mutex::Autolock _l(sLock);
128 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Dan Stozad3182402014-11-17 12:03:59 -0800129 uint32_t bpp = bytesPerPixel(format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 alloc_rec_t rec;
Dan Stozad3182402014-11-17 12:03:59 -0800131 rec.width = width;
132 rec.height = height;
133 rec.stride = *stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 rec.format = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700135 rec.layerCount = layerCount;
Chris Forbes82c04982017-04-19 14:29:54 -0700136 rec.usage = usage;
Dan Stozad3182402014-11-17 12:03:59 -0800137 rec.size = static_cast<size_t>(height * (*stride) * bpp);
Dan Stoza024e9312016-08-24 12:17:29 -0700138 rec.requestorName = std::move(requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 list.add(*handle, rec);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140
Chia-I Wucb8405e2017-04-17 15:20:19 -0700141 return NO_ERROR;
142 } else {
Chia-I Wu0ffa62b2017-04-19 22:20:55 -0700143 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
Chris Forbes82c04982017-04-19 14:29:54 -0700144 "usage %" PRIx64 ": %d",
145 width, height, layerCount, format, usage,
146 error);
Chia-I Wucb8405e2017-04-17 15:20:19 -0700147 return NO_MEMORY;
148 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149}
150
Mathias Agopian3330b202009-10-05 17:07:12 -0700151status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700152{
Mathias Agopiancf563192012-02-29 20:43:29 -0800153 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800154
Chia-I Wucb8405e2017-04-17 15:20:19 -0700155 // We allocated a buffer from the allocator and imported it into the
156 // mapper to get the handle. We just need to free the handle now.
157 mMapper.freeBuffer(handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700158
Dan Stoza8deb4da2016-06-01 18:21:44 -0700159 Mutex::Autolock _l(sLock);
160 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
161 list.removeItem(handle);
162
163 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700164}
165
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700166// ---------------------------------------------------------------------------
167}; // namespace android