1 /*
2 **
3 ** Copyright 2009, The Android Open Source Project
4 **
5 ** 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
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** 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
15 ** limitations under the License.
16 */
17
18 #define LOG_TAG "GraphicBufferAllocator"
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21 #include <ui/GraphicBufferAllocator.h>
22
23 #include <limits.h>
24 #include <stdio.h>
25
26 #include <grallocusage/GrallocUsageConversion.h>
27
28 #include <android-base/stringprintf.h>
29 #include <log/log.h>
30 #include <utils/Singleton.h>
31 #include <utils/Trace.h>
32
33 #include <ui/Gralloc.h>
34 #include <ui/Gralloc2.h>
35 #include <ui/Gralloc3.h>
36 #include <ui/Gralloc4.h>
37 #include <ui/Gralloc5.h>
38 #include <ui/GraphicBufferMapper.h>
39
40 namespace android {
41 // ---------------------------------------------------------------------------
42
43 using base::StringAppendF;
44
45 ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
46
47 Mutex GraphicBufferAllocator::sLock;
48 KeyedVector<buffer_handle_t,
49 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
50
GraphicBufferAllocator()51 GraphicBufferAllocator::GraphicBufferAllocator() : mMapper(GraphicBufferMapper::getInstance()) {
52 switch (mMapper.getMapperVersion()) {
53 case GraphicBufferMapper::GRALLOC_5:
54 mAllocator = std::make_unique<const Gralloc5Allocator>(
55 reinterpret_cast<const Gralloc5Mapper&>(mMapper.getGrallocMapper()));
56 break;
57 case GraphicBufferMapper::GRALLOC_4:
58 mAllocator = std::make_unique<const Gralloc4Allocator>(
59 reinterpret_cast<const Gralloc4Mapper&>(mMapper.getGrallocMapper()));
60 break;
61 case GraphicBufferMapper::GRALLOC_3:
62 mAllocator = std::make_unique<const Gralloc3Allocator>(
63 reinterpret_cast<const Gralloc3Mapper&>(mMapper.getGrallocMapper()));
64 break;
65 case GraphicBufferMapper::GRALLOC_2:
66 mAllocator = std::make_unique<const Gralloc2Allocator>(
67 reinterpret_cast<const Gralloc2Mapper&>(mMapper.getGrallocMapper()));
68 break;
69 }
70 LOG_ALWAYS_FATAL_IF(!mAllocator->isLoaded(),
71 "Failed to load matching allocator for mapper version %d",
72 mMapper.getMapperVersion());
73 }
74
~GraphicBufferAllocator()75 GraphicBufferAllocator::~GraphicBufferAllocator() {}
76
getTotalSize() const77 uint64_t GraphicBufferAllocator::getTotalSize() const {
78 Mutex::Autolock _l(sLock);
79 uint64_t total = 0;
80 for (size_t i = 0; i < sAllocList.size(); ++i) {
81 total += sAllocList.valueAt(i).size;
82 }
83 return total;
84 }
85
dump(std::string & result,bool less) const86 void GraphicBufferAllocator::dump(std::string& result, bool less) const {
87 Mutex::Autolock _l(sLock);
88 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
89 uint64_t total = 0;
90 result.append("GraphicBufferAllocator buffers:\n");
91 const size_t count = list.size();
92 StringAppendF(&result, "%10s | %11s | %18s | %s | %8s | %10s | %s\n", "Handle", "Size",
93 "W (Stride) x H", "Layers", "Format", "Usage", "Requestor");
94 for (size_t i = 0; i < count; i++) {
95 const alloc_rec_t& rec(list.valueAt(i));
96 std::string sizeStr = (rec.size)
97 ? base::StringPrintf("%7.2f KiB", static_cast<double>(rec.size) / 1024.0)
98 : "unknown";
99 StringAppendF(&result, "%10p | %11s | %4u (%4u) x %4u | %6u | %8X | 0x%8" PRIx64 " | %s\n",
100 list.keyAt(i), sizeStr.c_str(), rec.width, rec.stride, rec.height,
101 rec.layerCount, rec.format, rec.usage, rec.requestorName.c_str());
102 total += rec.size;
103 }
104 StringAppendF(&result, "Total allocated by GraphicBufferAllocator (estimate): %.2f KB\n",
105 static_cast<double>(total) / 1024.0);
106
107 result.append(mAllocator->dumpDebugInfo(less));
108 }
109
dumpToSystemLog(bool less)110 void GraphicBufferAllocator::dumpToSystemLog(bool less) {
111 std::string s;
112 GraphicBufferAllocator::getInstance().dump(s, less);
113 ALOGD("%s", s.c_str());
114 }
115
allocateHelper(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,buffer_handle_t * handle,uint32_t * stride,std::string requestorName,bool importBuffer)116 status_t GraphicBufferAllocator::allocateHelper(uint32_t width, uint32_t height, PixelFormat format,
117 uint32_t layerCount, uint64_t usage,
118 buffer_handle_t* handle, uint32_t* stride,
119 std::string requestorName, bool importBuffer) {
120 ATRACE_CALL();
121
122 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
123 // allowed from an API stand-point allocate a 1x1 buffer instead.
124 if (!width || !height)
125 width = height = 1;
126
127 const uint32_t bpp = bytesPerPixel(format);
128 if (std::numeric_limits<size_t>::max() / width / height < static_cast<size_t>(bpp)) {
129 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
130 "usage %" PRIx64 ": Requesting too large a buffer size",
131 width, height, layerCount, format, usage);
132 return BAD_VALUE;
133 }
134
135 // Ensure that layerCount is valid.
136 if (layerCount < 1) {
137 layerCount = 1;
138 }
139
140 // TODO(b/72323293, b/72703005): Remove these invalid bits from callers
141 usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13));
142
143 status_t error = mAllocator->allocate(requestorName, width, height, format, layerCount, usage,
144 1, stride, handle, importBuffer);
145 if (error != NO_ERROR) {
146 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
147 "usage %" PRIx64 ": %d",
148 width, height, layerCount, format, usage, error);
149 return error;
150 }
151
152 if (!importBuffer) {
153 return NO_ERROR;
154 }
155 size_t bufSize;
156
157 // if stride has no meaning or is too large,
158 // approximate size with the input width instead
159 if ((*stride) != 0 &&
160 std::numeric_limits<size_t>::max() / height / (*stride) < static_cast<size_t>(bpp)) {
161 bufSize = static_cast<size_t>(width) * height * bpp;
162 } else {
163 bufSize = static_cast<size_t>((*stride)) * height * bpp;
164 }
165
166 Mutex::Autolock _l(sLock);
167 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
168 alloc_rec_t rec;
169 rec.width = width;
170 rec.height = height;
171 rec.stride = *stride;
172 rec.format = format;
173 rec.layerCount = layerCount;
174 rec.usage = usage;
175 rec.size = bufSize;
176 rec.requestorName = std::move(requestorName);
177 list.add(*handle, rec);
178
179 return NO_ERROR;
180 }
allocate(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,buffer_handle_t * handle,uint32_t * stride,std::string requestorName)181 status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
182 uint32_t layerCount, uint64_t usage,
183 buffer_handle_t* handle, uint32_t* stride,
184 std::string requestorName) {
185 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
186 true);
187 }
188
allocateRawHandle(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,buffer_handle_t * handle,uint32_t * stride,std::string requestorName)189 status_t GraphicBufferAllocator::allocateRawHandle(uint32_t width, uint32_t height,
190 PixelFormat format, uint32_t layerCount,
191 uint64_t usage, buffer_handle_t* handle,
192 uint32_t* stride, std::string requestorName) {
193 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
194 false);
195 }
196
197 // DEPRECATED
allocate(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,buffer_handle_t * handle,uint32_t * stride,uint64_t,std::string requestorName)198 status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
199 uint32_t layerCount, uint64_t usage,
200 buffer_handle_t* handle, uint32_t* stride,
201 uint64_t /*graphicBufferId*/, std::string requestorName) {
202 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
203 true);
204 }
205
free(buffer_handle_t handle)206 status_t GraphicBufferAllocator::free(buffer_handle_t handle)
207 {
208 ATRACE_CALL();
209
210 // We allocated a buffer from the allocator and imported it into the
211 // mapper to get the handle. We just need to free the handle now.
212 mMapper.freeBuffer(handle);
213
214 Mutex::Autolock _l(sLock);
215 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
216 list.removeItem(handle);
217
218 return NO_ERROR;
219 }
220
221 // ---------------------------------------------------------------------------
222 }; // namespace android
223