GraphicBufferAllocator.cpp revision d31824004277f554000417cea349d69f18655e95
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 <cutils/log.h> 22 23#include <utils/Singleton.h> 24#include <utils/String8.h> 25#include <utils/Trace.h> 26 27#include <ui/GraphicBufferAllocator.h> 28 29namespace android { 30// --------------------------------------------------------------------------- 31 32ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) 33 34Mutex GraphicBufferAllocator::sLock; 35KeyedVector<buffer_handle_t, 36 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; 37 38GraphicBufferAllocator::GraphicBufferAllocator() 39 : mAllocDev(0) 40{ 41 hw_module_t const* module; 42 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); 43 ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); 44 if (err == 0) { 45 gralloc_open(module, &mAllocDev); 46 } 47} 48 49GraphicBufferAllocator::~GraphicBufferAllocator() 50{ 51 gralloc_close(mAllocDev); 52} 53 54void GraphicBufferAllocator::dump(String8& result) const 55{ 56 Mutex::Autolock _l(sLock); 57 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 58 size_t total = 0; 59 const size_t SIZE = 4096; 60 char buffer[SIZE]; 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 if (rec.size) { 67 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n", 68 list.keyAt(i), rec.size/1024.0f, 69 rec.width, rec.stride, rec.height, rec.format, rec.usage); 70 } else { 71 snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n", 72 list.keyAt(i), 73 rec.width, rec.stride, rec.height, rec.format, rec.usage); 74 } 75 result.append(buffer); 76 total += rec.size; 77 } 78 snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f); 79 result.append(buffer); 80 if (mAllocDev->common.version >= 1 && mAllocDev->dump) { 81 mAllocDev->dump(mAllocDev, buffer, SIZE); 82 result.append(buffer); 83 } 84} 85 86void GraphicBufferAllocator::dumpToSystemLog() 87{ 88 String8 s; 89 GraphicBufferAllocator::getInstance().dump(s); 90 ALOGD("%s", s.string()); 91} 92 93status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height, 94 PixelFormat format, uint32_t usage, buffer_handle_t* handle, 95 uint32_t* stride) 96{ 97 ATRACE_CALL(); 98 99 // make sure to not allocate a N x 0 or 0 x N buffer, since this is 100 // allowed from an API stand-point allocate a 1x1 buffer instead. 101 if (!width || !height) 102 width = height = 1; 103 104 // we have a h/w allocator and h/w buffer is requested 105 status_t err; 106 107 int outStride = 0; 108 err = mAllocDev->alloc(mAllocDev, static_cast<int>(width), 109 static_cast<int>(height), format, static_cast<int>(usage), handle, 110 &outStride); 111 *stride = static_cast<uint32_t>(outStride); 112 113 ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", 114 width, height, format, usage, err, strerror(-err)); 115 116 if (err == NO_ERROR) { 117 Mutex::Autolock _l(sLock); 118 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 119 uint32_t bpp = bytesPerPixel(format); 120 alloc_rec_t rec; 121 rec.width = width; 122 rec.height = height; 123 rec.stride = *stride; 124 rec.format = format; 125 rec.usage = usage; 126 rec.size = static_cast<size_t>(height * (*stride) * bpp); 127 list.add(*handle, rec); 128 } 129 130 return err; 131} 132 133status_t GraphicBufferAllocator::free(buffer_handle_t handle) 134{ 135 ATRACE_CALL(); 136 status_t err; 137 138 err = mAllocDev->free(mAllocDev, handle); 139 140 ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); 141 if (err == NO_ERROR) { 142 Mutex::Autolock _l(sLock); 143 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 144 list.removeItem(handle); 145 } 146 147 return err; 148} 149 150// --------------------------------------------------------------------------- 151}; // namespace android 152