GraphicBufferAllocator.cpp revision 9d4536835248525f32f1504a3d28d5bbfa0a2910
1076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian/* 2076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** 3076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** Copyright 2009, The Android Open Source Project 4076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** 5076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** Licensed under the Apache License, Version 2.0 (the "License"); 6076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** you may not use this file except in compliance with the License. 7076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** You may obtain a copy of the License at 8076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** 9076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** http://www.apache.org/licenses/LICENSE-2.0 10076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** 11076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** Unless required by applicable law or agreed to in writing, software 12076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** distributed under the License is distributed on an "AS IS" BASIS, 13076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** See the License for the specific language governing permissions and 15076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** limitations under the License. 16076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian*/ 17076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 185629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian#define LOG_TAG "GraphicBufferAllocator" 195629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian 20076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <cutils/log.h> 214243e666213029a293935987c979831093fb0779Mathias Agopian 224243e666213029a293935987c979831093fb0779Mathias Agopian#include <utils/Singleton.h> 23076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <utils/String8.h> 24076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 253330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBufferAllocator.h> 26076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 27076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopiannamespace android { 28076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// --------------------------------------------------------------------------- 29076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 303330b203039dea366d4981db1408a460134b2d2cMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) 314243e666213029a293935987c979831093fb0779Mathias Agopian 323330b203039dea366d4981db1408a460134b2d2cMathias AgopianMutex GraphicBufferAllocator::sLock; 33b26af23744fa73e8bc142b1eb98772fde5970c10Mathias AgopianKeyedVector<buffer_handle_t, 34b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; 35076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 363330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBufferAllocator::GraphicBufferAllocator() 37076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian : mAllocDev(0) 38076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 39076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian hw_module_t const* module; 40076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); 41076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); 42076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian if (err == 0) { 43076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian gralloc_open(module, &mAllocDev); 44076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 45076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 46076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 473330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBufferAllocator::~GraphicBufferAllocator() 48076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 49076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian gralloc_close(mAllocDev); 50076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 51076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 523330b203039dea366d4981db1408a460134b2d2cMathias Agopianvoid GraphicBufferAllocator::dump(String8& result) const 53076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 54076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian Mutex::Autolock _l(sLock); 55076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 56076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian size_t total = 0; 571d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling const size_t SIZE = 4096; 58076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian char buffer[SIZE]; 59076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian snprintf(buffer, SIZE, "Allocated buffers:\n"); 60076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian result.append(buffer); 61076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian const size_t c = list.size(); 62076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian for (size_t i=0 ; i<c ; i++) { 63076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian const alloc_rec_t& rec(list.valueAt(i)); 64a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian if (rec.size) { 65a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n", 66a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian list.keyAt(i), rec.size/1024.0f, 67a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian rec.w, rec.s, rec.h, rec.format, rec.usage); 68a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian } else { 69a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n", 70a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian list.keyAt(i), 71a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian rec.w, rec.s, rec.h, rec.format, rec.usage); 72a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian } 73076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian result.append(buffer); 74076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian total += rec.size; 75076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 76a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f); 77076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian result.append(buffer); 781d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling if (mAllocDev->common.version >= 1 && mAllocDev->dump) { 791d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling mAllocDev->dump(mAllocDev, buffer, SIZE); 801d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling result.append(buffer); 811d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling } 82076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 83076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 84678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopianvoid GraphicBufferAllocator::dumpToSystemLog() 85678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian{ 86678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian String8 s; 87678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian GraphicBufferAllocator::getInstance().dump(s); 889d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block ALOGD("%s", s.string()); 89678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian} 90678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian 913330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, 92076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian int usage, buffer_handle_t* handle, int32_t* stride) 93076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 945629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian // make sure to not allocate a N x 0 or 0 x N buffer, since this is 955629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian // allowed from an API stand-point allocate a 1x1 buffer instead. 965629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian if (!w || !h) 975629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian w = h = 1; 98cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian 99076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian // we have a h/w allocator and h/w buffer is requested 100b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian status_t err; 101b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian 1020a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride); 103cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian 104076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", 105076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian w, h, format, usage, err, strerror(-err)); 106076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 107076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian if (err == NO_ERROR) { 108076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian Mutex::Autolock _l(sLock); 109076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 110a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian int bpp = bytesPerPixel(format); 111a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian if (bpp < 0) { 112a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian // probably a HAL custom format. in any case, we don't know 113a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian // what its pixel size is. 114a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian bpp = 0; 115a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian } 116076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian alloc_rec_t rec; 117076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.w = w; 118076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.h = h; 1195629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian rec.s = *stride; 120076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.format = format; 121076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.usage = usage; 122a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian rec.size = h * stride[0] * bpp; 123076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian list.add(*handle, rec); 124076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 125076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 126076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian return err; 127076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 128076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 1293330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBufferAllocator::free(buffer_handle_t handle) 130076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 131b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian status_t err; 1320a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian 1330a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian err = mAllocDev->free(mAllocDev, handle); 134b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian 1354243e666213029a293935987c979831093fb0779Mathias Agopian LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); 136076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian if (err == NO_ERROR) { 137076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian Mutex::Autolock _l(sLock); 138076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 139076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian list.removeItem(handle); 140076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 141076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 142076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian return err; 143076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 144076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 145076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// --------------------------------------------------------------------------- 146076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}; // namespace android 147