3 ** Copyright 2009, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define LOG_TAG "GraphicBufferAllocator"
20 #include <cutils/log.h>
22 #include <utils/Singleton.h>
23 #include <utils/String8.h>
25 #include <ui/GraphicBufferAllocator.h>
27 #include <private/ui/sw_gralloc_handle.h>
30 // ---------------------------------------------------------------------------
32 ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
34 Mutex GraphicBufferAllocator::sLock;
35 KeyedVector<buffer_handle_t,
36 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
38 GraphicBufferAllocator::GraphicBufferAllocator()
41 hw_module_t const* module;
42 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
43 LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
45 gralloc_open(module, &mAllocDev);
49 GraphicBufferAllocator::~GraphicBufferAllocator()
51 gralloc_close(mAllocDev);
54 void GraphicBufferAllocator::dump(String8& result) const
56 Mutex::Autolock _l(sLock);
57 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
59 const size_t SIZE = 512;
61 snprintf(buffer, SIZE, "Allocated buffers:\n");
62 result.append(buffer);
63 const size_t c = list.size();
64 for (size_t i=0 ; i<c ; i++) {
65 const alloc_rec_t& rec(list.valueAt(i));
66 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n",
67 list.keyAt(i), rec.size/1024.0f,
68 rec.w, rec.s, rec.h, rec.format, rec.usage);
69 result.append(buffer);
72 snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f);
73 result.append(buffer);
76 void GraphicBufferAllocator::dumpToSystemLog()
79 GraphicBufferAllocator::getInstance().dump(s);
80 LOGD("%s", s.string());
83 status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
84 int usage, buffer_handle_t* handle, int32_t* stride)
86 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
87 // allowed from an API stand-point allocate a 1x1 buffer instead.
91 // we have a h/w allocator and h/w buffer is requested
94 if (usage & GRALLOC_USAGE_HW_MASK) {
95 err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
97 err = sw_gralloc_handle_t::alloc(w, h, format, usage, handle, stride);
100 LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
101 w, h, format, usage, err, strerror(-err));
103 if (err == NO_ERROR) {
104 Mutex::Autolock _l(sLock);
105 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
112 rec.size = h * stride[0] * bytesPerPixel(format);
113 list.add(*handle, rec);
119 status_t GraphicBufferAllocator::free(buffer_handle_t handle)
122 if (sw_gralloc_handle_t::validate(handle) < 0) {
123 err = mAllocDev->free(mAllocDev, handle);
125 err = sw_gralloc_handle_t::free((sw_gralloc_handle_t*)handle);
128 LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
129 if (err == NO_ERROR) {
130 Mutex::Autolock _l(sLock);
131 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
132 list.removeItem(handle);
138 // ---------------------------------------------------------------------------
139 }; // namespace android