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