blob: eaba1ed1aaf2c0741fc5c0439c1d190f77881a65 [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{
Chia-I Wu9ba189d2016-09-22 17:13:08 +080049}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050
Dan Stoza8deb4da2016-06-01 18:21:44 -070051GraphicBufferAllocator::~GraphicBufferAllocator() {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052
Mathias Agopian3330b202009-10-05 17:07:12 -070053void GraphicBufferAllocator::dump(String8& result) const
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054{
55 Mutex::Autolock _l(sLock);
56 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
57 size_t total = 0;
Erik Gilling1d21a9c2010-12-01 16:38:01 -080058 const size_t SIZE = 4096;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070059 char buffer[SIZE];
60 snprintf(buffer, SIZE, "Allocated buffers:\n");
61 result.append(buffer);
62 const size_t c = list.size();
63 for (size_t i=0 ; i<c ; i++) {
64 const alloc_rec_t& rec(list.valueAt(i));
Mathias Agopiana947de82011-07-29 16:35:41 -070065 if (rec.size) {
Craig Donnere96a3252017-02-02 12:13:34 -080066 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
Chris Forbes82c04982017-04-19 14:29:54 -070067 " | %s\n",
George Burgess IV1093cf32017-01-25 17:09:18 -080068 list.keyAt(i), rec.size/1024.0,
Craig Donner6ebc46a2016-10-21 15:23:44 -070069 rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
Chris Forbes82c04982017-04-19 14:29:54 -070070 rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070071 } else {
Craig Donnere96a3252017-02-02 12:13:34 -080072 snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
Chris Forbes82c04982017-04-19 14:29:54 -070073 " | %s\n",
Mathias Agopiana947de82011-07-29 16:35:41 -070074 list.keyAt(i),
Craig Donner6ebc46a2016-10-21 15:23:44 -070075 rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
Chris Forbes82c04982017-04-19 14:29:54 -070076 rec.usage, rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070077 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 result.append(buffer);
79 total += rec.size;
80 }
George Burgess IV1093cf32017-01-25 17:09:18 -080081 snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070082 result.append(buffer);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080083
Chia-I Wucb8405e2017-04-17 15:20:19 -070084 std::string deviceDump = mAllocator->dumpDebugInfo();
Dan Stoza8deb4da2016-06-01 18:21:44 -070085 result.append(deviceDump.c_str(), deviceDump.size());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086}
87
Mathias Agopian678bdd62010-12-03 17:33:09 -080088void GraphicBufferAllocator::dumpToSystemLog()
89{
90 String8 s;
91 GraphicBufferAllocator::getInstance().dump(s);
Steve Block9d453682011-12-20 16:23:08 +000092 ALOGD("%s", s.string());
Mathias Agopian678bdd62010-12-03 17:33:09 -080093}
94
Dan Stoza8deb4da2016-06-01 18:21:44 -070095status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
Chris Forbes82c04982017-04-19 14:29:54 -070096 PixelFormat format, uint32_t layerCount, uint64_t usage,
97 buffer_handle_t* handle, uint32_t* stride,
Chia-I Wucb8405e2017-04-17 15:20:19 -070098 uint64_t /*graphicBufferId*/, std::string requestorName)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099{
Mathias Agopiancf563192012-02-29 20:43:29 -0800100 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -0800101
Mathias Agopian5629eb12010-04-15 14:57:39 -0700102 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
103 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800104 if (!width || !height)
105 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700106
Craig Donner6ebc46a2016-10-21 15:23:44 -0700107 // Ensure that layerCount is valid.
108 if (layerCount < 1)
109 layerCount = 1;
110
Chia-I Wucb8405e2017-04-17 15:20:19 -0700111 Gralloc2::IMapper::BufferDescriptorInfo info = {};
112 info.width = width;
113 info.height = height;
114 info.layerCount = layerCount;
115 info.format = static_cast<Gralloc2::PixelFormat>(format);
Chris Forbes82c04982017-04-19 14:29:54 -0700116 info.usage = usage;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700117
Chia-I Wucb8405e2017-04-17 15:20:19 -0700118 Gralloc2::Error error = mAllocator->allocate(info, stride, handle);
119 if (error == Gralloc2::Error::NONE) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 Mutex::Autolock _l(sLock);
121 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Dan Stozad3182402014-11-17 12:03:59 -0800122 uint32_t bpp = bytesPerPixel(format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700123 alloc_rec_t rec;
Dan Stozad3182402014-11-17 12:03:59 -0800124 rec.width = width;
125 rec.height = height;
126 rec.stride = *stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700127 rec.format = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700128 rec.layerCount = layerCount;
Chris Forbes82c04982017-04-19 14:29:54 -0700129 rec.usage = usage;
Dan Stozad3182402014-11-17 12:03:59 -0800130 rec.size = static_cast<size_t>(height * (*stride) * bpp);
Dan Stoza024e9312016-08-24 12:17:29 -0700131 rec.requestorName = std::move(requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 list.add(*handle, rec);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133
Chia-I Wucb8405e2017-04-17 15:20:19 -0700134 return NO_ERROR;
135 } else {
Chia-I Wu0ffa62b2017-04-19 22:20:55 -0700136 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
Chris Forbes82c04982017-04-19 14:29:54 -0700137 "usage %" PRIx64 ": %d",
138 width, height, layerCount, format, usage,
139 error);
Chia-I Wucb8405e2017-04-17 15:20:19 -0700140 return NO_MEMORY;
141 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142}
143
Mathias Agopian3330b202009-10-05 17:07:12 -0700144status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700145{
Mathias Agopiancf563192012-02-29 20:43:29 -0800146 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800147
Chia-I Wucb8405e2017-04-17 15:20:19 -0700148 // We allocated a buffer from the allocator and imported it into the
149 // mapper to get the handle. We just need to free the handle now.
150 mMapper.freeBuffer(handle);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151
Dan Stoza8deb4da2016-06-01 18:21:44 -0700152 Mutex::Autolock _l(sLock);
153 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
154 list.removeItem(handle);
155
156 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157}
158
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159// ---------------------------------------------------------------------------
160}; // namespace android