blob: 1f6c53797679d826e3861d511c42b9ac8b88d9bd [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
Mark Salyzyn7823e122016-09-29 08:08:05 -070027#include <log/log.h>
Mathias Agopian4243e662009-04-15 18:34:24 -070028#include <utils/Singleton.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <utils/String8.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080030#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031
Chia-I Wu5bac7f32017-04-06 12:34:32 -070032#include <ui/Gralloc2.h>
Chia-I Wu9ba189d2016-09-22 17:13:08 +080033#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035namespace android {
36// ---------------------------------------------------------------------------
37
Mathias Agopian3330b202009-10-05 17:07:12 -070038ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070039
Mathias Agopian3330b202009-10-05 17:07:12 -070040Mutex GraphicBufferAllocator::sLock;
Mathias Agopianb26af232009-10-05 18:19:57 -070041KeyedVector<buffer_handle_t,
42 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070043
Mathias Agopian3330b202009-10-05 17:07:12 -070044GraphicBufferAllocator::GraphicBufferAllocator()
Chia-I Wu5bac7f32017-04-06 12:34:32 -070045 : mMapper(GraphicBufferMapper::getInstance()),
46 mAllocator(std::make_unique<Gralloc2::Allocator>(
47 mMapper.getGrallocMapper()))
Chia-I Wu9ba189d2016-09-22 17:13:08 +080048{
49 if (!mAllocator->valid()) {
50 mLoader = std::make_unique<Gralloc1::Loader>();
51 mDevice = mLoader->getDevice();
52 }
53}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054
Dan Stoza8deb4da2016-06-01 18:21:44 -070055GraphicBufferAllocator::~GraphicBufferAllocator() {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056
Mathias Agopian3330b202009-10-05 17:07:12 -070057void GraphicBufferAllocator::dump(String8& result) const
Mathias Agopian076b1cc2009-04-10 14:24:30 -070058{
59 Mutex::Autolock _l(sLock);
60 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
61 size_t total = 0;
Erik Gilling1d21a9c2010-12-01 16:38:01 -080062 const size_t SIZE = 4096;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070063 char buffer[SIZE];
64 snprintf(buffer, SIZE, "Allocated buffers:\n");
65 result.append(buffer);
66 const size_t c = list.size();
67 for (size_t i=0 ; i<c ; i++) {
68 const alloc_rec_t& rec(list.valueAt(i));
Mathias Agopiana947de82011-07-29 16:35:41 -070069 if (rec.size) {
Craig Donnere96a3252017-02-02 12:13:34 -080070 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
71 ", 0x%" PRIx64 " | %s\n",
George Burgess IV1093cf32017-01-25 17:09:18 -080072 list.keyAt(i), rec.size/1024.0,
Craig Donner6ebc46a2016-10-21 15:23:44 -070073 rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
Craig Donnere96a3252017-02-02 12:13:34 -080074 rec.producerUsage, rec.consumerUsage,
75 rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070076 } else {
Craig Donnere96a3252017-02-02 12:13:34 -080077 snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
78 ", 0x%" PRIx64 " | %s\n",
Mathias Agopiana947de82011-07-29 16:35:41 -070079 list.keyAt(i),
Craig Donner6ebc46a2016-10-21 15:23:44 -070080 rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
Craig Donnere96a3252017-02-02 12:13:34 -080081 rec.producerUsage, rec.consumerUsage,
82 rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070083 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 result.append(buffer);
85 total += rec.size;
86 }
George Burgess IV1093cf32017-01-25 17:09:18 -080087 snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088 result.append(buffer);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080089
90 std::string deviceDump;
91 if (mAllocator->valid()) {
92 deviceDump = mAllocator->dumpDebugInfo();
93 } else {
94 deviceDump = mDevice->dump();
95 }
96
Dan Stoza8deb4da2016-06-01 18:21:44 -070097 result.append(deviceDump.c_str(), deviceDump.size());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098}
99
Mathias Agopian678bdd62010-12-03 17:33:09 -0800100void GraphicBufferAllocator::dumpToSystemLog()
101{
102 String8 s;
103 GraphicBufferAllocator::getInstance().dump(s);
Steve Block9d453682011-12-20 16:23:08 +0000104 ALOGD("%s", s.string());
Mathias Agopian678bdd62010-12-03 17:33:09 -0800105}
106
Dan Stoza8deb4da2016-06-01 18:21:44 -0700107status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
Craig Donnere96a3252017-02-02 12:13:34 -0800108 PixelFormat format, uint32_t layerCount, uint64_t producerUsage,
109 uint64_t consumerUsage, buffer_handle_t* handle, uint32_t* stride,
110 uint64_t graphicBufferId, std::string requestorName)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111{
Mathias Agopiancf563192012-02-29 20:43:29 -0800112 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800113
Mathias Agopian5629eb12010-04-15 14:57:39 -0700114 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
115 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800116 if (!width || !height)
117 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700118
Craig Donner6ebc46a2016-10-21 15:23:44 -0700119 // Ensure that layerCount is valid.
120 if (layerCount < 1)
121 layerCount = 1;
122
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800123 gralloc1_error_t error;
124 if (mAllocator->valid()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700125 Gralloc2::IMapper::BufferDescriptorInfo info = {};
126 info.width = width;
127 info.height = height;
128 info.layerCount = layerCount;
129 info.format = static_cast<Gralloc2::PixelFormat>(format);
130 info.usage = static_cast<uint64_t>(android_convertGralloc1To0Usage(
131 producerUsage, consumerUsage));
132 error = static_cast<gralloc1_error_t>(mAllocator->allocate(info,
133 stride, handle));
134 if (error != GRALLOC1_ERROR_NONE) {
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800135 return NO_MEMORY;
136 }
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800137 } else {
138 auto descriptor = mDevice->createDescriptor();
139 error = descriptor->setDimensions(width, height);
140 if (error != GRALLOC1_ERROR_NONE) {
141 ALOGE("Failed to set dimensions to (%u, %u): %d",
142 width, height, error);
143 return BAD_VALUE;
144 }
145 error = descriptor->setFormat(
146 static_cast<android_pixel_format_t>(format));
147 if (error != GRALLOC1_ERROR_NONE) {
148 ALOGE("Failed to set format to %d: %d", format, error);
149 return BAD_VALUE;
150 }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700151 if (mDevice->hasCapability(GRALLOC1_CAPABILITY_LAYERED_BUFFERS)) {
152 error = descriptor->setLayerCount(layerCount);
153 if (error != GRALLOC1_ERROR_NONE) {
154 ALOGE("Failed to set layer count to %u: %d", layerCount, error);
155 return BAD_VALUE;
156 }
157 } else if (layerCount > 1) {
158 ALOGE("Failed to set layer count to %u: capability unsupported",
159 layerCount);
160 return BAD_VALUE;
161 }
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800162 error = descriptor->setProducerUsage(
Craig Donnere96a3252017-02-02 12:13:34 -0800163 static_cast<gralloc1_producer_usage_t>(producerUsage));
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800164 if (error != GRALLOC1_ERROR_NONE) {
Craig Donnere96a3252017-02-02 12:13:34 -0800165 ALOGE("Failed to set producer usage to %" PRIx64 ": %d",
166 producerUsage, error);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800167 return BAD_VALUE;
168 }
169 error = descriptor->setConsumerUsage(
Craig Donnere96a3252017-02-02 12:13:34 -0800170 static_cast<gralloc1_consumer_usage_t>(consumerUsage));
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800171 if (error != GRALLOC1_ERROR_NONE) {
Craig Donnere96a3252017-02-02 12:13:34 -0800172 ALOGE("Failed to set consumer usage to %" PRIx64 ": %d",
173 consumerUsage, error);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800174 return BAD_VALUE;
175 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700176
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800177 error = mDevice->allocate(descriptor, graphicBufferId, handle);
178 if (error != GRALLOC1_ERROR_NONE) {
Craig Donnere96a3252017-02-02 12:13:34 -0800179 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
180 "producerUsage %" PRIx64 " consumerUsage %" PRIx64 ": %d",
181 width, height, layerCount, format, producerUsage,
182 consumerUsage, error);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800183 return NO_MEMORY;
184 }
Dan Stozad3182402014-11-17 12:03:59 -0800185
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800186 error = mDevice->getStride(*handle, stride);
187 if (error != GRALLOC1_ERROR_NONE) {
188 ALOGW("Failed to get stride from buffer: %d", error);
189 }
Dan Stoza8deb4da2016-06-01 18:21:44 -0700190 }
191
192 if (error == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193 Mutex::Autolock _l(sLock);
194 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Dan Stozad3182402014-11-17 12:03:59 -0800195 uint32_t bpp = bytesPerPixel(format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 alloc_rec_t rec;
Dan Stozad3182402014-11-17 12:03:59 -0800197 rec.width = width;
198 rec.height = height;
199 rec.stride = *stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 rec.format = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700201 rec.layerCount = layerCount;
Craig Donnere96a3252017-02-02 12:13:34 -0800202 rec.producerUsage = producerUsage;
203 rec.consumerUsage = consumerUsage;
Dan Stozad3182402014-11-17 12:03:59 -0800204 rec.size = static_cast<size_t>(height * (*stride) * bpp);
Dan Stoza024e9312016-08-24 12:17:29 -0700205 rec.requestorName = std::move(requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 list.add(*handle, rec);
207 }
208
Dan Stoza8deb4da2016-06-01 18:21:44 -0700209 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700210}
211
Mathias Agopian3330b202009-10-05 17:07:12 -0700212status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213{
Mathias Agopiancf563192012-02-29 20:43:29 -0800214 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800215
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800216 gralloc1_error_t error;
217 if (mAllocator->valid()) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700218 error = static_cast<gralloc1_error_t>(mMapper.freeBuffer(handle));
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800219 } else {
220 error = mDevice->release(handle);
221 }
222
Dan Stoza8deb4da2016-06-01 18:21:44 -0700223 if (error != GRALLOC1_ERROR_NONE) {
224 ALOGE("Failed to free buffer: %d", error);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700225 }
226
Dan Stoza8deb4da2016-06-01 18:21:44 -0700227 Mutex::Autolock _l(sLock);
228 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
229 list.removeItem(handle);
230
231 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232}
233
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234// ---------------------------------------------------------------------------
235}; // namespace android