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