• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "GraphicBufferAllocator"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 #include <ui/GraphicBufferAllocator.h>
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include <grallocusage/GrallocUsageConversion.h>
23 #include <log/log.h>
24 #include <utils/Singleton.h>
25 #include <utils/String8.h>
26 #include <utils/Trace.h>
27 #include <ui/Gralloc2.h>
28 #include <ui/GraphicBufferMapper.h>
29 
30 namespace android {
31 // ---------------------------------------------------------------------------
32 ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
33 Mutex GraphicBufferAllocator::sLock;
34 std::unordered_map<buffer_handle_t,
35     GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
GraphicBufferAllocator()36 GraphicBufferAllocator::GraphicBufferAllocator()
37   : mMapper(GraphicBufferMapper::getInstance()),
38     mAllocator(std::make_unique<Gralloc2::Allocator>(
39                 mMapper.getGrallocMapper()))
40 {
41 }
~GraphicBufferAllocator()42 GraphicBufferAllocator::~GraphicBufferAllocator() {}
dump(String8 & result) const43 void GraphicBufferAllocator::dump(String8& result) const
44 {
45     Mutex::Autolock _l(sLock);
46     std::unordered_map<buffer_handle_t, alloc_rec_t>& list(sAllocList);
47     size_t total = 0;
48     const size_t SIZE = 4096;
49     char buffer[SIZE];
50     snprintf(buffer, SIZE, "Allocated buffers:\n");
51     result.append(buffer);
52     const size_t c = list.size();
53     (void)c;
54     for (auto it : list) {
55         const alloc_rec_t& rec = it.second;
56         if (rec.size) {
57             snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
58                     " | %s\n",
59                     it.first, rec.size/1024.0,
60                     rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
61                     rec.usage, rec.requestorName.c_str());
62         } else {
63             snprintf(buffer, SIZE, "%10p: unknown     | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64
64                     " | %s\n",
65                     it.first,
66                     rec.width, rec.stride, rec.height, rec.layerCount, rec.format,
67                     rec.usage, rec.requestorName.c_str());
68         }
69         result.append(buffer);
70         total += rec.size;
71     }
72     snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0);
73     result.append(buffer);
74     std::string deviceDump = mAllocator->dumpDebugInfo();
75     result.append(deviceDump.c_str(), deviceDump.size());
76 }
dumpToSystemLog()77 void GraphicBufferAllocator::dumpToSystemLog()
78 {
79     String8 s;
80     GraphicBufferAllocator::getInstance().dump(s);
81     ALOGD("%s", s.string());
82 }
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)83 status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
84         PixelFormat format, uint32_t layerCount, uint64_t usage,
85         buffer_handle_t* handle, uint32_t* stride,
86         uint64_t /*graphicBufferId*/, std::string requestorName)
87 {
88     ATRACE_CALL();
89     // make sure to not allocate a N x 0 or 0 x N buffer, since this is
90     // allowed from an API stand-point allocate a 1x1 buffer instead.
91     if (!width || !height)
92         width = height = 1;
93     // Ensure that layerCount is valid.
94     if (layerCount < 1)
95         layerCount = 1;
96     Gralloc2::IMapper::BufferDescriptorInfo info = {};
97     info.width = width;
98     info.height = height;
99     info.layerCount = layerCount;
100     info.format = static_cast<Gralloc2::PixelFormat>(format);
101     info.usage = usage;
102     Gralloc2::Error error = mAllocator->allocate(info, stride, handle);
103     if (error == Gralloc2::Error::NONE) {
104         Mutex::Autolock _l(sLock);
105         std::unordered_map<buffer_handle_t, alloc_rec_t>& list(sAllocList);
106         uint32_t bpp = bytesPerPixel(format);
107         alloc_rec_t rec;
108         rec.width = width;
109         rec.height = height;
110         rec.stride = *stride;
111         rec.format = format;
112         rec.layerCount = layerCount;
113         rec.usage = usage;
114         rec.size = static_cast<size_t>(height * (*stride) * bpp);
115         rec.requestorName = std::move(requestorName);
116         list[*handle] = rec;
117         return NO_ERROR;
118     } else {
119         ALOGE("Failed to allocate (%u x %u) layerCount %u format %d "
120                 "usage %" PRIx64 ": %d",
121                 width, height, layerCount, format, usage,
122                 error);
123         return NO_MEMORY;
124     }
125 }
free(buffer_handle_t handle)126 status_t GraphicBufferAllocator::free(buffer_handle_t handle)
127 {
128     ATRACE_CALL();
129     // We allocated a buffer from the allocator and imported it into the
130     // mapper to get the handle.  We just need to free the handle now.
131     mMapper.freeBuffer(handle);
132     Mutex::Autolock _l(sLock);
133     std::unordered_map<buffer_handle_t, alloc_rec_t>& list(sAllocList);
134     list.erase(handle);
135     return NO_ERROR;
136 }
137 // ---------------------------------------------------------------------------
138 }; // namespace android
139