blob: d2585867cfd7fe655315d162c1e439376591445b [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 Agopian076b1cc2009-04-10 14:24:30 -070021#include <cutils/log.h>
Mathias Agopian4243e662009-04-15 18:34:24 -070022
23#include <utils/Singleton.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070024#include <utils/String8.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080025#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026
Mathias Agopian3330b202009-10-05 17:07:12 -070027#include <ui/GraphicBufferAllocator.h>
Dan Stoza8deb4da2016-06-01 18:21:44 -070028#include <ui/Gralloc1On0Adapter.h>
Chia-I Wu9ba189d2016-09-22 17:13:08 +080029#include <ui/GrallocAllocator.h>
30#include <ui/GrallocMapper.h>
31#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033namespace android {
34// ---------------------------------------------------------------------------
35
Mathias Agopian3330b202009-10-05 17:07:12 -070036ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070037
Mathias Agopian3330b202009-10-05 17:07:12 -070038Mutex GraphicBufferAllocator::sLock;
Mathias Agopianb26af232009-10-05 18:19:57 -070039KeyedVector<buffer_handle_t,
40 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041
Mathias Agopian3330b202009-10-05 17:07:12 -070042GraphicBufferAllocator::GraphicBufferAllocator()
Chia-I Wu9ba189d2016-09-22 17:13:08 +080043 : mAllocator(std::make_unique<Gralloc2::Allocator>()),
44 mMapper(GraphicBufferMapper::getInstance())
45{
46 if (!mAllocator->valid()) {
47 mLoader = std::make_unique<Gralloc1::Loader>();
48 mDevice = mLoader->getDevice();
49 }
50}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051
Dan Stoza8deb4da2016-06-01 18:21:44 -070052GraphicBufferAllocator::~GraphicBufferAllocator() {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053
Mathias Agopian3330b202009-10-05 17:07:12 -070054void GraphicBufferAllocator::dump(String8& result) const
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055{
56 Mutex::Autolock _l(sLock);
57 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
58 size_t total = 0;
Erik Gilling1d21a9c2010-12-01 16:38:01 -080059 const size_t SIZE = 4096;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060 char buffer[SIZE];
61 snprintf(buffer, SIZE, "Allocated buffers:\n");
62 result.append(buffer);
63 const size_t c = list.size();
64 for (size_t i=0 ; i<c ; i++) {
65 const alloc_rec_t& rec(list.valueAt(i));
Mathias Agopiana947de82011-07-29 16:35:41 -070066 if (rec.size) {
Craig Donner6ebc46a2016-10-21 15:23:44 -070067 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%08x | %s\n",
Mathias Agopiana947de82011-07-29 16:35:41 -070068 list.keyAt(i), rec.size/1024.0f,
Craig Donner6ebc46a2016-10-21 15:23:44 -070069 rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
70 rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070071 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -070072 snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %4u | %8X | 0x%08x | %s\n",
Mathias Agopiana947de82011-07-29 16:35:41 -070073 list.keyAt(i),
Craig Donner6ebc46a2016-10-21 15:23:44 -070074 rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
75 rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070076 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077 result.append(buffer);
78 total += rec.size;
79 }
Mathias Agopiana947de82011-07-29 16:35:41 -070080 snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 result.append(buffer);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080082
83 std::string deviceDump;
84 if (mAllocator->valid()) {
85 deviceDump = mAllocator->dumpDebugInfo();
86 } else {
87 deviceDump = mDevice->dump();
88 }
89
Dan Stoza8deb4da2016-06-01 18:21:44 -070090 result.append(deviceDump.c_str(), deviceDump.size());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091}
92
Mathias Agopian678bdd62010-12-03 17:33:09 -080093void GraphicBufferAllocator::dumpToSystemLog()
94{
95 String8 s;
96 GraphicBufferAllocator::getInstance().dump(s);
Steve Block9d453682011-12-20 16:23:08 +000097 ALOGD("%s", s.string());
Mathias Agopian678bdd62010-12-03 17:33:09 -080098}
99
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800100namespace {
101
102class HalBuffer {
103public:
104 HalBuffer(const Gralloc2::Allocator* allocator,
105 uint32_t width, uint32_t height,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700106 PixelFormat format, uint32_t layerCount, uint32_t usage)
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800107 : mAllocator(allocator), mBufferValid(false)
108 {
Chia-I Wub018bf02016-11-22 13:29:49 +0800109 Gralloc2::IAllocatorClient::BufferDescriptorInfo info = {};
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800110 info.width = width;
111 info.height = height;
112 info.format = static_cast<Gralloc2::PixelFormat>(format);
Craig Donner6ebc46a2016-10-21 15:23:44 -0700113 info.layerCount = layerCount;
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800114 info.producerUsageMask = usage;
115 info.consumerUsageMask = usage;
116
117 Gralloc2::BufferDescriptor descriptor;
118 auto error = mAllocator->createBufferDescriptor(info, descriptor);
119 if (error != Gralloc2::Error::NONE) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700120 ALOGE("Failed to create desc (%u x %u) layerCount %u format %d usage %u: %d",
121 width, height, layerCount, format, usage, error);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800122 return;
123 }
124
125 error = mAllocator->allocate(descriptor, mBuffer);
126 if (error == Gralloc2::Error::NOT_SHARED) {
127 error = Gralloc2::Error::NONE;
128 }
129
130 if (error != Gralloc2::Error::NONE) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700131 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d usage %u: %d",
132 width, height, layerCount, format, usage, error);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800133 mAllocator->destroyBufferDescriptor(descriptor);
134 return;
135 }
136
137 error = mAllocator->exportHandle(descriptor, mBuffer, mHandle);
138 if (error != Gralloc2::Error::NONE) {
139 ALOGE("Failed to export handle");
140 mAllocator->free(mBuffer);
141 mAllocator->destroyBufferDescriptor(descriptor);
142 return;
143 }
144
145 mAllocator->destroyBufferDescriptor(descriptor);
146
147 mBufferValid = true;
148 }
149
150 ~HalBuffer()
151 {
152 if (mBufferValid) {
153 if (mHandle) {
154 native_handle_close(mHandle);
155 native_handle_delete(mHandle);
156 }
157
158 mAllocator->free(mBuffer);
159 }
160 }
161
162 bool exportHandle(GraphicBufferMapper& mapper,
163 buffer_handle_t* handle, uint32_t* stride)
164 {
165 if (!mBufferValid) {
166 return false;
167 }
168
169 if (mapper.registerBuffer(mHandle)) {
170 return false;
171 }
172
173 *handle = mHandle;
174
175 auto error = mapper.getGrallocMapper().getStride(mHandle, *stride);
176 if (error != Gralloc2::Error::NONE) {
177 ALOGW("Failed to get stride from buffer: %d", error);
178 *stride = 0;
179 }
180
181 mHandle = nullptr;
182 mAllocator->free(mBuffer);
183 mBufferValid = false;
184
185 return true;
186 }
187
188private:
189 const Gralloc2::Allocator* mAllocator;
190
191 bool mBufferValid;
192 Gralloc2::Buffer mBuffer;
193 native_handle_t* mHandle;
194};
195
196} // namespace
197
Dan Stoza8deb4da2016-06-01 18:21:44 -0700198status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700199 PixelFormat format, uint32_t layerCount, uint32_t usage,
200 buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
201 std::string requestorName)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202{
Mathias Agopiancf563192012-02-29 20:43:29 -0800203 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800204
Mathias Agopian5629eb12010-04-15 14:57:39 -0700205 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
206 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800207 if (!width || !height)
208 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700209
Craig Donner6ebc46a2016-10-21 15:23:44 -0700210 // Ensure that layerCount is valid.
211 if (layerCount < 1)
212 layerCount = 1;
213
Dan Stoza24fa67f2015-05-29 12:48:04 -0700214 // Filter out any usage bits that should not be passed to the gralloc module
215 usage &= GRALLOC_USAGE_ALLOC_MASK;
216
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800217 gralloc1_error_t error;
218 if (mAllocator->valid()) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700219 HalBuffer buffer(mAllocator.get(), width, height, format, layerCount,
220 usage);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800221 if (!buffer.exportHandle(mMapper, handle, stride)) {
222 return NO_MEMORY;
223 }
224 error = GRALLOC1_ERROR_NONE;
225 } else {
226 auto descriptor = mDevice->createDescriptor();
227 error = descriptor->setDimensions(width, height);
228 if (error != GRALLOC1_ERROR_NONE) {
229 ALOGE("Failed to set dimensions to (%u, %u): %d",
230 width, height, error);
231 return BAD_VALUE;
232 }
233 error = descriptor->setFormat(
234 static_cast<android_pixel_format_t>(format));
235 if (error != GRALLOC1_ERROR_NONE) {
236 ALOGE("Failed to set format to %d: %d", format, error);
237 return BAD_VALUE;
238 }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700239 if (mDevice->hasCapability(GRALLOC1_CAPABILITY_LAYERED_BUFFERS)) {
240 error = descriptor->setLayerCount(layerCount);
241 if (error != GRALLOC1_ERROR_NONE) {
242 ALOGE("Failed to set layer count to %u: %d", layerCount, error);
243 return BAD_VALUE;
244 }
245 } else if (layerCount > 1) {
246 ALOGE("Failed to set layer count to %u: capability unsupported",
247 layerCount);
248 return BAD_VALUE;
249 }
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800250 error = descriptor->setProducerUsage(
251 static_cast<gralloc1_producer_usage_t>(usage));
252 if (error != GRALLOC1_ERROR_NONE) {
253 ALOGE("Failed to set producer usage to %u: %d", usage, error);
254 return BAD_VALUE;
255 }
256 error = descriptor->setConsumerUsage(
257 static_cast<gralloc1_consumer_usage_t>(usage));
258 if (error != GRALLOC1_ERROR_NONE) {
259 ALOGE("Failed to set consumer usage to %u: %d", usage, error);
260 return BAD_VALUE;
261 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700262
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800263 error = mDevice->allocate(descriptor, graphicBufferId, handle);
264 if (error != GRALLOC1_ERROR_NONE) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700265 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d usage %u: %d",
266 width, height, layerCount, format, usage, error);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800267 return NO_MEMORY;
268 }
Dan Stozad3182402014-11-17 12:03:59 -0800269
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800270 error = mDevice->getStride(*handle, stride);
271 if (error != GRALLOC1_ERROR_NONE) {
272 ALOGW("Failed to get stride from buffer: %d", error);
273 }
Dan Stoza8deb4da2016-06-01 18:21:44 -0700274 }
275
276 if (error == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277 Mutex::Autolock _l(sLock);
278 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Dan Stozad3182402014-11-17 12:03:59 -0800279 uint32_t bpp = bytesPerPixel(format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280 alloc_rec_t rec;
Dan Stozad3182402014-11-17 12:03:59 -0800281 rec.width = width;
282 rec.height = height;
283 rec.stride = *stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700284 rec.format = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700285 rec.layerCount = layerCount;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700286 rec.usage = usage;
Dan Stozad3182402014-11-17 12:03:59 -0800287 rec.size = static_cast<size_t>(height * (*stride) * bpp);
Dan Stoza024e9312016-08-24 12:17:29 -0700288 rec.requestorName = std::move(requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700289 list.add(*handle, rec);
290 }
291
Dan Stoza8deb4da2016-06-01 18:21:44 -0700292 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700293}
294
Mathias Agopian3330b202009-10-05 17:07:12 -0700295status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700296{
Mathias Agopiancf563192012-02-29 20:43:29 -0800297 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800298
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800299 gralloc1_error_t error;
300 if (mAllocator->valid()) {
301 error = static_cast<gralloc1_error_t>(
302 mMapper.unregisterBuffer(handle));
303 } else {
304 error = mDevice->release(handle);
305 }
306
Dan Stoza8deb4da2016-06-01 18:21:44 -0700307 if (error != GRALLOC1_ERROR_NONE) {
308 ALOGE("Failed to free buffer: %d", error);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700309 }
310
Dan Stoza8deb4da2016-06-01 18:21:44 -0700311 Mutex::Autolock _l(sLock);
312 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
313 list.removeItem(handle);
314
315 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700316}
317
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700318// ---------------------------------------------------------------------------
319}; // namespace android