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, "%14s | %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, "%14p | %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
allocate(const AllocationRequest & request)116 auto GraphicBufferAllocator::allocate(const AllocationRequest& request) -> AllocationResult {
117 ATRACE_CALL();
118 if (!request.width || !request.height) {
119 return AllocationResult(BAD_VALUE);
120 }
121
122 const auto width = request.width;
123 const auto height = request.height;
124
125 const uint32_t bpp = bytesPerPixel(request.format);
126 if (std::numeric_limits<size_t>::max() / width / height < static_cast<size_t>(bpp)) {
127 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
128 "usage %" PRIx64 ": Requesting too large a buffer size",
129 request.width, request.height, request.layerCount, request.format, request.usage);
130 return AllocationResult(BAD_VALUE);
131 }
132
133 if (request.layerCount < 1) {
134 return AllocationResult(BAD_VALUE);
135 }
136
137 auto result = mAllocator->allocate(request);
138 if (result.status == UNKNOWN_TRANSACTION) {
139 if (!request.extras.empty()) {
140 ALOGE("Failed to allocate with additional options, allocator version mis-match? "
141 "gralloc version = %d",
142 (int)mMapper.getMapperVersion());
143 return result;
144 }
145 // If there's no additional options, fall back to previous allocate version
146 result.status = mAllocator->allocate(request.requestorName, request.width, request.height,
147 request.format, request.layerCount, request.usage,
148 &result.stride, &result.handle, request.importBuffer);
149 }
150
151 if (result.status != NO_ERROR) {
152 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
153 "usage %" PRIx64 ": %d",
154 request.width, request.height, request.layerCount, request.format, request.usage,
155 result.status);
156 return result;
157 }
158
159 if (!request.importBuffer) {
160 return result;
161 }
162 size_t bufSize;
163
164 // if stride has no meaning or is too large,
165 // approximate size with the input width instead
166 if ((result.stride) != 0 &&
167 std::numeric_limits<size_t>::max() / height / (result.stride) < static_cast<size_t>(bpp)) {
168 bufSize = static_cast<size_t>(width) * height * bpp;
169 } else {
170 bufSize = static_cast<size_t>((result.stride)) * height * bpp;
171 }
172
173 Mutex::Autolock _l(sLock);
174 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
175 alloc_rec_t rec;
176 rec.width = width;
177 rec.height = height;
178 rec.stride = result.stride;
179 rec.format = request.format;
180 rec.layerCount = request.layerCount;
181 rec.usage = request.usage;
182 rec.size = bufSize;
183 rec.requestorName = request.requestorName;
184 list.add(result.handle, rec);
185
186 return result;
187 }
188
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)189 status_t GraphicBufferAllocator::allocateHelper(uint32_t width, uint32_t height, PixelFormat format,
190 uint32_t layerCount, uint64_t usage,
191 buffer_handle_t* handle, uint32_t* stride,
192 std::string requestorName, bool importBuffer) {
193 ATRACE_CALL();
194
195 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
196 // allowed from an API stand-point allocate a 1x1 buffer instead.
197 if (!width || !height)
198 width = height = 1;
199
200 const uint32_t bpp = bytesPerPixel(format);
201 if (std::numeric_limits<size_t>::max() / width / height < static_cast<size_t>(bpp)) {
202 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
203 "usage %" PRIx64 ": Requesting too large a buffer size",
204 width, height, layerCount, format, usage);
205 return BAD_VALUE;
206 }
207
208 // Ensure that layerCount is valid.
209 if (layerCount < 1) {
210 layerCount = 1;
211 }
212
213 // TODO(b/72323293, b/72703005): Remove these invalid bits from callers
214 usage &= ~static_cast<uint64_t>((1 << 10) | (1 << 13));
215
216 status_t error = mAllocator->allocate(requestorName, width, height, format, layerCount, usage,
217 stride, handle, importBuffer);
218 if (error != NO_ERROR) {
219 ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
220 "usage %" PRIx64 ": %d",
221 width, height, layerCount, format, usage, error);
222 return error;
223 }
224
225 if (!importBuffer) {
226 return NO_ERROR;
227 }
228 size_t bufSize;
229
230 // if stride has no meaning or is too large,
231 // approximate size with the input width instead
232 if ((*stride) != 0 &&
233 std::numeric_limits<size_t>::max() / height / (*stride) < static_cast<size_t>(bpp)) {
234 bufSize = static_cast<size_t>(width) * height * bpp;
235 } else {
236 bufSize = static_cast<size_t>((*stride)) * height * bpp;
237 }
238
239 Mutex::Autolock _l(sLock);
240 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
241 alloc_rec_t rec;
242 rec.width = width;
243 rec.height = height;
244 rec.stride = *stride;
245 rec.format = format;
246 rec.layerCount = layerCount;
247 rec.usage = usage;
248 rec.size = bufSize;
249 rec.requestorName = std::move(requestorName);
250 list.add(*handle, rec);
251
252 return NO_ERROR;
253 }
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)254 status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
255 uint32_t layerCount, uint64_t usage,
256 buffer_handle_t* handle, uint32_t* stride,
257 std::string requestorName) {
258 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
259 true);
260 }
261
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)262 status_t GraphicBufferAllocator::allocateRawHandle(uint32_t width, uint32_t height,
263 PixelFormat format, uint32_t layerCount,
264 uint64_t usage, buffer_handle_t* handle,
265 uint32_t* stride, std::string requestorName) {
266 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
267 false);
268 }
269
270 // 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)271 status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, PixelFormat format,
272 uint32_t layerCount, uint64_t usage,
273 buffer_handle_t* handle, uint32_t* stride,
274 uint64_t /*graphicBufferId*/, std::string requestorName) {
275 return allocateHelper(width, height, format, layerCount, usage, handle, stride, requestorName,
276 true);
277 }
278
free(buffer_handle_t handle)279 status_t GraphicBufferAllocator::free(buffer_handle_t handle)
280 {
281 ATRACE_CALL();
282
283 // We allocated a buffer from the allocator and imported it into the
284 // mapper to get the handle. We just need to free the handle now.
285 mMapper.freeBuffer(handle);
286
287 Mutex::Autolock _l(sLock);
288 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
289 list.removeItem(handle);
290
291 return NO_ERROR;
292 }
293
supportsAdditionalOptions() const294 bool GraphicBufferAllocator::supportsAdditionalOptions() const {
295 return mAllocator->supportsAdditionalOptions();
296 }
297
298 // ---------------------------------------------------------------------------
299 }; // namespace android
300