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" 19cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian#define ATRACE_TAG ATRACE_TAG_GRAPHICS 205629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian 21076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <cutils/log.h> 224243e666213029a293935987c979831093fb0779Mathias Agopian 234243e666213029a293935987c979831093fb0779Mathias Agopian#include <utils/Singleton.h> 24076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <utils/String8.h> 25cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian#include <utils/Trace.h> 26076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 273330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBufferAllocator.h> 28076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 29076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopiannamespace android { 30076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// --------------------------------------------------------------------------- 31076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 323330b203039dea366d4981db1408a460134b2d2cMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) 334243e666213029a293935987c979831093fb0779Mathias Agopian 343330b203039dea366d4981db1408a460134b2d2cMathias AgopianMutex GraphicBufferAllocator::sLock; 35b26af23744fa73e8bc142b1eb98772fde5970c10Mathias AgopianKeyedVector<buffer_handle_t, 36b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList; 37076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 383330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBufferAllocator::GraphicBufferAllocator() 39076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian : mAllocDev(0) 40076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 41076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian hw_module_t const* module; 42076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); 43e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); 44076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian if (err == 0) { 45076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian gralloc_open(module, &mAllocDev); 46076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 47076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 48076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 493330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBufferAllocator::~GraphicBufferAllocator() 50076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 51076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian gralloc_close(mAllocDev); 52076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 53076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 543330b203039dea366d4981db1408a460134b2d2cMathias Agopianvoid GraphicBufferAllocator::dump(String8& result) const 55076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 56076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian Mutex::Autolock _l(sLock); 57076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 58076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian size_t total = 0; 591d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling const size_t SIZE = 4096; 60076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian char buffer[SIZE]; 61076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian snprintf(buffer, SIZE, "Allocated buffers:\n"); 62076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian result.append(buffer); 63076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian const size_t c = list.size(); 64076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian for (size_t i=0 ; i<c ; i++) { 65076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian const alloc_rec_t& rec(list.valueAt(i)); 66a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian if (rec.size) { 67a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n", 68a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian list.keyAt(i), rec.size/1024.0f, 69a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian rec.w, rec.s, rec.h, rec.format, rec.usage); 70a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian } else { 71a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n", 72a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian list.keyAt(i), 73a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian rec.w, rec.s, rec.h, rec.format, rec.usage); 74a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian } 75076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian result.append(buffer); 76076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian total += rec.size; 77076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 78a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f); 79076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian result.append(buffer); 801d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling if (mAllocDev->common.version >= 1 && mAllocDev->dump) { 811d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling mAllocDev->dump(mAllocDev, buffer, SIZE); 821d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling result.append(buffer); 831d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling } 84076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 85076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 86678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopianvoid GraphicBufferAllocator::dumpToSystemLog() 87678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian{ 88678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian String8 s; 89678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian GraphicBufferAllocator::getInstance().dump(s); 909d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block ALOGD("%s", s.string()); 91678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian} 92678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian 933330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, 94076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian int usage, buffer_handle_t* handle, int32_t* stride) 95076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 96cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian ATRACE_CALL(); 975629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian // make sure to not allocate a N x 0 or 0 x N buffer, since this is 985629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian // allowed from an API stand-point allocate a 1x1 buffer instead. 995629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian if (!w || !h) 1005629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian w = h = 1; 101cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian 102076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian // we have a h/w allocator and h/w buffer is requested 103b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian status_t err; 104b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian 1050a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride); 106cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian 10732397c1cd3327905173b36baa6fd1c579bc328ffSteve Block ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", 108076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian w, h, format, usage, err, strerror(-err)); 109076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 110076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian if (err == NO_ERROR) { 111076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian Mutex::Autolock _l(sLock); 112076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 113a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian int bpp = bytesPerPixel(format); 114a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian if (bpp < 0) { 115a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian // probably a HAL custom format. in any case, we don't know 116a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian // what its pixel size is. 117a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian bpp = 0; 118a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian } 119076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian alloc_rec_t rec; 120076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.w = w; 121076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.h = h; 1225629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian rec.s = *stride; 123076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.format = format; 124076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian rec.usage = usage; 125a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian rec.size = h * stride[0] * bpp; 126076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian list.add(*handle, rec); 127076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 128076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 129076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian return err; 130076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 131076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 1323330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBufferAllocator::free(buffer_handle_t handle) 133076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{ 134cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian ATRACE_CALL(); 135b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian status_t err; 1360a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian 1370a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian err = mAllocDev->free(mAllocDev, handle); 138b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian 13932397c1cd3327905173b36baa6fd1c579bc328ffSteve Block ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); 140076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian if (err == NO_ERROR) { 141076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian Mutex::Autolock _l(sLock); 142076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); 143076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian list.removeItem(handle); 144076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian } 145076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 146076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian return err; 147076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian} 148076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian 149076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// --------------------------------------------------------------------------- 150076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}; // namespace android 151