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